Skip to content

Commit

Permalink
Nico/add aave viem (#28)
Browse files Browse the repository at this point in the history
* - feature: replace webpack5 with esbuild for tools bundling

* - feature: adapted tools runner to spawn tools backend and use the API

* - fix: permissions for shinkai-tools-backend binary

* minor fixes

* - fix: dom lib in playwright example tsconfig when use jest

* stuck with window issue

* exp

* update

* - fix: permissions

* - fix: missing chrome

* - fix: node version

* - feature: bundle tools-backend macos compatible

* - fix: missing chrome in CI

* - fix: added tests for tool backend project

* - fix: missing depends on lint and tests

* - fix: single app binary bundler

* - fix: ddg search scrapper

* - fix: builds concurrency

* - fix: lint and test in ci

* update aave add coinbase

* - fix: stuck tests

* - feature: integrated shinkai-viem lib

* - fix: adapted to shinkai-viem

* - fix: removed unusded file

* fix output ethplorer

* update

* update

* buttons

* fixes

* change conf

* - fix: lock merge

* - fix: shinkai-viem

* - fix: tests

* add new tools

* - fix: rename aave

* fix headless defillama

* - fix: tests

* - fix: tests

* - fix: project names

* - fix: project rename

* - fix: tests

* - fix: add pass with no tests for coinbase projects

* - fix: add timeout limit to check

* - fix: tests

* - fix: removed unused file

* - fix: remove headless false

* - fix: force exit in jest

* temp fix

* fix

* fix coinbase wallet return

* - fix: aave tests

* update to support actual mpc

* update serversigner type in cb

* fix

* - fix: tests

* - fix: tests

* - fix: tests

---------

Co-authored-by: Alfredo Gallardo <[email protected]>
  • Loading branch information
nicarq and agallardol authored Aug 22, 2024
1 parent 3dbbd22 commit d9d0930
Show file tree
Hide file tree
Showing 93 changed files with 47,511 additions and 316 deletions.
1 change: 1 addition & 0 deletions .github/workflows/pr-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jobs:
check:
runs-on: ubuntu-22.04
environment: development
timeout-minutes: 7
steps:
- name: Check out repository
uses: actions/checkout@v4
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apps/**/node_modules
apps/**/node_modules
libs/**/node_modules

libs/shinkai-tools-runner/target
Expand All @@ -12,3 +12,5 @@ target
.nx/workspace-data
libs/shinkai-tools-runner/tools
libs/shinkai-tools-runner/shinkai-tools-runner-resources
apps/shinkai-tool-aave-load-requester/src/bundled-resources
launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as fs from 'fs';
import * as path from 'path';

const shinkaViemPath = path.join(
__dirname,
'../../dist/libs/shinkai-viem/shinkai-viem.js',
);
const shinkaiViemContent = fs.readFileSync(shinkaViemPath, 'utf-8');

const outputPath = path.join(
__dirname,
'src/bundled-resources/shinkai-viem.ts',
);
const outputContent = `// autogenerated
export const viemScriptContent = "${Buffer.from(shinkaiViemContent).toString('base64')}";
`;

fs.writeFileSync(outputPath, outputContent);
6 changes: 6 additions & 0 deletions apps/shinkai-tool-aave-loan-requester/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* eslint-disable */
export default {
displayName: '@shinkai_protocol/shinkai-tool-aave-loan-requester',
preset: '../../jest.preset.js',
coverageDirectory: '../../coverage/apps/shinkai-tool-aave-loan-requester',
};
4 changes: 4 additions & 0 deletions apps/shinkai-tool-aave-loan-requester/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "@shinkai_protocol/shinkai-tool-aave-loan-requester",
"type": "commonjs"
}
38 changes: 38 additions & 0 deletions apps/shinkai-tool-aave-loan-requester/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "@shinkai_protocol/shinkai-tool-aave-loan-requester",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "apps/shinkai-tool-aave-loan-requester/src",
"projectType": "library",
"tags": ["tool"],
"targets": {
"build": {
"dependsOn": [
{ "projects": "@shinkai_protocol/shinkai-viem", "target": "build" }
],
"executor": "nx:run-commands",
"defaultConfiguration": "production",
"options": {
"commands": [
"npx ts-node apps/shinkai-tool-aave-loan-requester/generate-shinkai-viem-export.ts",
"npx ts-node scripts/tool-bundler.ts --entry ./apps/shinkai-tool-aave-loan-requester/src/index.ts --outputFolder ./dist/apps/shinkai-tool-aave-loan-requester"
],
"parallel": false
},
"configurations": {
"development": {},
"production": {}
}
},
"lint": {
"dependsOn": ["build"],
"executor": "@nx/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": [
"apps/shinkai-tool-aave-loan-requester/**/*.ts",
"apps/shinkai-tool-aave-loan-requester/package.json"
]
}
}
}
}

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions apps/shinkai-tool-aave-loan-requester/src/chrome-paths.d.ts
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-aave-loan-requester/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 {};
18 changes: 18 additions & 0 deletions apps/shinkai-tool-aave-loan-requester/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Tool } from '../src/index';

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

test('run definition', async () => {
const tool = new Tool({
chromePath: process.env?.CHROME_PATH,
});
const run_result = await tool.run({
inputValue: '0.005',
assetSymbol: 'ETH',
});
console.log(run_result);
}, 25000);
146 changes: 146 additions & 0 deletions apps/shinkai-tool-aave-loan-requester/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import { BaseTool, RunResult } from '@shinkai_protocol/shinkai-tools-builder';
import { ToolDefinition } from 'libs/shinkai-tools-builder/src/tool-definition';
import * as playwright from 'playwright';
import * as chromePaths from 'chrome-paths';
import { createWalletClient, http, parseEther } from 'viem';

// Remove later. It's for debugging.
import * as fs from 'fs';
import * as path from 'path';
import { viemScriptContent } from './bundled-resources/shinkai-viem';

type Config = {
chromePath?: string;
};
type Params = {
inputValue: string;
assetSymbol: string;
};
type Result = {
amountProcessed: string;
};

// Extend the Window interface to include ethereum
declare global {
interface Window {
ethereum: any;
}
}

export class Tool extends BaseTool<Config, Params, Result> {
definition: ToolDefinition<Config, Params, Result> = {
id: 'shinkai-tool-aave-loan-requester',
name: 'Shinkai: Aave Loan Requester',
description:
'Tool for requesting a loan on Aave, including selecting assets to supply and borrow with their APYs',
author: 'Shinkai',
keywords: ['aave', 'market', 'extractor', 'shinkai'],
configurations: {
type: 'object',
properties: {
chromePath: { type: 'string', nullable: true },
},
required: [],
},
parameters: {
type: 'object',
properties: {
inputValue: { type: 'string' },
assetSymbol: { type: 'string' },
},
required: ['inputValue', 'assetSymbol'],
},
result: {
type: 'object',
properties: {
amountProcessed: { type: 'string' },
},
required: ['amountProcessed'],
},
};

async run(params: Params): Promise<RunResult<Result>> {
const browser = await playwright['chromium'].launch({
executablePath: this.config?.chromePath || chromePaths.chrome,
});
const context = await browser.newContext({
viewport: { width: 1280, height: 800 }, // Set viewport size
});

const page = await context.newPage();
// await page.goto(params.url);
await page.goto(
'https://staging.aave.com/?marketName=proto_arbitrum_sepolia_v3',
);

console.log('Viem script loaded');

await page.evaluate((scriptContent) => {
const script = document.createElement('script');
script.textContent = scriptContent;
document.head.appendChild(script);
console.log('Viem script injected');
}, Buffer.from(viemScriptContent, 'base64').toString('utf-8'));

// Click the "Opt-out" button
// Wait for the "Opt-out" button to appear and click it
await page.waitForSelector('#rcc-decline-button > p');
await page.click('#rcc-decline-button > p');

// Click the wallet button
await page.waitForSelector('#wallet-button');
await page.click('#wallet-button');

// Click the "Browser wallet" button
const browserWalletButton = page.locator(
'body > div:nth-child(20) > div[class*="MuiPaper-root MuiPaper-elevation MuiPaper-rounded MuiPaper-elevation1"] > div[class*="MuiBox-root"] > button:nth-child(2)',
);

await browserWalletButton.waitFor({ state: 'visible' });
console.log('Browser wallet button is visible');
await browserWalletButton.click();

const assetSymbolUpper = params.assetSymbol.toUpperCase();

// Find the div with data-cy="dashboardSupplyListItem_ETH" and click the first button inside the 5th internal div
const assetSupplyListItem = page.locator(
`div[data-cy="dashboardSupplyListItem_${assetSymbolUpper}"]`,
);
const buttonInFifthDiv = assetSupplyListItem
.locator('div:nth-child(5) button')
.first();
await buttonInFifthDiv.waitFor({ state: 'visible' });
await buttonInFifthDiv.click();
console.log(
`First button inside the 5th internal div for ${assetSymbolUpper} clicked`,
);

// Input a value of 0.1 into the specified input field
const inputField = page.locator(
'div.MuiModal-root div.MuiPaper-root div.MuiBox-root div.MuiBox-root div.MuiBox-root div.MuiInputBase-root input',
);
await inputField.waitFor({ state: 'visible' });
await inputField.fill(params.inputValue);
console.log('Input field filled with value 0.1');

// Click the button with data-cy="actionButton"
const actionButton = page.locator('button[data-cy="actionButton"]');
await actionButton.waitFor({ state: 'visible' });
await actionButton.click();
console.log('Action button clicked');

// Wait for the specified selector with a more flexible approach
await page.waitForSelector(
'body > div.MuiModal-root > div.MuiPaper-root.MuiPaper-elevation.MuiPaper-rounded.MuiPaper-elevation1 > div.MuiBox-root > h2',
);

// Take a screenshot and save it to ./tmp/
const screenshotPath = path.join(__dirname, 'tmp', 'screenshot.png');
await page.screenshot({ path: screenshotPath });

await browser.close();
return Promise.resolve({
data: { amountProcessed: params.inputValue },
});
}
}
4 changes: 4 additions & 0 deletions apps/shinkai-tool-aave-loan-requester/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-aave-loan-requester/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-aave-loan-requester/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"
]
}
6 changes: 6 additions & 0 deletions apps/shinkai-tool-aave-state/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* eslint-disable */
export default {
displayName: '@shinkai_protocol/shinkai-tool-aave-state',
preset: '../../jest.preset.js',
coverageDirectory: '../../coverage/apps/shinkai-tool-aave-state',
};
4 changes: 4 additions & 0 deletions apps/shinkai-tool-aave-state/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "@shinkai_protocol/shinkai-tool-aave-state",
"type": "commonjs"
}
39 changes: 39 additions & 0 deletions apps/shinkai-tool-aave-state/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "@shinkai_protocol/shinkai-tool-aave-state",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "apps/shinkai-tool-aave-state/src",
"projectType": "library",
"tags": ["tool"],
"targets": {
"build": {
"dependsOn": [
{ "projects": "@shinkai_protocol/shinkai-viem", "target": "build" }
],
"executor": "nx:run-commands",
"defaultConfiguration": "production",
"options": {
"commands": [
"npx copyfiles --up 3 --verbose dist/libs/shinkai-viem/* apps/shinkai-tool-aave-state/src/bundled-resources/",
"npx ts-node scripts/tool-bundler.ts --entry ./apps/shinkai-tool-aave-state/src/index.ts --outputFolder ./dist/apps/shinkai-tool-aave-state"
]
},
"configurations": {
"development": {},
"production": {}
}
},
"lint": {
"dependsOn": [
{ "projects": "@shinkai_protocol/shinkai-viem", "target": "build" }
],
"executor": "@nx/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": [
"apps/shinkai-tool-aave-state/**/*.ts",
"apps/shinkai-tool-aave-state/package.json"
]
}
}
}
}
Loading

0 comments on commit d9d0930

Please sign in to comment.