Support of explicit private key in ethereum send service

This commit is contained in:
Kirill Fedoseev 2019-10-14 23:38:00 +03:00
parent 5413a3e7a1
commit 9070333d46
2 changed files with 28 additions and 13 deletions

View File

@ -9,4 +9,4 @@ TARGET_NETWORK=${TARGET_NETWORK:=development}
docker build -t ethereum-send . > /dev/null
docker run --network blockchain_home --rm --env-file ".env.$TARGET_NETWORK" --env-file "../.keys.$TARGET_NETWORK" ethereum-send $@
docker run --network blockchain_home --rm --env-file ".env.$TARGET_NETWORK" --env-file "../.keys.$TARGET_NETWORK" -e "PRIVATE_KEY=$PRIVATE_KEY" ethereum-send $@

View File

@ -6,31 +6,29 @@ const { HOME_RPC_URL, HOME_BRIDGE_ADDRESS, HOME_PRIVATE_KEY, HOME_TOKEN_ADDRESS
const abiToken = require('./IERC20').abi
const abiBridge = require('./Bridge').abi
const PRIVATE_KEY = process.env.PRIVATE_KEY || HOME_PRIVATE_KEY
const web3 = new Web3(HOME_RPC_URL, null, { transactionConfirmationBlocks: 1 })
const token = new web3.eth.Contract(abiToken, HOME_TOKEN_ADDRESS)
const bridge = new web3.eth.Contract(abiBridge, HOME_BRIDGE_ADDRESS)
const sender = web3.eth.accounts.privateKeyToAccount(`0x${HOME_PRIVATE_KEY}`).address
const sender = web3.eth.accounts.privateKeyToAccount(`0x${PRIVATE_KEY}`).address
async function main () {
const HOME_CHAIN_ID = await web3.eth.net.getId()
const blockGasLimit = (await web3.eth.getBlock("latest", false)).gasLimit
let to = process.argv[2]
if (to === "bridge") {
to = HOME_BRIDGE_ADDRESS
}
const to = process.argv[2]
const amount = parseInt(process.argv[3])
let coins = process.argv[4]
const txCount = await web3.eth.getTransactionCount(sender)
if (amount !== 0) {
console.log(`Transfer from ${sender} to ${to}, ${amount} tokens`)
if (to === "bridge" && amount !== 0) {
console.log(`Transfer from ${sender} to ${HOME_BRIDGE_ADDRESS}, ${amount} tokens`)
const queryApprove = token.methods.approve(to, '0x'+(new BN(amount).multipliedBy(10 ** 18).toString(16)))
const queryApprove = token.methods.approve(HOME_BRIDGE_ADDRESS, '0x'+(new BN(amount).multipliedBy(10 ** 18).toString(16)))
const txApprove = {
data: queryApprove.encodeABI(),
from: sender,
@ -41,7 +39,7 @@ async function main () {
txApprove.gas = Math.min(Math.ceil(await queryApprove.estimateGas({
from: sender
}) * 1.5), blockGasLimit)
const signedTxApprove = await web3.eth.accounts.signTransaction(txApprove, HOME_PRIVATE_KEY)
const signedTxApprove = await web3.eth.accounts.signTransaction(txApprove, PRIVATE_KEY)
const receiptApprove = await web3.eth.sendSignedTransaction(signedTxApprove.rawTransaction)
console.log('txHash approve: ' + receiptApprove.transactionHash)
@ -57,10 +55,27 @@ async function main () {
txExchange.gas = Math.min(Math.ceil(await queryExchange.estimateGas({
from: sender
}) * 1.5), blockGasLimit)
const signedTxExchange = await web3.eth.accounts.signTransaction(txExchange, HOME_PRIVATE_KEY)
const signedTxExchange = await web3.eth.accounts.signTransaction(txExchange, PRIVATE_KEY)
const receiptExchange = await web3.eth.sendSignedTransaction(signedTxExchange.rawTransaction)
console.log('txHash exchange: ' + receiptExchange.transactionHash)
} else if (amount !== 0) {
console.log(`Transfer from ${sender} to ${to}, ${amount} tokens`)
const query = token.methods.transfer(to, '0x'+(new BN(amount).multipliedBy(10 ** 18).toString(16)))
const tx = {
data: query.encodeABI(),
from: sender,
to: HOME_TOKEN_ADDRESS,
nonce: txCount,
chainId: HOME_CHAIN_ID
}
tx.gas = Math.min(Math.ceil(await query.estimateGas({
from: sender
}) * 1.5), blockGasLimit)
const signedTx = await web3.eth.accounts.signTransaction(tx, PRIVATE_KEY)
const receipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction)
console.log('txHash transfer: ' + receipt.transactionHash)
}
if (coins) {
@ -76,7 +91,7 @@ async function main () {
value: web3.utils.toWei(new BN(coins).toString(), 'ether'),
gas: 21000
}
const signedTx = await web3.eth.accounts.signTransaction(tx, HOME_PRIVATE_KEY)
const signedTx = await web3.eth.accounts.signTransaction(tx, PRIVATE_KEY)
const receipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction)
console.log('txHash: ' + receipt.transactionHash)