简体   繁体   English

将 ERC20 代币与其他 ERC20 代币交换

[英]Exchange ERC20 token with other ERC20 token

I have 2 ERC20 tokens.我有 2 个 ERC20 代币。 The contract are designed as standard ERC20.该合约设计为标准 ERC20。 Here are the 2 tokens to take as an example -以下是以 2 个令牌为例 -

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

I want to transfer some DAI from wallet 0xWalletDAI to 0xWalletAUDC to receive converted AUDC in return (I have private keys of both the wallets).我想将一些 DAI 从钱包0xWalletDAI0xWalletAUDC以接收转换后的 AUDC(我有两个钱包的私钥)。

Looking for some help to know how this can be implemented.寻求一些帮助以了解如何实施。 I would try to be helpful with more information if needed.如果需要,我会尽力提供更多信息。

I am using ethers.js v4.0 to interact with blockchain.我正在使用ethers.js v4.0与区块链交互。

I found out the solution to implement this using ethers.js -我找到了使用 ethers.js 来实现它的解决方案 -

const ethers = require('ethers');

let provider = ethers.getDefaultProvider();

// WALLETS
const DAIUserWalletObj = new ethers.Wallet(DAIUserPrivateKey, provider);
const AUDCWalletObj = new ethers.Wallet(AUDCPrivateKey, provider);

//CONTRACTS
const contractDAI = new ethers.Contract(DAIContractAddress, DAIContractABI, provider);
constractDAI = contractDAI.connect(DAIUserWalletObj);
const contractAUDC = new ethers.Contract(AUDCContractAddress, AUDCContractABI, provider);
contractAUDC = contractAUDC.connect(AUDCWalletObj);

let overRide = { gasLimit: 7500000 }
let numDAITokens = 20;  // Just for example
let numOfAUDCTokens = 40; // Just for example
const receipt1 = await contractDAI.transfer(AUDCContractAddress, numDAITokens, overRide);
const receipt2 = await contractAUDC.transfer(DAIUserWalletAddress, numOfAUDCTokens, overRide);
console.log(receipt1);
console.log(receipt2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM