Calling Another Contract
The call
method allows one contract to interact with another contract by sending calldata to it.
Method
call(destinationContract: Address, calldata: BytesWriter): BytesReader
-
Parameters:
destinationContract
: The address of the contract you want to call.calldata
: The data to send to the destination contract, formatted usingBytesWriter
.
-
Returns:
BytesReader
: The response from the destination contract, which can be parsed as needed.
Example Usage
import {
Address,
BytesWriter,
Blockchain,
} from "@btc-vision/btc-runtime/runtime";
import { u256 } from 'as-bignum/assembly';
// Define the destination contract address
let destination: Address = Address.fromString(...);
// Create calldata
let calldata = new BytesWriter(64); // 64 is the length in bytes of the calldata
calldata.writeString("methodName");
calldata.writeU256(u256.from(1234)); // Example of adding additional method parameters
// Execute the call
let response = Blockchain.call(destination, calldata);
// Log the response
Blockchain.log(`Response: ${response}`);
Best Practices
- The
calldata
must be formatted correctly to match the expected input of the destination contract. - Ensure that the destination contract is deployed and the address is valid.
- Always parse the
BytesReader
response correctly to extract meaningful data. - Errors during the call may not directly throw exceptions but could result in an empty or unexpected response.