This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 677
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add gasLimit and gasPrice to README, fix gasPrice
Prior to this change, the `gasPrice` set in the `options` object wasn't respected, and neither gasPrice nor gasLimit were documented in our README.md. This change also adds tests for these options. fixes #314
- Loading branch information
1 parent
c35f6a5
commit 45e88ff
Showing
7 changed files
with
132 additions
and
5 deletions.
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
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
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,42 @@ | ||
const fs = require('fs') | ||
const solc = require('solc') | ||
const path = require('path') | ||
|
||
async function compileAndDeploy(contractPath, contractName, web3) { | ||
let contractFilename = path.basename(contractPath) | ||
|
||
let source = fs.readFileSync(contractPath, "utf8"); | ||
|
||
let result = solc.compile({sources: {[contractFilename]: source}}, 1); | ||
|
||
let bytecode = "0x" + result.contracts[`${contractFilename}:${contractName}`].bytecode; | ||
let abi = JSON.parse(result.contracts[`${contractFilename}:${contractName}`].interface); | ||
|
||
console.log('good') | ||
let contract = new web3.eth.Contract(abi); | ||
|
||
let accounts = await web3.eth.getAccounts() | ||
let block = await web3.eth.getBlock("latest"); | ||
let gasLimit = block.gasLimit; | ||
|
||
|
||
let instance = await contract.deploy({data: bytecode}).send({from: accounts[0], gas: gasLimit}) | ||
|
||
// TODO: ugly workaround - not sure why this is necessary. | ||
if (!instance._requestManager.provider) { | ||
instance._requestManager.setProvider(web3.eth._provider); | ||
} | ||
|
||
return { | ||
source, | ||
bytecode, | ||
abi, | ||
contract, | ||
instance | ||
} | ||
} | ||
|
||
exports = module.exports = { | ||
compileAndDeploy | ||
} | ||
|
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,30 @@ | ||
const Web3 = require('web3'); | ||
const assert = require('assert'); | ||
const Ganache = require("../../index.js"); | ||
const path = require("path"); | ||
|
||
const mnemonic = 'candy maple cake sugar pudding cream honey rich smooth crumble sweet treat' | ||
|
||
describe('options:gasLimit', function() { | ||
let options = {mnemonic} | ||
let provider = null | ||
let web3 = null | ||
let accounts = [] | ||
|
||
before ('setup web3', async function() { | ||
provider = new Ganache.provider(options) | ||
web3 = new Web3(provider) | ||
}) | ||
|
||
before('get accounts', async function() { | ||
accounts = await web3.eth.getAccounts() | ||
}) | ||
|
||
it('should respect the assigned gasLimit', async function() { | ||
let assignedGasLimit = provider.engine.manager.state.blockchain.blockGasLimit; | ||
let block = await web3.eth.getBlock('latest') | ||
assert.deepEqual(block.gasLimit, assignedGasLimit) | ||
}) | ||
}); | ||
|
||
|
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,49 @@ | ||
const to = require('../../lib/utils/to'); | ||
const Web3 = require('web3'); | ||
const assert = require('assert'); | ||
const Ganache = require("../../index"); | ||
const path = require("path"); | ||
|
||
const compileAndDeploy = require ('../helpers/contracts').compileAndDeploy | ||
|
||
const mnemonic = 'candy maple cake sugar pudding cream honey rich smooth crumble sweet treat' | ||
|
||
describe('options:gasLimit', function() { | ||
let options = {mnemonic} | ||
let provider = null | ||
let web3 = null | ||
let accounts = [] | ||
let contractArtifact = {} | ||
let instance = null | ||
|
||
before ('setup web3', async function() { | ||
provider = new Ganache.provider(options) | ||
web3 = new Web3(provider) | ||
}) | ||
|
||
before('get accounts', async function() { | ||
accounts = await web3.eth.getAccounts() | ||
}) | ||
|
||
before("compile source", async function() { | ||
this.timeout(10000) | ||
let contractName = 'Example' | ||
contractArtifact = await compileAndDeploy(path.join(__dirname, '..', `${contractName}.sol`), contractName, web3) | ||
instance = contractArtifact.instance | ||
}) | ||
|
||
it('should respect the default gasPrice', async function() { | ||
let assignedGasPrice = provider.engine.manager.state.gasPriceVal; | ||
|
||
let receipt = await instance.methods.setValue('0x10').send({from: accounts[0], gas: 3141592}) | ||
|
||
let transactionHash = receipt.transactionHash; | ||
let tx = await web3.eth.getTransaction(transactionHash) | ||
let gasPrice = tx.gasPrice | ||
|
||
assert.deepEqual(to.hex(gasPrice), to.hex(assignedGasPrice)) | ||
}) | ||
|
||
}); | ||
|
||
|
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