From bcbd2de5f361556d9f0b499410348eb6ff628020 Mon Sep 17 00:00:00 2001 From: ramil Date: Wed, 21 Apr 2021 16:05:24 +0300 Subject: [PATCH] integration tests: get balance --- test/contract/package.json | 1 + test/contract/src/index.js | 37 ++++++++++++++++++++---------- test/helper.go | 33 +++++++++++++++++++++++++- test/integration_test.go | 47 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+), 13 deletions(-) diff --git a/test/contract/package.json b/test/contract/package.json index f2c759a8..98733292 100644 --- a/test/contract/package.json +++ b/test/contract/package.json @@ -5,6 +5,7 @@ "scripts": { "compile": "npx hardhat compile", "start": "HARDHAT_NETWORK=docker node src/index.js", + "start:local": "ETH_ADDR=http://127.0.0.1:8545 npm run start", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], diff --git a/test/contract/src/index.js b/test/contract/src/index.js index 9fede5c4..37639542 100644 --- a/test/contract/src/index.js +++ b/test/contract/src/index.js @@ -22,18 +22,31 @@ fastify.get('/v1/deployContract', async (req, reply) => { } }); -// fastify.get('/v1/deployContract', async (req, reply) => { -// const GLDToken = await hre.ethers.getContractFactory("GLDToken"); -// const token = await GLDToken.deploy(); -// await token.deployed(); -// console.log("GLDToken deployed to:", token.address, token.deployTransaction); -// -// return { -// address: token.address, -// txHash: token.deployTransaction.hash, -// blockNumber: token.deployTransaction.blockNumber, -// } -// }); +fastify.get('/v1/sendEth', async (req, reply) => { + const to = req.query.to; + const value = req.query.value; + + const [owner] = await hre.ethers.getSigners(); + const tx = await owner.sendTransaction({ + to, + value: hre.ethers.utils.parseEther(value) + }); + await tx.wait(1) + + // console.log(tx); + // const coinbaseBalance = await hre.ethers.provider.getBalance(owner.address); + // const receiverBalance = await hre.ethers.provider.getBalance(to); + // console.log(coinbaseBalance.toString(), receiverBalance.toString()); + + return { + from: tx.from, + to: tx.to, + //value: tx.value.toString(), + txHash: tx.hash, + blockNumber: tx.blockNumber, + blockHash: tx.blockHash, + } +}); async function main() { try { diff --git a/test/helper.go b/test/helper.go index ac66014a..e458bfde 100644 --- a/test/helper.go +++ b/test/helper.go @@ -2,6 +2,8 @@ package integration import ( "encoding/json" + "fmt" + "math/big" "net/http" ) @@ -12,8 +14,19 @@ type ContractDeployed struct { BlockHash string `json:"blockHash"` } +type Tx struct { + From string `json:"from"` + To string `json:"to"` + Value *big.Int `json:"value"` + TransactionHash string `json:"txHash"` + BlockNumber int `json:"blockNumber"` + BlockHash string `json:"blockHash"` +} + +const srvUrl = "http://localhost:3000" + func DeployContract() (*ContractDeployed, error) { - res, err := http.Get("http://localhost:3000/v1/deployContract") + res, err := http.Get(fmt.Sprintf("%s/v1/deployContract", srvUrl)) if err != nil { return nil, err } @@ -29,3 +42,21 @@ func DeployContract() (*ContractDeployed, error) { return &contract, nil } + +func SendEth(to string, value string) (*Tx, error) { + res, err := http.Get(fmt.Sprintf("%s/v1/sendEth?to=%s&value=%s", srvUrl, to, value)) + if err != nil { + return nil, err + } + defer res.Body.Close() + + var tx Tx + + decoder := json.NewDecoder(res.Body) + err = decoder.Decode(&tx) + if err != nil { + return nil, err + } + + return &tx, nil +} diff --git a/test/integration_test.go b/test/integration_test.go index ba4b41ce..3e695b6b 100644 --- a/test/integration_test.go +++ b/test/integration_test.go @@ -206,6 +206,14 @@ var _ = Describe("Integration test", func() { Expect(err).ToNot(HaveOccurred()) Expect(gethCode).To(Equal(ipldCode)) }) + It("gets code of deployed contract with block number", func() { + gethCode, err := gethClient.CodeAt(ctx, common.HexToAddress(contract.Address), big.NewInt(int64(contract.BlockNumber))) + Expect(err).ToNot(HaveOccurred()) + + ipldCode, err := ipldClient.CodeAt(ctx, common.HexToAddress(contract.Address), big.NewInt(int64(contract.BlockNumber))) + Expect(err).ToNot(HaveOccurred()) + Expect(gethCode).To(Equal(ipldCode)) + }) It("gets code of contract that doesn't exist at this height", func() { gethCode, err := gethClient.CodeAt(ctx, common.HexToAddress(contract.Address), big.NewInt(int64(contract.BlockNumber-1))) Expect(err).ToNot(HaveOccurred()) @@ -218,6 +226,45 @@ var _ = Describe("Integration test", func() { }) }) + Describe("Get balance", func() { + address := "0x1111111111111111111111111111111111111112" + tx, txErr := integration.SendEth(address, "0.01") + + It("gets balance for an account with eth without block number", func() { + Expect(txErr).ToNot(HaveOccurred()) + + gethBalance, err := gethClient.BalanceAt(ctx, common.HexToAddress(address), nil) + Expect(err).ToNot(HaveOccurred()) + + ipldBalance, err := ipldClient.BalanceAt(ctx, common.HexToAddress(address), nil) + Expect(err).ToNot(HaveOccurred()) + + Expect(gethBalance).To(Equal(ipldBalance)) + }) + It("gets balance for an account with eth with block number", func() { + Expect(txErr).ToNot(HaveOccurred()) + + gethBalance, err := gethClient.BalanceAt(ctx, common.HexToAddress(address), big.NewInt(int64(tx.BlockNumber))) + Expect(err).ToNot(HaveOccurred()) + + ipldBalance, err := ipldClient.BalanceAt(ctx, common.HexToAddress(address), big.NewInt(int64(tx.BlockNumber))) + Expect(err).ToNot(HaveOccurred()) + + Expect(gethBalance).To(Equal(ipldBalance)) + }) + It("gets historical balance for an account with eth with block number", func() { + Expect(txErr).ToNot(HaveOccurred()) + + gethBalance, err := gethClient.BalanceAt(ctx, common.HexToAddress(address), big.NewInt(int64(tx.BlockNumber-1))) + Expect(err).ToNot(HaveOccurred()) + + ipldBalance, err := ipldClient.BalanceAt(ctx, common.HexToAddress(address), big.NewInt(int64(tx.BlockNumber-1))) + Expect(err).ToNot(HaveOccurred()) + + Expect(gethBalance).To(Equal(ipldBalance)) + }) + }) + Describe("Chain ID", func() { It("Check chain id", func() { gethChainId, err := gethClient.ChainID(ctx)