|
| 1 | +import { Details, createMetadata } from "@doc"; |
| 2 | + |
| 3 | +export const metadata = createMetadata({ |
| 4 | + title: "ThirdwebContract.Create | Thirdweb .NET SDK", |
| 5 | + description: |
| 6 | + "Instantiate a ThirdwebContract to interact with smart contracts.", |
| 7 | +}); |
| 8 | + |
| 9 | +# [Nebula AI](https://thirdweb.com/nebula) .NET Integration |
| 10 | +The last piece of the puzzle required to create .NET apps and games leveraging a blockchain-powered AI assistant or NPC that also has the power to fully execute transactions. |
| 11 | + |
| 12 | +## Read, Write, Reason. |
| 13 | +The best way to understand is to look at examples. |
| 14 | + |
| 15 | +We'll prepare some context for the AI - here, we'll generate a basic `PrivateKeyWallet` and upgrade it to a `SmartWallet` on Sepolia. |
| 16 | +```csharp |
| 17 | +// Prepare some context |
| 18 | +var myChain = 11155111; // Sepolia |
| 19 | +var mySigner = await PrivateKeyWallet.Generate(client); |
| 20 | +var myWallet = await SmartWallet.Create(mySigner, myChain); |
| 21 | +var myContractAddress = "0xe2cb0eb5147b42095c2FfA6F7ec953bb0bE347D8"; // DropERC1155 |
| 22 | +var usdcAddress = "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238"; // Sepolia USDC |
| 23 | +``` |
| 24 | + |
| 25 | +A one liner creates a Nebula session. |
| 26 | +```csharp |
| 27 | +// Create a Nebula session |
| 28 | +var nebula = await ThirdwebNebula.Create(client); |
| 29 | +``` |
| 30 | + |
| 31 | +You can **Chat** with Nebula. The Chat method accepts any `IThirdwebWallet` as an optional parameter, context will automatically be updated. |
| 32 | +```csharp |
| 33 | +// Chat, passing wallet context |
| 34 | +var response1 = await nebula.Chat(message: "What is my wallet address?", wallet: myWallet); |
| 35 | +Console.WriteLine($"Response 1: {response1.Message}"); |
| 36 | +``` |
| 37 | + |
| 38 | +You may also pass it smart contract context. |
| 39 | +```csharp |
| 40 | +// Chat, passing contract context |
| 41 | +var response2 = await nebula.Chat( |
| 42 | + message: "What's the total supply of token id 0 for this contract?", |
| 43 | + context: new NebulaContext(contractAddresses: new List<string> { myContractAddress }, chainIds: new List<BigInteger> { myChain }) |
| 44 | +); |
| 45 | +Console.WriteLine($"Response 2: {response2.Message}"); |
| 46 | +``` |
| 47 | + |
| 48 | +You can have a full on conversation with it with our **Chat** override accepting multiple messages. |
| 49 | +```csharp |
| 50 | +// Chat, passing multiple messages and context |
| 51 | +var response3 = await nebula.Chat( |
| 52 | + messages: new List<NebulaChatMessage> |
| 53 | + { |
| 54 | + new($"Tell me the name of this contract: {myContractAddress}", NebulaChatRole.User), |
| 55 | + new("The name of the contract is CatDrop", NebulaChatRole.Assistant), |
| 56 | + new("What's the symbol of this contract?", NebulaChatRole.User), |
| 57 | + }, |
| 58 | + context: new NebulaContext(contractAddresses: new List<string> { myContractAddress }, chainIds: new List<BigInteger> { myChain }) |
| 59 | +); |
| 60 | +Console.WriteLine($"Response 3: {response3.Message}"); |
| 61 | +``` |
| 62 | + |
| 63 | +Chatting is cool, but what if I just want it to _**do**_ things and stop talking so much? |
| 64 | + |
| 65 | +You can **Execute** transactions directly with a simple prompt. |
| 66 | +```csharp |
| 67 | +// Execute, this directly sends transactions |
| 68 | +var executionResult = await nebula.Execute("Approve 1 USDC to vitalik.eth", wallet: myWallet, context: new NebulaContext(contractAddresses: new List<string>() { usdcAddress })); |
| 69 | +if (executionResult.TransactionReceipts != null && executionResult.TransactionReceipts.Count > 0) |
| 70 | +{ |
| 71 | + Console.WriteLine($"Receipt: {executionResult.TransactionReceipts[0]}"); |
| 72 | +} |
| 73 | +else |
| 74 | +{ |
| 75 | + Console.WriteLine($"Message: {executionResult.Message}"); |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +Similarly, **Execute** can take in multiple messages. |
| 80 | +```csharp |
| 81 | +// Batch execute |
| 82 | +var batchExecutionResult = await nebula.Execute( |
| 83 | + new List<NebulaChatMessage> |
| 84 | + { |
| 85 | + new("What's the address of vitalik.eth", NebulaChatRole.User), |
| 86 | + new("The address of vitalik.eth is 0xd8dA6BF26964aF8E437eEa5e3616511D7G3a3298", NebulaChatRole.Assistant), |
| 87 | + new("Approve 1 USDC to them", NebulaChatRole.User), |
| 88 | + }, |
| 89 | + wallet: myWallet, |
| 90 | + context: new NebulaContext(contractAddresses: new List<string>() { usdcAddress }) |
| 91 | +); |
| 92 | +if (batchExecutionResult.TransactionReceipts != null && batchExecutionResult.TransactionReceipts.Count > 0) |
| 93 | +{ |
| 94 | + Console.WriteLine($"Receipts: {JsonConvert.SerializeObject(batchExecutionResult.TransactionReceipts, Formatting.Indented)}"); |
| 95 | +} |
| 96 | +else |
| 97 | +{ |
| 98 | + Console.WriteLine($"Message: {batchExecutionResult.Message}"); |
| 99 | +} |
| 100 | +``` |
0 commit comments