Skip to content

Commit

Permalink
Support aptos (#64)
Browse files Browse the repository at this point in the history
* support aptos

* remove unused and fix linting
  • Loading branch information
0xaptosj authored Dec 14, 2024
1 parent b5f7624 commit 6b59a48
Show file tree
Hide file tree
Showing 12 changed files with 520 additions and 7 deletions.
1 change: 1 addition & 0 deletions typescript/packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"dependencies": {
"@aptos-labs/ts-sdk": "^1.33.1",
"@lit-protocol/lit-node-client": "catalog:",
"@lit-protocol/wrapped-keys": "catalog:",
"@solana/web3.js": "catalog:",
Expand Down
49 changes: 49 additions & 0 deletions typescript/packages/core/src/core-plugins/send-apt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { parseUnits } from "viem";
import { z } from "zod";
import type { Plugin } from "../plugins";
import type { AptosWalletClient } from "../wallets";

export function sendAPT(): Plugin<AptosWalletClient> {
return {
name: "send_apt",
supportsSmartWallets: () => true,
supportsChain: (chain) => chain.type === "aptos",
getTools: async (walletClient: AptosWalletClient) => {
return [
{
name: "send_apt",
description: "This {{tool}} sends APT to an address.",
parameters: sendAPTParametersSchema,
method: (parameters: z.infer<typeof sendAPTParametersSchema>) =>
sendAPTMethod(walletClient, parameters),
},
];
},
};
}

const sendAPTParametersSchema = z.object({
to: z.string().describe("The address to send APT to"),
amount: z.string().describe("The amount of APT to send"),
});

async function sendAPTMethod(
walletClient: AptosWalletClient,
parameters: z.infer<typeof sendAPTParametersSchema>,
): Promise<string> {
try {
const { to, amount } = parameters;
const octas = parseUnits(amount, 8);

const tx = await walletClient.sendTransaction({
transactionData: {
function: "0x1::coin::transfer",
functionArguments: [to, octas],
typeArguments: ["0x1::aptos_coin::AptosCoin"],
},
});
return tx.hash;
} catch (error) {
throw new Error(`Failed to send SOL: ${error}`);
}
}
1 change: 1 addition & 0 deletions typescript/packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from "./core-plugins/send-chr";
export * from "./core-plugins/send-eth";
export * from "./core-plugins/send-sol";
export * from "./core-plugins/evm-send-transaction";
export * from "./core-plugins/send-apt";
33 changes: 33 additions & 0 deletions typescript/packages/core/src/wallets/aptos.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {
Aptos,
type InputGenerateTransactionPayloadData,
type InputViewFunctionData,
type LedgerVersionArg,
} from "@aptos-labs/ts-sdk";
import type { WalletClient } from "./core";

export function isAptosWalletClient(wallet: WalletClient): wallet is AptosWalletClient {
return wallet.getChain().type === "aptos";
}

export type AptosTransaction = {
transactionData: InputGenerateTransactionPayloadData;
};

export type AptosReadRequest = {
viewFunctionData: InputViewFunctionData;
ledgerVersionArg?: LedgerVersionArg;
};

export type AptosReadResult = {
value: unknown;
};

export type AptosTransactionResult = {
hash: string;
};

export interface AptosWalletClient extends WalletClient {
sendTransaction: (transaction: AptosTransaction) => Promise<AptosTransactionResult>;
read: (request: AptosReadRequest) => Promise<AptosReadResult>;
}
1 change: 1 addition & 0 deletions typescript/packages/core/src/wallets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from "./evm-smart-wallet";
export * from "./solana";
export * from "./chain";
export * from "./core";
export * from "./aptos";
37 changes: 37 additions & 0 deletions typescript/packages/wallets/aptos/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Goat Wallet Aptos 🐐 - TypeScript

## Installation

```
npm install @goat-sdk/wallet-aptos
```

## Usage

```typescript
import {
Account,
Aptos,
AptosConfig,
Ed25519PrivateKey,
Network,
} from "@aptos-labs/ts-sdk";

const aptosClient = new Aptos(
new AptosConfig({
network: Network.TESTNET,
})
);

const aptosPrivateKey = process.env.APTOS_PRIVATE_KEY;
const aptosAccount = Account.fromPrivateKey({
privateKey: new Ed25519PrivateKey(aptosPrivateKey),
});

const tools = await getOnChainTools({
wallet: aptos({
aptosClient,
aptosAccount,
}),
});
```
34 changes: 34 additions & 0 deletions typescript/packages/wallets/aptos/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "@goat-sdk/wallet-aptos",
"version": "0.1.7",
"sideEffects": false,
"files": ["dist/**/*", "README.md", "package.json"],
"scripts": {
"build": "tsup",
"clean": "rm -rf dist",
"test": "vitest run --passWithNoTests"
},
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"dependencies": {
"@goat-sdk/core": "workspace:*",
"@aptos-labs/ts-sdk": "^1.33.1",
"bs58": "^6.0.0",
"tweetnacl": "^1.0.3"
},
"peerDependencies": {
"@goat-sdk/core": "workspace:*",
"@aptos-labs/ts-sdk": "^1.33.1"
},
"homepage": "https://ohmygoat.dev",
"repository": {
"type": "git",
"url": "git+https://github.com/goat-sdk/goat.git"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/goat-sdk/goat/issues"
},
"keywords": ["ai", "agents", "web3"]
}
60 changes: 60 additions & 0 deletions typescript/packages/wallets/aptos/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import type { AptosReadRequest, AptosTransaction, AptosWalletClient } from "@goat-sdk/core";

import type { Account, Aptos } from "@aptos-labs/ts-sdk";

export type AptosWalletOptions = {
aptosAccount: Account;
aptosClient: Aptos;
};

export function aptos({ aptosAccount, aptosClient }: AptosWalletOptions): AptosWalletClient {
return {
getAddress: () => aptosAccount.accountAddress.toStringLong(),
getChain() {
return {
type: "aptos",
};
},
async signMessage(message: string) {
const signature = aptosAccount.sign(message).toString();
return {
signature,
};
},
async sendTransaction({ transactionData }: AptosTransaction) {
const transaction = await aptosClient.transaction.build.simple({
sender: aptosAccount.accountAddress,
data: transactionData,
});
const response = await aptosClient
.signAndSubmitTransaction({
signer: aptosAccount,
transaction,
})
.then((tx) => aptosClient.waitForTransaction({ transactionHash: tx.hash }));
return {
hash: response.hash,
};
},
async read({ viewFunctionData, ledgerVersionArg }: AptosReadRequest) {
const value = await aptosClient.view({
payload: viewFunctionData,
options: ledgerVersionArg,
});
return {
value,
};
},
async balanceOf(address: string) {
const balance = await aptosClient.getAccountAPTAmount({
accountAddress: address,
});
return {
decimals: 8,
symbol: "APT",
name: "Aptos",
value: BigInt(balance),
};
},
};
}
6 changes: 6 additions & 0 deletions typescript/packages/wallets/aptos/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "../../../tsconfig.base.json",
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}
6 changes: 6 additions & 0 deletions typescript/packages/wallets/aptos/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineConfig } from "tsup";
import { treeShakableConfig } from "../../../tsup.config.base";

export default defineConfig({
...treeShakableConfig,
});
11 changes: 11 additions & 0 deletions typescript/packages/wallets/aptos/turbo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "https://turbo.build/schema.json",
"extends": ["//"],
"tasks": {
"build": {
"inputs": ["src/**", "tsup.config.ts", "!./**/*.test.{ts,tsx}", "tsconfig.json"],
"dependsOn": ["^build"],
"outputs": ["dist/**"]
}
}
}
Loading

0 comments on commit 6b59a48

Please sign in to comment.