loading

Etherum: problem with “tx.origin” and “msg.sender” in the foundry test

As part of my current study on smart contracts, I was working on a test case. Howver, I encountered an unexpected problem that required heel problems.

The problem:

During the tests, I am noticed that the fields “TX. Origin” and “Msg.sender in my contract do not comport as planned. Specifically, these Fields Seemed to Be Providing Different Values ​​Than I was expecting.

The problem:

After examining the contract code and understanding how he interacts with the blockchain, I realized that there are the problems that the game here:

  • tx.origin : Thetx.origin Field Contains the Address of the Account That Initiated The Transaction (I.E., The Sender). Howver, during the Foundry test frame, this value is not always a precision.

  • Msg.sender

    Ethereum: Issue with tx.origin and msg.sender in Foundry test

    : the address called, the recipient). This currency can be differentiated from "TX. Origin", especially if the transction was a hundred via a different account.

The solution:

To resolve this problem, I applied several modifications to my test code:

1 and 1

  • UseContractaddress ‘as fall : If tx. Origin’ andMsg.sender are always differentiation, I am not back to constraint.

Here is an update code to sniff the demonstration of these modifications:

Javaccrot

Const {ethers} = require ("ethers");

// Test box for a telephone contract

Asynchronous function Testtephone () {

// Initial contract and account

CONSTPONECONTRACT = NEW ETHERS.Contract (

"

Abi telephone,

Ethers.FrombateKey (

"Your_prive_key", // replace with your private key

))

));

// Create a transaction object

Const tx = {

Free: Ethers.Wallet.FromprivateKey ("Your_Prive_Key"),

to: telephony.addresss, // correct address for your contract

Currency: Ethers.utils.parsether ("1"), // set to the transaction currency

Gasprice: 20n, // fix the price of gas (optional)

- 2000, // Define the maximum.

Data: "0x", // defined on the transaction data (empty in this case)

};

// Make the transaction

Const Result = Await TELEPHONECONTRACT.SENDRRANSAction (TX);

console.log (result);

}

// Run the test function

Testtelephone ();

` ‘

Conclusion:

By applying these modifications, I had to solve the problem with “TX. Origin” and “Msg.sender” in my test of testing. Now, the curament are precision, I can do it with additional tests and precision.

If you are faced with similar problems or wealthy to be quests on this solution, do not hesitate to ask!

Write a Reply or Comment

Your email address will not be published. Required fields are marked *