Solidity

如何呼叫帶有結構參數的函式

  • October 27, 2022
   struct MigrateParams {
       address pair; // the Uniswap v2-compatible pair
       uint256 liquidityToMigrate; // expected to be balanceOf(msg.sender)
       uint8 percentageToMigrate; // represented as a numerator over 100
       address token0;
       address token1;
       uint24 fee;
       int24 tickLower;
       int24 tickUpper;
       uint256 amount0Min; // must be discounted by percentageToMigrate
       uint256 amount1Min; // must be discounted by percentageToMigrate
       address recipient;
       uint256 deadline;
       bool refundAsETH;
   }
function migrate(MigrateParams calldata params) external {code}

我應該如何呼叫“遷移”這個函式

這是我的程式碼

   function callFunc() public {
       MigrateParams memory mig;
       mig.pair = address(this;
       mig.liquidityToMigrate = 40872475671826880538328523;
       mig.percentageToMigrate = 1;
       mig.token0 = 0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c;
       mig.token1 = 0xC3afDe95B6Eb9ba8553cDAea6645D45fB3a7FAF5;
       mig.fee = 500;
       mig.tickLower = 100;
       mig.tickUpper = 100;
       mig.amount0Min = 0;
       mig.amount1Min = 0;
       mig.recipient = address(this);
       mig.deadline = 8888888898993;
       mig.refundAsETH = true;

       token.migrate(mig);
   }

但是編譯不通過,不能這樣呼叫,這個問題真的很困擾我,誰能幫忙

由於您沒有指定編譯錯誤或token變數的定義方式,我將粘貼一個您可以直接在 remix 上使用的工作範例。

我最好的猜測是你還沒有定義token. 介面定義了可以在合約外部呼叫的函式。

注意:您應該首先部署Token合約,然後ContractToken’s 地址作為輸入。

pragma solidity 0.8.12;

struct MigrateParams {
   address pair; // the Uniswap v2-compatible pair
   uint256 liquidityToMigrate; // expected to be balanceOf(msg.sender)
   uint8 percentageToMigrate; // represented as a numerator over 100
   address token0;
   address token1;
   uint24 fee;
   int24 tickLower;
   int24 tickUpper;
   uint256 amount0Min; // must be discounted by percentageToMigrate
   uint256 amount1Min; // must be discounted by percentageToMigrate
   address recipient;
   uint256 deadline;
   bool refundAsETH;
}

interface IToken {
   function migrate(MigrateParams calldata params) external;
}

contract Token {
   function migrate(MigrateParams calldata params) external {
       // code
       return;
   }
}

contract Contract{
   
   IToken public token;

   constructor(IToken _token){
       token = _token;
   }

   function callFunc() public {
       MigrateParams memory mig;
       mig.pair = address(this);
       mig.liquidityToMigrate = 40872475671826880538328523;
       mig.percentageToMigrate = 1;
       mig.token0 = 0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c;
       mig.token1 = 0xC3afDe95B6Eb9ba8553cDAea6645D45fB3a7FAF5;
       mig.fee = 500;
       mig.tickLower = 100;
       mig.tickUpper = 100;
       mig.amount0Min = 0;
       mig.amount1Min = 0;
       mig.recipient = address(this);
       mig.deadline = 8888888898993;
       mig.refundAsETH = true;

       token.migrate(mig);
   }

}

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