Contract object for an already deployed contract is rather straightforward.
Calling Smart Contract Functions
To call a smart contract, you’ll use theCallFunction method to create a CallContractFunction object which will determine the appropriate gasPrice, gasLimit, nonce, and data to include in a newly assembled EthTransaction when provided with a client and a ContractCall object to the Create async Task
An example of calling a smart contract would look like:
SendTransactionMethod instead.
Alternatively, if you want to simply create the EthTransaction and send it at a later time, you can use the CallContractFunction object from CallFunction directly.
CallFunction method accepts an arbitrary number of arguments. You’ll want to provide the arguments in the order they are provided in the ABI/function signature.
Understanding Data Type Mappings
When interacting with smart contracts, it is important to understand how EVM datatypes are mapped to C# datatypes in the SequenceEthereum library. If you were to, for example, provide a string where the ABI expects an Integer, you will receive an exception, even if that string could be converted into an integer.Address
In C# you can either use astring type or create an instance of Address. Ensure that your string is a hexadecimal
string starting with 0x and a fixed length of 42 characters.
Integers
For integer types such asint256, uint8, or uint256 you use the BigInteger type from System.Numerics.
Bytes
To define byte data types from Solidity in C#, you have the options to create aFixedByte for types such as byte16 or byte32.
When your contract requires bytes you can convert any value into a byte[] of any length.
If your data is represented as a hex string in C#, make sure to use our HexStringToByteArray() function to convert
the hex value back to the original byte array.
For byte arrays such as byte32[], you simply create a FixedByte[] in C#.
Structs
You use Tuples to call an on-chain function with a struct. Here is an example Solidity struct and how to define it using Sequence’s Unity SDK and pass it as an arg in aContract.CallFunction function.
Solidity Struct
Other Types
For traditional data types in Solidity likestring or bool, you can use the same data types in C#.
Querying Contracts
To query a smart contract (read data from it), you’ll use theSendQuery<T> method to query the contract and return the result as type T (if possible).
An example of querying a smart contract would look like:
QueryContract<T> to create a delegate.
Deploying Contracts
If you want to deploy a contract, you can use theContractDeployer