Skip to content

Commit

Permalink
Merge pull request #199 from poanetwork/web3-latest
Browse files Browse the repository at this point in the history
(Fix) Async wait for RewardByBlock contract address
  • Loading branch information
varasev authored Sep 27, 2018
2 parents 771a10e + f343cb5 commit e6e04e9
Show file tree
Hide file tree
Showing 3 changed files with 2,660 additions and 59 deletions.
51 changes: 32 additions & 19 deletions migrations/2_deploy_contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ const VotingToChangeProxyAddress = artifacts.require("./VotingToChangeProxyAddre
const VotingToManageEmissionFunds = artifacts.require("./VotingToManageEmissionFunds");
const EmissionFunds = artifacts.require("./EmissionFunds");
const EternalStorageProxy = artifacts.require("./eternal-storage/EternalStorageProxy.sol");
const Web3 = require('web3')

const getWeb3Latest = () => {
const web3Latest = new Web3(web3.currentProvider)
return web3Latest
}

module.exports = function(deployer, network, accounts) {
if (network === 'sokol') {
Expand Down Expand Up @@ -154,22 +160,10 @@ module.exports = function(deployer, network, accounts) {
const rewardByBlockBytecode = `0x${rewardByBlockCompiled.bytecode}`;
const rewardByBlockGasEstimate = web3.eth.estimateGas({data: rewardByBlockBytecode});
const rewardByBlockImpl = web3.eth.contract(rewardByBlockCompiled.abi);
const rewardByBlockDeployed = await rewardByBlockImpl.new({
from: web3.eth.coinbase,
data: rewardByBlockBytecode,
gas: rewardByBlockGasEstimate
});
for (let i = 0; i < 10; i++) {
if (rewardByBlockDeployed.address) {
break;
} else {
await sleep(5000);
}
}
if (!rewardByBlockDeployed.address) {
const rewardByBlockImplAddress = await getRewardByBlockAddress(rewardByBlockBytecode, rewardByBlockCompiled.abi, rewardByBlockGasEstimate)
if (!rewardByBlockImplAddress) {
throw new Error('Cannot deploy RewardByBlock');
}
rewardByBlockImplAddress = rewardByBlockDeployed.address;
rewardByBlock = await EternalStorageProxy.new(
proxyStorage.address,
rewardByBlockImplAddress
Expand Down Expand Up @@ -243,13 +237,36 @@ module.exports = function(deployer, network, accounts) {
RewardByBlock.address (implementation) ............. ${rewardByBlockImplAddress}
RewardByBlock.address (storage) .................... ${rewardByBlock.address}
`
);
)
}).catch(function(error) {
console.error(error);
});
}
};

function getRewardByBlockAddress(bytecode, abi, estimatedGas) {
return new Promise((resolve, reject) => {
const deployOpts = {
data: bytecode,
}
const sendOpts = {
from: web3.eth.coinbase,
gas: estimatedGas
}
const web3Latest = getWeb3Latest()
const contractInstance = new web3Latest.eth.Contract(abi)
const deploy = contractInstance.deploy(deployOpts)
deploy.send(sendOpts)
.on('receipt', async (receipt) => {
resolve(receipt.contractAddress)
})
.on('error', async (err) => {
console.log(err)
reject(err)
})
})
}

async function compileContract(dir, contractName, contractCode) {
const compiled = solc.compile({
sources: {
Expand All @@ -274,9 +291,5 @@ async function compileContract(dir, contractName, contractCode) {
return {abi, bytecode};
}

function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}

// SAVE_TO_FILE=true POA_NETWORK_CONSENSUS_ADDRESS=0x8bf38d4764929064f2d4d3a56520a76ab3df415b MASTER_OF_CEREMONY=0xCf260eA317555637C55F70e55dbA8D5ad8414Cb0 OLD_KEYSMANAGER=0xfc90125492e58dbfe80c0bfb6a2a759c4f703ca8 ./node_modules/.bin/truffle migrate --reset --network sokol
// SAVE_TO_FILE=true DEPLOY_POA=true POA_NETWORK_CONSENSUS_ADDRESS=0x8bf38d4764929064f2d4d3a56520a76ab3df415b MASTER_OF_CEREMONY=0xCf260eA317555637C55F70e55dbA8D5ad8414Cb0 OLD_KEYSMANAGER=0xfc90125492e58dbfe80c0bfb6a2a759c4f703ca8 ./node_modules/.bin/truffle migrate --reset --network sokol
Loading

0 comments on commit e6e04e9

Please sign in to comment.