8
8
using Nethereum . ABI . FunctionEncoding ;
9
9
using System . Linq ;
10
10
using System ;
11
- using System . Collections ;
12
11
13
12
namespace Thirdweb
14
13
{
@@ -51,6 +50,12 @@ public class Contract : Routable
51
50
/// </summary>
52
51
public Events events ;
53
52
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>
54
59
public Contract ( string chain , string address , string abi = null )
55
60
: base ( abi != null ? $ "{ address } { Routable . subSeparator } { abi } " : address )
56
61
{
@@ -65,6 +70,10 @@ public Contract(string chain, string address, string abi = null)
65
70
this . events = new Events ( baseRoute ) ;
66
71
}
67
72
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>
68
77
public async Task < CurrencyValue > GetBalance ( )
69
78
{
70
79
if ( Utils . IsWebGLBuild ( ) )
@@ -82,23 +91,76 @@ public async Task<CurrencyValue> GetBalance()
82
91
}
83
92
84
93
/// <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.
86
148
/// </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>
90
152
public Task < TransactionResult > Write ( string functionName , params object [ ] args )
91
153
{
92
154
return Write ( functionName , null , args ) ;
93
155
}
94
156
95
157
/// <summary>
96
- /// Execute a write transaction on a contract
158
+ /// Execute a write transaction on a contract.
97
159
/// </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>
102
164
public async Task < TransactionResult > Write ( string functionName , TransactionRequest ? transactionOverrides , params object [ ] args )
103
165
{
104
166
if ( Utils . IsWebGLBuild ( ) )
@@ -134,11 +196,12 @@ public async Task<TransactionResult> Write(string functionName, TransactionReque
134
196
}
135
197
136
198
/// <summary>
137
- /// Read data from a contract
199
+ /// Read data from a contract.
138
200
/// </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>
142
205
public async Task < T > Read < T > ( string functionName , params object [ ] args )
143
206
{
144
207
if ( Utils . IsWebGLBuild ( ) )
0 commit comments