我在网上找到了这个tuto:https://cryptomarketpool.com/create-a-defi-bank-that-pays-interest-yield-farm/和我通过混合在testnet上部署了我的令牌和智能合同。
问题是,我完全是个菜鸟,我真的不知道如何与这个合同互动。不确定hasStaked和isStaking之间有什么区别。
当试图以某种地址和金额执行合同时,如下所示。交易进行得很顺利,但什么也没发生。我的链接不会从我的钱包里消失。
下一步是用python编写一个小API,但首先我需要了解如何使用这个智能契约。

以下是我最聪明的合同:
pragma solidity ^0.6.12;
interface IERC20 {
function totalSupply() external view returns (uint);
function balanceOf(address account) external view returns (uint);
function transfer(address recipient, uint amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint);
function approve(address spender, uint amount) external returns (bool);
function transferFrom(
address sender,
address recipient,
uint amount
) external returns (bool);
event Transfer(address indexed from, address indexed to, uint value);
event Approval(address indexed owner, address indexed spender, uint value);
}
contract mycontract {
// call it DefiBank
string public name = "DefiBank";
// create 2 state variables
address public LINK;
address public bankToken;
address[] public stakers;
mapping(address => uint) public stakingBalance;
mapping(address => bool) public hasStaked;
mapping(address => bool) public isStaking;
// in constructor pass in the address for LINK token and your custom bank token
// that will be used to pay interest
constructor() public {
LINK = 0x01BE23585060835E02B77ef475b0Cc51aA1e0709;
bankToken = 0x5A39644Axxxxxxxxxxxxxxx849cBf102be;
}
// allow user to stake LINK tokens in contract
function stakeTokens(uint _amount) public {
// Trasnfer LINK tokens to contract for staking
IERC20(LINK).transferFrom(msg.sender, address(this), _amount);
// Update the staking balance in map
stakingBalance[msg.sender] = stakingBalance[msg.sender] + _amount;
// Add user to stakers array if they haven't staked already
if(!hasStaked[msg.sender]) {
stakers.push(msg.sender);
}
// Update staking status to track
isStaking[msg.sender] = true;
hasStaked[msg.sender] = true;
}
// allow user to unstake total balance and withdraw LINK from the contract
function unstakeTokens() public {
// get the users staking balance in LINK
uint balance = stakingBalance[msg.sender];
// reqire the amount staked needs to be greater then 0
require(balance > 0, "staking balance can not be 0");
// transfer LINK tokens out of this contract to the msg.sender
IERC20(usdc).transfer(msg.sender, balance);
// reset staking balance map to 0
stakingBalance[msg.sender] = 0;
// update the staking status
isStaking[msg.sender] = false;
}
// Issue bank tokens as a reward for staking
function issueInterestToken() public {
for (uint i=0; i<stakers.length; i++) {
address recipient = stakers[i];
uint balance = stakingBalance[recipient];
// if there is a balance transfer the SAME amount of bank tokens to the account that is staking as a reward
if(balance >0 ) {
IERC20(bankToken).transfer(recipient, balance);
}
}
}
event Received(address, uint);
receive() external payable {
emit Received(msg.sender, msg.value);
}
} 发布于 2022-02-25 12:18:02
我在这一行看到你的合同有错误。
// transfer LINK tokens out of this contract to the msg.sender
IERC20(usdc).transfer(msg.sender, balance);如果要使用链接令牌,则不能使用usdc变量,但必须使用以下链接变量来更改它:
// transfer LINK tokens out of this contract to the msg.sender
IERC20(LINK).transfer(msg.sender, balance);对于利害关系问题,用户必须批准使用您的令牌的合同。要做到这一点,您必须进入链接智能契约(在testnet上),并调用审批函数连接您的钱包和插入智能契约的地址和金额。
hasStaked和isStaking的区别是:
https://ethereum.stackexchange.com/questions/122529
复制相似问题