Skip to content

Commit 88dfe83

Browse files
committed
Passing the ABI to underlying libraries to apply to respective routes.
This allows the use of ERC20, ERC721, ERC1155 shorthands with custom contracts implementing said interfaces.
1 parent 703eeeb commit 88dfe83

File tree

4 files changed

+57
-43
lines changed

4 files changed

+57
-43
lines changed

Assets/Thirdweb/Scripts/Contract.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@ public class Contract
2727
/// </summary>
2828
public Marketplace marketplace;
2929

30-
public Contract(string chain, string address, string abi = null) {
30+
public Contract(string chain, string address, string abi = null)
31+
{
3132
this.chain = chain;
3233
this.address = address;
3334
this.abi = abi;
34-
this.ERC20 = new ERC20(chain, address);
35-
this.ERC721 = new ERC721(chain, address);
36-
this.ERC1155 = new ERC1155(chain, address);
35+
this.ERC20 = new ERC20(chain, address, abi);
36+
this.ERC721 = new ERC721(chain, address, abi);
37+
this.ERC1155 = new ERC1155(chain, address, abi);
3738
this.marketplace = new Marketplace(chain, address);
3839
}
3940

@@ -45,7 +46,7 @@ public Contract(string chain, string address, string abi = null) {
4546
/// <returns>The data deserialized to the given typed</returns>
4647
public async Task<T> Read<T>(string functionName, params object[] args)
4748
{
48-
string [] argsEncoded = new string[args.Length + 1];
49+
string[] argsEncoded = new string[args.Length + 1];
4950
argsEncoded[0] = functionName;
5051
Utils.ToJsonStringArray(args).CopyTo(argsEncoded, 1);
5152
return await Bridge.InvokeRoute<T>(getRoute("call"), argsEncoded);
@@ -59,18 +60,15 @@ public async Task<T> Read<T>(string functionName, params object[] args)
5960
/// <returns>The transaction receipt</returns>
6061
public async Task<TransactionResult> Write(string functionName, params object[] args)
6162
{
62-
string [] argsEncoded = new string[args.Length + 1];
63+
string[] argsEncoded = new string[args.Length + 1];
6364
argsEncoded[0] = functionName;
6465
Utils.ToJsonStringArray(args).CopyTo(argsEncoded, 1);
6566
return await Bridge.InvokeRoute<TransactionResult>(getRoute("call"), argsEncoded);
6667
}
6768

68-
private string getRoute(string functionPath) {
69-
if (abi != null) {
70-
return this.address + "#" + abi + "." + functionPath;
71-
} else {
72-
return this.address + "." + functionPath;
73-
}
69+
private string getRoute(string functionPath)
70+
{
71+
return abi != null ? this.address + "#" + abi + "." + functionPath : this.address + "." + functionPath;
7472
}
7573
}
7674
}

Assets/Thirdweb/Scripts/ERC1155.cs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,19 @@ public class ERC1155
1515
/// Handle signature minting functionality
1616
/// </summary>
1717
public ERC1155Signature signature;
18-
/// <summary>
18+
/// <summary>
1919
/// Query claim conditions
2020
/// </summary>
2121
public ERC1155ClaimConditions claimConditions;
2222

2323
/// <summary>
2424
/// Interact with any ERC1155 compatible contract.
2525
/// </summary>
26-
public ERC1155(string chain, string address)
26+
public ERC1155(string chain, string address, string abi = null)
2727
{
2828
this.chain = chain;
2929
this.address = address;
30+
this.abi = abi;
3031
this.signature = new ERC1155Signature(chain, address);
3132
this.claimConditions = new ERC1155ClaimConditions(chain, address);
3233
}
@@ -69,7 +70,7 @@ public async Task<string> Balance(string tokenId)
6970
/// <summary>
7071
/// Get the balance of the given NFT for the given wallet address
7172
/// </summary>
72-
public async Task<string> BalanceOf(string address, string tokenId)
73+
public async Task<string> BalanceOf(string address, string tokenId)
7374
{
7475
return await Bridge.InvokeRoute<string>(getRoute("balanceOf"), Utils.ToJsonStringArray(address, tokenId));
7576
}
@@ -173,12 +174,13 @@ public async Task<TransactionResult> MintAdditionalSupplyTo(string address, stri
173174

174175
// PRIVATE
175176

176-
private string getRoute(string functionPath) {
177-
return this.address + ".erc1155." + functionPath;
177+
private string getRoute(string functionPath)
178+
{
179+
return abi != null ? this.address + "#" + abi + ".erc1155." + functionPath : this.address + ".erc1155." + functionPath;
178180
}
179181
}
180182

181-
/// <summary>
183+
/// <summary>
182184
/// Fetch claim conditions for a given ERC1155 drop contract
183185
/// </summary>
184186
public class ERC1155ClaimConditions
@@ -224,14 +226,15 @@ public async Task<bool> GetClaimerProofs(string tokenId, string claimerAddress)
224226
return await Bridge.InvokeRoute<bool>(getRoute("getClaimerProofs"), Utils.ToJsonStringArray(claimerAddress));
225227
}
226228

227-
private string getRoute(string functionPath) {
228-
return this.address + ".erc1155.claimConditions." + functionPath;
229+
private string getRoute(string functionPath)
230+
{
231+
return abi != null ? this.address + "#" + abi + ".erc1155.claimConditions." + functionPath : this.address + ".erc1155.claimConditions." + functionPath;
229232
}
230233
}
231234

232235
// TODO switch to another JSON serializer that supports polymorphism
233236
[System.Serializable]
234-
#nullable enable
237+
#nullable enable
235238
public class ERC1155MintPayload
236239
{
237240
public string to;
@@ -247,7 +250,8 @@ public class ERC1155MintPayload
247250
// public long mintStartTime;
248251
// public long mintEndTime;
249252

250-
public ERC1155MintPayload(string receiverAddress, NFTMetadata metadata) {
253+
public ERC1155MintPayload(string receiverAddress, NFTMetadata metadata)
254+
{
251255
this.metadata = metadata;
252256
this.to = receiverAddress;
253257
this.price = "0";
@@ -279,7 +283,8 @@ public class ERC1155MintAdditionalPayload
279283
// public long mintStartTime;
280284
// public long mintEndTime;
281285

282-
public ERC1155MintAdditionalPayload(string receiverAddress, string tokenId) {
286+
public ERC1155MintAdditionalPayload(string receiverAddress, string tokenId)
287+
{
283288
this.tokenId = tokenId;
284289
this.to = receiverAddress;
285290
this.price = "0";
@@ -350,8 +355,9 @@ public async Task<TransactionResult> Mint(ERC1155SignedPayload signedPayload)
350355
return await Bridge.InvokeRoute<TransactionResult>(getRoute("mint"), Utils.ToJsonStringArray(signedPayload));
351356
}
352357

353-
private string getRoute(string functionPath) {
354-
return this.address + ".erc1155.signature." + functionPath;
358+
private string getRoute(string functionPath)
359+
{
360+
return abi != null ? this.address + "#" + abi + ".erc1155.signature." + functionPath : this.address + ".erc1155.signature." + functionPath;
355361
}
356362
}
357363
}

Assets/Thirdweb/Scripts/ERC20.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ public class ERC20
2222
/// <summary>
2323
/// Interact with any ERC20 compatible contract.
2424
/// </summary>
25-
public ERC20(string chain, string address)
25+
public ERC20(string chain, string address, string abi = null)
2626
{
2727
this.chain = chain;
2828
this.address = address;
29+
this.abi = null;
2930
this.signature = new ERC20Signature(chain, address);
3031
this.claimConditions = new ERC20ClaimConditions(chain, address);
3132
}
@@ -140,13 +141,14 @@ public async Task<TransactionResult> MintTo(string address, string amount)
140141

141142
// PRIVATE
142143

143-
private string getRoute(string functionPath) {
144-
return this.address + ".erc20." + functionPath;
144+
private string getRoute(string functionPath)
145+
{
146+
return abi != null ? this.address + "#" + abi + ".erc20." + functionPath : this.address + ".erc20." + functionPath;
145147
}
146148
}
147149

148150
[System.Serializable]
149-
#nullable enable
151+
#nullable enable
150152
public class ERC20MintPayload
151153
{
152154
public string to;
@@ -159,7 +161,8 @@ public class ERC20MintPayload
159161
// public long mintStartTime;
160162
// public long mintEndTime;
161163

162-
public ERC20MintPayload(string receiverAddress, string quantity) {
164+
public ERC20MintPayload(string receiverAddress, string quantity)
165+
{
163166
this.to = receiverAddress;
164167
this.quantity = quantity;
165168
this.price = "0";
@@ -239,8 +242,9 @@ public async Task<bool> GetClaimerProofs(string claimerAddress)
239242
return await Bridge.InvokeRoute<bool>(getRoute("getClaimerProofs"), Utils.ToJsonStringArray(claimerAddress));
240243
}
241244

242-
private string getRoute(string functionPath) {
243-
return this.address + ".erc20.claimConditions." + functionPath;
245+
private string getRoute(string functionPath)
246+
{
247+
return abi != null ? this.address + "#" + abi + ".erc20.claimConditions." + functionPath : this.address + ".erc20.claimConditions." + functionPath;
244248
}
245249
}
246250

@@ -286,8 +290,9 @@ public async Task<TransactionResult> Mint(ERC20SignedPayload signedPayload)
286290
return await Bridge.InvokeRoute<TransactionResult>(getRoute("mint"), Utils.ToJsonStringArray(signedPayload));
287291
}
288292

289-
private string getRoute(string functionPath) {
290-
return this.address + ".erc20.signature." + functionPath;
293+
private string getRoute(string functionPath)
294+
{
295+
return abi != null ? this.address + "#" + abi + ".erc20.signature." + functionPath : this.address + ".erc20.signature." + functionPath;
291296
}
292297
}
293298
}

Assets/Thirdweb/Scripts/ERC721.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ public class ERC721
2323
/// <summary>
2424
/// Interact with any ERC721 compatible contract.
2525
/// </summary>
26-
public ERC721(string chain, string address)
26+
public ERC721(string chain, string address, string abi = null)
2727
{
2828
this.chain = chain;
2929
this.address = address;
30+
this.abi = null;
3031
this.signature = new ERC721Signature(chain, address);
3132
this.claimConditions = new ERC721ClaimConditions(chain, address);
3233
}
@@ -176,8 +177,9 @@ public async Task<TransactionResult> MintTo(string address, NFTMetadata nft)
176177

177178
// PRIVATE
178179

179-
private string getRoute(string functionPath) {
180-
return this.address + ".erc721." + functionPath;
180+
private string getRoute(string functionPath)
181+
{
182+
return abi != null ? this.address + "#" + abi + ".erc721." + functionPath : this.address + ".erc721." + functionPath;
181183
}
182184
}
183185

@@ -228,13 +230,14 @@ public async Task<bool> GetClaimerProofs(string claimerAddress)
228230
return await Bridge.InvokeRoute<bool>(getRoute("getClaimerProofs"), Utils.ToJsonStringArray(claimerAddress));
229231
}
230232

231-
private string getRoute(string functionPath) {
232-
return this.address + ".erc721.claimConditions." + functionPath;
233+
private string getRoute(string functionPath)
234+
{
235+
return abi != null ? this.address + "#" + abi + ".erc721.claimConditions." + functionPath : this.address + ".erc721.claimConditions." + functionPath;
233236
}
234237
}
235238

236239
[System.Serializable]
237-
#nullable enable
240+
#nullable enable
238241
public class ERC721MintPayload
239242
{
240243
public string to;
@@ -250,7 +253,8 @@ public class ERC721MintPayload
250253
// public long mintStartTime;
251254
// public long mintEndTime;
252255

253-
public ERC721MintPayload(string receiverAddress, NFTMetadata metadata) {
256+
public ERC721MintPayload(string receiverAddress, NFTMetadata metadata)
257+
{
254258
this.metadata = metadata;
255259
this.to = receiverAddress;
256260
this.price = "0";
@@ -315,8 +319,9 @@ public async Task<TransactionResult> Mint(ERC721SignedPayload signedPayload)
315319
return await Bridge.InvokeRoute<TransactionResult>(getRoute("mint"), Utils.ToJsonStringArray(signedPayload));
316320
}
317321

318-
private string getRoute(string functionPath) {
319-
return this.address + ".erc721.signature." + functionPath;
322+
private string getRoute(string functionPath)
323+
{
324+
return abi != null ? this.address + "#" + abi + ".erc721.signature." + functionPath : this.address + ".erc721.signature." + functionPath;
320325
}
321326
}
322327
}

0 commit comments

Comments
 (0)