Skip to content

Commit

Permalink
update to support actual mpc
Browse files Browse the repository at this point in the history
  • Loading branch information
nicarq committed Aug 22, 2024
1 parent 3d76132 commit c565ed0
Show file tree
Hide file tree
Showing 17 changed files with 275 additions and 87 deletions.
21 changes: 6 additions & 15 deletions apps/shinkai-tool-coinbase-call-faucet/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import { Coinbase, CoinbaseOptions } from '@coinbase/coinbase-sdk';
type Config = {
name: string;
privateKey: string;
walletId?: string;
};
type Params = {
walletId?: string;
walletId: string;
};
type Params = {};
type Result = {
data: string;
};
Expand All @@ -32,9 +30,7 @@ export class Tool extends BaseTool<Config, Params, Result> {
},
parameters: {
type: 'object',
properties: {
walletId: { type: 'string', nullable: true },
},
properties: {},
required: [],
},
result: {
Expand All @@ -47,22 +43,17 @@ export class Tool extends BaseTool<Config, Params, Result> {
};

async run(params: Params): Promise<RunResult<Result>> {
// Coinbase wallet creation using constructor
const coinbaseOptions: CoinbaseOptions = {
apiKeyName: this.config.name,
privateKey: this.config.privateKey,
useServerSigner: false,
debugging: false,
basePath: '',
maxNetworkRetries: 3,
};
const coinbase = new Coinbase(coinbaseOptions);
console.log(`Coinbase configured: `, coinbase);
const user = await coinbase.getDefaultUser();
console.log(`User: `, user);

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

let wallet;
if (walletId) {
Expand All @@ -85,7 +76,7 @@ export class Tool extends BaseTool<Config, Params, Result> {

return {
data: {
data: `Faucet transaction completed successfully: ${faucetTransaction.toString()}`,
data: `Faucet transaction completed successfully: ${faucetTransaction.toString()} for wallet: ${wallet.getDefaultAddress()}`,
},
};
}
Expand Down
22 changes: 16 additions & 6 deletions apps/shinkai-tool-coinbase-create-wallet/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import { Coinbase, CoinbaseOptions } from '@coinbase/coinbase-sdk';
type Config = {
name: string;
privateKey: string;
useServerSigner?: boolean;
};
type Params = {}; // Params type is now empty
type Result = {
walletId?: string;
seed?: string;
address?: string;
};

export class Tool extends BaseTool<Config, Params, Result> {
Expand All @@ -23,6 +26,7 @@ export class Tool extends BaseTool<Config, Params, Result> {
properties: {
name: { type: 'string' },
privateKey: { type: 'string' },
useServerSigner: { type: 'boolean', default: false, nullable: true },
},
required: ['name', 'privateKey'],
},
Expand All @@ -35,20 +39,18 @@ export class Tool extends BaseTool<Config, Params, Result> {
type: 'object',
properties: {
walletId: { type: 'string', nullable: true },
seed: { type: 'string', nullable: true },
address: { type: 'string', nullable: true },
},
required: [],
},
};

async run(params: Params): Promise<RunResult<Result>> {
// Coinbase wallet creation using constructor
const coinbaseOptions: CoinbaseOptions = {
apiKeyName: this.config.name,
privateKey: this.config.privateKey,
useServerSigner: false,
debugging: false,
basePath: '',
maxNetworkRetries: 3,
useServerSigner: this.config.useServerSigner ?? false,
};
const coinbase = new Coinbase(coinbaseOptions);
console.log(`Coinbase configured: `, coinbase);
Expand All @@ -61,9 +63,17 @@ export class Tool extends BaseTool<Config, Params, Result> {
});
console.log(`Wallet successfully created: `, wallet.toString());

let exportedWallet;
if (!this.config.useServerSigner) {
exportedWallet = await wallet.export();
}

const address = await wallet.getDefaultAddress();

return {
data: {
walletId: wallet.getId(),
...exportedWallet,
address: address?.getId(),
},
};
}
Expand Down
12 changes: 3 additions & 9 deletions apps/shinkai-tool-coinbase-get-balance/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import { Tool } from './index';

test('exists definition', async () => {
// const tool = new Tool({});
// const definition = tool.getDefinition();
// expect(definition).toBeInstanceOf(Object);
const tool = new Tool({ name: 'test', privateKey: 'test' });
const definition = tool.getDefinition();
expect(definition).toBeInstanceOf(Object);
});

test('run definition', async () => {
// const tool = new Tool({});
// const run_result = await tool.run({ walletId: '0d948159-3b3e-40f8-9001-741e64498fa9' });
// console.log(run_result);
}, 25000);
20 changes: 2 additions & 18 deletions apps/shinkai-tool-coinbase-get-balance/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export class Tool extends BaseTool<Config, Params, Result> {
definition: ToolDefinition<Config, Params, Result> = {
id: 'shinkai-tool-coinbase-get-balance',
name: 'Shinkai: Coinbase Balance Getter',
description: 'Tool for getting the balance of a Coinbase wallet after restoring it',
description:
'Tool for getting the balance of a Coinbase wallet after restoring it',
author: 'Shinkai',
keywords: ['coinbase', 'balance', 'shinkai'],
configurations: {
Expand Down Expand Up @@ -47,19 +48,12 @@ export class Tool extends BaseTool<Config, Params, Result> {
};

async run(params: Params): Promise<RunResult<Result>> {
// Coinbase wallet creation using constructor
const coinbaseOptions: CoinbaseOptions = {
apiKeyName: this.config.name,
privateKey: this.config.privateKey,
useServerSigner: false,
debugging: false,
basePath: '',
maxNetworkRetries: 3,
};
const coinbase = new Coinbase(coinbaseOptions);
console.log(`Coinbase configured: `, coinbase);
const user = await coinbase.getDefaultUser();
console.log(`User: `, user);

// Prioritize walletId from Params over Config
const walletId = params.walletId || this.config.walletId;
Expand All @@ -81,16 +75,6 @@ export class Tool extends BaseTool<Config, Params, Result> {
let balances = await wallet.listBalances();
console.log(`Balances: `, balances);

// If no balances, call the faucet and then list balances again
if (balances.size === 0) {
const faucetTransaction = await wallet.faucet();
console.log(`Faucet transaction completed successfully: `, faucetTransaction.toString());

// Retrieve the list of balances again
balances = await wallet.listBalances();
console.log(`Balances after faucet: `, balances);
}

return {
data: {
data: `Balances: ${balances.toString()}`,
Expand Down
7 changes: 7 additions & 0 deletions apps/shinkai-tool-coinbase-get-transactions/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-transactions',
preset: '../../jest.preset.js',
coverageDirectory: '../../coverage/apps/shinkai-tool-coinbase-get-transactions',
passWithNoTests: true,
};
4 changes: 4 additions & 0 deletions apps/shinkai-tool-coinbase-get-transactions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "@shinkai_protocol/shinkai-tool-coinbase-get-transactions",
"type": "commonjs"
}
30 changes: 30 additions & 0 deletions apps/shinkai-tool-coinbase-get-transactions/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "@shinkai_protocol/shinkai-tool-coinbase-get-transactions",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "apps/shinkai-tool-coinbase-get-transactions/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-transactions/src/index.ts --outputFolder ./dist/apps/shinkai-tool-coinbase-get-transactions"
},
"configurations": {
"development": {},
"production": {}
}
},
"lint": {
"executor": "@nx/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": [
"apps/shinkai-tool-coinbase-get-transactions/**/*.ts",
"apps/shinkai-tool-coinbase-get-transactions/package.json"
]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module 'chrome-paths' {
const chrome: string;
}
9 changes: 9 additions & 0 deletions apps/shinkai-tool-coinbase-get-transactions/src/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare global {
interface Window {
viemClient: any;
viemAccount: any;
ethereum: any;
}
}

export {};
7 changes: 7 additions & 0 deletions apps/shinkai-tool-coinbase-get-transactions/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);
});
99 changes: 99 additions & 0 deletions apps/shinkai-tool-coinbase-get-transactions/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
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;
};
type Params = {};
type Result = { tableCsv: string; rowsCount: number; columnsCount: number };

export class Tool extends BaseTool<Config, Params, Result> {
definition: ToolDefinition<Config, Params, Result> = {
id: 'shinkai-tool-coinbase-get-transactions',
name: 'Shinkai: Coinbase Transactions Getter',
description:
'Tool for getting the transactions of a Coinbase wallet after restoring it',
author: 'Shinkai',
keywords: ['coinbase', 'balance', 'shinkai'],
configurations: {
type: 'object',
properties: {
name: { type: 'string' },
privateKey: { type: 'string' },
walletId: { type: 'string' },
},
required: ['name', 'privateKey', 'walletId'],
},
parameters: {
type: 'object',
properties: {
walletId: { type: 'string', nullable: true },
},
required: [],
},
result: {
type: 'object',
properties: {
tableCsv: { type: 'string' },
rowsCount: { type: 'number' },
columnsCount: { type: 'number' },
},
required: ['tableCsv', 'rowsCount', 'columnsCount'],
},
};

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

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

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

// Retrieve the list of balances for the wallet
let address = await wallet.getDefaultAddress();
let transactions = (await address?.listTransfers()) ?? [];

// Convert transactions to CSV format
const headers = [
'transferId',
'networkId',
'fromAddressId',
'destinationAddressId',
'assetId',
'amount',
'transactionHash',
'transactionLink',
'status',
];
const rows = transactions.map((tx) => [
tx.getId(),
tx.getNetworkId(),
tx.getFromAddressId(),
tx.getDestinationAddressId(),
tx.getAssetId(),
tx.getAmount().toString(),
tx.getTransactionHash(),
tx.getTransactionLink(),
tx.getStatus(),
]);
const tableCsv = [headers, ...rows].map((row) => row.join(';')).join('\n');

return Promise.resolve({
data: {
tableCsv,
rowsCount: transactions.length,
columnsCount: headers.length,
},
});
}
}
4 changes: 4 additions & 0 deletions apps/shinkai-tool-coinbase-get-transactions/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-transactions/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-transactions/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"
]
}
5 changes: 0 additions & 5 deletions apps/shinkai-tool-coinbase-send-tx/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,3 @@ test('exists definition', async () => {
// expect(definition).toBeInstanceOf(Object);
});

test('run definition', async () => {
// const tool = new Tool({});
// const run_result = await tool.run({ walletId: '0d948159-3b3e-40f8-9001-741e64498fa9' });
// console.log(run_result);
}, 25000);
Loading

0 comments on commit c565ed0

Please sign in to comment.