Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Fix) Async wait for RewardByBlock contract address #199

Merged
merged 3 commits into from
Sep 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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