Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update coinbase tools #32

Merged
merged 2 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions apps/shinkai-tool-coinbase-get-balance/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type Config = {
name: string;
privateKey: string;
walletId?: string;
useServerSigner?: string;
};
type Params = {
walletId?: string;
Expand All @@ -28,6 +29,7 @@ export class Tool extends BaseTool<Config, Params, Result> {
name: { type: 'string' },
privateKey: { type: 'string' },
walletId: { type: 'string', nullable: true },
useServerSigner: { type: 'string', nullable: true },
},
required: ['name', 'privateKey'],
},
Expand All @@ -51,26 +53,22 @@ export class Tool extends BaseTool<Config, Params, Result> {
const coinbaseOptions: CoinbaseOptions = {
apiKeyName: this.config.name,
privateKey: this.config.privateKey,
useServerSigner: this.config.useServerSigner === 'true',
};
const coinbase = new Coinbase(coinbaseOptions);
const user = await coinbase.getDefaultUser();

// Prioritize walletId from Params over Config
const walletId = params.walletId || this.config.walletId;

let wallet;
if (walletId) {
// Retrieve existing Wallet using walletId
wallet = await user.getWallet(walletId);
console.log(`Wallet retrieved: `, wallet.toString());
} else {
// Create a new Wallet for the User
wallet = await user.createWallet({
networkId: Coinbase.networks.BaseSepolia,
});
console.log(`Wallet successfully created: `, wallet.toString());
// Throw an error if walletId is not defined
if (!walletId) {
throw new Error('walletId must be defined in either params or config');
}

const wallet = await user.getWallet(walletId);
console.log(`Wallet retrieved: `, wallet.toString());

// Retrieve the list of balances for the wallet
let balances = await wallet.listBalances();
console.log(`Balances: `, balances);
Expand Down
7 changes: 7 additions & 0 deletions apps/shinkai-tool-coinbase-get-my-address/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* eslint-disable */
export default {
displayName: '@shinkai_protocol/shinkai-tool-coinbase-get-my-address',
preset: '../../jest.preset.js',
coverageDirectory: '../../coverage/apps/shinkai-tool-coinbase-get-my-address',
passWithNoTests: true,
};
4 changes: 4 additions & 0 deletions apps/shinkai-tool-coinbase-get-my-address/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "@shinkai_protocol/shinkai-tool-coinbase-get-my-address",
"type": "commonjs"
}
30 changes: 30 additions & 0 deletions apps/shinkai-tool-coinbase-get-my-address/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "@shinkai_protocol/shinkai-tool-coinbase-get-my-address",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "apps/shinkai-tool-coinbase-get-my-address/src",
"projectType": "library",
"tags": ["tool"],
"targets": {
"build": {
"executor": "nx:run-commands",
"defaultConfiguration": "production",
"options": {
"command": "npx ts-node scripts/tool-bundler.ts --entry ./apps/shinkai-tool-coinbase-get-my-address/src/index.ts --outputFolder ./dist/apps/shinkai-tool-coinbase-get-my-address"
},
"configurations": {
"development": {},
"production": {}
}
},
"lint": {
"executor": "@nx/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": [
"apps/shinkai-tool-coinbase-get-my-address/**/*.ts",
"apps/shinkai-tool-coinbase-get-my-address/package.json"
]
}
}
}
}
7 changes: 7 additions & 0 deletions apps/shinkai-tool-coinbase-get-my-address/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Tool } from './index';

test('exists definition', async () => {
const tool = new Tool({ name: 'test', privateKey: 'test' });
const definition = tool.getDefinition();
expect(definition).toBeInstanceOf(Object);
});
81 changes: 81 additions & 0 deletions apps/shinkai-tool-coinbase-get-my-address/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { BaseTool, RunResult } from '@shinkai_protocol/shinkai-tools-builder';
import { ToolDefinition } from 'libs/shinkai-tools-builder/src/tool-definition';
import { Coinbase, CoinbaseOptions } from '@coinbase/coinbase-sdk';

type Config = {
name: string;
privateKey: string;
walletId?: string;
useServerSigner?: string;
};
type Params = {
walletId?: string;
};
type Result = {
data: string;
};

export class Tool extends BaseTool<Config, Params, Result> {
definition: ToolDefinition<Config, Params, Result> = {
id: 'shinkai-tool-coinbase-get-my-address',
name: 'Shinkai: Coinbase My Address Getter',
description: 'Tool for getting the default address of a Coinbase wallet',
author: 'Shinkai',
keywords: ['coinbase', 'address', 'shinkai'],
configurations: {
type: 'object',
properties: {
name: { type: 'string' },
privateKey: { type: 'string' },
walletId: { type: 'string', nullable: true },
useServerSigner: { type: 'string', nullable: true },
},
required: ['name', 'privateKey'],
},
parameters: {
type: 'object',
properties: {
walletId: { type: 'string', nullable: true },
},
required: [],
},
result: {
type: 'object',
properties: {
data: { type: 'string' },
},
required: ['data'],
},
};

async run(params: Params): Promise<RunResult<Result>> {
const coinbaseOptions: CoinbaseOptions = {
apiKeyName: this.config.name,
privateKey: this.config.privateKey,
useServerSigner: this.config.useServerSigner === 'true',
};
const coinbase = new Coinbase(coinbaseOptions);
const user = await coinbase.getDefaultUser();

// Prioritize walletId from Params over Config
const walletId = params.walletId || this.config.walletId;

// Throw an error if walletId is not defined
if (!walletId) {
throw new Error('walletId must be defined in either params or config');
}

const wallet = await user.getWallet(walletId);
console.log(`Wallet retrieved: `, wallet.toString());

// Retrieve the list of balances for the wallet
let address = await wallet.getDefaultAddress();
console.log(`Default Address: `, address);

return {
data: {
data: `Default Address: ${address?.getId()}`,
},
};
}
}
4 changes: 4 additions & 0 deletions apps/shinkai-tool-coinbase-get-my-address/tsconfig.app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"include": ["./src/**/*.ts"]
}
5 changes: 5 additions & 0 deletions apps/shinkai-tool-coinbase-get-my-address/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {},
"include": ["./src/**/*.ts"]
}
15 changes: 15 additions & 0 deletions apps/shinkai-tool-coinbase-get-my-address/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"],
"lib": ["dom"]
},
"include": [
"jest.config.ts",
"src/**/*.test.ts",
"src/**/*.spec.ts",
"src/**/*.d.ts"
]
}
10 changes: 10 additions & 0 deletions libs/shinkai-tools-runner/src/built_in_tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,16 @@ m.insert(
)))
.unwrap(),
)),
);
m.insert(
"shinkai-tool-coinbase-get-my-address",
&*Box::leak(Box::new(
serde_json::from_str::<ToolDefinition>(include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/tools/shinkai-tool-coinbase-get-my-address/definition.json"
)))
.unwrap(),
)),
);
// ntim: New tools will be inserted here, don't remove this comment
m
Expand Down