Skip to content

Commit

Permalink
feat: enabled swap by token symbol + added erc20 permit and allowance
Browse files Browse the repository at this point in the history
  • Loading branch information
zoli committed Jan 28, 2025
1 parent b9322f9 commit 7e5afdc
Show file tree
Hide file tree
Showing 8 changed files with 766 additions and 13 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,21 @@ const inference = await agent.getInferenceByTopicId(42);
console.log("Allora inference for topic 42:", inference);
```

### Cross-Chain Swap

```typescript
import { PublicKey } from "@solana/web3.js";

const signature = await agent.swap(
amount: "10",
fromChain: "bsc",
fromToken: "0x3c499c542cef5e3811e1192ce70d8cc03d5c3359",
toChain: "solana",
toToken: "0x0000000000000000000000000000000000000000",
dstAddr: "0xc2d3024d64f27d85e05c40056674Fd18772dd922",
);
```

## Examples

### LangGraph Multi-Agent System
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"@meteora-ag/alpha-vault": "^1.1.7",
"@meteora-ag/dlmm": "^1.3.0",
"@onsol/tldparser": "^0.6.7",
"@openzeppelin/contracts": "^5.2.0",
"@orca-so/common-sdk": "0.6.4",
"@orca-so/whirlpools-sdk": "^0.13.12",
"@pythnetwork/hermes-client": "^1.3.0",
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 23 additions & 10 deletions src/actions/mayan/swap.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { PublicKey } from "@solana/web3.js";
import { Action } from "../../types/action";
import { SolanaAgentKit } from "../../agent";
import { z } from "zod";
Expand Down Expand Up @@ -27,8 +26,6 @@ const swapAction: Action = {
explanation:
"swap 0.02 0x0000000000000000000000000000000000000000 from solana to 0x0000000000000000000000000000000000000000 polygon destination 0x0cae42c0ce52e6e64c1e384ff98e686c6ee225f0",
},
],
[
{
input: {
amount: "0.02",
Expand All @@ -47,6 +44,25 @@ const swapAction: Action = {
"swap 0.02 0x0000000000000000000000000000000000000000 from solana to HZ1JovNiVvGrGNiiYvEozEVgZ58xaU3RKwX8eACQBCt3 solana destination 4ZgCP2idpqrxuQNfsjakJEm9nFyZ2xnT4CrDPKPULJPk",
},
],
[
{
input: {
amount: "0.02",
fromChain: "solana",
fromToken: "sol",
toChain: "solana",
toToken: "HNT",
dstAddr: "4ZgCP2idpqrxuQNfsjakJEm9nFyZ2xnT4CrDPKPULJPk",
},
output: {
status: "success",
message: "Swap executed successfully",
url: "https://explorer.mayan.finance/swap/2GLNqs5gXCBSwRt6VjtfQRnLWYbcU1gzkgjWMWautv1RUj13Di4qJPjV29YRpoAdMYxgXj8ArMLzF3bCCZmVUXHz",
},
explanation:
"swap 0.02 sol from solana to hnt solana destination 4ZgCP2idpqrxuQNfsjakJEm9nFyZ2xnT4CrDPKPULJPk",
},
],
],
schema: z.object({
amount: z
Expand All @@ -65,7 +81,7 @@ const swapAction: Action = {
"optimism",
"base",
]),
fromToken: z.string().min(32, "Invalid from mint address"),
fromToken: z.string(),
toChain: z.enum([
"solana",
"ethereum",
Expand All @@ -76,7 +92,7 @@ const swapAction: Action = {
"optimism",
"base",
]),
toToken: z.string().min(32, "Invalid to mint address"),
toToken: z.string(),
dstAddr: z.string().min(32, "Invalid destination address"),
inputAmount: z.number().positive("Input amount must be positive"),
slippageBps: z.number().min(0).max(10000).optional(),
Expand All @@ -87,7 +103,7 @@ const swapAction: Action = {
throw new Error("one of the from or to chain should be solana.");
}

const tx = await swap(
const url = await swap(
agent,
input.amount,
input.fromChain,
Expand All @@ -101,10 +117,7 @@ const swapAction: Action = {
return {
status: "success",
message: "Swap executed successfully",
transaction: tx,
inputAmount: input.inputAmount,
inputToken: input.inputMint || "SOL",
outputToken: input.outputMint,
url,
};
},
};
Expand Down
4 changes: 2 additions & 2 deletions src/langchain/mayan/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export class SolanaCrossChainSwapTool extends Tool {
Inputs ( input is a JSON string):
amount: string, eg "0.02" or "7"
fromChain: string, eg "solana" or "ethereum"
fromToken: string, eg "0x0000000000000000000000000000000000000000" or "hntyVP6YFm1Hg25TN9WGLqM12b8TQmcknKrdu1oxWux"
fromToken: string, eg "0x0000000000000000000000000000000000000000" or "hntyVP6YFm1Hg25TN9WGLqM12b8TQmcknKrdu1oxWux" or "sol" or "Pol"
toChain: string, eg "solana" or "ethereum"
toToken: string, eg "0x0000000000000000000000000000000000000000" or "0x6b175474e89094c44da98b954eedeac495271d0f"
toToken: string, eg "0x0000000000000000000000000000000000000000" or "0x6b175474e89094c44da98b954eedeac495271d0f" or "SOL" or "eth"
dstAddr: string, eg "4ZgCP2idpqrxuQNfsjakJEm9nFyZ2xnT4CrDPKPULJPk" or "0x0cae42c0cE52E6E64C1e384fF98e686C6eE225f0"
slippageBps: number, eg 10 (optional)`;

Expand Down
Loading

0 comments on commit 7e5afdc

Please sign in to comment.