First, add "web3": "1.2.1"
to your package.json
file and run npm install
(or simply run npm install web3
).
Second, try the following node script:
const Web3 = require("web3");
async function run() {
const abi = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"standard","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"},{"name":"","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"_name","type":"string"},{"name":"_symbol","type":"string"},{"name":"_decimals","type":"uint8"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_from","type":"address"},{"indexed":true,"name":"_to","type":"address"},{"indexed":false,"name":"_value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_owner","type":"address"},{"indexed":true,"name":"_spender","type":"address"},{"indexed":false,"name":"_value","type":"uint256"}],"name":"Approval","type":"event"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"}];
const web3 = new Web3(YOUR_ETHEREUM_NODE_URL);
const contractDAI = new web3.eth.Contract(abi, "0xContractDAI");
const contractAUDC = new web3.eth.Contract(abi, "0xContractAUDC");
const receipt1 = await send(web3, contractDAI.methods.transfer("0xWalletAUDC", "DesiredAmountOfDAI"), "0xWalletDAIPrivateKey"));
const receipt2 = await send(web3, contractAUDC.methods.transfer("0xWalletDAI", "DesiredAmountOfAUDC"), "0xWalletAUDCPrivateKey"));
console.log(receipt1);
console.log(receipt2);
}
async function send(web3, transaction, privateKey, value = 0, retry = true) {
while (this.gasPrice == undefined) {
const nodeGasPrice = await web3.eth.getGasPrice();
process.stdout.write(`Enter gas-price or leave empty to use ${nodeGasPrice}: `);
const userGasPrice = await scan();
if (/^\d+$/.test(userGasPrice))
this.gasPrice = userGasPrice;
else if (userGasPrice == "")
this.gasPrice = nodeGasPrice;
else
console.log("Illegal gas-price");
}
while (true) {
try {
const options = {
value : value,
to : transaction._parent._address,
data : transaction.encodeABI(),
gas : (await web3.eth.getBlock("latest")).gasLimit,
gasPrice: this.gasPrice
};
const signed = await web3.eth.accounts.signTransaction(options, privateKey);
const receipt = await web3.eth.sendSignedTransaction(signed.rawTransaction);
return receipt;
}
catch (error) {
console.log(error.message);
if (retry) {
while (true) {
process.stdout.write("Enter transaction-hash or leave empty to retry: ");
const hash = await scan();
if (/^0x([0-9A-Fa-f]{64})$/.test(hash)) {
const receipt = await web3.eth.getTransactionReceipt(hash);
if (receipt)
return receipt;
console.log("Invalid transaction-hash");
}
else if (hash) {
console.log("Illegal transaction-hash");
}
else {
break;
}
}
}
else {
return {};
}
}
}
}
async function scan() {
return await new Promise(function(resolve, reject) {
process.stdin.resume();
process.stdin.once("data", function(data) {
process.stdin.pause();
resolve(data.toString().trim());
});
});
}
run();
You'll need to set YOUR_ETHEREUM_NODE_URL
of course.
Tested with node v10.16.0.