Skip to content

Commit faf6b5d

Browse files
authored
Transaction API (#75)
1 parent 3fffcf3 commit faf6b5d

File tree

9 files changed

+786
-72
lines changed

9 files changed

+786
-72
lines changed

Assets/Thirdweb/Core/Scripts/Contract.cs

+77-14
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using Nethereum.ABI.FunctionEncoding;
99
using System.Linq;
1010
using System;
11-
using System.Collections;
1211

1312
namespace 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())

Assets/Thirdweb/Core/Scripts/ThirdwebInterceptor.cs

-4
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ public ThirdwebInterceptor(IThirdwebWallet thirdwebWallet)
1818

1919
public override async Task<object> InterceptSendRequestAsync<T>(Func<RpcRequest, string, Task<T>> interceptedSendRequestAsync, RpcRequest request, string route = null)
2020
{
21-
UnityEngine.Debug.Log($"{request.Method} Request Intercepted: {JsonConvert.SerializeObject(request.RawParameters)}");
22-
2321
if (request.Method == "eth_accounts")
2422
{
2523
var addy = await _thirdwebWallet.GetAddress();
@@ -65,8 +63,6 @@ public override async Task<object> InterceptSendRequestAsync<T>(
6563
params object[] paramList
6664
)
6765
{
68-
UnityEngine.Debug.Log($"{method} Request Intercepted: {JsonConvert.SerializeObject(paramList)}");
69-
7066
if (method == "eth_accounts")
7167
{
7268
var addy = await _thirdwebWallet.GetAddress();

Assets/Thirdweb/Core/Scripts/ThirdwebSDK.cs

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ public struct StorageOptions
6161
public struct GaslessOptions
6262
{
6363
public OZDefenderOptions? openzeppelin;
64+
65+
[System.Obsolete("Biconomy is not fully supported and will be removed soon. Use OpenZeppelin Defender instead.")]
6466
public BiconomyOptions? biconomy;
6567
public bool experimentalChainlessSupport;
6668
}

0 commit comments

Comments
 (0)