Skip to content

Commit

Permalink
Duplicate function names =/= param count support (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xFirekeeper authored Jun 5, 2024
1 parent 86c4d43 commit 5456ad3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
14 changes: 7 additions & 7 deletions Assets/Thirdweb/Core/Scripts/Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public async Task<Transaction> Prepare(string functionName, string from = null,

var web3 = Utils.GetWeb3(_sdk.Session.ChainId, _sdk.Session.Options.clientId, _sdk.Session.Options.bundleId);
var contract = web3.Eth.GetContract(this.ABI, this.Address);
var function = contract.GetFunction(functionName);
var function = Utils.GetFunctionMatchSignature(contract, functionName, args);
var fromAddress = from ?? await _sdk.Wallet.GetAddress();
initialInput = function.CreateTransactionInput(fromAddress, args);
}
Expand All @@ -147,7 +147,7 @@ public string Encode(string functionName, params object[] args)
{
var web3 = Utils.GetWeb3(_sdk.Session.ChainId, _sdk.Session.Options.clientId, _sdk.Session.Options.bundleId);
var contract = web3.Eth.GetContract(this.ABI, this.Address);
var function = contract.GetFunction(functionName);
var function = Utils.GetFunctionMatchSignature(contract, functionName, args);
return function.GetData(args);
}

Expand All @@ -161,7 +161,7 @@ public List<ParameterOutput> Decode(string functionName, string encodedArgs)
{
var web3 = Utils.GetWeb3(_sdk.Session.ChainId, _sdk.Session.Options.clientId, _sdk.Session.Options.bundleId);
var contract = web3.Eth.GetContract(this.ABI, this.Address);
var function = contract.GetFunction(functionName);
var function = Utils.GetFunctionMatchSignature(contract, functionName);
return function.DecodeInput(encodedArgs);
}

Expand Down Expand Up @@ -212,8 +212,8 @@ public async Task<TransactionResult> Write(string functionName, TransactionReque
if (this.ABI == null)
this.ABI = await FetchAbi(this.Address, await _sdk.Wallet.GetChainId());

var service = new Nethereum.Contracts.Contract(null, this.ABI, this.Address);
var function = service.GetFunction(functionName);
var contract = new Nethereum.Contracts.Contract(null, this.ABI, this.Address);
var function = Utils.GetFunctionMatchSignature(contract, functionName, args);
var data = function.GetData(args);
var input = new TransactionInput
{
Expand Down Expand Up @@ -249,7 +249,7 @@ public async Task<T> Read<T>(string functionName, params object[] args)

var web3 = Utils.GetWeb3(_sdk.Session.ChainId, _sdk.Session.Options.clientId, _sdk.Session.Options.bundleId);
var contract = web3.Eth.GetContract(this.ABI, this.Address);
var function = contract.GetFunction(functionName);
var function = Utils.GetFunctionMatchSignature(contract, functionName, args);
var result = await function.CallDecodingToDefaultAsync(args);

var rawResults = new List<object>();
Expand Down Expand Up @@ -386,7 +386,7 @@ public async Task<T> ReadRaw<T>(string functionName, params object[] args)

var web3 = Utils.GetWeb3(_sdk.Session.ChainId, _sdk.Session.Options.clientId, _sdk.Session.Options.bundleId);
var contract = web3.Eth.GetContract(this.ABI, this.Address);
var function = contract.GetFunction(functionName);
var function = Utils.GetFunctionMatchSignature(contract, functionName, args);
return await function.CallDeserializingToObjectAsync<T>(args);
}

Expand Down
3 changes: 2 additions & 1 deletion Assets/Thirdweb/Core/Scripts/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ public Transaction SetArgs(params object[] args)
else
{
var web3 = Utils.GetWeb3(_sdk.Session.ChainId, _sdk.Session.Options.clientId, _sdk.Session.Options.bundleId);
var function = web3.Eth.GetContract(Contract.ABI, Contract.Address).GetFunction(Input.To);
var contract = web3.Eth.GetContract(Contract.ABI, Contract.Address);
var function = Utils.GetFunctionMatchSignature(contract, FunctionName, args);
Input.Data = function.GetData(args);
}
return this;
Expand Down
16 changes: 16 additions & 0 deletions Assets/Thirdweb/Core/Scripts/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -764,5 +764,21 @@ public static Dictionary<string, string> GetThirdwebHeaders(string clientId, str

return headers;
}

public static Nethereum.Contracts.Function GetFunctionMatchSignature(Nethereum.Contracts.Contract contract, string functionName, params object[] args)
{
var abi = contract.ContractBuilder.ContractABI;
var functions = abi.Functions;
int paramsCount = args?.Length ?? 0;
foreach (var function in functions)
{
if (function.Name == functionName && function.InputParameters.Length == paramsCount)
{
string sha = function.Sha3Signature;
return contract.GetFunctionBySignature(sha);
}
}
throw new UnityException($"Can't find function {functionName} in contract {contract.Address}, that takes: {paramsCount} arguments");
}
}
}

0 comments on commit 5456ad3

Please sign in to comment.