-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from cosmology-tech/ethersjs
Ethersjs
- Loading branch information
Showing
4 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters