-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdeployContract.js
More file actions
113 lines (100 loc) · 4.07 KB
/
deployContract.js
File metadata and controls
113 lines (100 loc) · 4.07 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { AIRDROP_BYTECODE, AIRDROP_ABI } from './metadata';
import { generateAccount } from './utils';
/**
* @desc Send transaction to deploy the Airdrop Smart Contract.
* @param {Object} [airdropParams] - Object wth airdrop params
* @param {Number} [txValue] - Amount of wei to send to contract
* @param {Number} [txGas] - Gas estimate for the deployment transaction
* @param {Object} [web3] - web3 object (from web3.js lib)
* @param {Function} [onTxMined] - Callback to fire after transaction is mined
* @return {Promise}
*/
const _sendContractDeploymentTx = ({
airdropParams,
txGas,
txValue,
web3,
onTxMined
}) => {
return new Promise((resolve, reject) => {
const AirdropContract = web3.eth.contract(AIRDROP_ABI);
let {
tokenAddress, claimAmountAtomic,
claimAmountEthInWei, airdropTransitAddress,
referralAmountAtomic
} = airdropParams;
console.log(airdropParams);
AirdropContract.new(tokenAddress, claimAmountAtomic, referralAmountAtomic,
claimAmountEthInWei, airdropTransitAddress, {
from: web3.eth.accounts[0],
data: AIRDROP_BYTECODE,
value: txValue,
gas: txGas
}, (err, airdropContract) => {
if(err) { reject(err); return null;}
// NOTE: The callback will fire twice!
// Once the contract has the transactionHash property set and once its deployed on an address.
// e.g. check tx hash on the first call (transaction send)
if(!airdropContract.address) {
resolve(airdropContract.transactionHash); // The hash of the transaction, which deploys the contract
// check address on the second call (contract deployed)
} else {
onTxMined(airdropContract.address);
}
});
});
}
/**
* @desc Send transaction to deploy the Airdrop Smart Contract.
* @param {Number} [claimAmount] - Amount of tokens to distribute on claim
* @param {String} [tokenAddress] - Token contract address
* @param {Number} [claimAmountEth] - Amount of wei to distribute on claim
* @param {Number} [decimals] - Token decimals
* @param {Number} [linksNumber] - amount of links
* @param {Object} [web3] - web3 object (from web3.js lib)
* @param {Function} [onTxMined] - Callback to fire after transaction is mined
* @return {Object}
*/
export const deployContract = async ({
claimAmount,
referralAmount=0,
tokenAddress,
decimals,
claimAmountEth,
linksNumber,
web3,
onTxMined
}) => {
// Generate special key pair (Aidrop Transit Key Pair) for the airdrop.
// (Ethereum address from the Airdrop Transit Private Key stored to the Airdrop Smart Contract as AIRDROP_TRANSIT_ADDRESS
//
// Airdrop Transit Private Key used for signing other transit private keys generated per link.
//
// The Airdrop Contract verifies that the private key from the link is signed by the Airdrop Transit Private Key,
// which means that the claim link was signed by the Airdropper)
const { privateKey: airdropTransitPK, address: airdropTransitAddress } = generateAccount();
console.log({airdropTransitPK});
// airdrop contract params
const claimAmountAtomic = web3.toBigNumber(claimAmount).shift(decimals);
const referralAmountAtomic = web3.toBigNumber(referralAmount).shift(decimals);
const claimAmountEthInWei = web3.toBigNumber(claimAmountEth).shift(18);
const airdropParams = {
tokenAddress,
claimAmountAtomic,
referralAmountAtomic,
claimAmountEthInWei,
airdropTransitAddress
};
// tx params
//const gasEstimate = await web3.eth.estimateGasPromise({data: BYTECODE});
const gasEstimate = 1600000;
const txGas = gasEstimate + 100000;
const txValue = claimAmountEthInWei * linksNumber;
// deploy contract
const txHash = await _sendContractDeploymentTx({airdropParams, txGas, txValue, web3, onTxMined});
return {
txHash,
airdropTransitPK,
airdropTransitAddress
};
}