88using Nethereum . ABI . FunctionEncoding ;
99using System . Linq ;
1010using System ;
11- using System . Collections ;
1211
1312namespace Thirdweb
1413{
@@ -51,6 +50,12 @@ public class Contract : Routable
5150 /// </summary>
5251 public Events events ;
5352
53+ /// <summary>
54+ /// Convenient wrapper to interact with any EVM contract
55+ /// </summary>
56+ /// <param name="chain">The chain identifier.</param>
57+ /// <param name="address">The contract address.</param>
58+ /// <param name="abi">The contract ABI.</param>
5459 public Contract ( string chain , string address , string abi = null )
5560 : base ( abi != null ? $ "{ address } { Routable . subSeparator } { abi } " : address )
5661 {
@@ -65,6 +70,10 @@ public Contract(string chain, string address, string abi = null)
6570 this . events = new Events ( baseRoute ) ;
6671 }
6772
73+ /// <summary>
74+ /// Get the balance of the contract.
75+ /// </summary>
76+ /// <returns>The balance of the contract as a <see cref="CurrencyValue"/> object.</returns>
6877 public async Task < CurrencyValue > GetBalance ( )
6978 {
7079 if ( Utils . IsWebGLBuild ( ) )
@@ -82,23 +91,76 @@ public async Task<CurrencyValue> GetBalance()
8291 }
8392
8493 /// <summary>
85- /// Execute a write transaction on a contract
94+ /// Prepare a transaction by creating a <see cref="Transaction"/> object.
95+ /// </summary>
96+ /// <param name="functionName">The name of the contract function.</param>
97+ /// <param name="args">Optional function arguments.</param>
98+ /// <returns>A <see cref="Transaction"/> object representing the prepared transaction.</returns>
99+ public async Task < Transaction > Prepare ( string functionName , params object [ ] args )
100+ {
101+ return await Prepare ( functionName , null , args ) ;
102+ }
103+
104+ /// <summary>
105+ /// Prepare a transaction by creating a <see cref="Transaction"/> object.
106+ /// </summary>
107+ /// <param name="functionName">The name of the contract function.</param>
108+ /// <param name="from">The address to send the transaction from.</param>
109+ /// <param name="args">Optional function arguments.</param>
110+ /// <returns>A <see cref="Transaction"/> object representing the prepared transaction.</returns>
111+ public async Task < Transaction > Prepare ( string functionName , string from = null , params object [ ] args )
112+ {
113+ var contract = new Web3 ( ThirdwebManager . Instance . SDK . session . RPC ) . Eth . GetContract ( this . abi , this . address ) ;
114+ var function = contract . GetFunction ( functionName ) ;
115+ var fromAddress = from ?? await ThirdwebManager . Instance . SDK . wallet . GetAddress ( ) ;
116+ var txInput = function . CreateTransactionInput ( fromAddress , args ) ;
117+ return new Transaction ( this , txInput ) ;
118+ }
119+
120+ /// <summary>
121+ /// Encode the function call with the given arguments.
122+ /// </summary>
123+ /// <param name="functionName">The name of the contract function.</param>
124+ /// <param name="args">The function arguments.</param>
125+ /// <returns>The encoded function call as a string.</returns>
126+ public string Encode ( string functionName , params object [ ] args )
127+ {
128+ var contract = new Web3 ( ThirdwebManager . Instance . SDK . session . RPC ) . Eth . GetContract ( this . abi , this . address ) ;
129+ var function = contract . GetFunction ( functionName ) ;
130+ return function . GetData ( args ) ;
131+ }
132+
133+ /// <summary>
134+ /// Decode the encoded arguments of a function call.
135+ /// </summary>
136+ /// <param name="functionName">The name of the contract function.</param>
137+ /// <param name="encodedArgs">The encoded arguments as a string.</param>
138+ /// <returns>A list of <see cref="ParameterOutput"/> objects representing the decoded arguments.</returns>
139+ public List < ParameterOutput > Decode ( string functionName , string encodedArgs )
140+ {
141+ var contract = new Web3 ( ThirdwebManager . Instance . SDK . session . RPC ) . Eth . GetContract ( this . abi , this . address ) ;
142+ var function = contract . GetFunction ( functionName ) ;
143+ return function . DecodeInput ( encodedArgs ) ;
144+ }
145+
146+ /// <summary>
147+ /// Execute a write transaction on a contract.
86148 /// </summary>
87- /// <param name="functionName">The contract function name to call</param>
88- /// <param name="args">Optional function arguments. Structs and Lists will get serialized automatically </param>
89- /// <returns>The transaction receipt</returns>
149+ /// <param name="functionName">The name of the contract function to call. </param>
150+ /// <param name="args">Optional function arguments.</param>
151+ /// <returns>The transaction receipt as a <see cref="TransactionResult"/> object. </returns>
90152 public Task < TransactionResult > Write ( string functionName , params object [ ] args )
91153 {
92154 return Write ( functionName , null , args ) ;
93155 }
94156
95157 /// <summary>
96- /// Execute a write transaction on a contract
158+ /// Execute a write transaction on a contract.
97159 /// </summary>
98- /// <param name="functionName">The contract function name to call</param>
99- /// <param name="transactionOverrides">Overrides to pass with the transaction</param>
100- /// <param name="args">Optional function arguments. Structs and Lists will get serialized automatically </param>
101- /// <returns>The transaction receipt</returns>
160+ /// <param name="functionName">The name of the contract function to call. </param>
161+ /// <param name="transactionOverrides">Overrides to pass with the transaction. </param>
162+ /// <param name="args">Optional function arguments.</param>
163+ /// <returns>The transaction receipt as a <see cref="TransactionResult"/> object. </returns>
102164 public async Task < TransactionResult > Write ( string functionName , TransactionRequest ? transactionOverrides , params object [ ] args )
103165 {
104166 if ( Utils . IsWebGLBuild ( ) )
@@ -134,11 +196,12 @@ public async Task<TransactionResult> Write(string functionName, TransactionReque
134196 }
135197
136198 /// <summary>
137- /// Read data from a contract
199+ /// Read data from a contract.
138200 /// </summary>
139- /// <param name="functionName">The contract function name to call</param>
140- /// <param name="args">Optional function arguments. Structs and Lists will get serialized automatically</param>
141- /// <returns>The data deserialized to the given typed</returns>
201+ /// <typeparam name="T">The type to deserialize the data into.</typeparam>
202+ /// <param name="functionName">The name of the contract function to call.</param>
203+ /// <param name="args">Optional function arguments.</param>
204+ /// <returns>The deserialized data of type <typeparamref name="T"/>.</returns>
142205 public async Task < T > Read < T > ( string functionName , params object [ ] args )
143206 {
144207 if ( Utils . IsWebGLBuild ( ) )
0 commit comments