Skip to content

Commit aeed2d5

Browse files
async wallet connection
1 parent 8724f83 commit aeed2d5

File tree

6 files changed

+64
-52
lines changed

6 files changed

+64
-52
lines changed

Assets/Plugin/thirdweb.jslib

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,22 @@ mergeInto(LibraryManager.library, {
1919
ThirdwebInitialize: function (chain) {
2020
window.bridge.initialize(UTF8ToString(chain));
2121
},
22-
ThirdwebConnect: function () {
23-
window.bridge.connect();
22+
ThirdwebConnect: function (taskId, cb) {
23+
// convert taskId from pointer to str and allocate it to keep in memory
24+
var id = UTF8ToString(taskId);
25+
var idSize = lengthBytesUTF8(id) + 1;
26+
var idPtr = _malloc(idSize);
27+
stringToUTF8(id, idPtr, idSize);
28+
// execute bridge call
29+
window.bridge.connect().then((address) => {
30+
if (address) {
31+
var bufferSize = lengthBytesUTF8(address) + 1;
32+
var buffer = _malloc(bufferSize);
33+
stringToUTF8(address, buffer, bufferSize);
34+
dynCall_vii(cb, idPtr, buffer);
35+
} else {
36+
dynCall_vii(cb, idPtr, null);
37+
}
38+
});
2439
},
2540
});

Assets/SDKTest.cs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,54 @@
11
using UnityEngine;
22
using Thirdweb;
3+
using TMPro;
34

45
public class SDKTest : MonoBehaviour
56
{
67
private ThirdwebSDK sdk;
78
private Contract contract;
89
private int count;
10+
public TMP_Text loginButton;
11+
public TMP_Text fetchButton;
12+
public TMP_Text claimButton;
13+
914
void Start()
1015
{
11-
this.sdk = new ThirdwebSDK("goerli");
12-
this.contract = sdk.GetContract("0x2e01763fA0e15e07294D74B63cE4b526B321E389");
16+
sdk = new ThirdwebSDK("goerli");
17+
contract = sdk.GetContract("0x2e01763fA0e15e07294D74B63cE4b526B321E389");
18+
}
19+
20+
void Update() {
1321
}
1422

15-
public void OnLoginCLick()
23+
public async void OnLoginCLick()
1624
{
1725
Debug.Log("Login clicked");
18-
sdk.Connect();
26+
27+
loginButton.text = "Connecting...";
28+
string address = await sdk.Connect();
29+
loginButton.text = "Connected as: " + address.Substring(0, 6) + "...";
1930
}
2031

2132
public async void OnButtonClick()
2233
{
2334
Debug.Log("Button clicked");
2435
count++;
25-
NFT result = await this.contract.ERC721.GetNFT(count.ToString());
36+
fetchButton.text = "Fetching Token: " + count;
37+
NFT result = await contract.ERC721.GetNFT(count.ToString());
2638
Debug.Log("name: " + result.metadata.name);
2739
Debug.Log("owner: " + result.owner);
40+
fetchButton.text = result.metadata.name;
2841
}
2942

3043
public async void OnWriteButtonClick()
3144
{
3245
Debug.Log("Claim button clicked");
3346
count++;
34-
//var result = await this.contract.ERC721.Transfer("0x2247d5d238d0f9d37184d8332aE0289d1aD9991b", count.ToString());
35-
var result = await this.contract.ERC721.Claim(1);
47+
//var result = await contract.ERC721.Transfer("0x2247d5d238d0f9d37184d8332aE0289d1aD9991b", count.ToString());
48+
claimButton.text = "claiming...";
49+
var result = await contract.ERC721.Claim(1);
3650
Debug.Log("result id: " + result[0].id);
3751
Debug.Log("result receipt: " + result[0].receipt.transactionHash);
52+
claimButton.text = "claimed tokenId: " + result[0].id;
3853
}
3954
}

Assets/Scenes/SampleScene.unity

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ GameObject:
269269
- component: {fileID: 65284396}
270270
- component: {fileID: 65284395}
271271
m_Layer: 5
272-
m_Name: Text (TMP)
272+
m_Name: LoginText
273273
m_TagString: Untagged
274274
m_Icon: {fileID: 0}
275275
m_NavMeshLayer: 0
@@ -524,6 +524,9 @@ MonoBehaviour:
524524
m_Script: {fileID: 11500000, guid: 408667fcf1581402885e572525603dd0, type: 3}
525525
m_Name:
526526
m_EditorClassIdentifier:
527+
loginButton: {fileID: 65284395}
528+
fetchButton: {fileID: 1828601337}
529+
claimButton: {fileID: 1600812348}
527530
--- !u!4 &354401068
528531
Transform:
529532
m_ObjectHideFlags: 0
@@ -1065,7 +1068,7 @@ GameObject:
10651068
- component: {fileID: 1600812349}
10661069
- component: {fileID: 1600812348}
10671070
m_Layer: 5
1068-
m_Name: Text (TMP)
1071+
m_Name: ClaimText
10691072
m_TagString: Untagged
10701073
m_Icon: {fileID: 0}
10711074
m_NavMeshLayer: 0
@@ -1200,7 +1203,7 @@ GameObject:
12001203
- component: {fileID: 1828601338}
12011204
- component: {fileID: 1828601337}
12021205
m_Layer: 5
1203-
m_Name: Text (TMP)
1206+
m_Name: FetchText
12041207
m_TagString: Untagged
12051208
m_Icon: {fileID: 0}
12061209
m_NavMeshLayer: 0

Assets/Thirdweb/Scripts/Bridge.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public RequestMessageBody(string[] arguments)
3030
[AOT.MonoPInvokeCallback(typeof(Action<string, string>))]
3131
private static void jsCallback(string taskId, string result)
3232
{
33+
Debug.Log("jsCallback: " + taskId + " " + result);
3334
if (taskMap.ContainsKey(taskId))
3435
{
3536
taskMap[taskId].TrySetResult(result);
@@ -41,8 +42,13 @@ public static void Initialize(string chainOrRPC) {
4142
ThirdwebInitialize(chainOrRPC);
4243
}
4344

44-
public static void Connect() {
45-
ThirdwebConnect();
45+
public static async Task<string> Connect() {
46+
var task = new TaskCompletionSource<string>();
47+
string taskId = System.Guid.NewGuid().ToString();
48+
taskMap[taskId] = task;
49+
ThirdwebConnect(taskId, jsCallback);
50+
string result = await task.Task;
51+
return result;
4652
}
4753

4854
public static async Task<T> InvokeRoute<T>(string route, string[] body)
@@ -74,6 +80,6 @@ public static async Task<string> InvokeRouteRaw(string route, string[] body)
7480
[DllImport("__Internal")]
7581
private static extern string ThirdwebInitialize(string chainOrRPC);
7682
[DllImport("__Internal")]
77-
private static extern string ThirdwebConnect();
83+
private static extern string ThirdwebConnect(string taskId, Action<string, string> cb);
7884
}
7985
}
Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Threading.Tasks;
2+
13
namespace Thirdweb
24
{
35

@@ -9,47 +11,13 @@ public ThirdwebSDK(string chainOrRPC) {
911
Bridge.Initialize(chainOrRPC);
1012
}
1113

12-
public void Connect() {
13-
Bridge.Connect();
14+
public Task<string> Connect() {
15+
return Bridge.Connect();
1416
}
1517

1618
public Contract GetContract(string address)
1719
{
1820
return new Contract(this.chainOrRPC, address);
1921
}
20-
// public static async Task<NFT> GetNFT(string id)
21-
// {
22-
// return await Bridge.InvokeRoute<NFT>("erc721.get", new string[] { id });
23-
// }
24-
25-
// public Currency GetCurrency(string address)
26-
// {
27-
// if (!currencyModules.ContainsKey(address))
28-
// {
29-
// currencyModules[address] = new Currency(this, this.bridge, address);
30-
// }
31-
32-
// return currencyModules[address];
33-
// }
34-
35-
// public NFT GetNFT(string address)
36-
// {
37-
// if (!nftModules.ContainsKey(address))
38-
// {
39-
// nftModules[address] = new NFT(this, this.bridge, address);
40-
// }
41-
42-
// return nftModules[address];
43-
// }
44-
45-
// public Market GetMarket(string address)
46-
// {
47-
// if (!marketModules.ContainsKey(address))
48-
// {
49-
// marketModules[address] = new Market(this, this.bridge, address);
50-
// }
51-
52-
// return marketModules[address];
53-
// }
5422
}
5523
}

Assets/WebGLTemplates/Better2020/index.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@
101101
const w = window;
102102
w.bridge = {};
103103
w.bridge.initialize = (chain) => {
104-
const sdk = new ThirdwebSDK("goerli");
104+
const sdk = new ThirdwebSDK(chain);
105105
w.thirdweb = sdk;
106106
}
107107

@@ -114,9 +114,14 @@
114114
if(w.thirdweb) {
115115
console.log("connecting SDK");
116116
w.thirdweb.updateSignerOrProvider(signer);
117+
return await w.thirdweb.wallet.getAddress();
118+
} else {
119+
console.error("window.thirdweb is not defined");
120+
return null;
117121
}
118122
} else {
119123
console.error("Please install a wallet browser extension")
124+
return null;
120125
}
121126
}
122127

0 commit comments

Comments
 (0)