diff --git a/networks/ethereum/ganache/README.md b/networks/ethereum/ganache/README.md new file mode 100644 index 00000000..4b026f81 --- /dev/null +++ b/networks/ethereum/ganache/README.md @@ -0,0 +1,10 @@ +1. use ganache js to run a ethereum node locally +``` +npx ganache \ + --account="0x0000000000000000000000000000000000000000000000000000000000000001,1000000000000000000" \ + --account="0x0000000000000000000000000000000000000000000000000000000000000002,1000000000000000000" +``` +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/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 3e1b9843..bf097221 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"