Solidity

MyToken.address 在這裡做什麼?

  • February 5, 2022

這是 truffle react 的部署者合約

var MyToken =  artifacts.require("MyToken.sol");
var MyTokenSale = artifacts.require("MyTokenSale.sol");

module.exports = async function(deployer){
   let addr = await wev3.eth.getAccounts();
   await deployer.deploy(MyToken, 10000);
   await deployer.deploy(MyTokenSale, 1, addr[0], MyToken.address);

   let instance = await MyToken.deployed();
   await instance.transfer(MyTokenSale.address, 10000);
}

MyToken 合約

// contracts/GLDToken.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.1;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract MyToken is ERC20 {
   constructor(uint256 initialSupply) ERC20("GoldBucks", "GLD") {
       _mint(msg.sender, initialSupply);
   }
}

MyTokenSale 合約

pragma solidity ^0.8.1;

import "./Crowdsale.sol";

contract MyTokenSale is Crowdsale{
   KycContract kyc;
   constructor(
       uint256 rate,
       address payable wallet,
       IERC20 token // this token will be sent to that particular addres that we specify in the deployer
   )

   Crowdsale(rate,wallet,token) public{

   }
}

部署者合約中 MyToken.address 有什麼用?

在您的部署腳本中,智能合約名稱後面的參數(例如MyTokenSale)是該合約的建構子參數。

因此,您可以看到以下參數MyTokenSaleuint256 rate, address payable wallet, IERC20 token

這意味著在通話中await deployer.deploy(MyTokenSale, 1, addr[0], MyToken.address);

rate = 1,錢包 = addr

$$ 0 $$, 令牌 = MyToken.address {contract}.address 可在合約部署並引用合約地址後在 truffle 中訪問。

高溫高壓

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