From 80dc242636814d293593d4fa395caeac312f114a Mon Sep 17 00:00:00 2001 From: Jakub Mucha Date: Thu, 8 Sep 2022 02:45:15 +0200 Subject: [PATCH] chore: update test-dapp Signed-off-by: Jakub Mucha --- tests/test-dapp/LICENSE | 20 - tests/test-dapp/alert-red.svg | 9 + tests/test-dapp/constants.json | 357 +++++++- tests/test-dapp/index.css | 27 + tests/test-dapp/index.html | 221 ++++- tests/test-dapp/index.js | 1400 ++++++++++++++++++++++++++++++++ 6 files changed, 2009 insertions(+), 25 deletions(-) delete mode 100644 tests/test-dapp/LICENSE create mode 100644 tests/test-dapp/alert-red.svg create mode 100644 tests/test-dapp/index.js diff --git a/tests/test-dapp/LICENSE b/tests/test-dapp/LICENSE deleted file mode 100644 index 08c7d36f7..000000000 --- a/tests/test-dapp/LICENSE +++ /dev/null @@ -1,20 +0,0 @@ -MIT License - -Copyright (c) 2020 MetaMask - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE diff --git a/tests/test-dapp/alert-red.svg b/tests/test-dapp/alert-red.svg new file mode 100644 index 000000000..29aa77e8c --- /dev/null +++ b/tests/test-dapp/alert-red.svg @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/tests/test-dapp/constants.json b/tests/test-dapp/constants.json index e5db8a4f0..14fce3687 100644 --- a/tests/test-dapp/constants.json +++ b/tests/test-dapp/constants.json @@ -341,5 +341,360 @@ "stateMutability": "nonpayable", "type": "constructor" } - ] + ], + "collectiblesAbi": [ + { + "inputs": [], + "stateMutability": "nonpayable", + "type": "constructor" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "owner", + "type": "address" + }, + { + "indexed": true, + "internalType": "address", + "name": "approved", + "type": "address" + }, + { + "indexed": true, + "internalType": "uint256", + "name": "tokenId", + "type": "uint256" + } + ], + "name": "Approval", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "owner", + "type": "address" + }, + { + "indexed": true, + "internalType": "address", + "name": "operator", + "type": "address" + }, + { + "indexed": false, + "internalType": "bool", + "name": "approved", + "type": "bool" + } + ], + "name": "ApprovalForAll", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "from", + "type": "address" + }, + { + "indexed": true, + "internalType": "address", + "name": "to", + "type": "address" + }, + { + "indexed": true, + "internalType": "uint256", + "name": "tokenId", + "type": "uint256" + } + ], + "name": "Transfer", + "type": "event" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "to", + "type": "address" + }, + { + "internalType": "uint256", + "name": "tokenId", + "type": "uint256" + } + ], + "name": "approve", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "owner", + "type": "address" + } + ], + "name": "balanceOf", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "tokenId", + "type": "uint256" + } + ], + "name": "getApproved", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "owner", + "type": "address" + }, + { + "internalType": "address", + "name": "operator", + "type": "address" + } + ], + "name": "isApprovedForAll", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "name", + "outputs": [ + { + "internalType": "string", + "name": "", + "type": "string" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "tokenId", + "type": "uint256" + } + ], + "name": "ownerOf", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "from", + "type": "address" + }, + { + "internalType": "address", + "name": "to", + "type": "address" + }, + { + "internalType": "uint256", + "name": "tokenId", + "type": "uint256" + } + ], + "name": "safeTransferFrom", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "from", + "type": "address" + }, + { + "internalType": "address", + "name": "to", + "type": "address" + }, + { + "internalType": "uint256", + "name": "tokenId", + "type": "uint256" + }, + { + "internalType": "bytes", + "name": "_data", + "type": "bytes" + } + ], + "name": "safeTransferFrom", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "operator", + "type": "address" + }, + { + "internalType": "bool", + "name": "approved", + "type": "bool" + } + ], + "name": "setApprovalForAll", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes4", + "name": "interfaceId", + "type": "bytes4" + } + ], + "name": "supportsInterface", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "symbol", + "outputs": [ + { + "internalType": "string", + "name": "", + "type": "string" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "from", + "type": "address" + }, + { + "internalType": "address", + "name": "to", + "type": "address" + }, + { + "internalType": "uint256", + "name": "tokenId", + "type": "uint256" + } + ], + "name": "transferFrom", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "numberOfTokens", + "type": "uint256" + } + ], + "name": "mintCollectibles", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "tokenId", + "type": "uint256" + } + ], + "name": "tokenURI", + "outputs": [ + { + "internalType": "string", + "name": "", + "type": "string" + } + ], + "stateMutability": "pure", + "type": "function" + } + ], + "collectiblesBytecode": "0x60806040523480156200001157600080fd5b506040518060400160405280601481526020017f5465737444617070436f6c6c65637469626c65730000000000000000000000008152506040518060400160405280600381526020017f5444430000000000000000000000000000000000000000000000000000000000815250816000908051906020019062000096929190620000b8565b508060019080519060200190620000af929190620000b8565b505050620001cd565b828054620000c69062000197565b90600052602060002090601f016020900481019282620000ea576000855562000136565b82601f106200010557805160ff191683800117855562000136565b8280016001018555821562000136579182015b828111156200013557825182559160200191906001019062000118565b5b50905062000145919062000149565b5090565b5b80821115620001645760008160009055506001016200014a565b5090565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b60006002820490506001821680620001b057607f821691505b60208210811415620001c757620001c662000168565b5b50919050565b612c8d80620001dd6000396000f3fe608060405234801561001057600080fd5b50600436106100ea5760003560e01c80636352211e1161008c578063a22cb46511610066578063a22cb4651461025b578063b88d4fde14610277578063c87b56dd14610293578063e985e9c5146102c3576100ea565b80636352211e146101dd57806370a082311461020d57806395d89b411461023d576100ea565b8063095ea7b3116100c8578063095ea7b31461016d578063178a85691461018957806323b872dd146101a557806342842e0e146101c1576100ea565b806301ffc9a7146100ef57806306fdde031461011f578063081812fc1461013d575b600080fd5b6101096004803603810190610104919061185d565b6102f3565b60405161011691906118a5565b60405180910390f35b6101276103d5565b6040516101349190611959565b60405180910390f35b610157600480360381019061015291906119b1565b610467565b6040516101649190611a1f565b60405180910390f35b61018760048036038101906101829190611a66565b6104ec565b005b6101a3600480360381019061019e91906119b1565b610604565b005b6101bf60048036038101906101ba9190611aa6565b610653565b005b6101db60048036038101906101d69190611aa6565b6106b3565b005b6101f760048036038101906101f291906119b1565b6106d3565b6040516102049190611a1f565b60405180910390f35b61022760048036038101906102229190611af9565b610785565b6040516102349190611b35565b60405180910390f35b61024561083d565b6040516102529190611959565b60405180910390f35b61027560048036038101906102709190611b7c565b6108cf565b005b610291600480360381019061028c9190611cf1565b610a50565b005b6102ad60048036038101906102a891906119b1565b610ab2565b6040516102ba9190611959565b60405180910390f35b6102dd60048036038101906102d89190611d74565b610b4b565b6040516102ea91906118a5565b60405180910390f35b60007f80ac58cd000000000000000000000000000000000000000000000000000000007bffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916827bffffffffffffffffffffffffffffffffffffffffffffffffffffffff191614806103be57507f5b5e139f000000000000000000000000000000000000000000000000000000007bffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916827bffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916145b806103ce57506103cd82610bdf565b5b9050919050565b6060600080546103e490611de3565b80601f016020809104026020016040519081016040528092919081815260200182805461041090611de3565b801561045d5780601f106104325761010080835404028352916020019161045d565b820191906000526020600020905b81548152906001019060200180831161044057829003601f168201915b5050505050905090565b600061047282610c49565b6104b1576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016104a890611e87565b60405180910390fd5b6004600083815260200190815260200160002060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff169050919050565b60006104f7826106d3565b90508073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff161415610568576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161055f90611f19565b60405180910390fd5b8073ffffffffffffffffffffffffffffffffffffffff16610587610cb5565b73ffffffffffffffffffffffffffffffffffffffff1614806105b657506105b5816105b0610cb5565b610b4b565b5b6105f5576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016105ec90611fab565b60405180910390fd5b6105ff8383610cbd565b505050565b6000600190505b81811161064f5761061c6006610d76565b60006106286006610d8c565b905061063b610635610cb5565b82610d9a565b50808061064790611ffa565b91505061060b565b5050565b61066461065e610cb5565b82610db8565b6106a3576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161069a906120b5565b60405180910390fd5b6106ae838383610e96565b505050565b6106ce83838360405180602001604052806000815250610a50565b505050565b6000806002600084815260200190815260200160002060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff169050600073ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff16141561077c576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161077390612147565b60405180910390fd5b80915050919050565b60008073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff1614156107f6576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016107ed906121d9565b60405180910390fd5b600360008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050919050565b60606001805461084c90611de3565b80601f016020809104026020016040519081016040528092919081815260200182805461087890611de3565b80156108c55780601f1061089a576101008083540402835291602001916108c5565b820191906000526020600020905b8154815290600101906020018083116108a857829003601f168201915b5050505050905090565b6108d7610cb5565b73ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff161415610945576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161093c90612245565b60405180910390fd5b8060056000610952610cb5565b73ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060006101000a81548160ff0219169083151502179055508173ffffffffffffffffffffffffffffffffffffffff166109ff610cb5565b73ffffffffffffffffffffffffffffffffffffffff167f17307eab39ab6107e8899845ad3d59bd9653f200f220920489ca2b5937696c3183604051610a4491906118a5565b60405180910390a35050565b610a61610a5b610cb5565b83610db8565b610aa0576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610a97906120b5565b60405180910390fd5b610aac848484846110f2565b50505050565b6060600060405180610160016040528061013c8152602001612b1c61013c913990506000610adf8461114e565b610ae8836112af565b610af18661114e565b604051602001610b0393929190612469565b60405160208183030381529060405290506000610b1f826112af565b604051602001610b2f9190612512565b6040516020818303038152906040529050809350505050919050565b6000600560008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060009054906101000a900460ff16905092915050565b60007f01ffc9a7000000000000000000000000000000000000000000000000000000007bffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916827bffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916149050919050565b60008073ffffffffffffffffffffffffffffffffffffffff166002600084815260200190815260200160002060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1614159050919050565b600033905090565b816004600083815260200190815260200160002060006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550808273ffffffffffffffffffffffffffffffffffffffff16610d30836106d3565b73ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b92560405160405180910390a45050565b6001816000016000828254019250508190555050565b600081600001549050919050565b610db4828260405180602001604052806000815250611428565b5050565b6000610dc382610c49565b610e02576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610df9906125a6565b60405180910390fd5b6000610e0d836106d3565b90508073ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff161480610e7c57508373ffffffffffffffffffffffffffffffffffffffff16610e6484610467565b73ffffffffffffffffffffffffffffffffffffffff16145b80610e8d5750610e8c8185610b4b565b5b91505092915050565b8273ffffffffffffffffffffffffffffffffffffffff16610eb6826106d3565b73ffffffffffffffffffffffffffffffffffffffff1614610f0c576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610f0390612638565b60405180910390fd5b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff161415610f7c576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610f73906126ca565b60405180910390fd5b610f87838383611483565b610f92600082610cbd565b6001600360008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254610fe291906126ea565b925050819055506001600360008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254611039919061271e565b92505081905550816002600083815260200190815260200160002060006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550808273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef60405160405180910390a4505050565b6110fd848484610e96565b61110984848484611488565b611148576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161113f906127e6565b60405180910390fd5b50505050565b60606000821415611196576040518060400160405280600181526020017f300000000000000000000000000000000000000000000000000000000000000081525090506112aa565b600082905060005b600082146111c85780806111b190611ffa565b915050600a826111c19190612835565b915061119e565b60008167ffffffffffffffff8111156111e4576111e3611bc6565b5b6040519080825280601f01601f1916602001820160405280156112165781602001600182028036833780820191505090505b5090505b600085146112a35760018261122f91906126ea565b9150600a8561123e9190612866565b603061124a919061271e565b60f81b8183815181106112605761125f612897565b5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a905350600a8561129c9190612835565b945061121a565b8093505050505b919050565b60606000825114156112d257604051806020016040528060008152509050611423565b6000604051806060016040528060408152602001612adc6040913990506000600360028551611301919061271e565b61130b9190612835565b600461131791906128c6565b90506000602082611328919061271e565b67ffffffffffffffff81111561134157611340611bc6565b5b6040519080825280601f01601f1916602001820160405280156113735781602001600182028036833780820191505090505b509050818152600183018586518101602084015b818310156113e2576003830192508251603f8160121c168501518253600182019150603f81600c1c168501518253600182019150603f8160061c168501518253600182019150603f8116850151825360018201915050611387565b6003895106600181146113fc576002811461140c57611417565b613d3d60f01b6002830352611417565b603d60f81b60018303525b50505050508093505050505b919050565b6114328383611610565b61143f6000848484611488565b61147e576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401611475906127e6565b60405180910390fd5b505050565b505050565b60006114a98473ffffffffffffffffffffffffffffffffffffffff166117de565b15611603578373ffffffffffffffffffffffffffffffffffffffff1663150b7a026114d2610cb5565b8786866040518563ffffffff1660e01b81526004016114f49493929190612975565b6020604051808303816000875af192505050801561153057506040513d601f19601f8201168201806040525081019061152d91906129d6565b60015b6115b3573d8060008114611560576040519150601f19603f3d011682016040523d82523d6000602084013e611565565b606091505b506000815114156115ab576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016115a2906127e6565b60405180910390fd5b805181602001fd5b63150b7a0260e01b7bffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916817bffffffffffffffffffffffffffffffffffffffffffffffffffffffff191614915050611608565b600190505b949350505050565b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff161415611680576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161167790612a4f565b60405180910390fd5b61168981610c49565b156116c9576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016116c090612abb565b60405180910390fd5b6116d560008383611483565b6001600360008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254611725919061271e565b92505081905550816002600083815260200190815260200160002060006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550808273ffffffffffffffffffffffffffffffffffffffff16600073ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef60405160405180910390a45050565b600080823b905060008111915050919050565b6000604051905090565b600080fd5b600080fd5b60007fffffffff0000000000000000000000000000000000000000000000000000000082169050919050565b61183a81611805565b811461184557600080fd5b50565b60008135905061185781611831565b92915050565b600060208284031215611873576118726117fb565b5b600061188184828501611848565b91505092915050565b60008115159050919050565b61189f8161188a565b82525050565b60006020820190506118ba6000830184611896565b92915050565b600081519050919050565b600082825260208201905092915050565b60005b838110156118fa5780820151818401526020810190506118df565b83811115611909576000848401525b50505050565b6000601f19601f8301169050919050565b600061192b826118c0565b61193581856118cb565b93506119458185602086016118dc565b61194e8161190f565b840191505092915050565b600060208201905081810360008301526119738184611920565b905092915050565b6000819050919050565b61198e8161197b565b811461199957600080fd5b50565b6000813590506119ab81611985565b92915050565b6000602082840312156119c7576119c66117fb565b5b60006119d58482850161199c565b91505092915050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000611a09826119de565b9050919050565b611a19816119fe565b82525050565b6000602082019050611a346000830184611a10565b92915050565b611a43816119fe565b8114611a4e57600080fd5b50565b600081359050611a6081611a3a565b92915050565b60008060408385031215611a7d57611a7c6117fb565b5b6000611a8b85828601611a51565b9250506020611a9c8582860161199c565b9150509250929050565b600080600060608486031215611abf57611abe6117fb565b5b6000611acd86828701611a51565b9350506020611ade86828701611a51565b9250506040611aef8682870161199c565b9150509250925092565b600060208284031215611b0f57611b0e6117fb565b5b6000611b1d84828501611a51565b91505092915050565b611b2f8161197b565b82525050565b6000602082019050611b4a6000830184611b26565b92915050565b611b598161188a565b8114611b6457600080fd5b50565b600081359050611b7681611b50565b92915050565b60008060408385031215611b9357611b926117fb565b5b6000611ba185828601611a51565b9250506020611bb285828601611b67565b9150509250929050565b600080fd5b600080fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b611bfe8261190f565b810181811067ffffffffffffffff82111715611c1d57611c1c611bc6565b5b80604052505050565b6000611c306117f1565b9050611c3c8282611bf5565b919050565b600067ffffffffffffffff821115611c5c57611c5b611bc6565b5b611c658261190f565b9050602081019050919050565b82818337600083830152505050565b6000611c94611c8f84611c41565b611c26565b905082815260208101848484011115611cb057611caf611bc1565b5b611cbb848285611c72565b509392505050565b600082601f830112611cd857611cd7611bbc565b5b8135611ce8848260208601611c81565b91505092915050565b60008060008060808587031215611d0b57611d0a6117fb565b5b6000611d1987828801611a51565b9450506020611d2a87828801611a51565b9350506040611d3b8782880161199c565b925050606085013567ffffffffffffffff811115611d5c57611d5b611800565b5b611d6887828801611cc3565b91505092959194509250565b60008060408385031215611d8b57611d8a6117fb565b5b6000611d9985828601611a51565b9250506020611daa85828601611a51565b9150509250929050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b60006002820490506001821680611dfb57607f821691505b60208210811415611e0f57611e0e611db4565b5b50919050565b7f4552433732313a20617070726f76656420717565727920666f72206e6f6e657860008201527f697374656e7420746f6b656e0000000000000000000000000000000000000000602082015250565b6000611e71602c836118cb565b9150611e7c82611e15565b604082019050919050565b60006020820190508181036000830152611ea081611e64565b9050919050565b7f4552433732313a20617070726f76616c20746f2063757272656e74206f776e6560008201527f7200000000000000000000000000000000000000000000000000000000000000602082015250565b6000611f036021836118cb565b9150611f0e82611ea7565b604082019050919050565b60006020820190508181036000830152611f3281611ef6565b9050919050565b7f4552433732313a20617070726f76652063616c6c6572206973206e6f74206f7760008201527f6e6572206e6f7220617070726f76656420666f7220616c6c0000000000000000602082015250565b6000611f956038836118cb565b9150611fa082611f39565b604082019050919050565b60006020820190508181036000830152611fc481611f88565b9050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b60006120058261197b565b91507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff82141561203857612037611fcb565b5b600182019050919050565b7f4552433732313a207472616e736665722063616c6c6572206973206e6f74206f60008201527f776e6572206e6f7220617070726f766564000000000000000000000000000000602082015250565b600061209f6031836118cb565b91506120aa82612043565b604082019050919050565b600060208201905081810360008301526120ce81612092565b9050919050565b7f4552433732313a206f776e657220717565727920666f72206e6f6e657869737460008201527f656e7420746f6b656e0000000000000000000000000000000000000000000000602082015250565b60006121316029836118cb565b915061213c826120d5565b604082019050919050565b6000602082019050818103600083015261216081612124565b9050919050565b7f4552433732313a2062616c616e636520717565727920666f7220746865207a6560008201527f726f206164647265737300000000000000000000000000000000000000000000602082015250565b60006121c3602a836118cb565b91506121ce82612167565b604082019050919050565b600060208201905081810360008301526121f2816121b6565b9050919050565b7f4552433732313a20617070726f766520746f2063616c6c657200000000000000600082015250565b600061222f6019836118cb565b915061223a826121f9565b602082019050919050565b6000602082019050818103600083015261225e81612222565b9050919050565b600081905092915050565b7f7b226e616d65223a202254657374204461707020436f6c6c65637469626c657360008201527f2023000000000000000000000000000000000000000000000000000000000000602082015250565b60006122cc602283612265565b91506122d782612270565b602282019050919050565b60006122ed826118c0565b6122f78185612265565b93506123078185602086016118dc565b80840191505092915050565b7f222c20226465736372697074696f6e223a202254657374204461707020436f6c60008201527f6c65637469626c657320666f722074657374696e672e222c2022696d6167652260208201527f3a2022646174613a696d6167652f7376672b786d6c3b6261736536342c000000604082015250565b6000612395605d83612265565b91506123a082612313565b605d82019050919050565b7f222c202261747472696275746573223a205b7b2274726169745f74797065223a60008201527f2022546f6b656e204964222c202276616c7565223a2022000000000000000000602082015250565b6000612407603783612265565b9150612412826123ab565b603782019050919050565b7f227d5d7d00000000000000000000000000000000000000000000000000000000600082015250565b6000612453600483612265565b915061245e8261241d565b600482019050919050565b6000612474826122bf565b915061248082866122e2565b915061248b82612388565b915061249782856122e2565b91506124a2826123fa565b91506124ae82846122e2565b91506124b982612446565b9150819050949350505050565b7f646174613a6170706c69636174696f6e2f6a736f6e3b6261736536342c000000600082015250565b60006124fc601d83612265565b9150612507826124c6565b601d82019050919050565b600061251d826124ef565b915061252982846122e2565b915081905092915050565b7f4552433732313a206f70657261746f7220717565727920666f72206e6f6e657860008201527f697374656e7420746f6b656e0000000000000000000000000000000000000000602082015250565b6000612590602c836118cb565b915061259b82612534565b604082019050919050565b600060208201905081810360008301526125bf81612583565b9050919050565b7f4552433732313a207472616e73666572206f6620746f6b656e2074686174206960008201527f73206e6f74206f776e0000000000000000000000000000000000000000000000602082015250565b60006126226029836118cb565b915061262d826125c6565b604082019050919050565b6000602082019050818103600083015261265181612615565b9050919050565b7f4552433732313a207472616e7366657220746f20746865207a65726f2061646460008201527f7265737300000000000000000000000000000000000000000000000000000000602082015250565b60006126b46024836118cb565b91506126bf82612658565b604082019050919050565b600060208201905081810360008301526126e3816126a7565b9050919050565b60006126f58261197b565b91506127008361197b565b92508282101561271357612712611fcb565b5b828203905092915050565b60006127298261197b565b91506127348361197b565b9250827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0382111561276957612768611fcb565b5b828201905092915050565b7f4552433732313a207472616e7366657220746f206e6f6e20455243373231526560008201527f63656976657220696d706c656d656e7465720000000000000000000000000000602082015250565b60006127d06032836118cb565b91506127db82612774565b604082019050919050565b600060208201905081810360008301526127ff816127c3565b9050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601260045260246000fd5b60006128408261197b565b915061284b8361197b565b92508261285b5761285a612806565b5b828204905092915050565b60006128718261197b565b915061287c8361197b565b92508261288c5761288b612806565b5b828206905092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b60006128d18261197b565b91506128dc8361197b565b9250817fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff048311821515161561291557612914611fcb565b5b828202905092915050565b600081519050919050565b600082825260208201905092915050565b600061294782612920565b612951818561292b565b93506129618185602086016118dc565b61296a8161190f565b840191505092915050565b600060808201905061298a6000830187611a10565b6129976020830186611a10565b6129a46040830185611b26565b81810360608301526129b6818461293c565b905095945050505050565b6000815190506129d081611831565b92915050565b6000602082840312156129ec576129eb6117fb565b5b60006129fa848285016129c1565b91505092915050565b7f4552433732313a206d696e7420746f20746865207a65726f2061646472657373600082015250565b6000612a396020836118cb565b9150612a4482612a03565b602082019050919050565b60006020820190508181036000830152612a6881612a2c565b9050919050565b7f4552433732313a20746f6b656e20616c7265616479206d696e74656400000000600082015250565b6000612aa5601c836118cb565b9150612ab082612a6f565b602082019050919050565b60006020820190508181036000830152612ad481612a98565b905091905056fe4142434445464748494a4b4c4d4e4f505152535455565758595a6162636465666768696a6b6c6d6e6f707172737475767778797a303132333435363738392b2f3c737667206865696768743d22333530222077696474683d22333530222076696577426f783d2230203020313030203130302220786d6c6e733d22687474703a2f2f7777772e77332e6f72672f323030302f737667223e3c646566733e3c706174682069643d224d7950617468222066696c6c3d226e6f6e6522207374726f6b653d227265642220643d224d31302c3930205139302c39302039302c3435205139302c31302035302c3130205131302c31302031302c3430205131302c37302034352c3730205137302c37302037352c353022202f3e3c2f646566733e3c746578743e3c746578745061746820687265663d22234d7950617468223e517569636b2062726f776e20666f78206a756d7073206f76657220746865206c617a7920646f672e3c2f74657874506174683e3c2f746578743e3c2f7376673ea2646970667358221220f0a59ed63c319fae610757a3b494e324c8e785a4ed0cbfe168d1e09aa0a0b52164736f6c634300080a0033", + "failingContractAbi": [ + { "payable": true, "stateMutability": "payable", "type": "fallback" } + ], + "failingContractBytecode": "0x6080604052348015600f57600080fd5b50608b8061001e6000396000f3fe6080604052610fff3411600e57fe5b3373ffffffffffffffffffffffffffffffffffffffff166108fc349081150290604051600060405180830381858888f193505050501580156053573d6000803e3d6000fd5b5000fea265627a7a72315820631b0dbb6b871cdbfdec2773af15ebfb8e52c794cf836fe27ec21f1aed17180f64736f6c634300050c0032" } diff --git a/tests/test-dapp/index.css b/tests/test-dapp/index.css index c6fa9105f..9ab163bb7 100644 --- a/tests/test-dapp/index.css +++ b/tests/test-dapp/index.css @@ -41,3 +41,30 @@ header { #encryptMessageInput { margin-bottom: 1rem; } + +.error-div { + margin-bottom: 12px; +} + +.error-message { + min-height: 32px; + border: 1px solid #e88f97; + color: #24292e; + background: #fcf2f3; + border-radius: 8px; + display: flex; + justify-content: flex-start; + align-items: center; + padding: 8px 10px; +} +.error-message-icon { + margin-right: 8px; + flex: 0 0 auto; +} +.error-message-text { + overflow: auto; +} + +.warning-invisible { + display: none; +} diff --git a/tests/test-dapp/index.html b/tests/test-dapp/index.html index 8c9299e9b..9fc5971eb 100644 --- a/tests/test-dapp/index.html +++ b/tests/test-dapp/index.html @@ -33,7 +33,17 @@

Status

- +
+
+ +
You are on the Ethereum Mainnet.
+
+

@@ -149,7 +159,7 @@


- Contract + Piggy bank contract

+ + + +

+ Failing Contract Status: Not clicked +

@@ -250,6 +285,102 @@

class="col-xl-4 col-lg-6 col-md-12 col-sm-12 col-12 d-flex align-items-stretch" >
+
+

+ Collectibles +

+ + + +
+ + +
+ +
+ +
+ +
+ + +
+ +
+ +
+ +
+ +
+ +
+ + +
+ +
+ +
+ +

+ Collectibles: +

+
+
+ + + + +
+
+
+

Encrypt / Decrypt @@ -499,14 +630,96 @@

id="addEthereumChain" disabled > - Add xDAI Chain + Add Localhost 8546 +

+
+
+
+
+
+
+
+
+
+

+ Send form +

+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
diff --git a/tests/test-dapp/index.js b/tests/test-dapp/index.js new file mode 100644 index 000000000..63f413abb --- /dev/null +++ b/tests/test-dapp/index.js @@ -0,0 +1,1400 @@ +import MetaMaskOnboarding from '@metamask/onboarding'; +// eslint-disable-next-line camelcase +import { + encrypt, + recoverPersonalSignature, + recoverTypedSignatureLegacy, + recoverTypedSignature, + recoverTypedSignature_v4 as recoverTypedSignatureV4, +} from 'eth-sig-util'; +import { ethers } from 'ethers'; +import { toChecksumAddress } from 'ethereumjs-util'; +import { + hstBytecode, + hstAbi, + piggybankBytecode, + piggybankAbi, + collectiblesAbi, + collectiblesBytecode, + failingContractAbi, + failingContractBytecode, +} from './constants.json'; + +let ethersProvider; +let hstFactory; +let piggybankFactory; +let collectiblesFactory; +let failingContractFactory; +let hstContract; +let piggybankContract; +let collectiblesContract; +let failingContract; + +const currentUrl = new URL(window.location.href); +const forwarderOrigin = + currentUrl.hostname === 'localhost' ? 'http://localhost:9010' : undefined; +const urlSearchParams = new URLSearchParams(window.location.search); +const deployedContractAddress = urlSearchParams.get('contract'); + +const { isMetaMaskInstalled } = MetaMaskOnboarding; + +// Dapp Status Section +const networkDiv = document.getElementById('network'); +const chainIdDiv = document.getElementById('chainId'); +const accountsDiv = document.getElementById('accounts'); +const warningDiv = document.getElementById('warning'); + +// Basic Actions Section +const onboardButton = document.getElementById('connectButton'); +const getAccountsButton = document.getElementById('getAccounts'); +const getAccountsResults = document.getElementById('getAccountsResult'); + +// Permissions Actions Section +const requestPermissionsButton = document.getElementById('requestPermissions'); +const getPermissionsButton = document.getElementById('getPermissions'); +const permissionsResult = document.getElementById('permissionsResult'); + +// Contract Section +const deployButton = document.getElementById('deployButton'); +const depositButton = document.getElementById('depositButton'); +const withdrawButton = document.getElementById('withdrawButton'); +const contractStatus = document.getElementById('contractStatus'); +const deployFailingButton = document.getElementById('deployFailingButton'); +const sendFailingButton = document.getElementById('sendFailingButton'); +const failingContractStatus = document.getElementById('failingContractStatus'); + +// Collectibles Section +const deployCollectiblesButton = document.getElementById( + 'deployCollectiblesButton', +); +const mintButton = document.getElementById('mintButton'); +const mintAmountInput = document.getElementById('mintAmountInput'); +const approveTokenInput = document.getElementById('approveTokenInput'); +const approveButton = document.getElementById('approveButton'); +const setApprovalForAllButton = document.getElementById( + 'setApprovalForAllButton', +); +const transferTokenInput = document.getElementById('transferTokenInput'); +const transferFromButton = document.getElementById('transferFromButton'); +const collectiblesStatus = document.getElementById('collectiblesStatus'); + +// Send Eth Section +const sendButton = document.getElementById('sendButton'); +const sendEIP1559Button = document.getElementById('sendEIP1559Button'); + +// Send Tokens Section +const decimalUnits = 4; +const tokenSymbol = 'TST'; +const tokenAddress = document.getElementById('tokenAddress'); +const createToken = document.getElementById('createToken'); +const watchAsset = document.getElementById('watchAsset'); +const transferTokens = document.getElementById('transferTokens'); +const approveTokens = document.getElementById('approveTokens'); +const transferTokensWithoutGas = document.getElementById( + 'transferTokensWithoutGas', +); +const approveTokensWithoutGas = document.getElementById( + 'approveTokensWithoutGas', +); + +// Encrypt / Decrypt Section +const getEncryptionKeyButton = document.getElementById( + 'getEncryptionKeyButton', +); +const encryptMessageInput = document.getElementById('encryptMessageInput'); +const encryptButton = document.getElementById('encryptButton'); +const decryptButton = document.getElementById('decryptButton'); +const encryptionKeyDisplay = document.getElementById('encryptionKeyDisplay'); +const ciphertextDisplay = document.getElementById('ciphertextDisplay'); +const cleartextDisplay = document.getElementById('cleartextDisplay'); + +// Ethereum Signature Section +const ethSign = document.getElementById('ethSign'); +const ethSignResult = document.getElementById('ethSignResult'); +const personalSign = document.getElementById('personalSign'); +const personalSignResult = document.getElementById('personalSignResult'); +const personalSignVerify = document.getElementById('personalSignVerify'); +const personalSignVerifySigUtilResult = document.getElementById( + 'personalSignVerifySigUtilResult', +); +const personalSignVerifyECRecoverResult = document.getElementById( + 'personalSignVerifyECRecoverResult', +); +const signTypedData = document.getElementById('signTypedData'); +const signTypedDataResult = document.getElementById('signTypedDataResult'); +const signTypedDataVerify = document.getElementById('signTypedDataVerify'); +const signTypedDataVerifyResult = document.getElementById( + 'signTypedDataVerifyResult', +); +const signTypedDataV3 = document.getElementById('signTypedDataV3'); +const signTypedDataV3Result = document.getElementById('signTypedDataV3Result'); +const signTypedDataV3Verify = document.getElementById('signTypedDataV3Verify'); +const signTypedDataV3VerifyResult = document.getElementById( + 'signTypedDataV3VerifyResult', +); +const signTypedDataV4 = document.getElementById('signTypedDataV4'); +const signTypedDataV4Result = document.getElementById('signTypedDataV4Result'); +const signTypedDataV4Verify = document.getElementById('signTypedDataV4Verify'); +const signTypedDataV4VerifyResult = document.getElementById( + 'signTypedDataV4VerifyResult', +); + +// Send form section +const fromDiv = document.getElementById('fromInput'); +const toDiv = document.getElementById('toInput'); +const type = document.getElementById('typeInput'); +const amount = document.getElementById('amountInput'); +const gasPrice = document.getElementById('gasInput'); +const maxFee = document.getElementById('maxFeeInput'); +const maxPriority = document.getElementById('maxPriorityFeeInput'); +const data = document.getElementById('dataInput'); +const gasPriceDiv = document.getElementById('gasPriceDiv'); +const maxFeeDiv = document.getElementById('maxFeeDiv'); +const maxPriorityDiv = document.getElementById('maxPriorityDiv'); +const submitFormButton = document.getElementById('submitForm'); + +// Miscellaneous +const addEthereumChain = document.getElementById('addEthereumChain'); +const switchEthereumChain = document.getElementById('switchEthereumChain'); + +const initialize = async () => { + try { + // We must specify the network as 'any' for ethers to allow network changes + ethersProvider = new ethers.providers.Web3Provider(window.ethereum, 'any'); + if (deployedContractAddress) { + hstContract = new ethers.Contract( + deployedContractAddress, + hstAbi, + ethersProvider.getSigner(), + ); + piggybankContract = new ethers.Contract( + deployedContractAddress, + piggybankAbi, + ethersProvider.getSigner(), + ); + collectiblesContract = new ethers.Contract( + deployedContractAddress, + collectiblesAbi, + ethersProvider.getSigner(), + ); + failingContract = new ethers.Contract( + deployedContractAddress, + failingContractAbi, + ethersProvider.getSigner(), + ); + } + hstFactory = new ethers.ContractFactory( + hstAbi, + hstBytecode, + ethersProvider.getSigner(), + ); + piggybankFactory = new ethers.ContractFactory( + piggybankAbi, + piggybankBytecode, + ethersProvider.getSigner(), + ); + collectiblesFactory = new ethers.ContractFactory( + collectiblesAbi, + collectiblesBytecode, + ethersProvider.getSigner(), + ); + failingContractFactory = new ethers.ContractFactory( + failingContractAbi, + failingContractBytecode, + ethersProvider.getSigner(), + ); + } catch (error) { + console.error(error); + } + + let onboarding; + try { + onboarding = new MetaMaskOnboarding({ forwarderOrigin }); + } catch (error) { + console.error(error); + } + + let accounts; + let accountButtonsInitialized = false; + + const accountButtons = [ + deployButton, + depositButton, + withdrawButton, + deployCollectiblesButton, + mintButton, + mintAmountInput, + approveTokenInput, + approveButton, + setApprovalForAllButton, + transferTokenInput, + transferFromButton, + deployFailingButton, + sendFailingButton, + sendButton, + createToken, + watchAsset, + transferTokens, + approveTokens, + transferTokensWithoutGas, + approveTokensWithoutGas, + getEncryptionKeyButton, + encryptMessageInput, + encryptButton, + decryptButton, + ethSign, + personalSign, + personalSignVerify, + signTypedData, + signTypedDataVerify, + signTypedDataV3, + signTypedDataV3Verify, + signTypedDataV4, + signTypedDataV4Verify, + ]; + + const isMetaMaskConnected = () => accounts && accounts.length > 0; + + const onClickInstall = () => { + onboardButton.innerText = 'Onboarding in progress'; + onboardButton.disabled = true; + onboarding.startOnboarding(); + }; + + const onClickConnect = async () => { + try { + const newAccounts = await ethereum.request({ + method: 'eth_requestAccounts', + }); + handleNewAccounts(newAccounts); + } catch (error) { + console.error(error); + } + }; + + const clearTextDisplays = () => { + encryptionKeyDisplay.innerText = ''; + encryptMessageInput.value = ''; + ciphertextDisplay.innerText = ''; + cleartextDisplay.innerText = ''; + }; + + const updateButtons = () => { + const accountButtonsDisabled = + !isMetaMaskInstalled() || !isMetaMaskConnected(); + if (accountButtonsDisabled) { + for (const button of accountButtons) { + button.disabled = true; + } + clearTextDisplays(); + } else { + deployButton.disabled = false; + deployCollectiblesButton.disabled = false; + sendButton.disabled = false; + deployFailingButton.disabled = false; + createToken.disabled = false; + personalSign.disabled = false; + signTypedData.disabled = false; + getEncryptionKeyButton.disabled = false; + ethSign.disabled = false; + personalSign.disabled = false; + signTypedData.disabled = false; + signTypedDataV3.disabled = false; + signTypedDataV4.disabled = false; + } + + if (isMetaMaskInstalled()) { + addEthereumChain.disabled = false; + switchEthereumChain.disabled = false; + } else { + onboardButton.innerText = 'Click here to install MetaMask!'; + onboardButton.onclick = onClickInstall; + onboardButton.disabled = false; + } + + if (isMetaMaskConnected()) { + onboardButton.innerText = 'Connected'; + onboardButton.disabled = true; + if (onboarding) { + onboarding.stopOnboarding(); + } + } else { + onboardButton.innerText = 'Connect'; + onboardButton.onclick = onClickConnect; + onboardButton.disabled = false; + } + + if (deployedContractAddress) { + // Piggy bank contract + contractStatus.innerHTML = 'Deployed'; + depositButton.disabled = false; + withdrawButton.disabled = false; + // Failing contract + failingContractStatus.innerHTML = 'Deployed'; + sendFailingButton.disabled = false; + // ERC721 Token - Collectibles contract + collectiblesStatus.innerHTML = 'Deployed'; + mintButton.disabled = false; + mintAmountInput.disabled = false; + approveTokenInput.disabled = false; + approveButton.disabled = false; + setApprovalForAllButton.disabled = false; + transferTokenInput.disabled = false; + transferFromButton.disabled = false; + // ERC20 Token - Send Tokens + tokenAddress.innerHTML = hstContract.address; + watchAsset.disabled = false; + transferTokens.disabled = false; + approveTokens.disabled = false; + transferTokensWithoutGas.disabled = false; + approveTokensWithoutGas.disabled = false; + } + }; + + addEthereumChain.onclick = async () => { + await ethereum.request({ + method: 'wallet_addEthereumChain', + params: [ + { + chainId: '0x53a', + rpcUrls: ['http://127.0.0.1:8546'], + chainName: 'Localhost 8546', + nativeCurrency: { name: 'TEST', decimals: 18, symbol: 'TEST' }, + blockExplorerUrls: null, + }, + ], + }); + }; + + switchEthereumChain.onclick = async () => { + await ethereum.request({ + method: 'wallet_switchEthereumChain', + params: [ + { + chainId: '0x53a', + }, + ], + }); + }; + + const initializeAccountButtons = () => { + if (accountButtonsInitialized) { + return; + } + accountButtonsInitialized = true; + + /** + * Piggy bank + */ + + deployButton.onclick = async () => { + contractStatus.innerHTML = 'Deploying'; + + try { + piggybankContract = await piggybankFactory.deploy(); + await piggybankContract.deployTransaction.wait(); + } catch (error) { + contractStatus.innerHTML = 'Deployment Failed'; + throw error; + } + + if (piggybankContract.address === undefined) { + return; + } + + console.log( + `Contract mined! address: ${piggybankContract.address} transactionHash: ${piggybankContract.deployTransaction.hash}`, + ); + contractStatus.innerHTML = 'Deployed'; + depositButton.disabled = false; + withdrawButton.disabled = false; + }; + + depositButton.onclick = async () => { + contractStatus.innerHTML = 'Deposit initiated'; + const result = await piggybankContract.deposit({ + from: accounts[0], + value: '0x3782dace9d900000', + }); + console.log(result); + const receipt = await result.wait(); + console.log(receipt); + contractStatus.innerHTML = 'Deposit completed'; + }; + + withdrawButton.onclick = async () => { + const result = await piggybankContract.withdraw('0xde0b6b3a7640000', { + from: accounts[0], + }); + console.log(result); + const receipt = await result.wait(); + console.log(receipt); + contractStatus.innerHTML = 'Withdrawn'; + }; + + /** + * Failing + */ + + deployFailingButton.onclick = async () => { + failingContractStatus.innerHTML = 'Deploying'; + + try { + failingContract = await failingContractFactory.deploy(); + await failingContract.deployTransaction.wait(); + } catch (error) { + failingContractStatus.innerHTML = 'Deployment Failed'; + throw error; + } + + if (failingContract.address === undefined) { + return; + } + + console.log( + `Contract mined! address: ${failingContract.address} transactionHash: ${failingContract.deployTransaction.hash}`, + ); + failingContractStatus.innerHTML = 'Deployed'; + sendFailingButton.disabled = false; + }; + + sendFailingButton.onclick = async () => { + try { + const result = await ethereum.request({ + method: 'eth_sendTransaction', + params: [ + { + from: accounts[0], + to: failingContract.address, + value: '0x0', + gasLimit: '0x5028', + maxFeePerGas: '0x2540be400', + maxPriorityFeePerGas: '0x3b9aca00', + }, + ], + }); + failingContractStatus.innerHTML = + 'Failed transaction process completed as expected.'; + console.log('send failing contract result', result); + } catch (error) { + console.log('error', error); + throw error; + } + }; + + /** + * ERC721 Token + */ + + deployCollectiblesButton.onclick = async () => { + collectiblesStatus.innerHTML = 'Deploying'; + + try { + collectiblesContract = await collectiblesFactory.deploy(); + await collectiblesContract.deployTransaction.wait(); + } catch (error) { + collectiblesStatus.innerHTML = 'Deployment Failed'; + throw error; + } + + if (collectiblesContract.address === undefined) { + return; + } + + console.log( + `Contract mined! address: ${collectiblesContract.address} transactionHash: ${collectiblesContract.deployTransaction.hash}`, + ); + collectiblesStatus.innerHTML = 'Deployed'; + mintButton.disabled = false; + mintAmountInput.disabled = false; + }; + + mintButton.onclick = async () => { + collectiblesStatus.innerHTML = 'Mint initiated'; + let result = await collectiblesContract.mintCollectibles( + mintAmountInput.value, + { + from: accounts[0], + }, + ); + result = await result.wait(); + console.log(result); + collectiblesStatus.innerHTML = 'Mint completed'; + approveTokenInput.disabled = false; + approveButton.disabled = false; + setApprovalForAllButton.disabled = false; + transferTokenInput.disabled = false; + transferFromButton.disabled = false; + }; + + approveButton.onclick = async () => { + collectiblesStatus.innerHTML = 'Approve initiated'; + let result = await collectiblesContract.approve( + '0x9bc5baF874d2DA8D216aE9f137804184EE5AfEF4', + approveTokenInput.value, + { + from: accounts[0], + }, + ); + result = await result.wait(); + console.log(result); + collectiblesStatus.innerHTML = 'Approve completed'; + }; + + setApprovalForAllButton.onclick = async () => { + collectiblesStatus.innerHTML = 'Set Approval For All initiated'; + let result = await collectiblesContract.setApprovalForAll( + '0x9bc5baF874d2DA8D216aE9f137804184EE5AfEF4', + true, + { + from: accounts[0], + }, + ); + result = await result.wait(); + console.log(result); + collectiblesStatus.innerHTML = 'Set Approval For All completed'; + }; + + transferFromButton.onclick = async () => { + collectiblesStatus.innerHTML = 'Transfer From initiated'; + let result = await collectiblesContract.transferFrom( + accounts[0], + '0x2f318C334780961FB129D2a6c30D0763d9a5C970', + transferTokenInput.value, + { + from: accounts[0], + }, + ); + result = await result.wait(); + console.log(result); + collectiblesStatus.innerHTML = 'Transfer From completed'; + }; + + /** + * Sending ETH + */ + + sendButton.onclick = async () => { + const result = await ethereum.request({ + method: 'eth_sendTransaction', + params: [ + { + from: accounts[0], + to: '0x0c54FcCd2e384b4BB6f2E405Bf5Cbc15a017AaFb', + value: '0x0', + gasLimit: '0x5028', + gasPrice: '0x2540be400', + type: '0x0', + }, + ], + }); + console.log(result); + }; + + sendEIP1559Button.onclick = async () => { + const result = await ethereum.request({ + method: 'eth_sendTransaction', + params: [ + { + from: accounts[0], + to: '0x0c54FcCd2e384b4BB6f2E405Bf5Cbc15a017AaFb', + value: '0x0', + gasLimit: '0x5028', + maxFeePerGas: '0x2540be400', + maxPriorityFeePerGas: '0x3b9aca00', + }, + ], + }); + console.log(result); + }; + + /** + * ERC20 Token + */ + + createToken.onclick = async () => { + const _initialAmount = 100; + const _tokenName = 'TST'; + + try { + hstContract = await hstFactory.deploy( + _initialAmount, + _tokenName, + decimalUnits, + tokenSymbol, + ); + await hstContract.deployTransaction.wait(); + } catch (error) { + tokenAddress.innerHTML = 'Creation Failed'; + throw error; + } + + if (hstContract.address === undefined) { + return; + } + + console.log( + `Contract mined! address: ${hstContract.address} transactionHash: ${hstContract.deployTransaction.hash}`, + ); + tokenAddress.innerHTML = hstContract.address; + watchAsset.disabled = false; + transferTokens.disabled = false; + approveTokens.disabled = false; + transferTokensWithoutGas.disabled = false; + approveTokensWithoutGas.disabled = false; + }; + + watchAsset.onclick = async () => { + const result = await ethereum.request({ + method: 'wallet_watchAsset', + params: { + type: 'ERC20', + options: { + address: hstContract.address, + symbol: tokenSymbol, + decimals: decimalUnits, + image: 'https://metamask.github.io/test-dapp/metamask-fox.svg', + }, + }, + }); + console.log('result', result); + }; + + transferTokens.onclick = async () => { + const result = await hstContract.transfer( + '0x2f318C334780961FB129D2a6c30D0763d9a5C970', + '15000', + { + from: accounts[0], + gasLimit: 60000, + gasPrice: '20000000000', + }, + ); + console.log('result', result); + }; + + approveTokens.onclick = async () => { + const result = await hstContract.approve( + '0x9bc5baF874d2DA8D216aE9f137804184EE5AfEF4', + '70000', + { + from: accounts[0], + gasLimit: 60000, + gasPrice: '20000000000', + }, + ); + console.log('result', result); + }; + + transferTokensWithoutGas.onclick = async () => { + const result = await hstContract.transfer( + '0x2f318C334780961FB129D2a6c30D0763d9a5C970', + '15000', + { + gasPrice: '20000000000', + }, + ); + console.log('result', result); + }; + + approveTokensWithoutGas.onclick = async () => { + const result = await hstContract.approve( + '0x2f318C334780961FB129D2a6c30D0763d9a5C970', + '70000', + { + gasPrice: '20000000000', + }, + ); + console.log('result', result); + }; + + /** + * Permissions + */ + + requestPermissionsButton.onclick = async () => { + try { + const permissionsArray = await ethereum.request({ + method: 'wallet_requestPermissions', + params: [{ eth_accounts: {} }], + }); + permissionsResult.innerHTML = + getPermissionsDisplayString(permissionsArray); + } catch (err) { + console.error(err); + permissionsResult.innerHTML = `Error: ${err.message}`; + } + }; + + getPermissionsButton.onclick = async () => { + try { + const permissionsArray = await ethereum.request({ + method: 'wallet_getPermissions', + }); + permissionsResult.innerHTML = + getPermissionsDisplayString(permissionsArray); + } catch (err) { + console.error(err); + permissionsResult.innerHTML = `Error: ${err.message}`; + } + }; + + getAccountsButton.onclick = async () => { + try { + const _accounts = await ethereum.request({ + method: 'eth_accounts', + }); + getAccountsResults.innerHTML = + _accounts[0] || 'Not able to get accounts'; + } catch (err) { + console.error(err); + getAccountsResults.innerHTML = `Error: ${err.message}`; + } + }; + + /** + * Encrypt / Decrypt + */ + + getEncryptionKeyButton.onclick = async () => { + try { + encryptionKeyDisplay.innerText = await ethereum.request({ + method: 'eth_getEncryptionPublicKey', + params: [accounts[0]], + }); + encryptMessageInput.disabled = false; + } catch (error) { + encryptionKeyDisplay.innerText = `Error: ${error.message}`; + encryptMessageInput.disabled = true; + encryptButton.disabled = true; + decryptButton.disabled = true; + } + }; + + encryptMessageInput.onkeyup = () => { + if ( + !getEncryptionKeyButton.disabled && + encryptMessageInput.value.length > 0 + ) { + if (encryptButton.disabled) { + encryptButton.disabled = false; + } + } else if (!encryptButton.disabled) { + encryptButton.disabled = true; + } + }; + + encryptButton.onclick = () => { + try { + ciphertextDisplay.innerText = stringifiableToHex( + encrypt( + encryptionKeyDisplay.innerText, + { data: encryptMessageInput.value }, + 'x25519-xsalsa20-poly1305', + ), + ); + decryptButton.disabled = false; + } catch (error) { + ciphertextDisplay.innerText = `Error: ${error.message}`; + decryptButton.disabled = true; + } + }; + + decryptButton.onclick = async () => { + try { + cleartextDisplay.innerText = await ethereum.request({ + method: 'eth_decrypt', + params: [ciphertextDisplay.innerText, ethereum.selectedAddress], + }); + } catch (error) { + cleartextDisplay.innerText = `Error: ${error.message}`; + } + }; + }; + + type.onchange = async () => { + if (type.value === '0x0') { + gasPriceDiv.style.display = 'block'; + maxFeeDiv.style.display = 'none'; + maxPriorityDiv.style.display = 'none'; + } else { + gasPriceDiv.style.display = 'none'; + maxFeeDiv.style.display = 'block'; + maxPriorityDiv.style.display = 'block'; + } + }; + + submitFormButton.onclick = async () => { + let params; + if (type.value === '0x0') { + params = [ + { + from: accounts[0], + to: toDiv.value, + value: amount.value, + gasPrice: gasPrice.value, + type: type.value, + data: data.value, + }, + ]; + } else { + params = [ + { + from: accounts[0], + to: toDiv.value, + value: amount.value, + maxFeePerGas: maxFee.value, + maxPriorityFeePerGas: maxPriority.value, + type: type.value, + data: data.value, + }, + ]; + } + const result = await ethereum.request({ + method: 'eth_sendTransaction', + params, + }); + console.log(result); + }; + + /** + * eth_sign + */ + ethSign.onclick = async () => { + try { + // const msg = 'Sample message to hash for signature' + // const msgHash = keccak256(msg) + const msg = + '0x879a053d4800c6354e76c7985a865d2922c82fb5b3f4577b2fe08b998954f2e0'; + const ethResult = await ethereum.request({ + method: 'eth_sign', + params: [accounts[0], msg], + }); + ethSignResult.innerHTML = JSON.stringify(ethResult); + } catch (err) { + console.error(err); + ethSign.innerHTML = `Error: ${err.message}`; + } + }; + + /** + * Personal Sign + */ + personalSign.onclick = async () => { + const exampleMessage = 'Example `personal_sign` message'; + try { + const from = accounts[0]; + const msg = `0x${Buffer.from(exampleMessage, 'utf8').toString('hex')}`; + const sign = await ethereum.request({ + method: 'personal_sign', + params: [msg, from, 'Example password'], + }); + personalSignResult.innerHTML = sign; + personalSignVerify.disabled = false; + } catch (err) { + console.error(err); + personalSign.innerHTML = `Error: ${err.message}`; + } + }; + + /** + * Personal Sign Verify + */ + personalSignVerify.onclick = async () => { + const exampleMessage = 'Example `personal_sign` message'; + try { + const from = accounts[0]; + const msg = `0x${Buffer.from(exampleMessage, 'utf8').toString('hex')}`; + const sign = personalSignResult.innerHTML; + const recoveredAddr = recoverPersonalSignature({ + data: msg, + sig: sign, + }); + if (recoveredAddr === from) { + console.log(`SigUtil Successfully verified signer as ${recoveredAddr}`); + personalSignVerifySigUtilResult.innerHTML = recoveredAddr; + } else { + console.log( + `SigUtil Failed to verify signer when comparing ${recoveredAddr} to ${from}`, + ); + console.log(`Failed comparing ${recoveredAddr} to ${from}`); + } + const ecRecoverAddr = await ethereum.request({ + method: 'personal_ecRecover', + params: [msg, sign], + }); + if (ecRecoverAddr === from) { + console.log(`Successfully ecRecovered signer as ${ecRecoverAddr}`); + personalSignVerifyECRecoverResult.innerHTML = ecRecoverAddr; + } else { + console.log( + `Failed to verify signer when comparing ${ecRecoverAddr} to ${from}`, + ); + } + } catch (err) { + console.error(err); + personalSignVerifySigUtilResult.innerHTML = `Error: ${err.message}`; + personalSignVerifyECRecoverResult.innerHTML = `Error: ${err.message}`; + } + }; + + /** + * Sign Typed Data Test + */ + signTypedData.onclick = async () => { + const msgParams = [ + { + type: 'string', + name: 'Message', + value: 'Hi, Alice!', + }, + { + type: 'uint32', + name: 'A number', + value: '1337', + }, + ]; + try { + const from = accounts[0]; + const sign = await ethereum.request({ + method: 'eth_signTypedData', + params: [msgParams, from], + }); + signTypedDataResult.innerHTML = sign; + signTypedDataVerify.disabled = false; + } catch (err) { + console.error(err); + signTypedDataResult.innerHTML = `Error: ${err.message}`; + } + }; + + /** + * Sign Typed Data Verification + */ + signTypedDataVerify.onclick = async () => { + const msgParams = [ + { + type: 'string', + name: 'Message', + value: 'Hi, Alice!', + }, + { + type: 'uint32', + name: 'A number', + value: '1337', + }, + ]; + try { + const from = accounts[0]; + const sign = signTypedDataResult.innerHTML; + const recoveredAddr = await recoverTypedSignatureLegacy({ + data: msgParams, + sig: sign, + }); + if (toChecksumAddress(recoveredAddr) === toChecksumAddress(from)) { + console.log(`Successfully verified signer as ${recoveredAddr}`); + signTypedDataVerifyResult.innerHTML = recoveredAddr; + } else { + console.log( + `Failed to verify signer when comparing ${recoveredAddr} to ${from}`, + ); + } + } catch (err) { + console.error(err); + signTypedDataV3VerifyResult.innerHTML = `Error: ${err.message}`; + } + }; + + /** + * Sign Typed Data Version 3 Test + */ + signTypedDataV3.onclick = async () => { + const networkId = parseInt(networkDiv.innerHTML, 10); + const chainId = parseInt(chainIdDiv.innerHTML, 16) || networkId; + + const msgParams = { + types: { + EIP712Domain: [ + { name: 'name', type: 'string' }, + { name: 'version', type: 'string' }, + { name: 'chainId', type: 'uint256' }, + { name: 'verifyingContract', type: 'address' }, + ], + Person: [ + { name: 'name', type: 'string' }, + { name: 'wallet', type: 'address' }, + ], + Mail: [ + { name: 'from', type: 'Person' }, + { name: 'to', type: 'Person' }, + { name: 'contents', type: 'string' }, + ], + }, + primaryType: 'Mail', + domain: { + name: 'Ether Mail', + version: '1', + chainId, + verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC', + }, + message: { + from: { + name: 'Cow', + wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826', + }, + to: { + name: 'Bob', + wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB', + }, + contents: 'Hello, Bob!', + }, + }; + try { + const from = accounts[0]; + const sign = await ethereum.request({ + method: 'eth_signTypedData_v3', + params: [from, JSON.stringify(msgParams)], + }); + signTypedDataV3Result.innerHTML = sign; + signTypedDataV3Verify.disabled = false; + } catch (err) { + console.error(err); + signTypedDataV3Result.innerHTML = `Error: ${err.message}`; + } + }; + + /** + * Sign Typed Data V3 Verification + */ + signTypedDataV3Verify.onclick = async () => { + const networkId = parseInt(networkDiv.innerHTML, 10); + const chainId = parseInt(chainIdDiv.innerHTML, 16) || networkId; + + const msgParams = { + types: { + EIP712Domain: [ + { name: 'name', type: 'string' }, + { name: 'version', type: 'string' }, + { name: 'chainId', type: 'uint256' }, + { name: 'verifyingContract', type: 'address' }, + ], + Person: [ + { name: 'name', type: 'string' }, + { name: 'wallet', type: 'address' }, + ], + Mail: [ + { name: 'from', type: 'Person' }, + { name: 'to', type: 'Person' }, + { name: 'contents', type: 'string' }, + ], + }, + primaryType: 'Mail', + domain: { + name: 'Ether Mail', + version: '1', + chainId, + verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC', + }, + message: { + from: { + name: 'Cow', + wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826', + }, + to: { + name: 'Bob', + wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB', + }, + contents: 'Hello, Bob!', + }, + }; + try { + const from = accounts[0]; + const sign = signTypedDataV3Result.innerHTML; + const recoveredAddr = await recoverTypedSignature({ + data: msgParams, + sig: sign, + }); + if (toChecksumAddress(recoveredAddr) === toChecksumAddress(from)) { + console.log(`Successfully verified signer as ${recoveredAddr}`); + signTypedDataV3VerifyResult.innerHTML = recoveredAddr; + } else { + console.log( + `Failed to verify signer when comparing ${recoveredAddr} to ${from}`, + ); + } + } catch (err) { + console.error(err); + signTypedDataV3VerifyResult.innerHTML = `Error: ${err.message}`; + } + }; + + /** + * Sign Typed Data V4 + */ + signTypedDataV4.onclick = async () => { + const networkId = parseInt(networkDiv.innerHTML, 10); + const chainId = parseInt(chainIdDiv.innerHTML, 16) || networkId; + const msgParams = { + domain: { + chainId: chainId.toString(), + name: 'Ether Mail', + verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC', + version: '1', + }, + message: { + contents: 'Hello, Bob!', + from: { + name: 'Cow', + wallets: [ + '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826', + '0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF', + ], + }, + to: [ + { + name: 'Bob', + wallets: [ + '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB', + '0xB0BdaBea57B0BDABeA57b0bdABEA57b0BDabEa57', + '0xB0B0b0b0b0b0B000000000000000000000000000', + ], + }, + ], + }, + primaryType: 'Mail', + types: { + EIP712Domain: [ + { name: 'name', type: 'string' }, + { name: 'version', type: 'string' }, + { name: 'chainId', type: 'uint256' }, + { name: 'verifyingContract', type: 'address' }, + ], + Group: [ + { name: 'name', type: 'string' }, + { name: 'members', type: 'Person[]' }, + ], + Mail: [ + { name: 'from', type: 'Person' }, + { name: 'to', type: 'Person[]' }, + { name: 'contents', type: 'string' }, + ], + Person: [ + { name: 'name', type: 'string' }, + { name: 'wallets', type: 'address[]' }, + ], + }, + }; + try { + const from = accounts[0]; + const sign = await ethereum.request({ + method: 'eth_signTypedData_v4', + params: [from, JSON.stringify(msgParams)], + }); + signTypedDataV4Result.innerHTML = sign; + signTypedDataV4Verify.disabled = false; + } catch (err) { + console.error(err); + signTypedDataV4Result.innerHTML = `Error: ${err.message}`; + } + }; + + /** + * Sign Typed Data V4 Verification + */ + signTypedDataV4Verify.onclick = async () => { + const networkId = parseInt(networkDiv.innerHTML, 10); + const chainId = parseInt(chainIdDiv.innerHTML, 16) || networkId; + const msgParams = { + domain: { + chainId, + name: 'Ether Mail', + verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC', + version: '1', + }, + message: { + contents: 'Hello, Bob!', + from: { + name: 'Cow', + wallets: [ + '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826', + '0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF', + ], + }, + to: [ + { + name: 'Bob', + wallets: [ + '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB', + '0xB0BdaBea57B0BDABeA57b0bdABEA57b0BDabEa57', + '0xB0B0b0b0b0b0B000000000000000000000000000', + ], + }, + ], + }, + primaryType: 'Mail', + types: { + EIP712Domain: [ + { name: 'name', type: 'string' }, + { name: 'version', type: 'string' }, + { name: 'chainId', type: 'uint256' }, + { name: 'verifyingContract', type: 'address' }, + ], + Group: [ + { name: 'name', type: 'string' }, + { name: 'members', type: 'Person[]' }, + ], + Mail: [ + { name: 'from', type: 'Person' }, + { name: 'to', type: 'Person[]' }, + { name: 'contents', type: 'string' }, + ], + Person: [ + { name: 'name', type: 'string' }, + { name: 'wallets', type: 'address[]' }, + ], + }, + }; + try { + const from = accounts[0]; + const sign = signTypedDataV4Result.innerHTML; + const recoveredAddr = recoverTypedSignatureV4({ + data: msgParams, + sig: sign, + }); + if (toChecksumAddress(recoveredAddr) === toChecksumAddress(from)) { + console.log(`Successfully verified signer as ${recoveredAddr}`); + signTypedDataV4VerifyResult.innerHTML = recoveredAddr; + } else { + console.log( + `Failed to verify signer when comparing ${recoveredAddr} to ${from}`, + ); + } + } catch (err) { + console.error(err); + signTypedDataV4VerifyResult.innerHTML = `Error: ${err.message}`; + } + }; + + function handleNewAccounts(newAccounts) { + accounts = newAccounts; + accountsDiv.innerHTML = accounts; + fromDiv.value = accounts; + gasPriceDiv.style.display = 'block'; + maxFeeDiv.style.display = 'none'; + maxPriorityDiv.style.display = 'none'; + if (isMetaMaskConnected()) { + initializeAccountButtons(); + } + updateButtons(); + } + + function handleNewChain(chainId) { + chainIdDiv.innerHTML = chainId; + + if (chainId === '0x1') { + warningDiv.classList.remove('warning-invisible'); + } else { + warningDiv.classList.add('warning-invisible'); + } + } + + function handleEIP1559Support(supported) { + if (supported && Array.isArray(accounts) && accounts.length >= 1) { + sendEIP1559Button.disabled = false; + sendEIP1559Button.hidden = false; + sendButton.innerText = 'Send Legacy Transaction'; + } else { + sendEIP1559Button.disabled = true; + sendEIP1559Button.hidden = true; + sendButton.innerText = 'Send'; + } + } + + function handleNewNetwork(networkId) { + networkDiv.innerHTML = networkId; + } + + async function getNetworkAndChainId() { + try { + const chainId = await ethereum.request({ + method: 'eth_chainId', + }); + handleNewChain(chainId); + + const networkId = await ethereum.request({ + method: 'net_version', + }); + handleNewNetwork(networkId); + + const block = await ethereum.request({ + method: 'eth_getBlockByNumber', + params: ['latest', false], + }); + + handleEIP1559Support(block.baseFeePerGas !== undefined); + } catch (err) { + console.error(err); + } + } + + updateButtons(); + + if (isMetaMaskInstalled()) { + ethereum.autoRefreshOnNetworkChange = false; + getNetworkAndChainId(); + + ethereum.autoRefreshOnNetworkChange = false; + getNetworkAndChainId(); + + ethereum.on('chainChanged', (chain) => { + handleNewChain(chain); + ethereum + .request({ + method: 'eth_getBlockByNumber', + params: ['latest', false], + }) + .then((block) => { + handleEIP1559Support(block.baseFeePerGas !== undefined); + }); + }); + ethereum.on('chainChanged', handleNewNetwork); + ethereum.on('accountsChanged', (newAccounts) => { + ethereum + .request({ + method: 'eth_getBlockByNumber', + params: ['latest', false], + }) + .then((block) => { + handleEIP1559Support(block.baseFeePerGas !== undefined); + }); + handleNewAccounts(newAccounts); + }); + + try { + const newAccounts = await ethereum.request({ + method: 'eth_accounts', + }); + handleNewAccounts(newAccounts); + } catch (err) { + console.error('Error on init when getting accounts', err); + } + } +}; + +window.addEventListener('load', initialize); + +// utils + +function getPermissionsDisplayString(permissionsArray) { + if (permissionsArray.length === 0) { + return 'No permissions found.'; + } + const permissionNames = permissionsArray.map((perm) => perm.parentCapability); + return permissionNames + .reduce((acc, name) => `${acc}${name}, `, '') + .replace(/, $/u, ''); +} + +function stringifiableToHex(value) { + return ethers.utils.hexlify(Buffer.from(JSON.stringify(value))); +}