Truffle

松露腳本:無法檢索交易收據,不知道如何獲取交易發送方和接收方

  • July 4, 2021

我正在執行以下腳本,我不知道如何從交易收據中檢索發送方和接收方。我正在嘗試檢索交易收據,但它不起作用。

// Contracts
const  MySC1 = artifacts.require("MySC1")
const MySC2 = artifacts.require("MySC2")

module.exports = async function(callback) {
try {
   // Fetch accounts from wallet - these are unlocked
   const accounts = await web3.eth.getAccounts()
   // Set up account to transferEther to Victim
   const acc2 = accounts[2]
   acc2bal = await web3.eth.getBalance(acc2)
   web3.utils.fromWei(acc2bal, "ether")
   console.log('acc2 balance', acc2bal, 'address',acc2)
   // Fetch the deployed exchange
   const sc1 = await MySC1.deployed()
   console.log('SC1 deployed', sc1.address)
   const sc2 = await MySC2.deployed()
   console.log('SC2 deployed', sc2.address)
   sc1bal = await web3.eth.getBalance(sc1.address)
   web3.utils.fromWei(sc1bal, "ether")
   console.log(`Initial SC1:`,sc1.address,` balance is ${sc1bal}`)
   sc2bal = await web3.eth.getBalance(sc2.address)
   web3.utils.fromWei(sc2bal, "ether")
   console.log(`Initial SC2:`,sc2.address,` balance is ${sc2bal}`)
   amount = '11'
   result = await web3.eth.sendTransaction({to:sc1.address, from:acc2, value: web3.utils.toWei(amount)})
   result.receipt
}
 catch(error) {
   console.log(error)
 }

 callback()
}

發送者和接收者的 SC 是:

pragma solidity ^0.5.8;

contract MySC1 {

   address owner;

   constructor() public {
       owner = msg.sender;
   }

   function sendTo(address payable receiver, uint amount) public {
       (bool success,) = receiver.call.value(amount)("");
       require(success);
   }

   function() external payable{
    }

}

pragma solidity ^0.5.8;
interface MySC1{
  function sendTo(address payable to, uint amount) external;
}
contract MySC2 {
   uint public cnt;
   //This contract will receive Ether sent by MyContract
   address owner;

   constructor() public {
      owner = msg.sender;
   }

   function() external payable {
      //cnt++;
     //if(cnt < 2) {
     //   MySC1(msg.sender).sendTo(address(this), msg.sender.balance);
      //}          
   }
}

有人請指導我如何檢索交易收據,以及交易的發送方和接收方。

祖爾菲。

要檢索交易收據對象、發送方和接收方,您可以在腳本末尾執行以下操作:

result = await web3.eth.sendTransaction({to:sc1.address, from:acc2, value: web3.utils.toWei(amount)})
console.log("receipt : ", result)
console.log("sender : ", result.from)
console.log("receiver : ", result.to)

您可以在此處找到有關 web3sendTrannsaction函式行為的更多資訊:https ://web3js.readthedocs.io/en/v1.3.4/web3-eth.html#sendtransaction 。

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