Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
Use fixed GAS_LIMIT to avoid ExhaustsResources
Browse files Browse the repository at this point in the history
  • Loading branch information
raykyri committed Jun 3, 2021
1 parent a62a5ce commit 3874f3f
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 31 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"scripts": {
"compile": "truffle compile",
"add-liquidity": "mocha --timeout 1000000 --exit web3tests/addLiquidity.js",
"all-tests": "ts-mocha --timeout 100000 --exit web3tests"
"all-tests": "ts-mocha --timeout 100000 --exit web3tests",
"test": "ts-mocha --timeout 100000 --exit"
},
"author": "",
"license": "ISC",
Expand Down
5 changes: 5 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const account = '0x19e7e376e7c213b7e7e7e46cc70a5dd086daff2a';
// const privKey = '99B3C12287537E38C90A9219D4CB074A89A16E9CDB20BF85728EBD97C343E342';
const privKey = '1111111111111111111111111111111111111111111111111111111111111111';

const GAS_LIMIT = 6_700_000; // eth block gas limit is 15m now
const GAS_PRICE = 1_000_000_000; // 1 gwei

const initWeb3 = (pkey = privKey) => {
console.log('Initializing web3 provider at http://localhost:9933/');
// const provider = new Web3.providers.HttpProvider('http://localhost:9933/');
Expand Down Expand Up @@ -72,4 +75,6 @@ module.exports = {
deployContract,
convertToEvmAddress,
convertToSubstrateAddress,
GAS_PRICE,
GAS_LIMIT
}
5 changes: 1 addition & 4 deletions web3tests/addLiquidity.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const contract = require("@truffle/contract");
const { assert } = require("chai");
const { account, initWeb3 } = require('../utils');
const { account, initWeb3, GAS_PRICE, GAS_LIMIT } = require('../utils');
const { deploy } = require('../deploy');

const TokenA = require('../build/contracts/TokenA.json');
Expand All @@ -10,9 +10,6 @@ const UniswapV2Router02 = require('../node_modules/@uniswap/v2-periphery/build/U
const UniswapV2Factory = require('../node_modules/@uniswap/v2-core/build/UniswapV2Factory.json');
const UniswapV2Pair = require('../node_modules/@uniswap/v2-core/build/UniswapV2Pair.json');

const GAS_LIMIT = 6_700_000; // eth block gas limit is 15m now
const GAS_PRICE = 1_000_000_000; // 1 gwei

describe('Add Liquidity Test', () => {
let FACTORY_ADDRESS
let ROUTER_ADDRESS
Expand Down
16 changes: 8 additions & 8 deletions web3tests/testCreate.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { assert } = require("chai");
const rlp = require('rlp');
const keccak = require('keccak');
const { account, initWeb3 } = require('../utils');
const { account, initWeb3, GAS_PRICE, GAS_LIMIT } = require('../utils');

const CreateContract = require('../build/contracts/CreateContract.json');
const SubContract = require('../build/contracts/SubContract.json');
Expand All @@ -17,12 +17,12 @@ describe("CreateContract test", async () => {
});
Create.setProvider(web3.currentProvider);

let c = await Create.new({ from: account });
let c = await Create.new({ from: account, gasLimit: GAS_LIMIT });
let startNonce = await web3.eth.getTransactionCount(c.address);
console.log(`CreateContract address: ${c.address}, nonce: ${startNonce}`);
// create without value
let receipt = await c.spawn({ from: account });
let address = await c.deployed.call({ from: account });
let receipt = await c.spawn({ from: account, gasLimit: GAS_LIMIT });
let address = await c.deployed.call({ from: account, gasLimit: GAS_LIMIT });

var Sub = contract({
abi: SubContract.abi,
Expand All @@ -47,21 +47,21 @@ describe("CreateContract test", async () => {
});
Create.setProvider(web3.currentProvider);

let c = await Create.new({ from: account });
let c = await Create.new({ from: account, gasLimit: GAS_LIMIT });
let startNonce = await web3.eth.getTransactionCount(c.address);
console.log(`CreateContract address: ${c.address}, nonce: ${startNonce}`);
// create with value
const value = web3.utils.toWei('10', 'ether');
await c.spawnWithValue({ value, from: account });
address = await c.deployed.call({ from: account });
await c.spawnWithValue({ value, from: account, gasLimit: GAS_LIMIT });
address = await c.deployed.call({ from: account, gasLimit: GAS_LIMIT });
var Sub = contract({
abi: SubContract.abi,
unlinked_binary: SubContract.bytecode,
});
Sub.setProvider(web3.currentProvider);
let cSub = await Sub.at(address);

let balOnContract = await cSub.getValue.call({ from: account });
let balOnContract = await cSub.getValue.call({ from: account, gasLimit: GAS_LIMIT });
let balance = await web3.eth.getBalance(cSub.address);
assert.equal(balOnContract, value, 'new subcontract should have balance paid to it');
assert.equal(balOnContract, balance, 'new subcontract should have balance paid to it');
Expand Down
10 changes: 5 additions & 5 deletions web3tests/testCreate2.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { assert } = require('chai');
const Create2Factory = require('../build/contracts/Create2Factory.json');
const ValueContract = require('../build/contracts/ValueContract.json');
const { deployContract, account, initWeb3 } = require('../utils');
const { deployContract, account, initWeb3, GAS_PRICE, GAS_LIMIT } = require('../utils');
const contract = require("@truffle/contract");

describe('Create2Factory test', async () => {
Expand All @@ -13,11 +13,11 @@ describe('Create2Factory test', async () => {
});
Create2.setProvider(web3.currentProvider);

let c = await Create2.new({ from: account });
let c = await Create2.new({ from: account, gasLimit: GAS_LIMIT });

// load bytecode and deploy
await c.deploy(5, { from: account, gasPrice: 1000000000 });
const addr = await c.viewAddr.call({ from: account, gasPrice: 1000000000 });
await c.deploy(5, { from: account, gasLimit: GAS_LIMIT });
const addr = await c.viewAddr.call({ from: account, gasLimit: GAS_LIMIT });

let Value = contract({
abi: ValueContract.abi,
Expand All @@ -27,7 +27,7 @@ describe('Create2Factory test', async () => {

// load new contract and check methods
const valueContract = await Value.at(addr);
const value = await valueContract.getValue.call({ from: account, gasPrice: 1000000000 });
const value = await valueContract.getValue.call({ from: account, gasLimit: GAS_LIMIT });
assert.equal(value, '0');
});
});
10 changes: 5 additions & 5 deletions web3tests/testECRecovery.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { assert } = require('chai');
const contract = require("@truffle/contract");
const { account, initWeb3, privKey } = require('../utils');
const { account, initWeb3, privKey, GAS_PRICE, GAS_LIMIT } = require('../utils');
const ECRecovery = require('../build/contracts/ECRecovery.json');
const EdgewarePrivateKeyProvider = require('../private-provider');
const signing_account = account;
Expand All @@ -16,7 +16,7 @@ describe('ECRecovery test', async () => {
const web3 = new Web3(provider);
return web3;
};

signWeb3 = signingWeb3(signing_privKey);
});

Expand All @@ -36,7 +36,7 @@ describe('ECRecovery test', async () => {
const messageHex = '0x' + Buffer.from(message).toString('hex');
const signature = await signWeb3.eth.sign(messageHex, signing_account);
const hash = signWeb3.utils.sha3('\x19Ethereum Signed Message:\n' + message.length + message);

// recover the signer
const address = await c.recover(hash, signature, { from: account, gas: web3.utils.toWei('1', 'ether') });
assert.equal(address.toLowerCase(), signing_account.toLowerCase());
Expand All @@ -58,7 +58,7 @@ describe('ECRecovery test', async () => {
const RAW_TX = {
from: signing_account,
gasPrice: "0x01",
gas: web3.utils.toWei('1', 'ether'),
gas: GAS_LIMIT,
to: ECRECOVER_PRECOMPILE_ADDRESS,
value: "0x0",
data: `0x${hash.toString('hex')}${sigPart}`,
Expand All @@ -73,7 +73,7 @@ describe('ECRecovery test', async () => {
from: signing_account,
to: ECRECOVER_PRECOMPILE_ADDRESS,
value: '0x0',
gas: web3.utils.toWei('1', 'ether'),
gas: GAS_LIMIT,
data: `0x${hash.toString('hex')}${sigPart}`,
});

Expand Down
6 changes: 3 additions & 3 deletions web3tests/testFallback.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { assert } = require('chai');
const { account, initWeb3 } = require('../utils');
const { account, initWeb3, GAS_PRICE, GAS_LIMIT } = require('../utils');
const contract = require("@truffle/contract");
const FallbackContract = require('../build/contracts/FallbackContract.json');

Expand All @@ -23,7 +23,7 @@ describe('Fallback test', async () => {
from: account,
to: c.address,
value: valueSent.toString(),
gas: web3.utils.toWei('1', 'ether'),
gas: GAS_LIMIT,
data: functionSig,
});
const balanceAfter = await web3.eth.getBalance(account);
Expand All @@ -35,4 +35,4 @@ describe('Fallback test', async () => {
// ensure some gas fees were spent
assert.isTrue(balanceDiff.gtn(0));
});
})
})
10 changes: 5 additions & 5 deletions web3tests/testLockdrop.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const rlp = require('rlp');
const keccak = require('keccak');
const { assert } = require('chai');
const contract = require("@truffle/contract");
const { deployContract, account, initWeb3 } = require('../utils');
const { deployContract, account, initWeb3, GAS_PRICE, GAS_LIMIT } = require('../utils');

describe("Lockdrop test", async () => {
const SECONDS_IN_DAY = 86400;
Expand Down Expand Up @@ -47,7 +47,7 @@ describe("Lockdrop test", async () => {
time = await utility.getCurrentTimestamp(web3);
assert.equal(web3.utils.toBN(lockdrop.address).toString(), web3.utils.toBN(contractAddr).toString());
});

// Events don't work
it('should lock funds and increment nonce', async function () {
let time = await utility.getCurrentTimestamp(web3);
Expand All @@ -68,7 +68,7 @@ describe("Lockdrop test", async () => {
await lockdrop.lock(THREE_MONTHS, account, true, {
from: account,
value: value,
gas: 1500000,
gas: GAS_LIMIT,
gasPrice: 1000,
});

Expand Down Expand Up @@ -98,7 +98,7 @@ describe("Lockdrop test", async () => {
await lockdrop.lock(THREE_MONTHS, account, true, {
from: account,
value: value2,
gas: 1500000,
gas: GAS_LIMIT,
gasPrice: 1000000000,
});

Expand Down Expand Up @@ -127,4 +127,4 @@ function getContractAddress(address, nonce) {
const contractAddressLong = keccak('keccak256').update(rlpEncoded).digest('hex');
const contractAddr = contractAddressLong.substring(24);
return contractAddr;
}
}

0 comments on commit 3874f3f

Please sign in to comment.