-
Notifications
You must be signed in to change notification settings - Fork 9
/
craft.js
44 lines (44 loc) · 2.2 KB
/
craft.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
var ethers = require('ethers');
var fs = require('fs');
var Wallet = ethers.Wallet;
//usage: node craft.js ABI_FILE BYTECODE_FILE GAS_LIMIT GAS_PRICE [...CONSTRUCTOR ARGUMENTS]
//node craft.js "../Sweeper/Sweeper.abi" "../Sweeper/Sweeper.bytecode" 250000 100000000000 (cat ../Loan/Loan.address) (cat ../SomeCoin/SomeCoin.address) 1
const wallet = ethers.Wallet.createRandom();
let httpProvider = new ethers.providers.JsonRpcProvider();
let abi = fs.readFileSync(process.argv[2]).toString()
let bytecode = fs.readFileSync(process.argv[3]).toString()
let factory = new ethers.ContractFactory(abi, bytecode, wallet);
let contractDeployTx = factory.getDeployTransaction(...process.argv.splice(6));
//
//ADAPTED FROM https://github.com/LimeChain/IdentityProxy/blob/master/relayer_api/services/relayerService.js
//
contractDeployTx.gasLimit = parseInt(process.argv[4])
contractDeployTx.gasPrice = parseInt(process.argv[5])
wallet.sign(contractDeployTx).then((signedDeployTx)=>{
const signedTransNoRSV = signedDeployTx.substring(0, signedDeployTx.length - 134);
let randomS = ethers.utils.keccak256(ethers.utils.randomBytes(3));
//add four cows to prove it's arbitrary and therefore random at a glance
// (there is no way someone controls this private key)
randomS = '0' + randomS.substring(3, randomS.length-16)+"beefbeefbeefbeef";
const counterfactualMagic = `1ba079be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798a0`;
let counterfactualTx = `${signedTransNoRSV}${counterfactualMagic}${randomS}`;
const parsedTrans = ethers.utils.parseTransaction(counterfactualTx);
const counterfactualDeploymentPayer = parsedTrans.from;
httpProvider.getTransactionCount(counterfactualDeploymentPayer).then((nonce)=>{
const transaction = {
from: counterfactualDeploymentPayer,
nonce: nonce
};
const counterfactualContractAddress = ethers.utils.getContractAddress(transaction);
let result = {
transactionWithoutSig: signedTransNoRSV,
transaction: counterfactualTx,
from: counterfactualDeploymentPayer,
gasPrice: contractDeployTx.gasPrice,
gasLimit: contractDeployTx.gasLimit,
address: counterfactualContractAddress,
nonce: nonce
}
console.log(JSON.stringify(result))
})
})