From 895357492153570ba364ea88f86eb3623a9ad0a6 Mon Sep 17 00:00:00 2001 From: Alexander Kolotov Date: Thu, 22 Aug 2019 08:24:08 +0300 Subject: [PATCH 1/8] get initial amount of tokens to mint from a new environment variable --- src/deploy/deploy-test/.env | 2 ++ src/deploy/deploy-test/migrations/2_deployment.js | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/deploy/deploy-test/.env b/src/deploy/deploy-test/.env index 01ffecd..1e36526 100644 --- a/src/deploy/deploy-test/.env +++ b/src/deploy/deploy-test/.env @@ -3,3 +3,5 @@ RPC_URL_DEV=http://ganache_home:8545 PRIVATE_KEY=e49fe947f224ae8e126c41b1be3e52be701509c2366e835ea8c08651f91030e0 PRIVATE_KEY_DEV=e2aeb24eaa63102d0c0821717c3b6384abdabd7af2ad4ec8e650dce300798b27 + +TOKEN_INITIAL_MINT=31415000000000000000000000 diff --git a/src/deploy/deploy-test/migrations/2_deployment.js b/src/deploy/deploy-test/migrations/2_deployment.js index 12d7671..442b643 100644 --- a/src/deploy/deploy-test/migrations/2_deployment.js +++ b/src/deploy/deploy-test/migrations/2_deployment.js @@ -1,8 +1,12 @@ const TokenContract = artifacts.require('ERC20Mintable') +const { + TOKEN_INITIAL_MINT +} = process.env + module.exports = async (deployer, network, accounts) => { await deployer.deploy(TokenContract) const instance = await TokenContract.deployed() - await instance.mint(accounts[0], 1000000) + await instance.mint(accounts[0], TOKEN_INITIAL_MINT) } From 7d0243b2ba556272711b03ea7e11f1b7dda83523 Mon Sep 17 00:00:00 2001 From: Alexander Kolotov Date: Thu, 22 Aug 2019 13:00:35 +0300 Subject: [PATCH 2/8] decimal shift for correct conversion ERC20 token to BEP2 token --- src/oracle/bncWatcher/bncWatcher.js | 2 +- src/oracle/ethWatcher/ethWatcher.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/oracle/bncWatcher/bncWatcher.js b/src/oracle/bncWatcher/bncWatcher.js index 2fa1e3b..a88989e 100644 --- a/src/oracle/bncWatcher/bncWatcher.js +++ b/src/oracle/bncWatcher/bncWatcher.js @@ -34,7 +34,7 @@ async function main () { await proxyHttpClient .post('/transfer', { to: computeAddress(Buffer.from(publicKeyEncoded, 'base64')), - value: new BN(tx.value).integerValue(BN.ROUND_FLOOR),//(new BN(tx.value).multipliedBy(10 ** 8)).toNumber(), + value: new BN(tx.value).multipliedBy(10 ** 18), hash: `0x${tx.txHash}` }) } diff --git a/src/oracle/ethWatcher/ethWatcher.js b/src/oracle/ethWatcher/ethWatcher.js index 4d0b606..77d4d87 100644 --- a/src/oracle/ethWatcher/ethWatcher.js +++ b/src/oracle/ethWatcher/ethWatcher.js @@ -177,7 +177,7 @@ async function sendSign (event) { x: publicKey.substr(4, 64), y: publicKey.substr(68, 64) }), - value: event.returnValues.value.toNumber(), + value: new BN(event.returnValues.value).dividedToIntegerBy(10 ** 8), epoch, nonce: foreignNonce[epoch], threshold: (await bridge.methods.getThreshold(epoch).call()).toNumber(), From 987295e8e405853521de97ddb5efd43bc40949ee Mon Sep 17 00:00:00 2001 From: Alexander Kolotov Date: Thu, 22 Aug 2019 22:00:32 +0300 Subject: [PATCH 3/8] preserve testnets data between system restarts --- .gitignore | 4 ++-- demo/clean.sh | 9 ++++++--- demo/start-environment.sh | 18 ++++++++++++++++-- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 1cc190d..3345883 100644 --- a/.gitignore +++ b/.gitignore @@ -8,8 +8,8 @@ data/ demo/validator*/db demo/validator*/keys demo/validator*/queue -demo/ganache_data/ -demo/ganache_data_side/ +demo/ganache_home +demo/ganache_side src/deploy/deploy-home/build/ src/deploy/deploy-side/build/ src/deploy/deploy-test/build/ diff --git a/demo/clean.sh b/demo/clean.sh index 9ef761f..76688e4 100755 --- a/demo/clean.sh +++ b/demo/clean.sh @@ -4,11 +4,14 @@ set -e cd $(dirname "$0") +rm -rf ganache_side_db +rm -rf ganache_home_db + for (( I = 1; I < 4; ++I )); do DIRNAME="validator$I" - rm -r "$DIRNAME/db" - rm -r "$DIRNAME/queue" - rm -r "$DIRNAME/keys" + rm -rf "$DIRNAME/db" + rm -rf "$DIRNAME/queue" + rm -rf "$DIRNAME/keys" mkdir "$DIRNAME/db" mkdir "$DIRNAME/queue" mkdir "$DIRNAME/keys" diff --git a/demo/start-environment.sh b/demo/start-environment.sh index ec4fe3b..92fd56d 100755 --- a/demo/start-environment.sh +++ b/demo/start-environment.sh @@ -6,15 +6,29 @@ cd $(dirname "$0") echo "Starting side test blockchain" +mntpoint=`pwd`/ganache_side_db +if [ ! -d ${mntpoint} ]; then + mkdir ${mntpoint} +fi + docker kill ganache_side > /dev/null 2>&1 || true docker network create blockchain_side > /dev/null 2>&1 || true -docker run -d --network blockchain_side --rm --name ganache_side trufflesuite/ganache-cli:latest -m "shrug dwarf easily blade trigger lucky reopen cage lake scatter desk boat" -i 33 -q +docker run -d --network blockchain_side --rm --name ganache_side -v ${mntpoint}:/app/db \ + trufflesuite/ganache-cli:latest \ + -m "shrug dwarf easily blade trigger lucky reopen cage lake scatter desk boat" -i 33 -q --db /app/db echo "Starting home test blockchain" +mntpoint=`pwd`/ganache_home_db +if [ ! -d ${mntpoint} ]; then + mkdir ${mntpoint} +fi + docker kill ganache_home > /dev/null 2>&1 || true docker network create blockchain_home > /dev/null 2>&1 || true -docker run -d --network blockchain_home --rm --name ganache_home trufflesuite/ganache-cli:latest -m "shrug dwarf easily blade trigger lucky reopen cage lake scatter desk boat" -i 44 -q +docker run -d --network blockchain_home --rm --name ganache_home -v ${mntpoint}:/app/db \ + trufflesuite/ganache-cli:latest \ + -m "shrug dwarf easily blade trigger lucky reopen cage lake scatter desk boat" -i 44 -q --db /app/db sleep 4 From 16ebb69b3358a1239bf81bc8489a327a5c205a86 Mon Sep 17 00:00:00 2001 From: Alexander Kolotov Date: Fri, 23 Aug 2019 00:20:16 +0300 Subject: [PATCH 4/8] convert properly large numbers --- src/test-services/ethereumBalance/testGetEthereumBalance.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test-services/ethereumBalance/testGetEthereumBalance.js b/src/test-services/ethereumBalance/testGetEthereumBalance.js index 71b803f..f05cf55 100644 --- a/src/test-services/ethereumBalance/testGetEthereumBalance.js +++ b/src/test-services/ethereumBalance/testGetEthereumBalance.js @@ -10,5 +10,5 @@ const token = new web3.eth.Contract(abiToken, HOME_TOKEN_ADDRESS) const address = process.argv[2] token.methods.balanceOf(address).call() - .then(x => console.log(x.toNumber())) + .then(x => console.log(x.toString())) .catch(() => console.log(0)) From 1d9939cef50c1c54583a1df41a5406159982ab63 Mon Sep 17 00:00:00 2001 From: Alexander Kolotov Date: Fri, 23 Aug 2019 00:22:32 +0300 Subject: [PATCH 5/8] specify sender rather than bridge contracts, proper sending of erc20 tokens --- src/test-services/ethereumSend/.env | 4 +++- src/test-services/ethereumSend/package.json | 1 + src/test-services/ethereumSend/testEthereumSend.js | 13 +++++++------ 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/test-services/ethereumSend/.env b/src/test-services/ethereumSend/.env index 0eace88..ec28166 100644 --- a/src/test-services/ethereumSend/.env +++ b/src/test-services/ethereumSend/.env @@ -1,6 +1,8 @@ HOME_RPC_URL=http://ganache_home:8545 HOME_BRIDGE_ADDRESS=0x94b40CC641Ed7db241A1f04C8896ba6f6cC36b85 +RECEIVER_ADDRESS=0x94b40CC641Ed7db241A1f04C8896ba6f6cC36b85 + HOME_CHAIN_ID=44 HOME_TOKEN_ADDRESS=0x44c158FE850821ae69DaF37AADF5c539e9d0025B -HOME_PRIVATE_KEY=e2aeb24eaa63102d0c0821717c3b6384abdabd7af2ad4ec8e650dce300798b27 +HOME_PRIVATE_KEY=e2aeb24eaa63102d0c0821717c3b6384abdabd7af2ad4ec8e650dce300798b27 \ No newline at end of file diff --git a/src/test-services/ethereumSend/package.json b/src/test-services/ethereumSend/package.json index 081078d..db04192 100644 --- a/src/test-services/ethereumSend/package.json +++ b/src/test-services/ethereumSend/package.json @@ -2,6 +2,7 @@ "name": "ethereum-send", "version": "0.0.1", "dependencies": { + "bignumber.js": "9.0.0", "web3": "1.0.0-beta.55" } } diff --git a/src/test-services/ethereumSend/testEthereumSend.js b/src/test-services/ethereumSend/testEthereumSend.js index 6e543f8..07a059a 100644 --- a/src/test-services/ethereumSend/testEthereumSend.js +++ b/src/test-services/ethereumSend/testEthereumSend.js @@ -1,6 +1,7 @@ const Web3 = require('web3') +const BN = require('bignumber.js') -const { HOME_RPC_URL, HOME_BRIDGE_ADDRESS, HOME_CHAIN_ID, HOME_PRIVATE_KEY, HOME_TOKEN_ADDRESS } = process.env +const { HOME_RPC_URL, RECEIVER_ADDRESS, HOME_CHAIN_ID, HOME_PRIVATE_KEY, HOME_TOKEN_ADDRESS } = process.env const abiToken = require('./IERC20').abi @@ -9,18 +10,18 @@ const token = new web3.eth.Contract(abiToken, HOME_TOKEN_ADDRESS) const amount = parseInt(process.argv[2]) -const deployAddress = web3.eth.accounts.privateKeyToAccount(`0x${HOME_PRIVATE_KEY}`).address +const sender = web3.eth.accounts.privateKeyToAccount(`0x${HOME_PRIVATE_KEY}`).address async function main () { - console.log(`Transfer from ${deployAddress} to ${HOME_BRIDGE_ADDRESS}, ${amount} tokens`) + console.log(`Transfer from ${sender} to ${RECEIVER_ADDRESS}, ${amount} tokens`) - const query = token.methods.transfer(HOME_BRIDGE_ADDRESS, amount) + const query = token.methods.transfer(RECEIVER_ADDRESS, '0x'+(new BN(amount).toString(16))) const encodedABI = query.encodeABI() const tx = { data: encodedABI, - from: deployAddress, + from: sender, to: HOME_TOKEN_ADDRESS, - nonce: await web3.eth.getTransactionCount(deployAddress), + nonce: await web3.eth.getTransactionCount(sender), chainId: parseInt(HOME_CHAIN_ID) } tx.gas = Math.min(Math.ceil(await query.estimateGas(tx) * 1.5), 6721975) From 3f76231978f521252698a6330846dc8ce0ee87a6 Mon Sep 17 00:00:00 2001 From: Alexander Kolotov Date: Fri, 23 Aug 2019 12:30:00 +0300 Subject: [PATCH 6/8] added ability to send native coins --- src/test-services/ethereumSend/.env | 1 - .../ethereumSend/testEthereumSend.js | 62 ++++++++++++++----- 2 files changed, 46 insertions(+), 17 deletions(-) diff --git a/src/test-services/ethereumSend/.env b/src/test-services/ethereumSend/.env index ec28166..05f03c6 100644 --- a/src/test-services/ethereumSend/.env +++ b/src/test-services/ethereumSend/.env @@ -1,6 +1,5 @@ HOME_RPC_URL=http://ganache_home:8545 HOME_BRIDGE_ADDRESS=0x94b40CC641Ed7db241A1f04C8896ba6f6cC36b85 -RECEIVER_ADDRESS=0x94b40CC641Ed7db241A1f04C8896ba6f6cC36b85 HOME_CHAIN_ID=44 HOME_TOKEN_ADDRESS=0x44c158FE850821ae69DaF37AADF5c539e9d0025B diff --git a/src/test-services/ethereumSend/testEthereumSend.js b/src/test-services/ethereumSend/testEthereumSend.js index 07a059a..3ef7c8f 100644 --- a/src/test-services/ethereumSend/testEthereumSend.js +++ b/src/test-services/ethereumSend/testEthereumSend.js @@ -1,34 +1,64 @@ const Web3 = require('web3') const BN = require('bignumber.js') -const { HOME_RPC_URL, RECEIVER_ADDRESS, HOME_CHAIN_ID, HOME_PRIVATE_KEY, HOME_TOKEN_ADDRESS } = process.env +const { HOME_RPC_URL, HOME_BRIDGE_ADDRESS, HOME_CHAIN_ID, HOME_PRIVATE_KEY, HOME_TOKEN_ADDRESS } = process.env const abiToken = require('./IERC20').abi const web3 = new Web3(HOME_RPC_URL, null, { transactionConfirmationBlocks: 1 }) const token = new web3.eth.Contract(abiToken, HOME_TOKEN_ADDRESS) -const amount = parseInt(process.argv[2]) - const sender = web3.eth.accounts.privateKeyToAccount(`0x${HOME_PRIVATE_KEY}`).address async function main () { - console.log(`Transfer from ${sender} to ${RECEIVER_ADDRESS}, ${amount} tokens`) - const query = token.methods.transfer(RECEIVER_ADDRESS, '0x'+(new BN(amount).toString(16))) - const encodedABI = query.encodeABI() - const tx = { - data: encodedABI, - from: sender, - to: HOME_TOKEN_ADDRESS, - nonce: await web3.eth.getTransactionCount(sender), - chainId: parseInt(HOME_CHAIN_ID) + let to = process.argv[2] + + if (to == "bridge") { + to = HOME_BRIDGE_ADDRESS + } + + const amount = parseInt(process.argv[3]) + let coins = process.argv[4] + + if (amount != 0) { + console.log(`Transfer from ${sender} to ${to}, ${amount} tokens`) + + const query = token.methods.transfer(to, '0x'+(new BN(amount).toString(16))) + const encodedABI = query.encodeABI() + const tx = { + data: encodedABI, + from: sender, + to: HOME_TOKEN_ADDRESS, + nonce: await web3.eth.getTransactionCount(sender), + chainId: parseInt(HOME_CHAIN_ID) + } + tx.gas = Math.min(Math.ceil(await query.estimateGas(tx) * 1.5), 6721975) + let signedTx = await web3.eth.accounts.signTransaction(tx, HOME_PRIVATE_KEY) + + let receipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction) + console.log('txHash: ' + receipt.transactionHash) + } + + if (coins) { + coins = parseFloat(coins) + console.log(`Transfer from ${sender} to ${to}, ${coins} coins`) + + const tx_coins = { + data: '0x', + from: sender, + to: to, + nonce: await web3.eth.getTransactionCount(sender), + chainId: parseInt(HOME_CHAIN_ID), + value: web3.utils.toWei(new BN(coins).toString(), 'ether'), + gas: 21000 + } + signedTx = await web3.eth.accounts.signTransaction(tx_coins, HOME_PRIVATE_KEY) + + receipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction) + console.log('txHash: ' + receipt.transactionHash) } - tx.gas = Math.min(Math.ceil(await query.estimateGas(tx) * 1.5), 6721975) - const signedTx = await web3.eth.accounts.signTransaction(tx, HOME_PRIVATE_KEY) - const receipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction) - console.log(receipt.transactionHash) } main() From 5838fae64edf01317e71441e3fe42189811e41c5 Mon Sep 17 00:00:00 2001 From: Alexander Kolotov Date: Fri, 23 Aug 2019 16:10:27 +0300 Subject: [PATCH 7/8] amount of tokens is a float --- src/oracle/ethWatcher/ethWatcher.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/oracle/ethWatcher/ethWatcher.js b/src/oracle/ethWatcher/ethWatcher.js index 77d4d87..6bbad56 100644 --- a/src/oracle/ethWatcher/ethWatcher.js +++ b/src/oracle/ethWatcher/ethWatcher.js @@ -172,20 +172,23 @@ async function sendSign (event) { }) const hash = web3Home.utils.sha3(msg) const publicKey = utils.recoverPublicKey(hash, { r: tx.r, s: tx.s, v: tx.v }) - channel.sendToQueue(signQueue.queue, Buffer.from(JSON.stringify({ + const msgToQueue = JSON.stringify({ recipient: publicKeyToAddress({ x: publicKey.substr(4, 64), y: publicKey.substr(68, 64) }), - value: new BN(event.returnValues.value).dividedToIntegerBy(10 ** 8), + value: (new BN(event.returnValues.value)).dividedBy(10 ** 18).toFixed(8, 3), epoch, nonce: foreignNonce[epoch], threshold: (await bridge.methods.getThreshold(epoch).call()).toNumber(), parties: (await bridge.methods.getParties(epoch).call()).toNumber() - })), { + }) + + channel.sendToQueue(signQueue.queue, Buffer.from(msgToQueue), { persistent: true }) console.log('Sent new sign event') + console.log(msgToQueue) redisTx.incr(`foreignNonce${epoch}`) foreignNonce[epoch]++ From 5def2168558202e4903b11b2ace938c73876af16 Mon Sep 17 00:00:00 2001 From: Alexander Kolotov Date: Fri, 23 Aug 2019 16:48:26 +0300 Subject: [PATCH 8/8] correct convertation to ERC20 tokens --- src/oracle/bncWatcher/bncWatcher.js | 2 +- src/oracle/proxy/index.js | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/oracle/bncWatcher/bncWatcher.js b/src/oracle/bncWatcher/bncWatcher.js index a88989e..f9ef526 100644 --- a/src/oracle/bncWatcher/bncWatcher.js +++ b/src/oracle/bncWatcher/bncWatcher.js @@ -34,7 +34,7 @@ async function main () { await proxyHttpClient .post('/transfer', { to: computeAddress(Buffer.from(publicKeyEncoded, 'base64')), - value: new BN(tx.value).multipliedBy(10 ** 18), + value: new BN(tx.value).multipliedBy(10 ** 18).integerValue(), hash: `0x${tx.txHash}` }) } diff --git a/src/oracle/proxy/index.js b/src/oracle/proxy/index.js index 5f725d6..380ad37 100644 --- a/src/oracle/proxy/index.js +++ b/src/oracle/proxy/index.js @@ -213,6 +213,7 @@ function homeSendQuery (query) { chainId: parseInt(HOME_CHAIN_ID) } tx.gas = Math.min(Math.ceil(await query.estimateGas(tx) * 1.5), 6721975) + console.log(tx) const signedTx = await homeWeb3.eth.accounts.signTransaction(tx, VALIDATOR_PRIVATE_KEY) return homeWeb3.eth.sendSignedTransaction(signedTx.rawTransaction) @@ -333,8 +334,8 @@ async function transfer (req, res) { console.log('Transfer start') const { hash, to, value } = req.body if (homeWeb3.utils.isAddress(to)) { - console.log('Calling transfer') - const query = bridge.methods.transfer(hash, to, value) + console.log(`Calling transfer to ${to}, ${value} tokens`) + const query = bridge.methods.transfer(hash, to, '0x'+(new BN(value).toString(16))) await homeSendQuery(query) } else { // return funds ?