Skip to content

Commit

Permalink
Iterate on names and fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
0xaguspunk committed Dec 8, 2024
1 parent d4fad06 commit 746f2b2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
8 changes: 6 additions & 2 deletions typescript/examples/vercel-ai/abstract/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ const walletClient = createWalletClient({
(async () => {
const tools = await getOnChainTools({
wallet: viem(walletClient, {
defaultPaymaster: process.env.PAYMASTER_ADDRESS as `0x${string}`,
paymaster: {
defaultPaymaster:
process.env.PAYMASTER_ADDRESS as `0x${string}`,
},
}),
plugins: [sendETH(), erc20({ tokens: [USDC, PEPE] })],
});
Expand All @@ -36,7 +39,8 @@ const walletClient = createWalletClient({
model: openai("gpt-4o-mini"),
tools: tools,
maxSteps: 5,
prompt: "Send 1 USDC to 0x016c0803FFC6880a9a871ba104709cDBf341A90a",
prompt:
"Send 0.000001 ETH to <address> and return the tx id",
});

console.log(result.text);
Expand Down
6 changes: 4 additions & 2 deletions typescript/packages/core/src/wallets/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ export type EVMTransaction = {
};

export type EVMTransactionOptions = {
paymaster?: string;
paymasterInput?: string;
paymaster?: {
address: `0x${string}`;
input: `0x${string}`;
};
};

export type EVMReadRequest = {
Expand Down
31 changes: 19 additions & 12 deletions typescript/packages/wallets/viem/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,27 @@ import {
} from "viem";
import { mainnet } from "viem/chains";
import { normalize } from "viem/ens";
import { eip712WalletActions } from "viem/zksync";
import { eip712WalletActions, getGeneralPaymasterInput } from "viem/zksync";


export type ViemOptions = {
// Only used for zkSync Stack networks
defaultPaymaster?: string;
defaultPaymasterInput?: string;
paymaster?: {
defaultAddress: `0x${string}`;
defaultInput: `0x${string}`;
};
};

export function viem(
client: ViemWalletClient,
options?: ViemOptions
): EVMWalletClient {
const defaultPaymaster = options?.defaultPaymaster;
const defaultPaymasterInput = options?.defaultPaymasterInput;
const defaultPaymaster = options?.paymaster?.defaultAddress ?? "";
const defaultPaymasterInput =
options?.paymaster?.defaultInput ??
getGeneralPaymasterInput({
innerInput: "0x",
});


const publicClient = client.extend(publicActions);

Expand Down Expand Up @@ -85,13 +92,13 @@ export function viem(

const toAddress = await this.resolveAddress(to);

const paymaster = options?.paymaster ?? defaultPaymaster;
const paymaster = options?.paymaster?.address ?? defaultPaymaster;
const paymasterInput =
options?.paymasterInput ?? defaultPaymasterInput;
const isPaymasterTx = !!paymaster || !!paymasterInput;
options?.paymaster?.input ?? defaultPaymasterInput;
const txHasPaymaster = !!paymaster || !!paymasterInput;

// If paymaster params exist, extend with EIP712 actions
const sendingClient = isPaymasterTx
const sendingClient = txHasPaymaster
? client.extend(eip712WalletActions())
: client;

Expand All @@ -102,7 +109,7 @@ export function viem(
to: toAddress,
chain: client.chain,
value,
...(isPaymasterTx ? { paymaster, paymasterInput } : {}),
...(txHasPaymaster ? { paymaster, paymasterInput } : {}),
};

const txHash = await sendingClient.sendTransaction(txParams);
Expand Down Expand Up @@ -130,7 +137,7 @@ export function viem(
args,
});

if (isPaymasterTx) {
if (txHasPaymaster) {
// With paymaster, we must use sendTransaction() directly
const txParams = {
account: client.account,
Expand Down

0 comments on commit 746f2b2

Please sign in to comment.