integration tests: get balance

This commit is contained in:
ramil 2021-04-21 16:05:24 +03:00
parent d09b756768
commit bcbd2de5f3
4 changed files with 105 additions and 13 deletions

View File

@ -5,6 +5,7 @@
"scripts": { "scripts": {
"compile": "npx hardhat compile", "compile": "npx hardhat compile",
"start": "HARDHAT_NETWORK=docker node src/index.js", "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" "test": "echo \"Error: no test specified\" && exit 1"
}, },
"keywords": [], "keywords": [],

View File

@ -22,18 +22,31 @@ fastify.get('/v1/deployContract', async (req, reply) => {
} }
}); });
// fastify.get('/v1/deployContract', async (req, reply) => { fastify.get('/v1/sendEth', async (req, reply) => {
// const GLDToken = await hre.ethers.getContractFactory("GLDToken"); const to = req.query.to;
// const token = await GLDToken.deploy(); const value = req.query.value;
// await token.deployed();
// console.log("GLDToken deployed to:", token.address, token.deployTransaction); const [owner] = await hre.ethers.getSigners();
// const tx = await owner.sendTransaction({
// return { to,
// address: token.address, value: hre.ethers.utils.parseEther(value)
// txHash: token.deployTransaction.hash, });
// blockNumber: token.deployTransaction.blockNumber, 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() { async function main() {
try { try {

View File

@ -2,6 +2,8 @@ package integration
import ( import (
"encoding/json" "encoding/json"
"fmt"
"math/big"
"net/http" "net/http"
) )
@ -12,8 +14,19 @@ type ContractDeployed struct {
BlockHash string `json:"blockHash"` 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) { 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 { if err != nil {
return nil, err return nil, err
} }
@ -29,3 +42,21 @@ func DeployContract() (*ContractDeployed, error) {
return &contract, nil 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
}

View File

@ -206,6 +206,14 @@ var _ = Describe("Integration test", func() {
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(gethCode).To(Equal(ipldCode)) 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() { 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))) gethCode, err := gethClient.CodeAt(ctx, common.HexToAddress(contract.Address), big.NewInt(int64(contract.BlockNumber-1)))
Expect(err).ToNot(HaveOccurred()) 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() { Describe("Chain ID", func() {
It("Check chain id", func() { It("Check chain id", func() {
gethChainId, err := gethClient.ChainID(ctx) gethChainId, err := gethClient.ChainID(ctx)