Skip to content

Instantly share code, notes, and snippets.

@shrugs
Last active August 31, 2018 01:58
Show Gist options
  • Save shrugs/364020905fd88420e698c17ad8927869 to your computer and use it in GitHub Desktop.
Save shrugs/364020905fd88420e698c17ad8927869 to your computer and use it in GitHub Desktop.
820 deploy code
const pify = require("pify");
const peth = pify(web3.eth);
const Transaction = require("eth-lib/lib/transaction");
const Bytes = require("eth-lib/lib/bytes");
const RLP = require("eth-lib/lib/rlp");
const deterministicallyDeploy = async (
TruffleContractInstance,
args = [],
gasPrice = web3.toHex(web3.toWei(100, "gwei")),
extra
) => {
// console.log("gasPrice", gasPrice);
// construct bytecode
const bytecode = web3.eth
.contract(TruffleContractInstance._json.abi)
.new.getData(...args, {
data: TruffleContractInstance._json.bytecode
});
console.log("bytecode", bytecode);
// estimate gas
const estimatedGas = web3.toHex(
await peth.estimateGas({
data: bytecode,
...extra
})
);
console.log("estimatedGas", estimatedGas);
// construct pseudo transaction
const rawTransaction = RLP.encode([
Bytes.fromNumber(0), // nonce
Bytes.fromNat(gasPrice), // gas price
Bytes.fromNat(estimatedGas), // gas
"0x", // to address
Bytes.fromNumber(0), // value
bytecode, // data
Bytes.fromNumber(27), // v
"0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798".toLowerCase(), // r
"0x0aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" // s
]);
console.log("rawTransaction", rawTransaction);
// recover signer
const signer = Transaction.recover(rawTransaction);
console.log("signer", signer);
// calculate gas cost
const gasCost = web3
.toBigNumber(gasPrice)
.mul(web3.toBigNumber(estimatedGas));
// console.log("total gas cost", gasCost.toString());
// send that money to signer
await peth.sendTransaction({
from: extra.from,
to: signer,
value: web3.toWei(1, "ether")
});
console.log("balance", (await peth.getBalance(signer)).toString());
// deploy contract
const txId = await peth.sendRawTransaction(rawTransaction);
const { contractAddress: address } = await peth.getTransactionReceipt(txId);
const code = await peth.getCode(address);
console.log(code);
};
module.exports = {
deterministicallyDeploy
};
@shrugs
Copy link
Author

shrugs commented Aug 31, 2018

ethereum/EIPs#820

 <   {
 <     "id": 16,
 <     "jsonrpc": "2.0",
 <     "error": {
 <       "message": "sender doesn't have enough funds to send tx. The upfront cost is: 38923000000000000 and the sender's account only has: 0",
 <       "code": -32000,
 <       "data": {
 <         "stack": "Error: sender doesn't have enough funds to send tx. The upfront cost is: 38923000000000000 and the sender's account only has: 0\n    at /Users/matt/Projects/crypto/XLNT/trusted-signer/node_modules/ganache-cli/build/cli.node.js:134:468321\n    at /Users/matt/Projects/crypto/XLNT/trusted-signer/node_modules/ganache-cli/build/cli.node.js:2:82373\n    at d (/Users/matt/Projects/crypto/XLNT/trusted-signer/node_modules/ganache-cli/build/cli.node.js:2:71007)\n    at /Users/matt/Projects/crypto/XLNT/trusted-signer/node_modules/ganache-cli/build/cli.node.js:2:71035\n    at we (/Users/matt/Projects/crypto/XLNT/trusted-signer/node_modules/ganache-cli/build/cli.node.js:2:71066)\n    at /Users/matt/Projects/crypto/XLNT/trusted-signer/node_modules/ganache-cli/build/cli.node.js:2:71124\n    at st (/Users/matt/Projects/crypto/XLNT/trusted-signer/node_modules/ganache-cli/build/cli.node.js:2:82349)\n    at Object.St [as series] (/Users/matt/Projects/crypto/XLNT/trusted-signer/node_modules/ganache-cli/build/cli.node.js:2:84499)\n    at v.e.exports [as runTx] (/Users/matt/Projects/crypto/XLNT/trusted-signer/node_modules/ganache-cli/build/cli.node.js:134:467765)\n    at /Users/matt/Projects/crypto/XLNT/trusted-signer/node_modules/ganache-cli/build/cli.node.js:134:471223\n    at /Users/matt/Projects/crypto/XLNT/trusted-signer/node_modules/ganache-cli/build/cli.node.js:2:80398\n    at d (/Users/matt/Projects/crypto/XLNT/trusted-signer/node_modules/ganache-cli/build/cli.node.js:2:71007)\n    at /Users/matt/Projects/crypto/XLNT/trusted-signer/node_modules/ganache-cli/build/cli.node.js:2:71035\n    at Da (/Users/matt/Projects/crypto/XLNT/trusted-signer/node_modules/ganache-cli/build/cli.node.js:2:80468)\n    at Object.<anonymous> (/Users/matt/Projects/crypto/XLNT/trusted-signer/node_modules/ganache-cli/build/cli.node.js:2:71124)\n    at /Users/matt/Projects/crypto/XLNT/trusted-signer/node_modules/ganache-cli/build/cli.node.js:134:471055\n    at /Users/matt/Projects/crypto/XLNT/trusted-signer/node_modules/ganache-cli/build/cli.node.js:2:82373\n    at d (/Users/matt/Projects/crypto/XLNT/trusted-signer/node_modules/ganache-cli/build/cli.node.js:2:71007)\n    at o (/Users/matt/Projects/crypto/XLNT/trusted-signer/node_modules/ganache-cli/build/cli.node.js:2:70911)\n    at /Users/matt/Projects/crypto/XLNT/trusted-signer/node_modules/ganache-cli/build/cli.node.js:2:70705\n    at /Users/matt/Projects/crypto/XLNT/trusted-signer/node_modules/ganache-cli/build/cli.node.js:2:82434\n    at /Users/matt/Projects/crypto/XLNT/trusted-signer/node_modules/ganache-cli/build/cli.node.js:2:67763",
 <         "name": "Error"
 <       }
 <     }
 <   }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment