Skip to content

Commit e1d10c2

Browse files
committedNov 16, 2022
support getcontract from ABI
1 parent 37f1e8b commit e1d10c2

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed
 

‎Assets/Thirdweb/Scripts/Contract.cs

+8-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class Contract
99
{
1010
public string chain;
1111
public string address;
12+
public string abi;
1213
/// <summary>
1314
/// Call any ERC20 supported functions
1415
/// </summary>
@@ -26,9 +27,10 @@ public class Contract
2627
/// </summary>
2728
public Marketplace marketplace;
2829

29-
public Contract(string chain, string address) {
30+
public Contract(string chain, string address, string abi = null) {
3031
this.chain = chain;
3132
this.address = address;
33+
this.abi = abi;
3234
this.ERC20 = new ERC20(chain, address);
3335
this.ERC721 = new ERC721(chain, address);
3436
this.ERC1155 = new ERC1155(chain, address);
@@ -64,7 +66,11 @@ public async Task<TransactionResult> Write(string functionName, params object[]
6466
}
6567

6668
private string getRoute(string functionPath) {
67-
return this.address + "." + functionPath;
69+
if (abi != null) {
70+
return this.address + "#" + abi + "." + functionPath;
71+
} else {
72+
return this.address + "." + functionPath;
73+
}
6874
}
6975
}
7076
}

‎Assets/Thirdweb/Scripts/ThirdwebSDK.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,11 @@ public struct BiconomyOptions
7373
/// Get an instance of a deployed contract.
7474
/// </summary>
7575
/// <param name="address">The contract address</param>
76+
/// <param name="abi">Optionally pass the ABI for contracts that cannot be auto resolved. Expected format for the ABI is escaped JSON string</param>
7677
/// <returns>A contract instance</returns>
77-
public Contract GetContract(string address)
78+
public Contract GetContract(string address, string abi = null)
7879
{
79-
return new Contract(this.chainOrRPC, address);
80+
return new Contract(this.chainOrRPC, address, abi);
8081
}
8182
}
8283
}

‎Assets/WebGLTemplates/Thirdweb/lib/thirdweb.js

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// --- Thirdweb Brige ---
22
import { ethers } from "./ethers.js";
33
import { ThirdwebSDK } from "https://esm.sh/@thirdweb-dev/sdk@nightly?bundle";
4-
// import { ethers } from "https://cdn.ethers.io/lib/ethers-5.2.esm.min.js";
4+
55
// big number transform
66
const bigNumberReplacer = (key, value) => {
77
// if we find a BigNumber then make it into a string (since that is safe)
@@ -20,7 +20,7 @@ const bigNumberReplacer = (key, value) => {
2020
const w = window;
2121
w.bridge = {};
2222
w.bridge.initialize = (chain, options) => {
23-
console.log("bridge.initialize", chain, options);
23+
console.debug("thirdwebSDK initialization:", chain, options);
2424
const sdk = new ThirdwebSDK(chain, JSON.parse(options));
2525
w.thirdweb = sdk;
2626
};
@@ -31,14 +31,11 @@ w.bridge.connect = async () => {
3131
const provider = new ethers.providers.Web3Provider(w.ethereum);
3232
await provider.send("eth_requestAccounts", []);
3333
if (w.thirdweb) {
34-
console.log("connecting SDK");
3534
w.thirdweb.updateSignerOrProvider(provider.getSigner());
3635
w.ethereum.on("accountsChanged", async (accounts) => {
37-
console.log("accountsChanged", accounts);
3836
w.thirdweb.updateSignerOrProvider(provider.getSigner());
3937
});
4038
w.ethereum.on("chainChanged", async (chain) => {
41-
console.log("chainChanged", chain);
4239
w.thirdweb.updateSignerOrProvider(provider.getSigner());
4340
});
4441
return await w.thirdweb.wallet.getAddress();
@@ -57,7 +54,7 @@ w.bridge.switchNetwork = async (chainId) => {
5754
if (chainId) {
5855
await window.ethereum.request({
5956
method: "wallet_switchEthereumChain",
60-
params: [{ chainId: "0x" + chainId.toString(16) }], // chainId must be in hexadecimal numbers
57+
params: [{ chainId: "0x" + chainId.toString(16) }],
6158
});
6259
} else {
6360
console.error("Error switrching network");
@@ -82,7 +79,7 @@ w.bridge.invoke = async (route, payload) => {
8279
return arg;
8380
}
8481
});
85-
console.log("invoke called", route, parsedArgs);
82+
console.debug("thirdwebSDK call:", route, parsedArgs);
8683

8784
// wallet call
8885
if (addrOrSDK.startsWith("sdk")) {
@@ -91,7 +88,6 @@ w.bridge.invoke = async (route, payload) => {
9188
prop = firstArg[1];
9289
}
9390
if (prop && routeArgs.length === 2) {
94-
// TODO assumes contract call
9591
const result = await w.thirdweb[prop][routeArgs[1]](...parsedArgs);
9692
return JSON.stringify({ result: result }, bigNumberReplacer);
9793
} else {
@@ -102,13 +98,16 @@ w.bridge.invoke = async (route, payload) => {
10298

10399
// contract call
104100
if (addrOrSDK.startsWith("0x")) {
105-
let type = undefined;
101+
let typeOrAbi = undefined;
106102
if (firstArg.length > 1) {
107-
type = firstArg[1];
103+
try {
104+
typeOrAbi = JSON.parse(firstArg[1]); // try to parse ABI
105+
} catch (e) {
106+
typeOrAbi = firstArg[1];
107+
}
108108
}
109-
const contract = await w.thirdweb.getContract(addrOrSDK, type);
109+
const contract = await w.thirdweb.getContract(addrOrSDK, typeOrAbi);
110110
if (routeArgs.length === 2) {
111-
// TODO assumes contract call
112111
const result = await contract[routeArgs[1]](...parsedArgs);
113112
return JSON.stringify({ result: result }, bigNumberReplacer);
114113
} else if (routeArgs.length === 3) {

0 commit comments

Comments
 (0)
Please sign in to comment.