Solidity

如何在 remix 中與我的第一個 defi 智能合約進行互動?

  • February 25, 2022

我在網上找到了這個 tuto:https ://cryptomarketpool.com/create-a-defi-bank-that-pays-interest-yield-farm/我通過 remix 在測試網上部署了我的代幣和我的智能合約。

問題是我完全是個菜鳥,我真的不知道如何與這份契約互動。hasStaked不知道和有什麼區別isStaking

當嘗試使用一些地址和一些金額執行契約時,如下所示。交易進行得很順利,但什麼也沒有發生。我的 LINK 不會從我的錢包中消失。

下一步是用 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);
   }
} 

我在這一行看到你的契約有錯誤

// transfer LINK tokens out of this contract to the msg.sender
IERC20(usdc).transfer(msg.sender, balance);

如果您要質押 LINK 代幣,則不能使用 usdc 變數,但必須使用 LINK 變數更改它,如下所示:

// transfer LINK tokens out of this contract to the msg.sender
IERC20(LINK).transfer(msg.sender, balance);

對於質押問題,使用者必須批准合約才能使用您的代幣。為此,您必須進入 LINK 智能合約(在測試網上)並呼叫批准功能連接您的錢包並插入智能合約的地址和金額。

hasStaked 和 isStaking 的區別在於:

  • hasStaked:是一個映射關係到他們已經質押過代幣的所有使用者。如果他們在之前的時間進行了質押,則不會將使用者添加到質押者映射中;
  • isStaking:是所有使用者的映射關係,他們(目前)將您的代幣抵押到智能合約中。

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