Solidity

將 ERC20 代幣與其他 ERC20 代幣交換

  • September 28, 2019

我有 2 個 ERC20 代幣。該合約設計為標準 ERC20。以下是以 2 個令牌為例 -

AUDC --> Contract Address: (0xContractAUDC)
        Wallet Address:   (0xWalletAUDC)
DAI  --> Contract Address: (0xContractDAI)
        Wallet Address:   (0xWalletDAI)

我想從錢包中轉移一些 DAI0xWalletDAI0xWalletAUDC接收轉換後的 AUDC 作為回報(我有兩個錢包的私鑰)。

尋求一些幫助以了解如何實施。如果需要,我會盡力提供更多資訊。

我正在使用ethers.js v4.0與區塊鏈進行互動。

首先,添加"web3": "1.2.1"到您的package.json文件並執行npm install(或簡單地執行npm install web3)。

其次,嘗試以下節點腳本:

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();

YOUR_ETHEREUM_NODE_URL當然需要設置。

使用節點 v10.16.0 測試。

引用自:https://ethereum.stackexchange.com/questions/76191