Skip to content

Commit fd20217

Browse files
committed
Working Execute & Multi-Execute!
1 parent 6d3bcdb commit fd20217

File tree

4 files changed

+76
-47
lines changed

4 files changed

+76
-47
lines changed

Thirdweb.Console/Program.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
var myChain = 11155111;
4343
var myWallet = await SmartWallet.Create(personalWallet: await PrivateKeyWallet.Generate(client), chainId: myChain, gasless: true);
4444
var myContractAddress = "0xe2cb0eb5147b42095c2FfA6F7ec953bb0bE347D8"; // DropERC1155
45+
var usdcAddress = "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238";
4546

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

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

8384
// Batch execute
8485
var batchExecutionResult = await nebula.Execute(
85-
new List<NebulaChatMessage> { new("Send 0 ETH to vitalik.eth", NebulaChatRole.User), new("Are you sure?", NebulaChatRole.Assistant), new("Yes", NebulaChatRole.User) },
86+
new List<NebulaChatMessage>
87+
{
88+
new("What's the address of vitalik.eth", NebulaChatRole.User),
89+
new("The address of vitalik.eth is 0xd8dA6BF26964aF8E437eEa5e3616511D7G3a3298", NebulaChatRole.Assistant),
90+
new("Approve 1 USDC to them", NebulaChatRole.User),
91+
},
8692
wallet: myWallet,
87-
context: new NebulaContext(chainIds: new List<BigInteger> { myChain })
93+
context: new NebulaContext(contractAddresses: new List<string>() { usdcAddress })
8894
);
8995
if (batchExecutionResult.TransactionReceipts != null && batchExecutionResult.TransactionReceipts.Count > 0)
9096
{

Thirdweb.Tests/Thirdweb.AI/Thirdweb.AI.Tests.cs

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace Thirdweb.Tests.AI;
66
public class NebulaTests : BaseTests
77
{
88
private const string NEBULA_TEST_CONTRACT = "0xe2cb0eb5147b42095c2FfA6F7ec953bb0bE347D8";
9+
private const string NEBULA_TEST_USDC_ADDRESS = "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238";
910
private const int NEBULA_TEST_CHAIN = 11155111;
1011

1112
public NebulaTests(ITestOutputHelper output)
@@ -70,7 +71,7 @@ public async Task Chat_Multiple_ReturnsResponse()
7071
);
7172
Assert.NotNull(response);
7273
Assert.NotNull(response.Message);
73-
Assert.Contains("CatDrop", response.Message);
74+
Assert.Contains("CatDrop", response.Message, StringComparison.OrdinalIgnoreCase);
7475
}
7576

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

88-
// [Fact(Timeout = 120000)]
89-
// public async Task Execute_ReturnsMessageAndReceipt()
90-
// {
91-
// var signer = await PrivateKeyWallet.Generate(this.Client);
92-
// var wallet = await SmartWallet.Create(signer, NEBULA_TEST_CHAIN);
93-
// var nebula = await ThirdwebNebula.Create(this.Client);
94-
// var response = await nebula.Execute("Send 0 ETH to vitalik.eth", wallet: wallet);
95-
// Assert.NotNull(response);
96-
// Assert.NotNull(response.Message);
97-
// Assert.NotNull(response.TransactionReceipts);
98-
// Assert.NotEmpty(response.TransactionReceipts);
99-
// Assert.NotNull(response.TransactionReceipts[0].TransactionHash);
100-
// Assert.True(response.TransactionReceipts[0].TransactionHash.Length == 66);
101-
// }
89+
[Fact(Timeout = 120000)]
90+
public async Task Execute_ReturnsMessageAndReceipt()
91+
{
92+
var signer = await PrivateKeyWallet.Generate(this.Client);
93+
var wallet = await SmartWallet.Create(signer, NEBULA_TEST_CHAIN);
94+
var nebula = await ThirdwebNebula.Create(this.Client);
95+
var response = await nebula.Execute(
96+
new List<NebulaChatMessage>
97+
{
98+
new("What's the address of vitalik.eth", NebulaChatRole.User),
99+
new("The address of vitalik.eth is 0xd8dA6BF26964aF8E437eEa5e3616511D7G3a3298", NebulaChatRole.Assistant),
100+
new("Approve 1 USDC to them", NebulaChatRole.User),
101+
},
102+
wallet: wallet,
103+
context: new NebulaContext(contractAddresses: new List<string>() { NEBULA_TEST_USDC_ADDRESS })
104+
);
105+
Assert.NotNull(response);
106+
Assert.NotNull(response.Message);
107+
Assert.NotNull(response.TransactionReceipts);
108+
Assert.NotEmpty(response.TransactionReceipts);
109+
Assert.NotNull(response.TransactionReceipts[0].TransactionHash);
110+
Assert.True(response.TransactionReceipts[0].TransactionHash.Length == 66);
111+
}
102112

103-
// [Fact(Timeout = 120000)]
104-
// public async Task Execute_ReturnsMessageAndReceipts()
105-
// {
106-
// var signer = await PrivateKeyWallet.Generate(this.Client);
107-
// var wallet = await SmartWallet.Create(signer, NEBULA_TEST_CHAIN);
108-
// var nebula = await ThirdwebNebula.Create(this.Client);
109-
// var response = await nebula.Execute(
110-
// 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) },
111-
// wallet: wallet
112-
// );
113-
// Assert.NotNull(response);
114-
// Assert.NotNull(response.Message);
115-
// Assert.NotNull(response.TransactionReceipts);
116-
// Assert.NotEmpty(response.TransactionReceipts);
117-
// Assert.NotNull(response.TransactionReceipts[0].TransactionHash);
118-
// Assert.True(response.TransactionReceipts[0].TransactionHash.Length == 66);
119-
// Assert.NotNull(response.TransactionReceipts[1].TransactionHash);
120-
// Assert.True(response.TransactionReceipts[1].TransactionHash.Length == 66);
121-
// }
113+
[Fact(Timeout = 120000)]
114+
public async Task Execute_ReturnsMessageAndReceipts()
115+
{
116+
var signer = await PrivateKeyWallet.Generate(this.Client);
117+
var wallet = await SmartWallet.Create(signer, NEBULA_TEST_CHAIN);
118+
var nebula = await ThirdwebNebula.Create(this.Client);
119+
var response = await nebula.Execute(
120+
new List<NebulaChatMessage>
121+
{
122+
new("What's the address of vitalik.eth", NebulaChatRole.User),
123+
new("The address of vitalik.eth is 0xd8dA6BF26964aF8E437eEa5e3616511D7G3a3298", NebulaChatRole.Assistant),
124+
new("Approve 1 USDC to them", NebulaChatRole.User),
125+
},
126+
wallet: wallet,
127+
context: new NebulaContext(contractAddresses: new List<string>() { NEBULA_TEST_USDC_ADDRESS })
128+
);
129+
Assert.NotNull(response);
130+
Assert.NotNull(response.Message);
131+
Assert.NotNull(response.TransactionReceipts);
132+
Assert.NotEmpty(response.TransactionReceipts);
133+
Assert.NotNull(response.TransactionReceipts[0].TransactionHash);
134+
Assert.True(response.TransactionReceipts[0].TransactionHash.Length == 66);
135+
}
122136
}

Thirdweb/Thirdweb.AI/ThirdwebNebula.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -234,14 +234,14 @@ private static async Task<ContextFilter> PrepareContextFilter(IThirdwebWallet wa
234234
// If it's a smart wallet, add the contract address and chain ID to the context
235235
if (wallet is SmartWallet smartWallet)
236236
{
237-
if (context.ContractAddresses == null || context.ContractAddresses.Count == 0)
238-
{
239-
context.ContractAddresses = new List<string>() { walletAddress };
240-
}
241-
else if (!context.ContractAddresses.Contains(walletAddress))
242-
{
243-
context.ContractAddresses.Add(walletAddress);
244-
}
237+
// if (context.ContractAddresses == null || context.ContractAddresses.Count == 0)
238+
// {
239+
// context.ContractAddresses = new List<string>() { walletAddress };
240+
// }
241+
// else if (!context.ContractAddresses.Contains(walletAddress))
242+
// {
243+
// context.ContractAddresses.Add(walletAddress);
244+
// }
245245

246246
if (context.ChainIds == null || context.ChainIds.Count == 0)
247247
{
@@ -269,9 +269,9 @@ private static async Task<List<ThirdwebTransaction>> PrepareTransactions(IThirdw
269269
var transactionTasks = actions
270270
.Select(action =>
271271
{
272-
if (action.Type == "transaction")
272+
if (action.Type == "sign_transaction")
273273
{
274-
var txInput = JsonConvert.DeserializeObject<ThirdwebTransactionInput>(action.Data.ToString());
274+
var txInput = JsonConvert.DeserializeObject<ThirdwebTransactionInput>(action.Data);
275275
return ThirdwebTransaction.Create(wallet, txInput);
276276
}
277277
else
@@ -281,6 +281,13 @@ private static async Task<List<ThirdwebTransaction>> PrepareTransactions(IThirdw
281281
})
282282
.ToList();
283283

284+
if (transactionTasks == null || transactionTasks.Count == 0)
285+
{
286+
return null;
287+
}
288+
289+
_ = transactionTasks.RemoveAll(task => task == null);
290+
284291
return (await Task.WhenAll(transactionTasks)).Where(tx => tx != null).ToList();
285292
}
286293
else

Thirdweb/Thirdweb.Transactions/ThirdwebTransactionInput.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace Thirdweb;
1010
/// </summary>
1111
public class ThirdwebTransactionInput
1212
{
13+
internal ThirdwebTransactionInput() { }
14+
1315
public ThirdwebTransactionInput(BigInteger chainId)
1416
{
1517
this.ChainId = chainId > 0 ? new HexBigInteger(chainId) : throw new ArgumentException("Invalid Chain ID");

0 commit comments

Comments
 (0)