From 52c39db73f2529dbe6c359533f71f913e8d440ea Mon Sep 17 00:00:00 2001 From: Eason Smith Date: Mon, 11 Nov 2024 15:17:07 +0800 Subject: [PATCH 1/4] install ethers --- networks/ethereum/package.json | 3 ++- yarn.lock | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/networks/ethereum/package.json b/networks/ethereum/package.json index fea4e16f..4d466d88 100644 --- a/networks/ethereum/package.json +++ b/networks/ethereum/package.json @@ -31,7 +31,8 @@ "@ethersproject/transactions": "^5.7.0", "@interchainjs/types": "^0.0.1-beta.34", "@interchainjs/utils": "^0.0.1-beta.34", - "@noble/hashes": "^1.3.1" + "@noble/hashes": "^1.3.1", + "ethers": "^6.13.4" }, "keywords": [ "ethereum", diff --git a/yarn.lock b/yarn.lock index ba43fd40..7484eb7d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5553,6 +5553,13 @@ resolved "https://registry.yarnpkg.com/@types/node/-/node-18.15.13.tgz#f64277c341150c979e42b00e4ac289290c9df469" integrity sha512-N+0kuo9KgrUQ1Sn/ifDXsvg0TTleP7rIy4zOBGECxAljqvqfqpTfzx0Q1NUedOixRMBfe2Whhb056a42cWs26Q== +"@types/node@22.7.5": + version "22.7.5" + resolved "https://registry.yarnpkg.com/@types/node/-/node-22.7.5.tgz#cfde981727a7ab3611a481510b473ae54442b92b" + integrity sha512-jML7s2NAzMWc//QSJ1a3prpk78cOPchGvXJsC3C6R6PSMoooztvRVQEz89gmBTBY1SPMaqo5teB4uNHPdetShQ== + dependencies: + undici-types "~6.19.2" + "@types/node@^12.12.54": version "12.20.55" resolved "https://registry.yarnpkg.com/@types/node/-/node-12.20.55.tgz#c329cbd434c42164f846b909bd6f85b5537f6240" @@ -8632,6 +8639,19 @@ ethers@5.7.2: "@ethersproject/web" "5.7.1" "@ethersproject/wordlists" "5.7.0" +ethers@^6.13.4: + version "6.13.4" + resolved "https://registry.yarnpkg.com/ethers/-/ethers-6.13.4.tgz#bd3e1c3dc1e7dc8ce10f9ffb4ee40967a651b53c" + integrity sha512-21YtnZVg4/zKkCQPjrDj38B1r4nQvTZLopUGMLQ1ePU2zV/joCfDC3t3iKQjWRzjjjbzR+mdAIoikeBRNkdllA== + dependencies: + "@adraffy/ens-normalize" "1.10.1" + "@noble/curves" "1.2.0" + "@noble/hashes" "1.3.2" + "@types/node" "22.7.5" + aes-js "4.0.0-beta.5" + tslib "2.7.0" + ws "8.17.1" + ethers@^6.5.1: version "6.13.1" resolved "https://registry.yarnpkg.com/ethers/-/ethers-6.13.1.tgz#2b9f9c7455cde9d38b30fe6589972eb083652961" @@ -14651,6 +14671,11 @@ tslib@2.4.0: resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.4.0.tgz#7cecaa7f073ce680a05847aa77be941098f36dc3" integrity sha512-d6xOpEDfsi2CZVlPQzGeux8XMwLT9hssAsaPYExaQMuYskwb+x1x7J371tWlbBdWHroy99KnVB6qIkUbs5X3UQ== +tslib@2.7.0: + version "2.7.0" + resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.7.0.tgz#d9b40c5c40ab59e8738f297df3087bf1a2690c01" + integrity sha512-gLXCKdN1/j47AiHiOkJN69hJmcbGTHI0ImLmbYLHykhgeN0jVGola9yVjFgzCUklsZQMW55o+dW7IXv3RCXDzA== + tslib@^2.0.3, tslib@^2.1.0, tslib@^2.3.0, tslib@^2.4.0: version "2.6.2" resolved "https://registry.npmjs.org/tslib/-/tslib-2.6.2.tgz" @@ -14808,6 +14833,11 @@ undici-types@~5.26.4: resolved "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz" integrity sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA== +undici-types@~6.19.2: + version "6.19.8" + resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-6.19.8.tgz#35111c9d1437ab83a7cdc0abae2f26d88eda0a02" + integrity sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw== + unenv@^1.9.0: version "1.10.0" resolved "https://registry.yarnpkg.com/unenv/-/unenv-1.10.0.tgz#c3394a6c6e4cfe68d699f87af456fe3f0db39571" From 88c7e4776e0590ab88bba452159f5353ed0aebfb Mon Sep 17 00:00:00 2001 From: Eason Smith Date: Mon, 11 Nov 2024 15:17:24 +0800 Subject: [PATCH 2/4] unit test for ethers in local node --- networks/ethereum/ganache/README.md | 10 +++++ .../ethereum/ganache/__tests__/send.test.ts | 41 +++++++++++++++++++ networks/ethereum/ganache/scripts/index.js | 15 +++++++ 3 files changed, 66 insertions(+) create mode 100644 networks/ethereum/ganache/README.md create mode 100644 networks/ethereum/ganache/__tests__/send.test.ts create mode 100644 networks/ethereum/ganache/scripts/index.js diff --git a/networks/ethereum/ganache/README.md b/networks/ethereum/ganache/README.md new file mode 100644 index 00000000..291bf735 --- /dev/null +++ b/networks/ethereum/ganache/README.md @@ -0,0 +1,10 @@ +1. use ganache js to run a ethereum node locallly +``` +npx ganache \ + --account="0x0000000000000000000000000000000000000000000000000000000000000001,1" \ + --account="0x0000000000000000000000000000000000000000000000000000000000000002,1" +``` +2. run test: +``` +npx jest --preset ts-jest ganache/__tests__/send.test.ts +``` \ No newline at end of file diff --git a/networks/ethereum/ganache/__tests__/send.test.ts b/networks/ethereum/ganache/__tests__/send.test.ts new file mode 100644 index 00000000..c82ffb3c --- /dev/null +++ b/networks/ethereum/ganache/__tests__/send.test.ts @@ -0,0 +1,41 @@ +import { ethers } from 'ethers'; + +describe('ETH Transfer Test', () => { + let provider: ethers.JsonRpcProvider; + let wallet0: ethers.Wallet; + let wallet1: ethers.Wallet; + + const privateKey0 = '0x0000000000000000000000000000000000000000000000000000000000000001'; + const privateKey1 = '0x0000000000000000000000000000000000000000000000000000000000000002'; + + beforeAll(async () => { + provider = new ethers.JsonRpcProvider('http://127.0.0.1:8545'); + // provider = new ethers.BrowserProvider(window.ethereum) // if in browser + + wallet0 = new ethers.Wallet(privateKey0, provider); + // wallet0 = await provider.getSigner() // if in browser. like the offline signer in keplr cosmos + + wallet1 = new ethers.Wallet(privateKey1, provider); // only used to receive token + }); + + it('should transfer ETH from wallet0 to wallet1 and check balances', async () => { + const initialBalance0 = await provider.getBalance(wallet0.address); + const initialBalance1 = await provider.getBalance(wallet1.address); + + const amountToSend = ethers.parseEther('0.01'); + + const tx = await wallet0.sendTransaction({ + to: wallet1.address, + value: amountToSend, + }); + + await tx.wait(); + + const finalBalance0 = await provider.getBalance(wallet0.address); + const finalBalance1 = await provider.getBalance(wallet1.address); + + expect(finalBalance0).toBeLessThan(initialBalance0); + expect(finalBalance1).toBeGreaterThan(initialBalance1); + expect(finalBalance1).toEqual(initialBalance1 + amountToSend); + }); +}); \ No newline at end of file diff --git a/networks/ethereum/ganache/scripts/index.js b/networks/ethereum/ganache/scripts/index.js new file mode 100644 index 00000000..d0ed056c --- /dev/null +++ b/networks/ethereum/ganache/scripts/index.js @@ -0,0 +1,15 @@ +const { ethers } = require("ethers"); + +const provider = new ethers.providers.JsonRpcProvider("http://127.0.0.1:8545"); + +(async () => { + const accounts = await provider.listAccounts(); + console.log("Accounts:", accounts); + + const privateKey = "0x4f3edf983ac636a65a842ce7c78d9aa706d3b113b37c7a2b005b3a4e7d9f3c74"; // 示例私钥 + const wallet = new ethers.Wallet(privateKey, provider); + + console.log("Wallet address:", wallet.address); + const balance = await wallet.getBalance(); + console.log("Wallet balance:", ethers.utils.formatEther(balance), "ETH"); +})() \ No newline at end of file From 056d822c82ff59741e2126a1f076d82a91ee535d Mon Sep 17 00:00:00 2001 From: Eason Smith Date: Mon, 11 Nov 2024 15:25:13 +0800 Subject: [PATCH 3/4] updated readme --- networks/ethereum/ganache/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/networks/ethereum/ganache/README.md b/networks/ethereum/ganache/README.md index 291bf735..4b026f81 100644 --- a/networks/ethereum/ganache/README.md +++ b/networks/ethereum/ganache/README.md @@ -1,8 +1,8 @@ -1. use ganache js to run a ethereum node locallly +1. use ganache js to run a ethereum node locally ``` npx ganache \ - --account="0x0000000000000000000000000000000000000000000000000000000000000001,1" \ - --account="0x0000000000000000000000000000000000000000000000000000000000000002,1" + --account="0x0000000000000000000000000000000000000000000000000000000000000001,1000000000000000000" \ + --account="0x0000000000000000000000000000000000000000000000000000000000000002,1000000000000000000" ``` 2. run test: ``` From 60b9fb17e4158cace24a5c2618edc3ec3c945506 Mon Sep 17 00:00:00 2001 From: Eason Smith Date: Mon, 11 Nov 2024 15:31:32 +0800 Subject: [PATCH 4/4] remove some unused code --- networks/ethereum/ganache/scripts/index.js | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 networks/ethereum/ganache/scripts/index.js diff --git a/networks/ethereum/ganache/scripts/index.js b/networks/ethereum/ganache/scripts/index.js deleted file mode 100644 index d0ed056c..00000000 --- a/networks/ethereum/ganache/scripts/index.js +++ /dev/null @@ -1,15 +0,0 @@ -const { ethers } = require("ethers"); - -const provider = new ethers.providers.JsonRpcProvider("http://127.0.0.1:8545"); - -(async () => { - const accounts = await provider.listAccounts(); - console.log("Accounts:", accounts); - - const privateKey = "0x4f3edf983ac636a65a842ce7c78d9aa706d3b113b37c7a2b005b3a4e7d9f3c74"; // 示例私钥 - const wallet = new ethers.Wallet(privateKey, provider); - - console.log("Wallet address:", wallet.address); - const balance = await wallet.getBalance(); - console.log("Wallet balance:", ethers.utils.formatEther(balance), "ETH"); -})() \ No newline at end of file