light.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // load network.js to get network/chain id
  2. document.body.appendChild(Object.assign(document.createElement("script"), { type: "text/javascript", src: "./network.js" }));
  3. // load web3modal to connect to wallet
  4. document.body.appendChild(Object.assign(document.createElement("script"), { type: "text/javascript", src: "./web3/lib/web3modal.js" }));
  5. // load web3js to create transactions
  6. document.body.appendChild(Object.assign(document.createElement("script"), { type: "text/javascript", src: "./web3/lib/web3.min.js" }));
  7. // uncomment to enable torus wallet
  8. // document.body.appendChild(Object.assign(document.createElement("script"), { type: "text/javascript", src: "https://unpkg.com/@toruslabs/torus-embed" }));
  9. // uncomment to enable walletconnect
  10. document.body.appendChild(Object.assign(document.createElement("script"), { type: "text/javascript", src: "https://unpkg.com/@walletconnect/web3-provider@1.2.1/dist/umd/index.min.js" }));
  11. // load web3gl to connect to unity
  12. window.web3gl = {
  13. networkId: 0,
  14. connect,
  15. connectAccount: "",
  16. clearResponse,
  17. sendAsync,
  18. sendAsyncResponse: "",
  19. sendAsyncError: "",
  20. };
  21. // will be defined after connect()
  22. let provider;
  23. let web3;
  24. /*
  25. paste this in inspector to connect to wallet:
  26. window.web3gl.connect()
  27. */
  28. async function connect() {
  29. // uncomment to enable torus
  30. const providerOptions = {
  31. // torus: {
  32. // package: Torus,
  33. // },
  34. };
  35. if (window.web3InfuraId !== undefined && window.web3InfuraId !== "" && window.web3InfuraId !== "00000000000000000000000000000000") {
  36. providerOptions.walletconnect = {
  37. package: window.WalletConnectProvider.default,
  38. options: {
  39. infuraId: window.web3InfuraId
  40. },
  41. }
  42. }
  43. const web3Modal = new window.Web3Modal.default({
  44. providerOptions,
  45. });
  46. web3Modal.clearCachedProvider();
  47. // set provider
  48. provider = await web3Modal.connect();
  49. web3 = new Web3(provider);
  50. // set current account
  51. // provider.selectedAddress works for metamask and torus
  52. // provider.accounts[0] works for walletconnect
  53. web3gl.connectAccount = provider.selectedAddress || provider.accounts[0];
  54. // refresh page if player changes account
  55. provider.on("accountsChanged", (accounts) => {
  56. window.location.reload();
  57. });
  58. // update if player changes network
  59. provider.on("chainChanged", (chainId) => {
  60. web3gl.networkId = parseInt(chainId);
  61. });
  62. }
  63. function clearResponse() {
  64. window.web3gl.sendAsyncResponse = "";
  65. window.web3gl.sendAsyncError = "";
  66. }
  67. async function sendAsync(method, params) {
  68. web3.currentProvider.sendAsync(
  69. {
  70. jsonrpc: "2.0",
  71. method: method,
  72. params: JSON.parse(params),
  73. id: new Date().getTime(),
  74. },
  75. async (error, result) => {
  76. if (error) {
  77. window.web3gl.sendAsyncError = JSON.stringify(error);
  78. } else {
  79. window.web3gl.sendAsyncResponse = JSON.stringify(result.result);
  80. }
  81. }
  82. );
  83. }