Skip to content

Commit

Permalink
Initial fund wallet functionality (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
joaquim-verges authored Dec 2, 2022
1 parent c90c9ff commit 18e8928
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 70 deletions.
20 changes: 20 additions & 0 deletions Assets/Plugin/thirdweb.jslib
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,26 @@ var plugin = {
dynCall_viii(cb, idPtr, null, buffer);
});
},
ThirdwebFundWallet: async function (taskId, payload, cb) {
// convert taskId from pointer to str and allocate it to keep in memory
var id = UTF8ToString(taskId);
var idSize = lengthBytesUTF8(id) + 1;
var idPtr = _malloc(idSize);
stringToUTF8(id, idPtr, idSize);
// execute bridge call
window.bridge
.fundWallet(UTF8ToString(payload))
.then(() => {
dynCall_viii(cb, idPtr, idPtr, null);
})
.catch((err) => {
var msg = err.message;
var bufferSize = lengthBytesUTF8(msg) + 1;
var buffer = _malloc(bufferSize);
stringToUTF8(msg, buffer, bufferSize);
dynCall_viii(cb, idPtr, null, buffer);
});
},
};

mergeInto(LibraryManager.library, plugin);
18 changes: 17 additions & 1 deletion Assets/Thirdweb/Scripts/Bridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ public static async Task SwitchNetwork(int chainId)
taskMap[taskId] = task;
ThirdwebSwitchNetwork(taskId, chainId, jsCallback);
await task.Task;
return;
}

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

public static async Task FundWallet(FundWalletOptions payload)
{
if (Application.isEditor)
{
Debug.LogWarning("Interacting with the thirdweb SDK is not supported in the editor. Please build and run the app instead.");
return;
}
var msg = Utils.ToJson(payload);
string taskId = Guid.NewGuid().ToString();
var task = new TaskCompletionSource<string>();
taskMap[taskId] = task;
ThirdwebFundWallet(taskId, msg, jsCallback);
await task.Task;
}

[DllImport("__Internal")]
private static extern string ThirdwebInvoke(string taskId, string route, string payload, Action<string, string, string> cb);
[DllImport("__Internal")]
Expand All @@ -126,5 +140,7 @@ public static async Task<T> InvokeRoute<T>(string route, string[] body)
private static extern string ThirdwebDisconnect(string taskId, Action<string, string, string> cb);
[DllImport("__Internal")]
private static extern string ThirdwebSwitchNetwork(string taskId, int chainId, Action<string, string, string> cb);
[DllImport("__Internal")]
private static extern string ThirdwebFundWallet(string taskId, string payload, Action<string, string, string> cb);
}
}
11 changes: 11 additions & 0 deletions Assets/Thirdweb/Scripts/ThirdwebSDK.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Threading.Tasks;

namespace Thirdweb
{
/// <summary>
Expand Down Expand Up @@ -81,5 +83,14 @@ public Contract GetContract(string address, string abi = null)
{
return new Contract(this.chainOrRPC, address, abi);
}

/// <summary>
/// Prompt the user to fund their wallet using one of the thirdweb pay providers (defaults to Coinbase Pay).
/// </summary>
/// <param name="options">The options like wallet address to fund, on which chain, etc</param>
public async Task FundWallet(FundWalletOptions options)
{
await Bridge.FundWallet(options);
}
}
}
9 changes: 9 additions & 0 deletions Assets/Thirdweb/Scripts/Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -447,4 +447,13 @@ public override string ToString()
+ $"\n>chain_id: {chain_id}";
}
}

[System.Serializable]
public struct FundWalletOptions
{
public string appId;
public string address;
public int chainId;
public List<string> assets;
}
}
138 changes: 69 additions & 69 deletions Assets/WebGLTemplates/Thirdweb/lib/thirdweb-unity-bridge.js

Large diffs are not rendered by default.

0 comments on commit 18e8928

Please sign in to comment.