@coinbase/agentkit 0.10.2 → 0.10.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +89 -10
- package/dist/action-providers/across/acrossActionProvider.js +2 -4
- package/dist/action-providers/across/acrossActionProvider.test.js +10 -5
- package/dist/action-providers/baseAccount/baseAccountActionProvider.js +5 -7
- package/dist/action-providers/cdp/cdpApiActionProvider.js +7 -30
- package/dist/action-providers/cdp/cdpApiActionProvider.test.js +2 -8
- package/dist/action-providers/cdp/faucetUtils.d.ts +38 -0
- package/dist/action-providers/cdp/faucetUtils.js +81 -0
- package/dist/action-providers/clanker/schemas.d.ts +4 -4
- package/dist/action-providers/clanker/utils.d.ts +2 -1
- package/dist/action-providers/clanker/utils.js +2 -2
- package/dist/action-providers/enso/constants.d.ts +4 -0
- package/dist/action-providers/enso/constants.js +10 -0
- package/dist/action-providers/enso/ensoActionProvider.d.ts +34 -0
- package/dist/action-providers/enso/ensoActionProvider.js +125 -0
- package/dist/action-providers/enso/ensoActionProvider.test.d.ts +1 -0
- package/dist/action-providers/enso/ensoActionProvider.test.js +141 -0
- package/dist/action-providers/enso/index.d.ts +1 -0
- package/dist/action-providers/enso/index.js +17 -0
- package/dist/action-providers/enso/schemas.d.ts +23 -0
- package/dist/action-providers/enso/schemas.js +22 -0
- package/dist/action-providers/erc20/constants.d.ts +2 -0
- package/dist/action-providers/erc20/constants.js +2 -0
- package/dist/action-providers/erc20/erc20ActionProvider.d.ts +17 -1
- package/dist/action-providers/erc20/erc20ActionProvider.js +103 -1
- package/dist/action-providers/erc20/erc20ActionProvider.test.js +201 -0
- package/dist/action-providers/erc20/schemas.d.ts +29 -0
- package/dist/action-providers/erc20/schemas.js +34 -1
- package/dist/action-providers/index.d.ts +4 -1
- package/dist/action-providers/index.js +4 -1
- package/dist/action-providers/jupiter/jupiterActionProvider.js +2 -2
- package/dist/action-providers/spl/splActionProvider.js +12 -13
- package/dist/action-providers/superfluid/graphQueries/superfluidGraphQueries.js +2 -2
- package/dist/action-providers/sushi/constants.d.ts +35 -0
- package/dist/action-providers/sushi/constants.js +7 -0
- package/dist/action-providers/sushi/index.d.ts +4 -0
- package/dist/action-providers/sushi/index.js +20 -0
- package/dist/action-providers/sushi/sushiDataActionProvider.d.ts +32 -0
- package/dist/action-providers/sushi/sushiDataActionProvider.js +113 -0
- package/dist/action-providers/sushi/sushiDataSchemas.d.ts +11 -0
- package/dist/action-providers/sushi/sushiDataSchemas.js +16 -0
- package/dist/action-providers/sushi/sushiRouterActionProvider.d.ts +40 -0
- package/dist/action-providers/sushi/sushiRouterActionProvider.js +386 -0
- package/dist/action-providers/sushi/sushiRouterActionProvider.test.d.ts +1 -0
- package/dist/action-providers/sushi/sushiRouterActionProvider.test.js +392 -0
- package/dist/action-providers/sushi/sushiRouterSchemas.d.ts +36 -0
- package/dist/action-providers/sushi/sushiRouterSchemas.js +55 -0
- package/dist/action-providers/vaultsfyi/constants.d.ts +8 -12
- package/dist/action-providers/vaultsfyi/constants.js +47 -13
- package/dist/action-providers/vaultsfyi/schemas.d.ts +120 -65
- package/dist/action-providers/vaultsfyi/schemas.js +72 -38
- package/dist/action-providers/vaultsfyi/sdk.d.ts +8 -0
- package/dist/action-providers/vaultsfyi/sdk.js +15 -0
- package/dist/action-providers/vaultsfyi/utils.d.ts +151 -55
- package/dist/action-providers/vaultsfyi/utils.js +29 -75
- package/dist/action-providers/vaultsfyi/vaultsfyiActionProvider.d.ts +55 -16
- package/dist/action-providers/vaultsfyi/vaultsfyiActionProvider.js +413 -217
- package/dist/action-providers/vaultsfyi/vaultsfyiActionProvider.test.js +509 -316
- package/dist/action-providers/wallet/walletActionProvider.js +1 -1
- package/dist/action-providers/weth/constants.d.ts +0 -1
- package/dist/action-providers/weth/constants.js +1 -2
- package/dist/action-providers/weth/schemas.js +6 -2
- package/dist/action-providers/weth/wethActionProvider.d.ts +7 -0
- package/dist/action-providers/weth/wethActionProvider.js +57 -32
- package/dist/action-providers/weth/wethActionProvider.test.js +60 -11
- package/dist/action-providers/x402/constants.d.ts +67 -0
- package/dist/action-providers/x402/constants.js +37 -0
- package/dist/action-providers/x402/schemas.d.ts +45 -5
- package/dist/action-providers/x402/schemas.js +81 -11
- package/dist/action-providers/x402/utils.d.ts +89 -14
- package/dist/action-providers/x402/utils.js +335 -31
- package/dist/action-providers/x402/x402ActionProvider.d.ts +21 -7
- package/dist/action-providers/x402/x402ActionProvider.js +250 -184
- package/dist/action-providers/x402/x402ActionProvider.test.js +240 -236
- package/dist/action-providers/yelay/constants.d.ts +64 -0
- package/dist/action-providers/yelay/constants.js +137 -0
- package/dist/action-providers/yelay/index.d.ts +2 -0
- package/dist/action-providers/yelay/index.js +18 -0
- package/dist/action-providers/yelay/schemas.d.ts +47 -0
- package/dist/action-providers/yelay/schemas.js +59 -0
- package/dist/action-providers/yelay/types.d.ts +24 -0
- package/dist/action-providers/yelay/yelayActionProvider.d.ts +70 -0
- package/dist/action-providers/yelay/yelayActionProvider.js +329 -0
- package/dist/action-providers/yelay/yelayActionProvider.test.d.ts +1 -0
- package/dist/action-providers/yelay/yelayActionProvider.test.js +302 -0
- package/dist/action-providers/zora/zoraActionProvider.js +4 -5
- package/dist/wallet-providers/cdpSmartWalletProvider.js +1 -3
- package/dist/wallet-providers/cdpSolanaWalletProvider.d.ts +14 -0
- package/dist/wallet-providers/cdpSolanaWalletProvider.js +39 -3
- package/dist/wallet-providers/cdpSolanaWalletProvider.test.js +16 -0
- package/dist/wallet-providers/privySvmWalletProvider.d.ts +14 -0
- package/dist/wallet-providers/privySvmWalletProvider.js +17 -0
- package/dist/wallet-providers/privySvmWalletProvider.test.js +10 -0
- package/dist/wallet-providers/solanaKeypairWalletProvider.d.ts +14 -0
- package/dist/wallet-providers/solanaKeypairWalletProvider.js +17 -0
- package/dist/wallet-providers/svmWalletProvider.d.ts +34 -0
- package/dist/wallet-providers/svmWalletProvider.js +43 -0
- package/dist/wallet-providers/svmWalletProvider.test.js +10 -0
- package/package.json +11 -6
- package/dist/action-providers/vaultsfyi/api/actions.d.ts +0 -41
- package/dist/action-providers/vaultsfyi/api/actions.js +0 -28
- package/dist/action-providers/vaultsfyi/api/historicalData.d.ts +0 -31
- package/dist/action-providers/vaultsfyi/api/historicalData.js +0 -44
- package/dist/action-providers/vaultsfyi/api/types.d.ts +0 -34
- package/dist/action-providers/vaultsfyi/api/vaults.d.ts +0 -66
- package/dist/action-providers/vaultsfyi/api/vaults.js +0 -57
- /package/dist/action-providers/{vaultsfyi/api → yelay}/types.js +0 -0
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.sushiDataActionProvider = exports.SushiDataActionProvider = void 0;
|
|
13
|
+
const zod_1 = require("zod");
|
|
14
|
+
const wallet_providers_1 = require("../../wallet-providers");
|
|
15
|
+
const actionDecorator_1 = require("../actionDecorator");
|
|
16
|
+
const actionProvider_1 = require("../actionProvider");
|
|
17
|
+
const sushiDataSchemas_1 = require("./sushiDataSchemas");
|
|
18
|
+
const evm_1 = require("sushi/evm");
|
|
19
|
+
const viem_1 = require("viem");
|
|
20
|
+
/**
|
|
21
|
+
* SushiDataActionProvider is an action provider for Sushi.
|
|
22
|
+
*
|
|
23
|
+
* This provider is used for any action that uses the Sushi Data API.
|
|
24
|
+
*/
|
|
25
|
+
class SushiDataActionProvider extends actionProvider_1.ActionProvider {
|
|
26
|
+
/**
|
|
27
|
+
* Constructor for the SushiDataActionProvider class.
|
|
28
|
+
*/
|
|
29
|
+
constructor() {
|
|
30
|
+
super("sushi-data", []);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Swaps a specified amount of a from token to a to token for the wallet.
|
|
34
|
+
*
|
|
35
|
+
* @param walletProvider - The wallet provider to swap the tokens from.
|
|
36
|
+
* @param args - The input arguments for the action.
|
|
37
|
+
* @returns A message containing the swap details.
|
|
38
|
+
*/
|
|
39
|
+
async findToken(walletProvider, args) {
|
|
40
|
+
try {
|
|
41
|
+
const chainId = Number((await walletProvider.getNetwork()).chainId);
|
|
42
|
+
if (!(0, evm_1.isEvmChainId)(chainId)) {
|
|
43
|
+
return `Unsupported chainId: ${chainId}`;
|
|
44
|
+
}
|
|
45
|
+
const request = await fetch(`${evm_1.SUSHI_DATA_API_HOST}/graphql`, {
|
|
46
|
+
method: "POST",
|
|
47
|
+
headers: {
|
|
48
|
+
"Content-Type": "application/json",
|
|
49
|
+
Accept: "application/json",
|
|
50
|
+
},
|
|
51
|
+
body: `{"query":"query { tokenList(chainId: ${chainId}, first: 10, search: \\"${args.search}\\") { address symbol name decimals } }"}`,
|
|
52
|
+
});
|
|
53
|
+
const response = await request.json();
|
|
54
|
+
const schema = zod_1.z.object({
|
|
55
|
+
data: zod_1.z.object({
|
|
56
|
+
tokenList: zod_1.z.array(zod_1.z.object({
|
|
57
|
+
address: zod_1.z.string().refine(val => (0, viem_1.isAddress)(val, { strict: false })),
|
|
58
|
+
symbol: zod_1.z.string(),
|
|
59
|
+
name: zod_1.z.string(),
|
|
60
|
+
decimals: zod_1.z.number(),
|
|
61
|
+
})),
|
|
62
|
+
}),
|
|
63
|
+
});
|
|
64
|
+
const result = schema.safeParse(response);
|
|
65
|
+
if (!result.success) {
|
|
66
|
+
return `Error parsing response: ${result.error.message}`;
|
|
67
|
+
}
|
|
68
|
+
const tokens = result.data.data.tokenList;
|
|
69
|
+
const chain = (0, evm_1.getEvmChainById)(chainId);
|
|
70
|
+
let message = `Found ${tokens.length} tokens on ${chain.shortName}:`;
|
|
71
|
+
tokens.forEach(token => {
|
|
72
|
+
message += `\n- ${token.symbol} (${token.name}) - ${token.address}`;
|
|
73
|
+
});
|
|
74
|
+
return message;
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
return `Error finding tokens: ${error}`;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Custom action providers are supported on all networks
|
|
82
|
+
*
|
|
83
|
+
* @param network - The network to checkpointSaver
|
|
84
|
+
* @returns True if the network is supported, false otherwise
|
|
85
|
+
*/
|
|
86
|
+
supportsNetwork(network) {
|
|
87
|
+
if (network.protocolFamily !== "evm" || !network.chainId) {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
return (0, evm_1.isEvmChainId)(Number(network.chainId));
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
exports.SushiDataActionProvider = SushiDataActionProvider;
|
|
94
|
+
__decorate([
|
|
95
|
+
(0, actionDecorator_1.CreateAction)({
|
|
96
|
+
name: "find-token",
|
|
97
|
+
description: `This tool finds tokens (erc20) using the Sushi Data API
|
|
98
|
+
It takes the following inputs:
|
|
99
|
+
- Search: Either the token symbol (either full or partial) or token address to search for
|
|
100
|
+
|
|
101
|
+
Important notes:
|
|
102
|
+
- Only returns the first 10 tokens found
|
|
103
|
+
- Always use the full token symbol for better results
|
|
104
|
+
- Always use this tool to verify the token address if you are not sure about the address
|
|
105
|
+
`,
|
|
106
|
+
schema: sushiDataSchemas_1.FindTokenSchema,
|
|
107
|
+
}),
|
|
108
|
+
__metadata("design:type", Function),
|
|
109
|
+
__metadata("design:paramtypes", [wallet_providers_1.EvmWalletProvider, void 0]),
|
|
110
|
+
__metadata("design:returntype", Promise)
|
|
111
|
+
], SushiDataActionProvider.prototype, "findToken", null);
|
|
112
|
+
const sushiDataActionProvider = () => new SushiDataActionProvider();
|
|
113
|
+
exports.sushiDataActionProvider = sushiDataActionProvider;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FindTokenSchema = void 0;
|
|
4
|
+
const zod_1 = require("zod");
|
|
5
|
+
/**
|
|
6
|
+
* Input schema for find token action
|
|
7
|
+
*/
|
|
8
|
+
exports.FindTokenSchema = zod_1.z
|
|
9
|
+
.object({
|
|
10
|
+
search: zod_1.z
|
|
11
|
+
.string()
|
|
12
|
+
.min(2)
|
|
13
|
+
.describe("Either the (partial or complete) symbol OR the address of the token to find"),
|
|
14
|
+
})
|
|
15
|
+
.strip()
|
|
16
|
+
.describe("Instructions for finding a token");
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { EvmWalletProvider } from "../../wallet-providers";
|
|
3
|
+
import { ActionProvider } from "../actionProvider";
|
|
4
|
+
import { SushiQuoteSchema, SushiSwapSchema } from "./sushiRouterSchemas";
|
|
5
|
+
import { Network } from "../../network";
|
|
6
|
+
/**
|
|
7
|
+
* SushiRouterActionProvider is an action provider for Sushi.
|
|
8
|
+
*
|
|
9
|
+
* This provider is used for any action that uses the Sushi Router API.
|
|
10
|
+
*/
|
|
11
|
+
export declare class SushiRouterActionProvider extends ActionProvider<EvmWalletProvider> {
|
|
12
|
+
/**
|
|
13
|
+
* Constructor for the SushiRouterActionProvider class.
|
|
14
|
+
*/
|
|
15
|
+
constructor();
|
|
16
|
+
/**
|
|
17
|
+
* Swaps a specified amount of a from token to a to token for the wallet.
|
|
18
|
+
*
|
|
19
|
+
* @param walletProvider - The wallet provider to swap the tokens from.
|
|
20
|
+
* @param args - The input arguments for the action.
|
|
21
|
+
* @returns A message containing the swap details.
|
|
22
|
+
*/
|
|
23
|
+
swap(walletProvider: EvmWalletProvider, args: z.infer<typeof SushiSwapSchema>): Promise<string>;
|
|
24
|
+
/**
|
|
25
|
+
* Gets a quote for a specified amount of a from token to a to token
|
|
26
|
+
*
|
|
27
|
+
* @param walletProvider - The wallet provider to swap the tokens from.
|
|
28
|
+
* @param args - The input arguments for the action.
|
|
29
|
+
* @returns A message containing the quote details.
|
|
30
|
+
*/
|
|
31
|
+
quote(walletProvider: EvmWalletProvider, args: z.infer<typeof SushiQuoteSchema>): Promise<string>;
|
|
32
|
+
/**
|
|
33
|
+
* Custom action providers are supported on all networks
|
|
34
|
+
*
|
|
35
|
+
* @param network - The network to checkpointSaver
|
|
36
|
+
* @returns True if the network is supported, false otherwise
|
|
37
|
+
*/
|
|
38
|
+
supportsNetwork(network: Network): boolean;
|
|
39
|
+
}
|
|
40
|
+
export declare const sushiRouterActionProvider: () => SushiRouterActionProvider;
|
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.sushiRouterActionProvider = exports.SushiRouterActionProvider = void 0;
|
|
13
|
+
const zod_1 = require("zod");
|
|
14
|
+
const wallet_providers_1 = require("../../wallet-providers");
|
|
15
|
+
const actionDecorator_1 = require("../actionDecorator");
|
|
16
|
+
const actionProvider_1 = require("../actionProvider");
|
|
17
|
+
const sushiRouterSchemas_1 = require("./sushiRouterSchemas");
|
|
18
|
+
const evm_1 = require("sushi/evm");
|
|
19
|
+
const viem_1 = require("viem");
|
|
20
|
+
const constants_1 = require("./constants");
|
|
21
|
+
/**
|
|
22
|
+
* SushiRouterActionProvider is an action provider for Sushi.
|
|
23
|
+
*
|
|
24
|
+
* This provider is used for any action that uses the Sushi Router API.
|
|
25
|
+
*/
|
|
26
|
+
class SushiRouterActionProvider extends actionProvider_1.ActionProvider {
|
|
27
|
+
/**
|
|
28
|
+
* Constructor for the SushiRouterActionProvider class.
|
|
29
|
+
*/
|
|
30
|
+
constructor() {
|
|
31
|
+
super("sushi-router", []);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Swaps a specified amount of a from token to a to token for the wallet.
|
|
35
|
+
*
|
|
36
|
+
* @param walletProvider - The wallet provider to swap the tokens from.
|
|
37
|
+
* @param args - The input arguments for the action.
|
|
38
|
+
* @returns A message containing the swap details.
|
|
39
|
+
*/
|
|
40
|
+
async swap(walletProvider, args) {
|
|
41
|
+
try {
|
|
42
|
+
const chainId = Number((await walletProvider.getNetwork()).chainId);
|
|
43
|
+
// Compatible chainId is expected since it should be pre-checked in supportsNetwork
|
|
44
|
+
if (!(0, evm_1.isSwapApiSupportedChainId)(chainId)) {
|
|
45
|
+
return `Unsupported chainId: ${chainId}`;
|
|
46
|
+
}
|
|
47
|
+
const chain = (0, evm_1.getEvmChainById)(chainId);
|
|
48
|
+
const decimalsIn = await fetchDecimals({ walletProvider, token: args.fromAssetAddress });
|
|
49
|
+
if (!decimalsIn.success) {
|
|
50
|
+
return decimalsIn.message;
|
|
51
|
+
}
|
|
52
|
+
const amountIn = (0, viem_1.parseUnits)(args.amount, decimalsIn.decimals);
|
|
53
|
+
// First fetch to see if the swap is even possible
|
|
54
|
+
const firstSwap = await handleGetSwap({
|
|
55
|
+
amount: amountIn,
|
|
56
|
+
chainId,
|
|
57
|
+
tokenIn: args.fromAssetAddress,
|
|
58
|
+
tokenOut: args.toAssetAddress,
|
|
59
|
+
maxSlippage: args.maxSlippage,
|
|
60
|
+
recipient: walletProvider.getAddress(),
|
|
61
|
+
});
|
|
62
|
+
if (firstSwap.swap.status !== evm_1.RouteStatus.Success) {
|
|
63
|
+
return firstSwap.message;
|
|
64
|
+
}
|
|
65
|
+
// Check if the wallet has enough balance to perform the swap
|
|
66
|
+
const balance = await handleBalance({
|
|
67
|
+
walletProvider,
|
|
68
|
+
token: firstSwap.swap.tokenFrom,
|
|
69
|
+
minAmount: amountIn,
|
|
70
|
+
});
|
|
71
|
+
if (!balance.success) {
|
|
72
|
+
return balance.message;
|
|
73
|
+
}
|
|
74
|
+
const approval = await handleApproval({
|
|
75
|
+
walletProvider,
|
|
76
|
+
token: args.fromAssetAddress,
|
|
77
|
+
to: firstSwap.swap.tx.to,
|
|
78
|
+
amount: amountIn,
|
|
79
|
+
});
|
|
80
|
+
if (!approval.success) {
|
|
81
|
+
return approval.message;
|
|
82
|
+
}
|
|
83
|
+
// Refetch in case the route changed during approval
|
|
84
|
+
const secondSwap = await handleGetSwap({
|
|
85
|
+
amount: amountIn,
|
|
86
|
+
chainId,
|
|
87
|
+
tokenIn: args.fromAssetAddress,
|
|
88
|
+
tokenOut: args.toAssetAddress,
|
|
89
|
+
maxSlippage: args.maxSlippage,
|
|
90
|
+
recipient: walletProvider.getAddress(),
|
|
91
|
+
});
|
|
92
|
+
if (secondSwap.swap.status !== evm_1.RouteStatus.Success) {
|
|
93
|
+
return secondSwap.message;
|
|
94
|
+
}
|
|
95
|
+
const swapHash = await walletProvider.sendTransaction({
|
|
96
|
+
from: secondSwap.swap.tx.from,
|
|
97
|
+
to: secondSwap.swap.tx.to,
|
|
98
|
+
data: secondSwap.swap.tx.data,
|
|
99
|
+
value: BigInt(secondSwap.swap.tx.value || 0),
|
|
100
|
+
});
|
|
101
|
+
const swapReceipt = await walletProvider.waitForTransactionReceipt(swapHash);
|
|
102
|
+
if (swapReceipt.status === "reverted" || swapReceipt.status === "failed") {
|
|
103
|
+
return `Swap failed: Transaction Reverted.\n - Transaction hash: ${swapHash}\n - Transaction link: ${chain.getTransactionUrl(swapHash)}`;
|
|
104
|
+
}
|
|
105
|
+
// Find the Route event log, which includes the actual amountOut
|
|
106
|
+
const [routeLog] = swapReceipt.logs
|
|
107
|
+
.filter(log => (0, viem_1.encodeEventTopics)({
|
|
108
|
+
abi: constants_1.routeProcessor9Abi_Route,
|
|
109
|
+
eventName: "Route",
|
|
110
|
+
})[0] === log.topics[0])
|
|
111
|
+
.map(log => (0, viem_1.decodeEventLog)({
|
|
112
|
+
abi: constants_1.routeProcessor9Abi_Route,
|
|
113
|
+
data: log.data,
|
|
114
|
+
topics: log.topics,
|
|
115
|
+
}));
|
|
116
|
+
return `Swapped ${(0, viem_1.formatUnits)(routeLog.args.amountIn, secondSwap.swap.tokenFrom.decimals)} of ${secondSwap.swap.tokenFrom.symbol} (${args.fromAssetAddress}) for ${(0, viem_1.formatUnits)(routeLog.args.amountOut, secondSwap.swap.tokenTo.decimals)} of ${secondSwap.swap.tokenTo.symbol} (${args.toAssetAddress}) on ${chain.shortName}
|
|
117
|
+
- Transaction hash: ${swapHash}
|
|
118
|
+
- Transaction link: ${chain.getTransactionUrl(swapHash)}`;
|
|
119
|
+
}
|
|
120
|
+
catch (error) {
|
|
121
|
+
return `Error swapping tokens: ${error}`;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Gets a quote for a specified amount of a from token to a to token
|
|
126
|
+
*
|
|
127
|
+
* @param walletProvider - The wallet provider to swap the tokens from.
|
|
128
|
+
* @param args - The input arguments for the action.
|
|
129
|
+
* @returns A message containing the quote details.
|
|
130
|
+
*/
|
|
131
|
+
async quote(walletProvider, args) {
|
|
132
|
+
try {
|
|
133
|
+
const chainId = Number((await walletProvider.getNetwork()).chainId);
|
|
134
|
+
// Compatible chainId is expected since it should be pre-checked in supportsNetwork
|
|
135
|
+
if (!(0, evm_1.isSwapApiSupportedChainId)(chainId)) {
|
|
136
|
+
return `Unsupported chainId: ${chainId}`;
|
|
137
|
+
}
|
|
138
|
+
const decimalsIn = await fetchDecimals({ walletProvider, token: args.fromAssetAddress });
|
|
139
|
+
if (!decimalsIn.success) {
|
|
140
|
+
return decimalsIn.message;
|
|
141
|
+
}
|
|
142
|
+
const amountIn = (0, viem_1.parseUnits)(args.amount, decimalsIn.decimals);
|
|
143
|
+
const swap = await handleGetSwap({
|
|
144
|
+
amount: amountIn,
|
|
145
|
+
chainId,
|
|
146
|
+
tokenIn: args.fromAssetAddress,
|
|
147
|
+
tokenOut: args.toAssetAddress,
|
|
148
|
+
maxSlippage: 0.0005, // 0.05%
|
|
149
|
+
recipient: walletProvider.getAddress(),
|
|
150
|
+
});
|
|
151
|
+
return swap.message;
|
|
152
|
+
}
|
|
153
|
+
catch (error) {
|
|
154
|
+
return `Error quoting for tokens: ${error}`;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Custom action providers are supported on all networks
|
|
159
|
+
*
|
|
160
|
+
* @param network - The network to checkpointSaver
|
|
161
|
+
* @returns True if the network is supported, false otherwise
|
|
162
|
+
*/
|
|
163
|
+
supportsNetwork(network) {
|
|
164
|
+
if (network.protocolFamily !== "evm" || !network.chainId) {
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
return (0, evm_1.isSwapApiSupportedChainId)(Number(network.chainId));
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
exports.SushiRouterActionProvider = SushiRouterActionProvider;
|
|
171
|
+
__decorate([
|
|
172
|
+
(0, actionDecorator_1.CreateAction)({
|
|
173
|
+
name: "swap",
|
|
174
|
+
description: `This tool will swap a specified amount of a 'from token' (erc20) to a 'to token' (erc20) for the wallet.
|
|
175
|
+
It takes the following inputs:
|
|
176
|
+
- The human-readable amount of the 'from token' to swap
|
|
177
|
+
- The from token address to trade
|
|
178
|
+
- The token address to receive from the swap
|
|
179
|
+
- The maximum slippage allowed for the swap, where 0 is 0% and 1 is 100%, the default is 0.005 (0.05%)
|
|
180
|
+
|
|
181
|
+
Important notes:
|
|
182
|
+
- The native asset (ie 'eth' on 'ethereum-mainnet') is represented by ${evm_1.nativeAddress} (not the wrapped native asset!)
|
|
183
|
+
- Fetch a quote first before the swap action. Stop, ask the user if they want to proceed. If the user answers affirmatively, then swap
|
|
184
|
+
- If you are not absolutely sure about token addresses, either use an action to fetch the token address or ask the user
|
|
185
|
+
`,
|
|
186
|
+
schema: sushiRouterSchemas_1.SushiSwapSchema,
|
|
187
|
+
}),
|
|
188
|
+
__metadata("design:type", Function),
|
|
189
|
+
__metadata("design:paramtypes", [wallet_providers_1.EvmWalletProvider, void 0]),
|
|
190
|
+
__metadata("design:returntype", Promise)
|
|
191
|
+
], SushiRouterActionProvider.prototype, "swap", null);
|
|
192
|
+
__decorate([
|
|
193
|
+
(0, actionDecorator_1.CreateAction)({
|
|
194
|
+
name: "quote",
|
|
195
|
+
description: `This tool will fetch a quote for a specified amount of a 'from token' (erc20 or native ETH) to a 'to token' (erc20 or native ETH).
|
|
196
|
+
It takes the following inputs:
|
|
197
|
+
- The human-readable amount of the 'from token' to fetch a quote for
|
|
198
|
+
- The from token address to fetch a quote for
|
|
199
|
+
- The token address to receive from the quoted swap
|
|
200
|
+
|
|
201
|
+
Important notes:
|
|
202
|
+
- The native asset (ie 'eth' on 'ethereum-mainnet') is represented by ${evm_1.nativeAddress} (not the wrapped native asset!)
|
|
203
|
+
- This action does not require any on-chain transactions or gas
|
|
204
|
+
- If you are not 100% certain about token addresses, use an action to fetch the token address first or ask the user
|
|
205
|
+
- NEVER assume that tokens have the same address on across networks (ie the address of 'usdc' on 'ethereum-mainnet' is different from 'usdc' on 'base-mainnet')
|
|
206
|
+
`,
|
|
207
|
+
schema: sushiRouterSchemas_1.SushiQuoteSchema,
|
|
208
|
+
}),
|
|
209
|
+
__metadata("design:type", Function),
|
|
210
|
+
__metadata("design:paramtypes", [wallet_providers_1.EvmWalletProvider, void 0]),
|
|
211
|
+
__metadata("design:returntype", Promise)
|
|
212
|
+
], SushiRouterActionProvider.prototype, "quote", null);
|
|
213
|
+
/**
|
|
214
|
+
* Fetches the number of decimals for the token
|
|
215
|
+
*
|
|
216
|
+
* @param root0 - The input arguments for the action
|
|
217
|
+
* @param root0.walletProvider - The wallet provider to fetch the decimals from
|
|
218
|
+
* @param root0.token - The token address to fetch the decimals for
|
|
219
|
+
*
|
|
220
|
+
* @returns The number of decimals for the token
|
|
221
|
+
*/
|
|
222
|
+
async function fetchDecimals({ walletProvider, token, }) {
|
|
223
|
+
const chainId = Number((await walletProvider.getNetwork()).chainId);
|
|
224
|
+
if (!(0, evm_1.isSwapApiSupportedChainId)(chainId)) {
|
|
225
|
+
return {
|
|
226
|
+
success: false,
|
|
227
|
+
message: `Unsupported chainId: ${chainId}`,
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
if (token === evm_1.nativeAddress) {
|
|
231
|
+
return { success: true, decimals: evm_1.EvmNative.fromChainId(chainId).decimals };
|
|
232
|
+
}
|
|
233
|
+
const decimals = (await walletProvider.readContract({
|
|
234
|
+
address: token,
|
|
235
|
+
abi: viem_1.erc20Abi,
|
|
236
|
+
functionName: "decimals",
|
|
237
|
+
}));
|
|
238
|
+
return { success: true, decimals };
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Checks if the wallet has enough balance to perform the swap
|
|
242
|
+
*
|
|
243
|
+
* @param root0 - The input arguments for the action
|
|
244
|
+
* @param root0.walletProvider - The wallet provider to fetch the balance from
|
|
245
|
+
* @param root0.token - The token address to fetch the balance for
|
|
246
|
+
* @param root0.token.address - The token address to fetch the balance for
|
|
247
|
+
* @param root0.token.symbol - The token symbol to fetch the balance for
|
|
248
|
+
* @param root0.token.decimals - The token decimals to fetch the balance for
|
|
249
|
+
* @param root0.minAmount - The minimum amount to check for
|
|
250
|
+
*
|
|
251
|
+
* @returns The balance of the wallet
|
|
252
|
+
*/
|
|
253
|
+
async function handleBalance({ walletProvider, token, minAmount, }) {
|
|
254
|
+
let balance;
|
|
255
|
+
if (token.address.toLowerCase() === evm_1.nativeAddress) {
|
|
256
|
+
balance = await walletProvider.getBalance();
|
|
257
|
+
}
|
|
258
|
+
else {
|
|
259
|
+
balance = (await walletProvider.readContract({
|
|
260
|
+
address: token.address,
|
|
261
|
+
abi: viem_1.erc20Abi,
|
|
262
|
+
functionName: "balanceOf",
|
|
263
|
+
args: [walletProvider.getAddress()],
|
|
264
|
+
}));
|
|
265
|
+
}
|
|
266
|
+
if (balance < minAmount) {
|
|
267
|
+
return {
|
|
268
|
+
success: false,
|
|
269
|
+
message: `Swap failed: Insufficient balance for ${token.symbol} (${token.address})
|
|
270
|
+
- Balance: ${(0, viem_1.formatUnits)(balance, token.decimals)}
|
|
271
|
+
- Required: ${(0, viem_1.formatUnits)(minAmount, token.decimals)}`,
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
return {
|
|
275
|
+
success: true,
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
*
|
|
280
|
+
* Wraps the getSwap function, providing messages for possible states
|
|
281
|
+
*
|
|
282
|
+
* @param root0 - The input arguments for the action
|
|
283
|
+
* @param root0.amount - The amount to swap
|
|
284
|
+
* @param root0.chainId - The chainId to swap on
|
|
285
|
+
* @param root0.tokenIn - The input token address
|
|
286
|
+
* @param root0.tokenOut - The output token address
|
|
287
|
+
* @param root0.maxSlippage - The maximum slippage allowed
|
|
288
|
+
* @param root0.recipient - The recipient of the swap
|
|
289
|
+
*
|
|
290
|
+
* @returns The result of the swap and a message
|
|
291
|
+
*/
|
|
292
|
+
async function handleGetSwap({ amount, chainId, tokenIn, tokenOut, maxSlippage, recipient, }) {
|
|
293
|
+
if (!(0, evm_1.isRedSnwapperChainId)(chainId)) {
|
|
294
|
+
return {
|
|
295
|
+
swap: { status: evm_1.RouteStatus.NoWay },
|
|
296
|
+
message: `Unsupported chainId: ${chainId}`,
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
const swap = await (0, evm_1.getSwap)({
|
|
300
|
+
amount,
|
|
301
|
+
chainId,
|
|
302
|
+
tokenIn,
|
|
303
|
+
tokenOut,
|
|
304
|
+
maxSlippage,
|
|
305
|
+
sender: recipient,
|
|
306
|
+
recipient,
|
|
307
|
+
});
|
|
308
|
+
const chain = (0, evm_1.getEvmChainById)(chainId);
|
|
309
|
+
if (swap.status === evm_1.RouteStatus.NoWay) {
|
|
310
|
+
return {
|
|
311
|
+
swap,
|
|
312
|
+
message: `No route found to swap ${amount} of ${tokenIn} for ${tokenOut} on ${chain.shortName}`,
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
if (swap.status === evm_1.RouteStatus.Partial) {
|
|
316
|
+
return {
|
|
317
|
+
swap,
|
|
318
|
+
message: `Found a partial quote for ${swap.tokenFrom.symbol} -> ${swap.tokenTo.symbol}. Swapping the full amount is not possible.
|
|
319
|
+
- AmountIn: ${(0, viem_1.formatUnits)(BigInt(swap.amountIn), swap.tokenFrom.decimals)}
|
|
320
|
+
- AmountOut: ${(0, viem_1.formatUnits)(BigInt(swap.assumedAmountOut), swap.tokenTo.decimals)}`,
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
return {
|
|
324
|
+
swap,
|
|
325
|
+
message: `Found a quote for ${swap.tokenFrom.symbol} (${swap.tokenFrom.address}) -> ${swap.tokenTo.symbol} (${swap.tokenTo.address})
|
|
326
|
+
- AmountIn: ${(0, viem_1.formatUnits)(BigInt(swap.amountIn), swap.tokenFrom.decimals)} ${swap.tokenFrom.symbol}
|
|
327
|
+
- AmountOut: ${(0, viem_1.formatUnits)(BigInt(swap.assumedAmountOut), swap.tokenTo.decimals)} ${swap.tokenTo.symbol}`,
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
*
|
|
332
|
+
* Handles the approval for the token
|
|
333
|
+
*
|
|
334
|
+
* @param root0 - The input arguments for the action
|
|
335
|
+
* @param root0.walletProvider - The wallet provider to handle the approval with
|
|
336
|
+
* @param root0.token - The token address to approve
|
|
337
|
+
* @param root0.to - The address to approve the token to
|
|
338
|
+
* @param root0.amount - The amount to approve
|
|
339
|
+
*
|
|
340
|
+
* @returns Either success: true on success or success: false with a message containing the reason for failure
|
|
341
|
+
*/
|
|
342
|
+
async function handleApproval({ walletProvider, token, to, amount, }) {
|
|
343
|
+
// No need to approve if the token is the native token
|
|
344
|
+
if (token.toLowerCase() === evm_1.nativeAddress) {
|
|
345
|
+
return { success: true };
|
|
346
|
+
}
|
|
347
|
+
// Check if the wallet already has enough allowance
|
|
348
|
+
const allowance = (await walletProvider.readContract({
|
|
349
|
+
address: token,
|
|
350
|
+
abi: viem_1.erc20Abi,
|
|
351
|
+
functionName: "allowance",
|
|
352
|
+
args: [walletProvider.getAddress(), to],
|
|
353
|
+
}));
|
|
354
|
+
if (allowance >= amount) {
|
|
355
|
+
return { success: true };
|
|
356
|
+
}
|
|
357
|
+
// Exact approval
|
|
358
|
+
const approvalHash = await walletProvider.sendTransaction({
|
|
359
|
+
to: token,
|
|
360
|
+
data: (0, viem_1.encodeFunctionData)({
|
|
361
|
+
abi: viem_1.erc20Abi,
|
|
362
|
+
functionName: "approve",
|
|
363
|
+
args: [to, BigInt(amount)],
|
|
364
|
+
}),
|
|
365
|
+
});
|
|
366
|
+
const approvalReceipt = await walletProvider.waitForTransactionReceipt(approvalHash);
|
|
367
|
+
const chainId = Number((await walletProvider.getNetwork()).chainId);
|
|
368
|
+
if (!(0, evm_1.isSwapApiSupportedChainId)(chainId)) {
|
|
369
|
+
return {
|
|
370
|
+
success: false,
|
|
371
|
+
message: `Unsupported chainId: ${chainId}`,
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
const chain = (0, evm_1.getEvmChainById)(chainId);
|
|
375
|
+
if (approvalReceipt.status === "reverted") {
|
|
376
|
+
return {
|
|
377
|
+
success: false,
|
|
378
|
+
message: `Swap failed: Approval Reverted.
|
|
379
|
+
- Transaction hash: ${approvalHash}
|
|
380
|
+
- Transaction link: ${chain.getTransactionUrl(approvalHash)}`,
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
return { success: true };
|
|
384
|
+
}
|
|
385
|
+
const sushiRouterActionProvider = () => new SushiRouterActionProvider();
|
|
386
|
+
exports.sushiRouterActionProvider = sushiRouterActionProvider;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|