Skip to content

Commit 18e8928

Browse files
Initial fund wallet functionality (#15)
1 parent c90c9ff commit 18e8928

File tree

5 files changed

+126
-70
lines changed

5 files changed

+126
-70
lines changed

Assets/Plugin/thirdweb.jslib

+20
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,26 @@ var plugin = {
9494
dynCall_viii(cb, idPtr, null, buffer);
9595
});
9696
},
97+
ThirdwebFundWallet: async function (taskId, payload, cb) {
98+
// convert taskId from pointer to str and allocate it to keep in memory
99+
var id = UTF8ToString(taskId);
100+
var idSize = lengthBytesUTF8(id) + 1;
101+
var idPtr = _malloc(idSize);
102+
stringToUTF8(id, idPtr, idSize);
103+
// execute bridge call
104+
window.bridge
105+
.fundWallet(UTF8ToString(payload))
106+
.then(() => {
107+
dynCall_viii(cb, idPtr, idPtr, null);
108+
})
109+
.catch((err) => {
110+
var msg = err.message;
111+
var bufferSize = lengthBytesUTF8(msg) + 1;
112+
var buffer = _malloc(bufferSize);
113+
stringToUTF8(msg, buffer, bufferSize);
114+
dynCall_viii(cb, idPtr, null, buffer);
115+
});
116+
},
97117
};
98118

99119
mergeInto(LibraryManager.library, plugin);

Assets/Thirdweb/Scripts/Bridge.cs

+17-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ public static async Task SwitchNetwork(int chainId)
9696
taskMap[taskId] = task;
9797
ThirdwebSwitchNetwork(taskId, chainId, jsCallback);
9898
await task.Task;
99-
return;
10099
}
101100

102101
public static async Task<T> InvokeRoute<T>(string route, string[] body)
@@ -116,6 +115,21 @@ public static async Task<T> InvokeRoute<T>(string route, string[] body)
116115
return JsonConvert.DeserializeObject<Result<T>>(result).result;
117116
}
118117

118+
public static async Task FundWallet(FundWalletOptions payload)
119+
{
120+
if (Application.isEditor)
121+
{
122+
Debug.LogWarning("Interacting with the thirdweb SDK is not supported in the editor. Please build and run the app instead.");
123+
return;
124+
}
125+
var msg = Utils.ToJson(payload);
126+
string taskId = Guid.NewGuid().ToString();
127+
var task = new TaskCompletionSource<string>();
128+
taskMap[taskId] = task;
129+
ThirdwebFundWallet(taskId, msg, jsCallback);
130+
await task.Task;
131+
}
132+
119133
[DllImport("__Internal")]
120134
private static extern string ThirdwebInvoke(string taskId, string route, string payload, Action<string, string, string> cb);
121135
[DllImport("__Internal")]
@@ -126,5 +140,7 @@ public static async Task<T> InvokeRoute<T>(string route, string[] body)
126140
private static extern string ThirdwebDisconnect(string taskId, Action<string, string, string> cb);
127141
[DllImport("__Internal")]
128142
private static extern string ThirdwebSwitchNetwork(string taskId, int chainId, Action<string, string, string> cb);
143+
[DllImport("__Internal")]
144+
private static extern string ThirdwebFundWallet(string taskId, string payload, Action<string, string, string> cb);
129145
}
130146
}

Assets/Thirdweb/Scripts/ThirdwebSDK.cs

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Threading.Tasks;
2+
13
namespace Thirdweb
24
{
35
/// <summary>
@@ -81,5 +83,14 @@ public Contract GetContract(string address, string abi = null)
8183
{
8284
return new Contract(this.chainOrRPC, address, abi);
8385
}
86+
87+
/// <summary>
88+
/// Prompt the user to fund their wallet using one of the thirdweb pay providers (defaults to Coinbase Pay).
89+
/// </summary>
90+
/// <param name="options">The options like wallet address to fund, on which chain, etc</param>
91+
public async Task FundWallet(FundWalletOptions options)
92+
{
93+
await Bridge.FundWallet(options);
94+
}
8495
}
8596
}

Assets/Thirdweb/Scripts/Types.cs

+9
Original file line numberDiff line numberDiff line change
@@ -447,4 +447,13 @@ public override string ToString()
447447
+ $"\n>chain_id: {chain_id}";
448448
}
449449
}
450+
451+
[System.Serializable]
452+
public struct FundWalletOptions
453+
{
454+
public string appId;
455+
public string address;
456+
public int chainId;
457+
public List<string> assets;
458+
}
450459
}

Assets/WebGLTemplates/Thirdweb/lib/thirdweb-unity-bridge.js

+69-69
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)