Skip to content

Commit

Permalink
Account Abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
0xFirekeeper committed Apr 3, 2024
1 parent 1afe66d commit 5642841
Show file tree
Hide file tree
Showing 40 changed files with 1,149 additions and 580 deletions.
122 changes: 65 additions & 57 deletions Thirdweb.Console/Program.cs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Thirdweb.Tests/ClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void NoTimeoutOptions()
{
var client = new ThirdwebClient(new ThirdwebClientOptions(secretKey: _secretKey));
Assert.NotNull(client.FetchTimeoutOptions);
Assert.Equal(Constants.DefaultFetchTimeout, client.FetchTimeoutOptions.GetTimeout(TimeoutType.Storage));
Assert.Equal(Constants.DefaultFetchTimeout, client.FetchTimeoutOptions.GetTimeout(TimeoutType.Rpc));
Assert.Equal(Constants.DEFAULT_FETCH_TIMEOUT, client.FetchTimeoutOptions.GetTimeout(TimeoutType.Storage));
Assert.Equal(Constants.DEFAULT_FETCH_TIMEOUT, client.FetchTimeoutOptions.GetTimeout(TimeoutType.Rpc));
}
}
2 changes: 1 addition & 1 deletion Thirdweb/Thirdweb.Client/ITimeoutOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
public interface ITimeoutOptions
{
int GetTimeout(TimeoutType type, int fallback = Constants.DefaultFetchTimeout);
int GetTimeout(TimeoutType type, int fallback = Constants.DEFAULT_FETCH_TIMEOUT);
}
}
2 changes: 1 addition & 1 deletion Thirdweb/Thirdweb.Client/TimeoutOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public TimeoutOptions(int? storage = null, int? rpc = null, int? other = null)
Other = other;
}

public int GetTimeout(TimeoutType type, int fallback = Constants.DefaultFetchTimeout)
public int GetTimeout(TimeoutType type, int fallback = Constants.DEFAULT_FETCH_TIMEOUT)
{
return type switch
{
Expand Down
44 changes: 29 additions & 15 deletions Thirdweb/Thirdweb.Contracts/ThirdwebContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,32 @@ public class ThirdwebContract
internal BigInteger Chain { get; private set; }
internal string Abi { get; private set; }

public ThirdwebContract(ThirdwebContractOptions options)
public ThirdwebContract(ThirdwebClient client, string address, BigInteger chain, string abi)
{
if (options.Client == null)
if (client == null)
{
throw new ArgumentException("Client must be provided");
}

if (string.IsNullOrEmpty(options.Address))
if (string.IsNullOrEmpty(address))
{
throw new ArgumentException("Address must be provided");
}

if (options.Chain == 0)
if (chain == 0)
{
throw new ArgumentException("Chain must be provided");
}

if (string.IsNullOrEmpty(options.Abi))
if (string.IsNullOrEmpty(abi))
{
throw new ArgumentException("Abi must be provided");
}

Client = options.Client;
Address = options.Address;
Chain = options.Chain;
Abi = options.Abi;
Client = client;
Address = address;
Chain = chain;
Abi = abi;
}

public static async Task<T> ReadContract<T>(ThirdwebContract contract, string method, params object[] parameters)
Expand All @@ -43,7 +51,7 @@ public static async Task<T> ReadContract<T>(ThirdwebContract contract, string me
return function.DecodeTypeOutput<T>(resultData);
}

public static async Task<string> WriteContract(ThirdwebAccount account, ThirdwebContract contract, string method, params object[] parameters)
public static async Task<string> WriteContract(ThirdwebWallet wallet, ThirdwebContract contract, string method, BigInteger weiValue, params object[] parameters)
{
var rpc = ThirdwebRPC.GetRpcInstance(contract.Client, contract.Chain);

Expand All @@ -53,26 +61,32 @@ public static async Task<string> WriteContract(ThirdwebAccount account, Thirdweb

var transaction = new TransactionInput
{
From = account.GetAddress(),
From = await wallet.GetAddress(),
To = contract.Address,
Data = data,
};

// TODO: Implement 1559
transaction.Gas = new HexBigInteger(await rpc.SendRequestAsync<string>("eth_estimateGas", transaction));
transaction.GasPrice = new HexBigInteger(await rpc.SendRequestAsync<string>("eth_gasPrice"));
transaction.Nonce = new HexBigInteger(await rpc.SendRequestAsync<string>("eth_getTransactionCount", account.GetAddress(), "latest"));
transaction.Value = new HexBigInteger(weiValue);

string hash;
if (account.Options.Type == WalletType.PrivateKey || account.Options.Type == WalletType.Embedded)
if (wallet.ActiveAccount.AccountType is ThirdwebAccountType.PrivateKeyAccount)
{
var signedTx = account.SignTransaction(transaction, contract.Chain);
transaction.Nonce = new HexBigInteger(await rpc.SendRequestAsync<string>("eth_getTransactionCount", wallet.GetAddress(), "latest"));
var signedTx = wallet.SignTransaction(transaction, contract.Chain);
Console.WriteLine($"Signed transaction: {signedTx}");
hash = await rpc.SendRequestAsync<string>("eth_sendRawTransaction", signedTx);
}
else if (wallet.ActiveAccount.AccountType is ThirdwebAccountType.SmartAccount)
{
var smartAccount = wallet.ActiveAccount as SmartAccount;
hash = await smartAccount.SendTransaction(transaction);
}
else
{
hash = await rpc.SendRequestAsync<string>("eth_sendTransaction", transaction);
throw new NotImplementedException("Account type not supported");
}
return hash;
}
Expand Down
20 changes: 0 additions & 20 deletions Thirdweb/Thirdweb.Contracts/ThirdwebContractOptions.cs

This file was deleted.

2 changes: 1 addition & 1 deletion Thirdweb/Thirdweb.RPC/ThirdwebRPC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ static ThirdwebRPC()
_httpClient.DefaultRequestHeaders.Add("x-sdk-name", "Thirdweb.NET");
_httpClient.DefaultRequestHeaders.Add("x-sdk-os", System.Runtime.InteropServices.RuntimeInformation.OSDescription);
_httpClient.DefaultRequestHeaders.Add("x-sdk-platform", "dotnet");
_httpClient.DefaultRequestHeaders.Add("x-sdk-version", Constants.Version);
_httpClient.DefaultRequestHeaders.Add("x-sdk-version", Constants.VERSION);
}

private ThirdwebRPC(ThirdwebClient client, BigInteger chainId)
Expand Down
8 changes: 6 additions & 2 deletions Thirdweb/Thirdweb.Utils/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
{
public static class Constants
{
internal const string Version = "0.0.1";
internal const int DefaultFetchTimeout = 60000;
internal const string VERSION = "0.0.1";
internal const int DEFAULT_FETCH_TIMEOUT = 60000;
internal const string DEFAULT_ENTRYPOINT_ADDRESS = "0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789"; // v0.6
internal const string DUMMY_SIG = "0xfffffffffffffffffffffffffffffff0000000000000000000000000000000007aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1c";
internal const string DUMMY_PAYMASTER_AND_DATA_HEX =
"0x0101010101010101010101010101010101010101000000000000000000000000000000000000000000000000000001010101010100000000000000000000000000000000000000000000000000000000000000000101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101";
}
}
12 changes: 12 additions & 0 deletions Thirdweb/Thirdweb.Utils/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,17 @@ public static string ComputeClientIdFromSecretKey(string secretKey)
var hash = sha256.ComputeHash(Encoding.UTF8.GetBytes(secretKey));
return BitConverter.ToString(hash).Replace("-", "").ToLower().Substring(0, 32);
}

public static string HexConcat(params string[] hexStrings)
{
var hex = new StringBuilder("0x");

foreach (var hexStr in hexStrings)
{
_ = hex.Append(hexStr[2..]);
}

return hex.ToString();
}
}
}
Loading

0 comments on commit 5642841

Please sign in to comment.