Skip to content

Commit

Permalink
Working Execute & Multi-Execute!
Browse files Browse the repository at this point in the history
  • Loading branch information
0xFirekeeper committed Jan 24, 2025
1 parent 6d3bcdb commit fd20217
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 47 deletions.
12 changes: 9 additions & 3 deletions Thirdweb.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
var myChain = 11155111;
var myWallet = await SmartWallet.Create(personalWallet: await PrivateKeyWallet.Generate(client), chainId: myChain, gasless: true);
var myContractAddress = "0xe2cb0eb5147b42095c2FfA6F7ec953bb0bE347D8"; // DropERC1155
var usdcAddress = "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238";

// Create a Nebula session
var nebula = await ThirdwebNebula.Create(client);
Expand Down Expand Up @@ -70,7 +71,7 @@
Console.WriteLine($"Response 3: {response3.Message}");

// Execute, this directly sends transactions
var executionResult = await nebula.Execute("Send 0 ETH to vitalik.eth", wallet: myWallet, context: new NebulaContext(chainIds: new List<BigInteger> { myChain }));
var executionResult = await nebula.Execute("Approve 1 USDC to vitalik.eth", wallet: myWallet, context: new NebulaContext(contractAddresses: new List<string>() { usdcAddress }));
if (executionResult.TransactionReceipts != null && executionResult.TransactionReceipts.Count > 0)
{
Console.WriteLine($"Receipt: {executionResult.TransactionReceipts[0]}");
Expand All @@ -82,9 +83,14 @@

// Batch execute
var batchExecutionResult = await nebula.Execute(
new List<NebulaChatMessage> { new("Send 0 ETH to vitalik.eth", NebulaChatRole.User), new("Are you sure?", NebulaChatRole.Assistant), new("Yes", NebulaChatRole.User) },
new List<NebulaChatMessage>
{
new("What's the address of vitalik.eth", NebulaChatRole.User),
new("The address of vitalik.eth is 0xd8dA6BF26964aF8E437eEa5e3616511D7G3a3298", NebulaChatRole.Assistant),
new("Approve 1 USDC to them", NebulaChatRole.User),
},
wallet: myWallet,
context: new NebulaContext(chainIds: new List<BigInteger> { myChain })
context: new NebulaContext(contractAddresses: new List<string>() { usdcAddress })
);
if (batchExecutionResult.TransactionReceipts != null && batchExecutionResult.TransactionReceipts.Count > 0)
{
Expand Down
82 changes: 48 additions & 34 deletions Thirdweb.Tests/Thirdweb.AI/Thirdweb.AI.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Thirdweb.Tests.AI;
public class NebulaTests : BaseTests
{
private const string NEBULA_TEST_CONTRACT = "0xe2cb0eb5147b42095c2FfA6F7ec953bb0bE347D8";
private const string NEBULA_TEST_USDC_ADDRESS = "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238";
private const int NEBULA_TEST_CHAIN = 11155111;

public NebulaTests(ITestOutputHelper output)
Expand Down Expand Up @@ -70,7 +71,7 @@ public async Task Chat_Multiple_ReturnsResponse()
);
Assert.NotNull(response);
Assert.NotNull(response.Message);
Assert.Contains("CatDrop", response.Message);
Assert.Contains("CatDrop", response.Message, StringComparison.OrdinalIgnoreCase);
}

[Fact(Timeout = 120000)]
Expand All @@ -85,38 +86,51 @@ public async Task Chat_UnderstandsWalletContext()
Assert.Contains(expectedAddress, response.Message);
}

// [Fact(Timeout = 120000)]
// public async Task Execute_ReturnsMessageAndReceipt()
// {
// var signer = await PrivateKeyWallet.Generate(this.Client);
// var wallet = await SmartWallet.Create(signer, NEBULA_TEST_CHAIN);
// var nebula = await ThirdwebNebula.Create(this.Client);
// var response = await nebula.Execute("Send 0 ETH to vitalik.eth", wallet: wallet);
// Assert.NotNull(response);
// Assert.NotNull(response.Message);
// Assert.NotNull(response.TransactionReceipts);
// Assert.NotEmpty(response.TransactionReceipts);
// Assert.NotNull(response.TransactionReceipts[0].TransactionHash);
// Assert.True(response.TransactionReceipts[0].TransactionHash.Length == 66);
// }
[Fact(Timeout = 120000)]
public async Task Execute_ReturnsMessageAndReceipt()
{
var signer = await PrivateKeyWallet.Generate(this.Client);
var wallet = await SmartWallet.Create(signer, NEBULA_TEST_CHAIN);
var nebula = await ThirdwebNebula.Create(this.Client);
var response = await nebula.Execute(
new List<NebulaChatMessage>
{
new("What's the address of vitalik.eth", NebulaChatRole.User),
new("The address of vitalik.eth is 0xd8dA6BF26964aF8E437eEa5e3616511D7G3a3298", NebulaChatRole.Assistant),
new("Approve 1 USDC to them", NebulaChatRole.User),
},
wallet: wallet,
context: new NebulaContext(contractAddresses: new List<string>() { NEBULA_TEST_USDC_ADDRESS })
);
Assert.NotNull(response);
Assert.NotNull(response.Message);
Assert.NotNull(response.TransactionReceipts);
Assert.NotEmpty(response.TransactionReceipts);
Assert.NotNull(response.TransactionReceipts[0].TransactionHash);
Assert.True(response.TransactionReceipts[0].TransactionHash.Length == 66);
}

// [Fact(Timeout = 120000)]
// public async Task Execute_ReturnsMessageAndReceipts()
// {
// var signer = await PrivateKeyWallet.Generate(this.Client);
// var wallet = await SmartWallet.Create(signer, NEBULA_TEST_CHAIN);
// var nebula = await ThirdwebNebula.Create(this.Client);
// var response = await nebula.Execute(
// new List<NebulaChatMessage> { new("Send 0 ETH to vitalik.eth and satoshi.eth", NebulaChatRole.User), new("Are you sure?", NebulaChatRole.Assistant), new("Yes", NebulaChatRole.User) },
// wallet: wallet
// );
// Assert.NotNull(response);
// Assert.NotNull(response.Message);
// Assert.NotNull(response.TransactionReceipts);
// Assert.NotEmpty(response.TransactionReceipts);
// Assert.NotNull(response.TransactionReceipts[0].TransactionHash);
// Assert.True(response.TransactionReceipts[0].TransactionHash.Length == 66);
// Assert.NotNull(response.TransactionReceipts[1].TransactionHash);
// Assert.True(response.TransactionReceipts[1].TransactionHash.Length == 66);
// }
[Fact(Timeout = 120000)]
public async Task Execute_ReturnsMessageAndReceipts()
{
var signer = await PrivateKeyWallet.Generate(this.Client);
var wallet = await SmartWallet.Create(signer, NEBULA_TEST_CHAIN);
var nebula = await ThirdwebNebula.Create(this.Client);
var response = await nebula.Execute(
new List<NebulaChatMessage>
{
new("What's the address of vitalik.eth", NebulaChatRole.User),
new("The address of vitalik.eth is 0xd8dA6BF26964aF8E437eEa5e3616511D7G3a3298", NebulaChatRole.Assistant),
new("Approve 1 USDC to them", NebulaChatRole.User),
},
wallet: wallet,
context: new NebulaContext(contractAddresses: new List<string>() { NEBULA_TEST_USDC_ADDRESS })
);
Assert.NotNull(response);
Assert.NotNull(response.Message);
Assert.NotNull(response.TransactionReceipts);
Assert.NotEmpty(response.TransactionReceipts);
Assert.NotNull(response.TransactionReceipts[0].TransactionHash);
Assert.True(response.TransactionReceipts[0].TransactionHash.Length == 66);
}
}
27 changes: 17 additions & 10 deletions Thirdweb/Thirdweb.AI/ThirdwebNebula.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,14 +234,14 @@ private static async Task<ContextFilter> PrepareContextFilter(IThirdwebWallet wa
// If it's a smart wallet, add the contract address and chain ID to the context
if (wallet is SmartWallet smartWallet)
{
if (context.ContractAddresses == null || context.ContractAddresses.Count == 0)
{
context.ContractAddresses = new List<string>() { walletAddress };
}
else if (!context.ContractAddresses.Contains(walletAddress))
{
context.ContractAddresses.Add(walletAddress);
}
// if (context.ContractAddresses == null || context.ContractAddresses.Count == 0)
// {
// context.ContractAddresses = new List<string>() { walletAddress };
// }
// else if (!context.ContractAddresses.Contains(walletAddress))
// {
// context.ContractAddresses.Add(walletAddress);
// }

if (context.ChainIds == null || context.ChainIds.Count == 0)
{
Expand Down Expand Up @@ -269,9 +269,9 @@ private static async Task<List<ThirdwebTransaction>> PrepareTransactions(IThirdw
var transactionTasks = actions
.Select(action =>
{
if (action.Type == "transaction")
if (action.Type == "sign_transaction")
{
var txInput = JsonConvert.DeserializeObject<ThirdwebTransactionInput>(action.Data.ToString());
var txInput = JsonConvert.DeserializeObject<ThirdwebTransactionInput>(action.Data);
return ThirdwebTransaction.Create(wallet, txInput);
}
else
Expand All @@ -281,6 +281,13 @@ private static async Task<List<ThirdwebTransaction>> PrepareTransactions(IThirdw
})
.ToList();

if (transactionTasks == null || transactionTasks.Count == 0)
{
return null;
}

_ = transactionTasks.RemoveAll(task => task == null);

return (await Task.WhenAll(transactionTasks)).Where(tx => tx != null).ToList();
}
else
Expand Down
2 changes: 2 additions & 0 deletions Thirdweb/Thirdweb.Transactions/ThirdwebTransactionInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace Thirdweb;
/// </summary>
public class ThirdwebTransactionInput
{
internal ThirdwebTransactionInput() { }

public ThirdwebTransactionInput(BigInteger chainId)
{
this.ChainId = chainId > 0 ? new HexBigInteger(chainId) : throw new ArgumentException("Invalid Chain ID");
Expand Down

0 comments on commit fd20217

Please sign in to comment.