1

I am executing the following script, I don't know how to retrieve the sender and receiver from the transaction receipt. I am trying to retrieve the transaction receipt but it does not work.

// 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()
}

The SCs for sender and receiver are:

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{
     }

}

and

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);
       //}          
    }
}

Somebody please guide me how to retrieve the transaction receipt, and the sender and receiver of the transaction.

Zulfi.

1 Answer 1

1

To retrieve the transaction receipt object, sender, and receiver, you can do this at the end of the script :

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)

You can find more info about the web3 sendTrannsaction function behavior here : https://web3js.readthedocs.io/en/v1.3.4/web3-eth.html#sendtransaction.

1
  • @@clement: if I do: result = await sc1.sendTo(sc2.address, web3.utils.toWei(amount,"ether"), {from:accounts[0]}) console.log("receipt : ", result) console.log("sender : ", result.from) console.log("receiver : ", result.to) console.log(Deposited ${amount} Ether from sc1:, sc1.address), I am getting the transaction receipt but sender and receiver are undefined: '0x000000000000000000000000000000000000000000000000000000000000000000.....00000000000000000000000000000000', rawLogs: [] }, logs: [] } receiver : undefined, please guide me, Zulfi.
    – zak100
    Jul 5, 2021 at 4:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.