Skip to content

Commit 2842a08

Browse files
make switch network awaitable
1 parent e89f7fd commit 2842a08

File tree

7 files changed

+36
-10
lines changed

7 files changed

+36
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,5 @@ crashlytics-build.properties
6868

6969
build/
7070
releases/
71+
Documentation/
7172

Assets/Plugin/thirdweb.jslib

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,25 @@ var plugin = {
5454
dynCall_viii(cb, idPtr, null, buffer);
5555
});
5656
},
57-
ThirdwebSwitchNetwork: async function (chainId) {
58-
await window.bridge.switchNetwork(chainId);
57+
ThirdwebSwitchNetwork: async function (taskId, chainId, cb) {
58+
// convert taskId from pointer to str and allocate it to keep in memory
59+
var id = UTF8ToString(taskId);
60+
var idSize = lengthBytesUTF8(id) + 1;
61+
var idPtr = _malloc(idSize);
62+
stringToUTF8(id, idPtr, idSize);
63+
// execute bridge call
64+
window.bridge
65+
.switchNetwork(chainId)
66+
.then(() => {
67+
dynCall_viii(cb, idPtr, null, null);
68+
})
69+
.catch((err) => {
70+
var msg = err.message;
71+
var bufferSize = lengthBytesUTF8(msg) + 1;
72+
var buffer = _malloc(bufferSize);
73+
stringToUTF8(msg, buffer, bufferSize);
74+
dynCall_viii(cb, idPtr, null, buffer);
75+
});
5976
},
6077
};
6178

Assets/Thirdweb/Scripts/Bridge.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,17 @@ public static async Task<string> Connect() {
6666
return result;
6767
}
6868

69-
public static void SwitchNetwork(int chainId) {
69+
public static async Task SwitchNetwork(int chainId) {
7070
if (Application.isEditor) {
7171
Debug.LogWarning("Switching networks is not supported in the editor. Please build and run the app instead.");
7272
return;
7373
}
74-
ThirdwebSwitchNetwork(chainId);
74+
var task = new TaskCompletionSource<string>();
75+
string taskId = Guid.NewGuid().ToString();
76+
taskMap[taskId] = task;
77+
ThirdwebSwitchNetwork(taskId, chainId, jsCallback);
78+
await task.Task;
79+
return;
7580
}
7681

7782
public static async Task<T> InvokeRoute<T>(string route, string[] body)
@@ -105,6 +110,6 @@ public static async Task<T> InvokeRoute<T>(string route, string[] body)
105110
[DllImport("__Internal")]
106111
private static extern string ThirdwebConnect(string taskId, Action<string, string, string> cb);
107112
[DllImport("__Internal")]
108-
private static extern string ThirdwebSwitchNetwork(int chainId);
113+
private static extern string ThirdwebSwitchNetwork(string taskId, int chainId, Action<string, string, string> cb);
109114
}
110115
}

Assets/Thirdweb/Scripts/ERC1155.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace Thirdweb
66
{
77
/// <summary>
8-
/// Interact with any <c>ERC1155</c> compatible contract.
8+
/// Interact with any ERC1155 compatible contract.
99
/// </summary>
1010
public class ERC1155
1111
{

Assets/Thirdweb/Scripts/Wallet.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ public async Task<int> GetChainId()
6060
/// <summary>
6161
/// Prompt the connected wallet to switch to the giiven chainId
6262
/// </summary>
63-
public void SwitchNetwork(int chainId)
63+
public async Task SwitchNetwork(int chainId)
6464
{
65-
Bridge.SwitchNetwork(chainId);
65+
await Bridge.SwitchNetwork(chainId);
6666
}
6767

6868
/// <summary>

Assets/ThirdwebSDKDemos.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public async void OnLoginCLick()
2626
loginButton.text = "Connected as: " + address.Substring(0, 6) + "...";
2727
int chain = await sdk.wallet.GetChainId();
2828
if (chain != 5) {
29-
sdk.wallet.SwitchNetwork(5);
29+
await sdk.wallet.SwitchNetwork(5);
3030
}
3131
}
3232

@@ -61,6 +61,9 @@ public async void GetERC721()
6161
// resultText.text = "Fetching all NFTs";
6262
// List<NFT> result = await contract.ERC721.GetAll();
6363
// resultText.text = "Fetched " + result.Count + " NFTs";
64+
// for (int i = 0; i < result.Count; i++) {
65+
// Debug.Log(result[i].metadata.name + " owned by " + result[i].owner);
66+
// }
6467

6568
// custom function call
6669
// string uri = await contract.Read<string>("tokenURI", count);

Assets/WebGLTemplates/Thirdweb/lib/thirdweb.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// --- Thirdweb Brige ---
22
import { ethers } from "./ethers.js";
3-
import { ThirdwebSDK } from "https://esm.sh/@thirdweb-dev/sdk@nightly?bundle";
3+
import { ThirdwebSDK } from "https://esm.sh/@thirdweb-dev/sdk?bundle";
44

55
// big number transform
66
const bigNumberReplacer = (key, value) => {

0 commit comments

Comments
 (0)