diff --git a/dapp/package.json b/dapp/package.json index d1cd607..b54cf46 100644 --- a/dapp/package.json +++ b/dapp/package.json @@ -4,6 +4,7 @@ "description": "dapp learning ts", "dependencies": { "dotenv": "^16.4.5", + "ethers": "^6.13.1", "solc": "^0.8.26", "web3": "^4.10.0" }, diff --git a/dapp/src/call-contract-ethers.ts b/dapp/src/call-contract-ethers.ts new file mode 100644 index 0000000..7ffc0ea --- /dev/null +++ b/dapp/src/call-contract-ethers.ts @@ -0,0 +1,25 @@ +import { ethers } from 'ethers'; +import { getPrivateKey } from './deploy-contract-web3'; +import { getContractInfo } from './call-contract-web3'; + +export function getWallet(rpc: string): ethers.Wallet { + const provider = new ethers.JsonRpcProvider(rpc); + // const signer = provider.getSigner(); + const wallet = new ethers.Wallet(getPrivateKey(), provider); + return wallet; +} + +export async function callContract(wallet: ethers.Wallet, abi: any, contractAddress: string) { + const contractInstance = new ethers.Contract(contractAddress, abi, wallet); + const contractReader = new ethers.Contract(contractAddress, abi, wallet.provider); + contractReader.on('Increment', (value) => { + console.log(`listen in Increment, value ${value}`); + }) + let num = await contractReader.getNumber(); + console.log(`ethers call contract before update num:${num}`) + const addVal = 3; + const incReceipt = await contractInstance.increment(addVal); + await incReceipt.wait(); + num = await contractReader.getNumber(); + console.log(`ethers call contract update(inc ${addVal}), num is ${num}`); +} diff --git a/dapp/src/web3.ts b/dapp/src/web3.ts index 1dad0b1..89532ea 100644 --- a/dapp/src/web3.ts +++ b/dapp/src/web3.ts @@ -1,11 +1,14 @@ import { deploy } from './deploy-contract-web3'; import { getContractInfo, callContract, initWeb3, deployContract } from './call-contract-web3'; +import { getWallet, callContract as callContractByEthers } from './call-contract-ethers'; // deploy contract (async function () { - let { web3, account } = await initWeb3("wss://ethereum-sepolia-rpc.publicnode.com"); + const rpc = "https://rpc2.sepolia.org"; + let { web3, account } = await initWeb3(rpc); // let { contract, abi } = await deployContract(web3, account); let contract = "0x329f6ec542a232c44572659a8c47bd04beb02bc7"; let { abi } = getContractInfo(); if (contract) - await callContract(web3, account, abi, contract); + //await callContract(web3, account, abi, contract); + await callContractByEthers(getWallet(rpc), abi, contract); })() diff --git a/dapp/yarn.lock b/dapp/yarn.lock index 8663436..e62a91c 100644 --- a/dapp/yarn.lock +++ b/dapp/yarn.lock @@ -2,7 +2,7 @@ # yarn lockfile v1 -"@adraffy/ens-normalize@^1.8.8": +"@adraffy/ens-normalize@1.10.1", "@adraffy/ens-normalize@^1.8.8": version "1.10.1" resolved "https://registry.yarnpkg.com/@adraffy/ens-normalize/-/ens-normalize-1.10.1.tgz#63430d04bd8c5e74f8d7d049338f1cd9d4f02069" integrity sha512-96Z2IP3mYmF1Xg2cDm8f1gWGf/HUVedQ3FMifV4kG/PQ4yEP51xDtRAEfhVNt5f/uzpNkZHwWQuUcu6D6K+Ekw== @@ -12,6 +12,13 @@ resolved "https://registry.yarnpkg.com/@ethereumjs/rlp/-/rlp-4.0.1.tgz#626fabfd9081baab3d0a3074b0c7ecaf674aaa41" integrity sha512-tqsQiBQDQdmPWE1xkkBq4rlSW5QZpLOUJ5RJh2/9fug+q9tnUhuZoVLk7s0scUIKTOzEtR72DFBXI4WiZcMpvw== +"@noble/curves@1.2.0": + version "1.2.0" + resolved "https://registry.yarnpkg.com/@noble/curves/-/curves-1.2.0.tgz#92d7e12e4e49b23105a2555c6984d41733d65c35" + integrity sha512-oYclrNgRaM9SsBUBVbb8M6DTV7ZHRTKugureoYEncY5c65HOmRzvSiTE3y5CYaPYJA/GVkrhXEoF0M3Ya9PMnw== + dependencies: + "@noble/hashes" "1.3.2" + "@noble/curves@1.4.0", "@noble/curves@~1.4.0": version "1.4.0" resolved "https://registry.yarnpkg.com/@noble/curves/-/curves-1.4.0.tgz#f05771ef64da724997f69ee1261b2417a49522d6" @@ -19,6 +26,11 @@ dependencies: "@noble/hashes" "1.4.0" +"@noble/hashes@1.3.2": + version "1.3.2" + resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.3.2.tgz#6f26dbc8fbc7205873ce3cee2f690eba0d421b39" + integrity sha512-MVC8EAQp7MvEcm30KWENFjgR+Mkmf+D189XJTkFIlwohU5hcBbn1ZkKq7KVTi2Hme3PMGF390DaL52beVrIihQ== + "@noble/hashes@1.4.0", "@noble/hashes@~1.4.0": version "1.4.0" resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.4.0.tgz#45814aa329f30e4fe0ba49426f49dfccdd066426" @@ -53,6 +65,11 @@ dependencies: undici-types "~5.26.4" +"@types/node@18.15.13": + version "18.15.13" + resolved "https://registry.yarnpkg.com/@types/node/-/node-18.15.13.tgz#f64277c341150c979e42b00e4ac289290c9df469" + integrity sha512-N+0kuo9KgrUQ1Sn/ifDXsvg0TTleP7rIy4zOBGECxAljqvqfqpTfzx0Q1NUedOixRMBfe2Whhb056a42cWs26Q== + "@types/ws@8.5.3": version "8.5.3" resolved "https://registry.yarnpkg.com/@types/ws/-/ws-8.5.3.tgz#7d25a1ffbecd3c4f2d35068d0b283c037003274d" @@ -65,6 +82,11 @@ abitype@0.7.1: resolved "https://registry.yarnpkg.com/abitype/-/abitype-0.7.1.tgz#16db20abe67de80f6183cf75f3de1ff86453b745" integrity sha512-VBkRHTDZf9Myaek/dO3yMmOzB/y2s3Zo6nVU7yaw1G+TvCHAjwaJzNGN9yo4K5D8bU/VZXKP1EJpRhFr862PlQ== +aes-js@4.0.0-beta.5: + version "4.0.0-beta.5" + resolved "https://registry.yarnpkg.com/aes-js/-/aes-js-4.0.0-beta.5.tgz#8d2452c52adedebc3a3e28465d858c11ca315873" + integrity sha512-G965FqalsNyrPqgEGON7nIx1e/OVENSgiEIzyC63haUMuvNnwIgIjMs52hlTCKhkBny7A2ORNlfY9Zu+jmGk1Q== + available-typed-arrays@^1.0.7: version "1.0.7" resolved "https://registry.yarnpkg.com/available-typed-arrays/-/available-typed-arrays-1.0.7.tgz#a5cc375d6a03c2efc87a553f3e0b1522def14846" @@ -141,6 +163,19 @@ ethereum-cryptography@^2.0.0: "@scure/bip32" "1.4.0" "@scure/bip39" "1.3.0" +ethers@^6.13.1: + version "6.13.1" + resolved "https://registry.yarnpkg.com/ethers/-/ethers-6.13.1.tgz#2b9f9c7455cde9d38b30fe6589972eb083652961" + integrity sha512-hdJ2HOxg/xx97Lm9HdCWk949BfYqYWpyw4//78SiwOLgASyfrNszfMUNB2joKjvGUdwhHfaiMMFFwacVVoLR9A== + dependencies: + "@adraffy/ens-normalize" "1.10.1" + "@noble/curves" "1.2.0" + "@noble/hashes" "1.3.2" + "@types/node" "18.15.13" + aes-js "4.0.0-beta.5" + tslib "2.4.0" + ws "8.17.1" + eventemitter3@^5.0.1: version "5.0.1" resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-5.0.1.tgz#53f5ffd0a492ac800721bb42c66b841de96423c4" @@ -323,6 +358,11 @@ tr46@~0.0.3: resolved "https://registry.yarnpkg.com/tr46/-/tr46-0.0.3.tgz#8184fd347dac9cdc185992f3a6622e14b9d9ab6a" integrity sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw== +tslib@2.4.0: + version "2.4.0" + resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.4.0.tgz#7cecaa7f073ce680a05847aa77be941098f36dc3" + integrity sha512-d6xOpEDfsi2CZVlPQzGeux8XMwLT9hssAsaPYExaQMuYskwb+x1x7J371tWlbBdWHroy99KnVB6qIkUbs5X3UQ== + undici-types@~5.26.4: version "5.26.5" resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-5.26.5.tgz#bcd539893d00b56e964fd2657a4866b221a65617" @@ -587,7 +627,7 @@ which-typed-array@^1.1.14, which-typed-array@^1.1.2: gopd "^1.0.1" has-tostringtag "^1.0.2" -ws@^8.8.1: +ws@8.17.1, ws@^8.8.1: version "8.17.1" resolved "https://registry.yarnpkg.com/ws/-/ws-8.17.1.tgz#9293da530bb548febc95371d90f9c878727d919b" integrity sha512-6XQFvXTkbfUOZOKKILFG1PDK2NDQs4azKQl26T0YS5CxqWLgXajbPZ+h4gZekJyRqFU8pvnbAbbs/3TgRPy+GQ==