Skip to content

Commit 70b47a6

Browse files
updated contract to use forge
redeployed contract updated abi update action code
1 parent dc5b52a commit 70b47a6

File tree

205 files changed

+75264
-2619
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

205 files changed

+75264
-2619
lines changed

.vscode/settings.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
"editor.defaultFormatter": "biomejs.biome"
1919
},
2020
"biome.rename": true,
21-
// Disable prettier to avoid conflicts
22-
"prettier.enable": false,
23-
"eslint.enable": false
21+
22+
"[solidity]": {
23+
"editor.defaultFormatter": "JuanBlanco.solidity"
24+
},
25+
"solidity.formatter": "forge",
26+
"prettier.configPath": "./contracts/prettier.config.js"
2427
}

backend/.gitignore

-15
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,3 @@ package-lock.json
4545

4646
node_modules
4747
.env
48-
49-
# Hardhat files
50-
/cache
51-
/artifacts
52-
53-
# TypeChain files
54-
/typechain
55-
/typechain-types
56-
57-
# solidity-coverage files
58-
/coverage
59-
/coverage.json
60-
61-
# Hardhat Ignition default folder for deployments against a local node
62-
ignition/deployments/chain-31337

backend/package.json

-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@
1818
"zod": "^3.24.2"
1919
},
2020
"devDependencies": {
21-
"@nomicfoundation/hardhat-toolbox": "^5.0.0",
2221
"@types/yargs": "^17.0.33",
2322
"bun-types": "latest",
2423
"dotenv": "^16.4.7",
25-
"hardhat": "^2.22.19"
2624
},
2725
"engines": {
2826
"node": ">=22"

backend/scripts/deploy-vault-factory.ts

-58
This file was deleted.

backend/src/plugins/plugin-sonic/actions/create-vault.ts

+60-59
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,26 @@ import {
99
elizaLogger,
1010
} from '@elizaos/core';
1111
import { v4 as uuidv4 } from 'uuid';
12-
import { http, createPublicClient, createWalletClient } from 'viem';
13-
import { privateKeyToAccount } from 'viem/accounts';
14-
import { sonic } from 'viem/chains';
12+
import type { Address, PublicClient, WalletClient } from 'viem';
1513
import { z } from 'zod';
16-
import VaultFactoryArtifact from '../../../../../contracts/artifacts/src/contracts/create-vault/VaultFactory.sol/VaultFactory.json';
17-
import { env } from '../../../config/env';
14+
import { ethereumAddressSchema } from '../../../validators/ethereum';
15+
import { VAULT_FACTORY_ABI } from '../constants/vault-factory-abi';
16+
import { initSonicProvider } from '../providers/sonic';
1817

18+
// TODO Move this to a type
1919
interface MessageMetadata {
20-
walletAddress?: string;
20+
walletAddress?: Address;
2121
[key: string]: unknown;
2222
}
2323

2424
const createVaultContentSchema = z.object({
25-
userId: z.string(),
26-
walletAddress: z.string(),
27-
tokenAddress: z.string(),
28-
vaultName: z.string().optional(),
29-
vaultDescription: z.string().optional(),
25+
userId: z.string().uuid(),
26+
walletAddress: ethereumAddressSchema,
27+
agentAddress: ethereumAddressSchema,
28+
vaultFactoryAddress: ethereumAddressSchema,
29+
// FIXME Should be typed
30+
publicClient: z.unknown(),
31+
walletClient: z.unknown(),
3032
});
3133

3234
type CreateVaultContent = z.infer<typeof createVaultContentSchema>;
@@ -40,52 +42,39 @@ interface CreateVaultResponse {
4042
async function createVault(
4143
params: CreateVaultContent & { runtime: IAgentRuntime },
4244
): Promise<CreateVaultResponse> {
43-
const {
44-
userId,
45-
walletAddress,
46-
tokenAddress,
47-
vaultName,
48-
vaultDescription,
49-
runtime,
50-
} = params;
51-
5245
try {
53-
// Initialize Viem clients
54-
const publicClient = createPublicClient({
55-
chain: sonic,
56-
transport: http('https://rpc.soniclabs.com'),
57-
});
58-
59-
// Create wallet client with private key
60-
const account = privateKeyToAccount(env.EVM_PRIVATE_KEY);
61-
const walletClient = createWalletClient({
62-
account,
63-
chain: sonic,
64-
transport: http('https://rpc.soniclabs.com'),
65-
});
46+
const parsedParams = createVaultContentSchema.parse(params);
47+
const {
48+
userId,
49+
walletAddress,
50+
vaultFactoryAddress,
51+
agentAddress,
52+
publicClient,
53+
walletClient,
54+
} = parsedParams;
55+
const typedPublicClient = publicClient as PublicClient;
56+
const typedWalletClient = walletClient as WalletClient;
57+
const { runtime } = params;
6658

6759
// Create vault through factory
68-
const hash = await walletClient.writeContract({
69-
address: env.VAULT_FACTORY_ADDRESS,
70-
abi: VaultFactoryArtifact.abi,
60+
// Approve from the vault address
61+
const { request } = await typedPublicClient.simulateContract({
62+
account: agentAddress,
63+
address: vaultFactoryAddress,
64+
abi: VAULT_FACTORY_ABI,
7165
functionName: 'createVault',
72-
args: [
73-
tokenAddress as `0x${string}`,
74-
vaultName || 'My Sonic Vault',
75-
vaultDescription || 'AI-managed vault for Sonic users',
76-
'SONIC_VAULT',
77-
],
66+
args: [agentAddress],
7867
});
7968

80-
const receipt = await publicClient.waitForTransactionReceipt({ hash });
69+
const hash = await typedWalletClient.writeContract(request);
70+
const receipt = await typedPublicClient.waitForTransactionReceipt({ hash });
8171

8272
// Log the transaction receipt for debugging
83-
elizaLogger.info('Transaction receipt:', {
84-
logs: receipt.logs,
85-
status: receipt.status,
73+
elizaLogger.info('Successfully created vault', {
8674
contractAddress: receipt.contractAddress,
8775
});
8876

77+
// TODO Add viem parsing here
8978
// Look for the VaultCreated event in the transaction receipt
9079
const vaultCreatedEvent = receipt.logs.find(
9180
(log) =>
@@ -114,9 +103,6 @@ async function createVault(
114103
vaultAddress,
115104
userId,
116105
walletAddress,
117-
tokenAddress,
118-
vaultName,
119-
vaultDescription,
120106
createdAt: new Date().toISOString(),
121107
transactionHash: hash,
122108
},
@@ -133,9 +119,6 @@ async function createVault(
133119
userId,
134120
walletAddress,
135121
vaultAddress,
136-
tokenAddress,
137-
vaultName,
138-
vaultDescription,
139122
agentId: runtime.agentId,
140123
transactionHash: hash,
141124
});
@@ -180,7 +163,24 @@ export const createVaultAction: Action = {
180163
},
181164
],
182165
],
183-
validate: async (_runtime: IAgentRuntime) => {
166+
validate: async (runtime: IAgentRuntime, message: Memory) => {
167+
const vaultFactoryAddress = runtime.getSetting('VAULT_FACTORY_ADDRESS');
168+
if (!vaultFactoryAddress) {
169+
return false;
170+
}
171+
const rpcUrl = runtime.getSetting('SONIC_RPC_URL');
172+
if (!rpcUrl) {
173+
return false;
174+
}
175+
const privateKey = runtime.getSetting('SONIC_PRIVATE_KEY') as `0x${string}`;
176+
if (!privateKey) {
177+
return false;
178+
}
179+
const metadata = message.content.metadata as MessageMetadata;
180+
const walletAddress = metadata?.walletAddress;
181+
if (!walletAddress) {
182+
return false;
183+
}
184184
return true;
185185
},
186186
handler: async (
@@ -193,10 +193,12 @@ export const createVaultAction: Action = {
193193
callback?: HandlerCallback,
194194
) => {
195195
elizaLogger.log('Create Vault handler called');
196+
const sonicProvider = await initSonicProvider(runtime);
196197

197198
// Extract wallet address from message metadata
198199
const metadata = message.content.metadata as MessageMetadata;
199200
const walletAddress = metadata?.walletAddress;
201+
// Should not happen, it is validated
200202
if (!walletAddress) {
201203
elizaLogger.error('No wallet address provided in message metadata');
202204
if (callback) {
@@ -208,14 +210,13 @@ export const createVaultAction: Action = {
208210
}
209211

210212
// Use the userId from the message
211-
const createVaultContent = {
213+
const createVaultContent: CreateVaultContent = {
212214
userId: message.userId,
213215
walletAddress,
214-
tokenAddress:
215-
process.env.DEFAULT_TOKEN_ADDRESS ||
216-
'0x039e2fB66102314Ce7b64Ce5Ce3E5183bc94aD38', // Sonic (S) token address
217-
vaultName: 'My Sonic Vault',
218-
vaultDescription: 'AI-managed vault for Sonic users',
216+
agentAddress: sonicProvider.account.address,
217+
vaultFactoryAddress: sonicProvider.vaultFactoryAddress,
218+
publicClient: sonicProvider.getPublicClient(),
219+
walletClient: sonicProvider.getWalletClient(),
219220
};
220221

221222
try {

0 commit comments

Comments
 (0)