如何在 Rust 中编写用于众筹/ICO 的智能合约?我正在尝试用 rust 重写 openzeppelin。
构造函数在 Solidity 中采用令牌参数,其中 IERC20 是一个接口。
constructor (uint256 rate, address payable wallet, IERC20 token) public {
require(rate > 0, "Crowdsale: rate is 0");
require(wallet != address(0), "Crowdsale: wallet is the zero address");
require(address(token) != address(0), "Crowdsale: token is the zero address");
_rate = rate;
_wallet = wallet;
_token = token;
}
如何在 Rust 协议中做到这一点? 我应该传递智能合约的地址吗?
#[near_bindgen]
impl Crowdsale {
#[init]
pub fn new(rate: U128, owner_id: AccountId, token_id: AccountId) -> Self {
let mut ft = Self {
owner_id: owner_id,
// owner id is the address of this smart contract with fungible token balance
token_id: token_id,
rate: rate;
}
ft
}
}
还需要buyTokens的rust函数,接收near token到token_id,然后将可互换的token部分转移给受益人。
会是这样吗:
#[payable]
pub fn buyTokens(&mut self, benificiary_id:AccountId, neartoken:u128) {
// Receive near token
// Send fungible token
owner_id.transfer(benificiary_id, amount);
}