Interaction with deployed contracts in solidity: guide
In the Ethereum world, the deployment of contracts is the basic aspect of building decentralized applications (DAPP). Although it is common to interact with these contracts using the Web3 library, there are cases where we need to bypass this restriction and communicate directly with the contracts deployed. In this article, we will examine how to interact with the set -up contracts in solidity, we focus on direct calls to other contract addresses and using keywords.
Direct calls vs. Web3 Library
Before you dive into the topic of interaction with the deployed contracts, it is necessary to understand the difference between direct calls and interactions Web3:
* Direct calls : You can send the message directly to another contract address using the syntax contraddress
in solidity.
* Example:
`
Contractual function {
MYFUNCTION function () Public Returns (String Memory) {
Return CallContract (« OtherContract », « MyFunction »);
}
}
`
* Web3 Library: Web3 library provides an interface to interact with EThereum networks, allowing you to submit applications and send messages to other contracts.
Interaction with deployed contracts
If you want to interact with deployed contracts without using the Web3 library, you will need to use a combination of « callcontract » and CallContract keywords. Here are some examples:
1. Direct call
You can call another contract address directly by handing it over as an argument of the « callcontract:
Solidity
// Contract a
Pragma of solidity ^0.8.0;
Contractual function {
// function to interact with another contract
MYFUNCTION function () Public Returns (String Memory) {
Return CallContract (« OtherContract », « MyFunction »);
}
}
`
2. Using keywords of assembly
You can also use the keywords of assembly to direct calls or send messages to other contracts:
`Solidity
// Contract a
Pragma of solidity ^0.8.0;
Contractual function {
// function to interact with another contract
MYFUNCTION function () Public Returns (String Memory) {
// Send a message to another contract
assembly
Let MSG = MJMPACK (« Other Contract », « MyFunction », 0)
Call (0, MSG, 0)
}
}
`
In this example, we use the MJPACK
feature to pack the message before sending it to the second contract.
Example of use
Here is an example of how you can communicate with two contracts: Treaty A and Treaty B. In the Treaty and we define « myfunction » that returns the chain value. Then we call this feature from another contract using keywords of direct calls or assembly:
`Solidity
// Contract a
Pragma of solidity ^0.8.0;
Contractual function {
// function to interact with another contract
MYFUNCTION function () Public Returns (String Memory) {
Return « Hi, World! »;
}
}
`
In this example, we call Contracta.Myfunction ()
directly or using keyword assembly sending a message to the contract B:
`Solidity
// Contract b
Pragma of solidity ^0.8.0;
Contractual function {
// function to interact with another contract
MYFUNCTION function () Public Returns (String Memory) {
Return « Hi, World! »;
}
}
`
According to these instructions and examples, you can now interact with the deployed solidness contracts without relying on the Web3 library.
Conclusion
Interaction with the deployed contracts is an essential aspect of building decentralized applications. By understanding how to use direct calls and mounting keywords in the fort, you will be able to create more complex interactions between contracts and improve your overall understanding of the Ethereum programming model.