Truffle

如何將乙太幣從 SC2 轉移到 SC1?

  • December 16, 2021

我有兩個契約:

pragma solidity ^0.5.8;
contract SC1 {
   event Log(uint gas);
   // Fallback function must be declared as external.
   function() external payable {
       // send / transfer (forwards 2300 gas to this fallback function)
       // call (forwards all of the gas)   
   }
}

==

pragma solidity ^0.5.8;
contract SC2 {
   function transferToFallback(address payable _to) public payable {
       _to.transfer(msg.value);
   }

   function callFallback(address payable _to) public payable {
       //(bool success,) = bank.call.value(msg.value)(payload); 
       //(bool success,) = bank.call{value: msg.value}(payload);
       //require(success, "Ether transfer failed.");
       
       (bool sent, ) = _to.call.value(msg.value)("");
       require(sent, "Failed to send Ether");
   }
  function() external payable{
   }
}

問題:我正在將乙太幣從 SC2 發送到 SC1,但在轉移後 SC1 餘額為零且 SC2 餘額沒有改變。我認為問題在於 SC2 的 transferToFallback 函式的簽名。它不接受發送乙太幣的任何論據。請指導我如何將乙太幣從 SC2 發送到 SC1。

我的工作:我使用 truffle 控制台發送乙太幣。我正在使用以下命令:

)> senderSTB.transferToFallback(receiverFB.address, {from:accounts[0]})

下面執行的語句:

{ tx:
  '0x288ff695c288fe58f1a907eb95ee63c403017d73d7c202bf4d76b71a2dc4e51b',
 receipt:
  { transactionHash:

但兩個 SC 的平衡沒有變化。

遷移文件即 2_deploy_contracts.js 是:

const FB = artifacts.require("SC1");
const STFB = artifacts.require("SC2");

module.exports = function(deployer) {
deployer.deploy(FB);
deployer.deploy(STFB);
};

祖爾菲。

問題是transferToFallback傳輸 msg.value

_to.transfer(msg.value);

但是從控制台沒有發送任何值senderSTB.transferToFallback(receiverFB.address, {from:accounts[0]})

如果您發送一些金額,它應該達到目標契約

> senderSTB.transferToFallback(receiverFB.address, {from:accounts[0], value: "1000" })

另一種選擇是從 SC2 餘額轉發

function transferToFallback(address payable _to, uint amount) public {
   _to.transfer(amount);
}

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