Skip to content

Commit

Permalink
Adding missing web3 contract deployment tests (Zilliqa#3021)
Browse files Browse the repository at this point in the history
Co-authored-by: bzawisto <bartosz@zilliqa.com>
  • Loading branch information
bzawisto and bzawisto authored Sep 22, 2022
1 parent fab691a commit 29be3bb
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 31 deletions.
18 changes: 18 additions & 0 deletions tests/EvmAcceptanceTests/contracts/Deployment.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ contract WithUintConstructor {
}
}

contract WithAddressConstructor {
address public someAddress;

constructor(address _addr) {
someAddress = _addr;
}
}

contract WithEnumConstructor {
enum MyEnum { Zero, One, Two }

MyEnum public someEnum;

constructor(MyEnum _enum) {
someEnum = _enum;
}
}


contract MultiParamConstructor {
string public name;
Expand Down
41 changes: 27 additions & 14 deletions tests/EvmAcceptanceTests/helper/Web3Helper.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
const { web3 } = require("hardhat");
const general_helper = require('./GeneralHelper')

var web3_helper = {
primaryAccount: web3.eth.accounts.privateKeyToAccount(general_helper.getPrivateAddressAt(0)),
auxiliaryAccount: web3.eth.accounts.privateKeyToAccount(general_helper.getPrivateAddressAt(1)),

deploy: async function(contractName, ...arguments) {
const ContractRaw = hre.artifacts.readArtifactSync(contractName)
const contract = new web3.eth.Contract(ContractRaw.abi)
return contract.deploy({ data: ContractRaw.bytecode, arguments: arguments })
.send({
from: this.primaryAccount.address,
...(general_helper.isZilliqaNetworkSelected()) && { gas: 30_000 },
...(general_helper.isZilliqaNetworkSelected()) && { gasPrice: 2_000_000_000_000_000 },
})
.on('error', function (error) { console.log(error) })
class Web3Helper {
constructor() {
this.primaryAccount = web3.eth.accounts.privateKeyToAccount(general_helper.getPrivateAddressAt(0));
this.auxiliaryAccount = web3.eth.accounts.privateKeyToAccount(general_helper.getPrivateAddressAt(1));
}

getPrimaryAccount() {
return this.primaryAccount;
}

async deploy(contractName, options = {}, ...args) {
const contractRaw = hre.artifacts.readArtifactSync(contractName);
const contract = new web3.eth.Contract(contractRaw.abi);
const nonce = (options.nonce || await web3.eth.getTransactionCount(this.primaryAccount.address));
const gasPrice = (options.gasPrice || await web3.eth.getGasPrice());
const gasLimit = (options.gasLimit || 21_000);

const deployedContract = await contract.deploy({data: contractRaw.bytecode, arguments: args}).send({
from: this.primaryAccount.address,
nonce,
gas: gasLimit,
gasPrice: gasPrice,
value: options.value ?? 0
})

return deployedContract;
}
}

module.exports = web3_helper
module.exports = { Web3Helper}
11 changes: 5 additions & 6 deletions tests/EvmAcceptanceTests/helper/ZilliqaHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ class ZilliqaHelper {

const senderAccount = (options.senderAccount || this.auxiliaryAccount)
const constructorArgs = (options.constructorArgs || []);

// Give our Eth address some monies
await this.moveFunds("0x3635c9adc5dea00000", senderAccount.address)
const gasLimit = (options.gasLimit || 250000);
const gasPrice = (options.gasPrice || await web3.eth.getGasPrice());

// Deploy a SC using web3 API ONLY
const nonce = await web3.eth.getTransactionCount(senderAccount.address, 'latest'); // nonce starts counting from 0
Expand All @@ -46,8 +45,8 @@ class ZilliqaHelper {
'from': senderAccount.address,
'value': options.value ?? 0,
'data': Contract.getDeployTransaction(...constructorArgs).data,
'gas': 300000,
'gasPrice': 2000000000000000,
'gas': gasLimit,
'gasPrice': gasPrice,
'chainId': general_helper.getEthChainId(),
'nonce': nonce,
};
Expand Down Expand Up @@ -113,7 +112,7 @@ class ZilliqaHelper {
'to': toAddr,
'value': amount,
'gas': 21_000,
'gasPrice': 1_000_000_000,
'gasPrice': await web3.eth.getGasPrice(),
'nonce': nonce,
'chainId': general_helper.getEthChainId(),
'data': ""
Expand Down
113 changes: 102 additions & 11 deletions tests/EvmAcceptanceTests/test/ContractDeployment.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { expect } = require("chai");
const { ethers } = require("hardhat");
const { ethers, web3 } = require("hardhat");
const { ZilliqaHelper } = require('../helper/ZilliqaHelper');
const general_helper = require('../helper/GeneralHelper')
const web3_helper = require('../helper/Web3Helper')
const { Web3Helper } = require('../helper/Web3Helper')

describe("Contract Deployment", function () {
describe("Contract with zero parameter constructor", function () {
Expand Down Expand Up @@ -46,15 +46,16 @@ describe("Contract Deployment", function () {
describe("When web3.js is used", function () {
let contract;
before(async function () {
contract = await web3_helper.deploy("ZeroParamConstructor")
web3Helper = new Web3Helper();
contract = await web3Helper.deploy("ZeroParamConstructor", { gasLimit: 220000});
})

it("Should be deployed successfully", async function () {
expect(contract.options.address).exist;
})

it("Should return 123 when number view function is called", async function () {
expect(await contract.methods.number().call()).to.be.eq(ethers.BigNumber.from(123))
expect(await contract.methods.number().call()).to.be.eq(web3.utils.toBN(123));
})
});
})
Expand Down Expand Up @@ -170,20 +171,70 @@ describe("Contract Deployment", function () {
describe("When web3.js is used", function () {
describe("When constructor parameter is a uint256", async function () {
let contract;
let INITIAL_NUMBER = 100;
const INITIAL_NUMBER = 100;
const gasLimit = 220000;

before(async function () {
contract = await web3_helper.deploy("WithUintConstructor", INITIAL_NUMBER)
const web3Helper = new Web3Helper();
contract = await web3Helper.deploy("WithUintConstructor", { gasLimit }, INITIAL_NUMBER)
})

it("Should be deployed successfully", async function () {
expect(contract.options.address).exist;
})

it("Should return 100 when number view function is called", async function () {
expect(await contract.methods.number().call()).to.be.eq(ethers.BigNumber.from(INITIAL_NUMBER))
expect(await contract.methods.number().call()).to.be.eq(web3.utils.toBN(INITIAL_NUMBER))
})
})
describe("When constructor parameter is a string", async function () {
let contract;
let INITIAL_NAME = "Zilliqa";
const gasLimit = 220000;
before(async function () {
const web3Helper = new Web3Helper();
contract = await web3Helper.deploy("WithStringConstructor", { gasLimit }, INITIAL_NAME)
})

it("Should be deployed successfully", async function () {
expect(contract.options.address).exist;
})

it("Should return Zilliqa when name view function is called", async function () {
expect(await contract.methods.name().call()).to.be.eq(INITIAL_NAME)
})
})
describe("When constructor parameter is an address", async function () {
let contract;
let ADDRESS = "0x71C7656EC7ab88b098defB751B7401B5f6d8976F";
const gasLimit = "220000";
before(async function () {
const web3Helper = new Web3Helper();
contract = await web3Helper.deploy("WithAddressConstructor", { gasLimit }, ADDRESS)
})
it("Should be deployed successfully", async function () {
expect(contract.options.address).exist;
})

it("Should return constructor address when ctorAddress view function is called", async function () {
expect(await contract.methods.someAddress().call()).to.be.eq(ADDRESS)
})
})
describe("When constructor parameter is an enum", async function () {
let contract;
let ENUM = '1';
const gasLimit = "220000";
before(async function () {
const web3Helper = new Web3Helper();
contract = await web3Helper.deploy("WithEnumConstructor", { gasLimit }, ENUM)
})
it("Should be deployed successfully", async function () {
expect(contract.options.address).exist;
})
it("Should return constructor enum when someEnum view function is called", async function () {
expect(await contract.methods.someEnum().call()).to.be.eq(ENUM)
})
})
// TODO add the rest
});
})

Expand Down Expand Up @@ -238,8 +289,28 @@ describe("Contract Deployment", function () {
})
});

describe("When web3.js is used", function () {
// TODO
describe("When web3.js is used www", function () {
let contract;
let NAME = "Zilliqa"
let NUMBER = 100;
const gasLimit = "350000";

before(async function () {
const web3Helper = new Web3Helper();
contract = await web3Helper.deploy("MultiParamConstructor", { gasLimit }, NAME, NUMBER)
})

it("Should be deployed successfully", async function () {
expect(contract.options.address).exist;
})

it("Should return 100 when number view function is called", async function () {
expect(await contract.methods.number().call()).to.be.eq(web3.utils.toBN(NUMBER))
})

it("Should return Zilliqa when name view function is called", async function () {
expect(await contract.methods.name().call()).to.be.eq(NAME)
})
});
})

Expand Down Expand Up @@ -294,7 +365,27 @@ describe("Contract Deployment", function () {
});

describe("When web3.js is used", function () {
// TODO
let contract;
let INITIAL_BALANCE = 10;
const gasLimit = "350000";
let web3Helper;

before(async function () {
web3Helper = new Web3Helper();
contract = await web3Helper.deploy("WithPayableConstructor", { gasLimit, value: INITIAL_BALANCE })
})

it("Should be deployed successfully", async function () {
expect(contract.options.address).exist;
})

it("Should return 10 when balance view function is called", async function () {
expect(await contract.methods.balance().call()).to.be.eq(web3.utils.toBN(INITIAL_BALANCE))
})

it("Should return Zilliqa when name view function is called", async function () {
expect(await contract.methods.owner().call()).to.be.eq(web3Helper.getPrimaryAccount().address)
})
});
})
})

0 comments on commit 29be3bb

Please sign in to comment.