如何使用本地主机测试戴



虽然我使用Compound的交换接口成功地创建了测试Dai,但在使用Ganache和我的本地机器时,我遇到了一些问题。在尝试mint时,我有以下脚本(这也是从教程中发布的,在这里,关于minting测试dai(

const Web3 = require("web3");
const web3 = new Web3("http://127.0.0.1:8545");
const daiAbi = []; // left out for brevity, can be found at https://changelog.makerdao.com/ 
// Address of DAI contract
const daiMainNetAddress = "0x6B175474E89094C44Da98b954EedeAC495271d0F";
// Address of Join
const daiMcdJoin = "0x9759A6Ac90977b93B58547b4A71c78317f391A28";
let daiContract;
let accounts;
web3.eth
.getAccounts()
.then((ganacheAccounts) => {
accounts = ganacheAccounts;
daiContract = new web3.eth.Contract(daiAbi, daiMainNetAddress);
// 500 DAI
const numbDaiToMint = web3.utils.toWei("500", "ether");
return daiContract.methods.mint(accounts[0], numbDaiToMint).send({
from: daiMcdJoin,
gasPrice: web3.utils.toHex(0),
});
})
.then(() => {
console.log("DAI mint success");
return daiContract.methods.balanceOf(accounts[0]).call();
})
.then((balanceOf) => {
console.log("balanceOf:", balanceOf);
const dai = web3.utils.fromWei(balanceOf, "ether");
console.log("DAI amount in first Ganache account wallet:", dai);
})
.catch((err) => {
console.error(err);
});

然而,每次运行此程序时,我都会获得"DAI mint成功",但"返回的值无效,是不是用完了?"我需要明确设置Gas吗?

你的ganache连接到主网了吗?

如果没有,你需要通过设置truffle.config将其连接到主网。如果你想在本地测试网上尝试,你需要在本地测试网上部署dai合约,或者你可以使用这个方便的技巧https://medium.com/@samajammin/how-to-interact-with-etherums-mainnet-in-a-development-environment-with-ganache-3d8649df0876 to克隆主网的当前状态

最新更新