Skip to content

Commit b7f012e

Browse files
add mint and mintTo
1 parent 26e0154 commit b7f012e

File tree

9 files changed

+117
-64
lines changed

9 files changed

+117
-64
lines changed

Assets/SDKTest.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,18 @@ public async void OnWriteButtonClick()
5050
count++;
5151
//var result = await contract.ERC721.Transfer("0x2247d5d238d0f9d37184d8332aE0289d1aD9991b", count.ToString());
5252
claimButton.text = "claiming...";
53-
var result = await contract.ERC721.Claim(1);
54-
Debug.Log("result id: " + result[0].id);
55-
Debug.Log("result receipt: " + result[0].receipt.transactionHash);
56-
claimButton.text = "claimed tokenId: " + result[0].id;
53+
// var result = await contract.ERC721.Claim(1);
54+
// Debug.Log("result id: " + result[0].id);
55+
// Debug.Log("result receipt: " + result[0].receipt.transactionHash);
56+
// claimButton.text = "claimed tokenId: " + result[0].id;
57+
58+
var nftCollection = sdk.GetContract("0x8bFD00BD1D3A2778BDA12AFddE5E65Cca95082DF");
59+
var meta = new NFTMetadata();
60+
meta.name = "Unity NFT";
61+
meta.description = "Minted From Unity";
62+
// get a cat image url
63+
64+
var result = await nftCollection.ERC721.Mint(meta);
65+
claimButton.text = "minted tokenId: " + result.id;
5766
}
5867
}
Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using System.Threading.Tasks;
2-
using UnityEngine;
32

43
namespace Thirdweb
54
{
6-
75
/// <summary>
86
/// Convenient wrapper to interact with any EVM contract
97
/// </summary>
@@ -22,37 +20,21 @@ public async Task<T> Read<T>(string functionName, params object[] args)
2220
{
2321
string [] argsEncoded = new string[args.Length + 1];
2422
argsEncoded[0] = functionName;
25-
toJsonStringArray(args).CopyTo(argsEncoded, 1);
23+
Utils.ToJsonStringArray(args).CopyTo(argsEncoded, 1);
2624
return await Bridge.InvokeRoute<T>(getRoute("call"), argsEncoded);
2725
}
2826

2927
public async Task<TransactionResult> Write(string functionName, params object[] args)
3028
{
3129
string [] argsEncoded = new string[args.Length + 1];
3230
argsEncoded[0] = functionName;
33-
toJsonStringArray(args).CopyTo(argsEncoded, 1);
31+
Utils.ToJsonStringArray(args).CopyTo(argsEncoded, 1);
3432
return await Bridge.InvokeRoute<TransactionResult>(getRoute("call"), argsEncoded);
3533
}
3634

3735
private string getRoute(string functionPath) {
3836
return this.address + "." + functionPath;
3937
}
4038

41-
private string[] toJsonStringArray(params object[] args) {
42-
string[] stringArgs = new string[args.Length];
43-
for (int i = 0; i < args.Length; i++)
44-
{
45-
// if value type, convert to string otherwise serialize to json
46-
if (args[i].GetType().IsPrimitive || args[i] is string)
47-
{
48-
stringArgs[i] = args[i].ToString();
49-
}
50-
else
51-
{
52-
stringArgs[i] = JsonUtility.ToJson(args[i]);
53-
}
54-
}
55-
return stringArgs;
56-
}
5739
}
5840
}

Assets/Thirdweb/Scripts/ERC721.cs

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,6 @@
22

33
namespace Thirdweb
44
{
5-
6-
[System.Serializable]
7-
public struct NFTMetadata
8-
{
9-
public string id;
10-
public string uri;
11-
public string description;
12-
public string image;
13-
public string name;
14-
// TODO: support properties;
15-
}
16-
17-
[System.Serializable]
18-
public struct NFT
19-
{
20-
public NFTMetadata metadata;
21-
public string owner;
22-
}
23-
24-
[System.Serializable]
25-
public struct Receipt
26-
{
27-
public string from;
28-
public string to;
29-
public int transactionIndex;
30-
public string gasUsed;
31-
public string blockHash;
32-
public string transactionHash;
33-
}
34-
35-
[System.Serializable]
36-
public struct TransactionResult
37-
{
38-
public Receipt receipt;
39-
public string id;
40-
}
41-
425
/// <summary>
436
/// Interact with any <c>ERC721</c> compatible contract.
447
/// </summary>
@@ -132,6 +95,16 @@ public async Task<TransactionResult[]> ClaimTo(string address, int quantity)
13295
return await Bridge.InvokeRoute<TransactionResult[]>(getRoute("claimTo"), new string[] { address, quantity.ToString() });
13396
}
13497

98+
public async Task<TransactionResult> Mint(NFTMetadata nft)
99+
{
100+
return await Bridge.InvokeRoute<TransactionResult>(getRoute("mint"), Utils.ToJsonStringArray(nft));
101+
}
102+
103+
public async Task<TransactionResult> MintTo(string address, NFTMetadata nft)
104+
{
105+
return await Bridge.InvokeRoute<TransactionResult>(getRoute("mintTo"), Utils.ToJsonStringArray(address, nft));
106+
}
107+
135108
private string getRoute(string functionPath) {
136109
return this.address + ".erc721." + functionPath;
137110
}

Assets/Thirdweb/Scripts/ThirdwebSDK.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Thirdweb
44
{
5-
65
/// <summary>
76
/// The entry point for the thirdweb SDK.
87
/// </summary>

Assets/Thirdweb/Scripts/Types.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
namespace Thirdweb {
2+
[System.Serializable]
3+
public struct NFTMetadata
4+
{
5+
public string id;
6+
public string uri;
7+
public string description;
8+
public string image;
9+
public string name;
10+
// TODO: support properties;
11+
}
12+
13+
[System.Serializable]
14+
public struct NFT
15+
{
16+
public NFTMetadata metadata;
17+
public string owner;
18+
}
19+
20+
[System.Serializable]
21+
public struct Receipt
22+
{
23+
public string from;
24+
public string to;
25+
public int transactionIndex;
26+
public string gasUsed;
27+
public string blockHash;
28+
public string transactionHash;
29+
}
30+
31+
[System.Serializable]
32+
public struct TransactionResult
33+
{
34+
public Receipt receipt;
35+
public string id;
36+
}
37+
}

Assets/Thirdweb/Scripts/Types.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Thirdweb/Scripts/Utils.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using UnityEngine;
2+
3+
namespace Thirdweb
4+
{
5+
public class Utils
6+
{
7+
public static string[] ToJsonStringArray(params object[] args) {
8+
string[] stringArgs = new string[args.Length];
9+
for (int i = 0; i < args.Length; i++)
10+
{
11+
// if value type, convert to string otherwise serialize to json
12+
if (args[i].GetType().IsPrimitive || args[i] is string)
13+
{
14+
stringArgs[i] = args[i].ToString();
15+
}
16+
else
17+
{
18+
stringArgs[i] = JsonUtility.ToJson(args[i]);
19+
}
20+
}
21+
return stringArgs;
22+
}
23+
}
24+
}

Assets/Thirdweb/Scripts/Utils.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/WebGLTemplates/Better2020/index.html

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,19 @@
132132
const addr = routeArgs[0];
133133
const contract = await w.thirdweb.getContract(addr)
134134
const fnArgs = JSON.parse(payload).arguments;
135+
const parsedARgs = fnArgs.map((arg) => {
136+
try {
137+
return JSON.parse(arg);
138+
} catch (e) {
139+
return arg;
140+
}
141+
});
135142
if(routeArgs.length === 2) {
136143
// TODO assumes contract call
137-
const result = await contract[routeArgs[1]](...fnArgs)
144+
const result = await contract[routeArgs[1]](...parsedARgs)
138145
return JSON.stringify({ result: result }, bigNumberReplacer);
139146
} else if(routeArgs.length === 3) {
140-
const result = await contract[routeArgs[1]][routeArgs[2]](...fnArgs)
147+
const result = await contract[routeArgs[1]][routeArgs[2]](...parsedARgs)
141148
return JSON.stringify({ result: result }, bigNumberReplacer);
142149
} else {
143150
console.error("invalid route", route);

0 commit comments

Comments
 (0)