Skip to content

Commit

Permalink
Teach Zilliqa plugin about PlunderSwap currency exchange.
Browse files Browse the repository at this point in the history
  • Loading branch information
mtbc committed Feb 5, 2025
1 parent ba3a9e3 commit e2be2ba
Show file tree
Hide file tree
Showing 7 changed files with 1,420 additions and 1,206 deletions.
6 changes: 6 additions & 0 deletions agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import { bootstrapPlugin } from "@elizaos/plugin-bootstrap";
import { normalizeCharacter } from "@elizaos/plugin-di";
import createGoatPlugin from "@elizaos/plugin-goat";
import createZilliqaPlugin from "@elizaos/plugin-zilliqa";
import createZilliqaEvmPlugin from "@elizaos/plugin-zilliqa";

// import { intifacePlugin } from "@elizaos/plugin-intiface";
import { ThreeDGenerationPlugin } from "@elizaos/plugin-3d-generation";
Expand Down Expand Up @@ -952,10 +953,14 @@ export async function createAgent(
}

let zilliqaPlugin: any | undefined;
let zilliqaEvmPlugin: any | undefined;
if (getSecret(character, "ZILLIQA_PRIVATE_KEY")) {
zilliqaPlugin = await createZilliqaPlugin((secret) =>
getSecret(character, secret)
);
zilliqaEvmPlugin = await createZilliqaEvmPlugin((secret) =>
getSecret(character, secret)
);
}

// Initialize Reclaim adapter if environment variables are present
Expand Down Expand Up @@ -1141,6 +1146,7 @@ export async function createAgent(
: null,
goatPlugin,
zilliqaPlugin,
zilliqaEvmPlugin,
getSecret(character, "COINGECKO_API_KEY") ||
getSecret(character, "COINGECKO_PRO_API_KEY")
? coingeckoPlugin
Expand Down
1 change: 0 additions & 1 deletion packages/plugin-zilliqa/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ pnpm install @elizaos/plugin-zilliqa
```typescript
EVM_PRIVATE_KEY=<Your EVM wallet private key>
EVM_PROVIDER_URL=<Your RPC provider URL (e.g., Infura, Alchemy)>
ENABLE_ZILLIQA=1
```

## Common Issues & Troubleshooting
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin-zilliqa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
"@elizaos/core": "workspace:*",
"@goat-sdk/adapter-vercel-ai": "0.2.7",
"@goat-sdk/core": "0.4.6",
"@goat-sdk/plugin-plunderswap": "1.0.0",
"@goat-sdk/plugin-zilliqa": "0.1.3",
"@goat-sdk/wallet-evm": "0.2.6",
"@goat-sdk/wallet-viem": "0.2.6",
"@goat-sdk/wallet-zilliqa": "0.2.6",
"@goat-sdk/wallet-zilliqa": "0.3.0",
"@zilliqa-js/account": "^3.5.0",
"@zilliqa-js/zilliqa": "^3.5.0",
"tsup": "8.3.5"
Expand Down
280 changes: 271 additions & 9 deletions packages/plugin-zilliqa/src/actions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { getOnChainTools } from "@goat-sdk/adapter-vercel-ai";
import { MODE, USDC, erc20 } from "@goat-sdk/plugin-erc20";
import { kim } from "@goat-sdk/plugin-kim";
import { sendETH } from "@goat-sdk/wallet-evm";
import { WalletClientBase } from "@goat-sdk/core";
import { plunderswap } from "@goat-sdk/plugin-plunderswap";
import { zilliqa } from "@goat-sdk/plugin-zilliqa";
import { ZilliqaWalletClientViemOnly } from "@goat-sdk/wallet-zilliqa";

import {
generateText,
Expand All @@ -15,11 +15,11 @@ import {
composeContext,
elizaLogger,
} from "@elizaos/core";
import { Zilliqa } from "@zilliqa-js/zilliqa";

export async function getOnChainActions(
evmWallet: WalletClientBase,
zilliqaWallet: WalletClientBase
zilliqaWallet: WalletClientBase,
zilliqaEvmWallet: ZilliqaWalletClientViemOnly
) {
const actionsWithoutHandler = [
{
Expand All @@ -34,7 +34,6 @@ export async function getOnChainActions(
user: "{{user1}}",
content: {
text: "Tell me the balance of account 0xf0cb24ac66ba7375bf9b9c4fa91e208d9eaabd2e",
action: "GET_BALANCE",
},
},
{
Expand Down Expand Up @@ -131,6 +130,258 @@ export async function getOnChainActions(
// 1. Add your actions here
];

const actionsEvmWithoutHandler = [
{
name: "GET_SWAP_TOKENS",
description:
"Find out which tokens can be swapped using the GET_SWAP_QUOTE action.",
similes: [],
validate: async () => true,
examples: [
[
{
user: "{{user1}}",
content: {
text: "Which tokens can I swap for other tokens?",
},
},
{
user: "{{agentName}}",
content: {
text: "You can ask for quotes on swapping WZIL, zETH, FPS, HRSE, SEED, kUSD, stZIL, WZIL, and gZIL.",
action: "GET_SWAP_TOKENS",
},
},
],
[
{
user: "{{user1}}",
content: {
text: "Which tokens can I trade using PlunderSwap?",
},
},
{
user: "{{agentName}}",
content: {
text: "Liquidity may be available for exchanging WZIL, zETH, FPS, HRSE, SEED, kUSD, stZIL, WZIL, and gZIL.",
action: "GET_SWAP_TOKENS",
},
},
],
],
},
{
name: "GET_SWAP_BALANCE",
description:
"Find out how much I have of a token that I can swap using the GET_SWAP_QUOTE action.",
similes: [],
validate: async () => true,
examples: [
[
{
user: "{{user1}}",
content: {
text: "How much SEED do I have for swapping?",
},
},
{
user: "{{agentName}}",
content: {
text: "You have 350.25 SEED that you could swap.",
action: "GET_SWAP_BALANCE",
},
},
],
[
{
user: "{{user1}}",
content: {
text: "What is all the tradable WZIL in my wallet?",
},
},
{
user: "{{agentName}}",
content: {
text: "You have 2,800 WZIL available for trading for another kind of coin.",
action: "GET_SWAP_BALANCE",
},
},
],
],
},
{
name: "GET_SWAP_QUOTE",
description:
"Get a quote for swapping one token for another using the GET_SWAP_QUOTE action. Tokens must be as provided from the GET_SWAP_TOKENS tool and a quantity of the from token must be provided.",
similes: [],
validate: async () => true,
examples: [
[
{
user: "{{user1}}",
content: {
text: "Tell me how many SEED I would get in exchange for 240 WZIL.",
},
},
{
user: "{{agentName}}",
content: {
text: "If you exchange 240 WZIL at the moment then you would expect to receive around 18.77584 SEED if you convert WZIL directly to SEED",
action: "GET_SWAP_QUOTE",
},
},
],
[
{
user: "{{user1}}",
content: {
text: "I want to swap 1500 HRSE for FPS. How many would I get?",
},
},
{
user: "{{agentName}}",
content: {
text: "At this time, you should get around 320.9 FPS for your HRSE if you convert FPS to WZIL to HRSE.",
action: "GET_SWAP_QUOTE",
},
},
],
],
},
{
name: "PERFORM_SWAP",
description:
"Swapping one token for another using the PERFORM_SWAP action. Specify a minimum number of tokens to receive and where to deposit them to.",
similes: [],
validate: async () => true,
examples: [
[
{
user: "{{user1}}",
content: {
text: "Please make the trade.",
},
},
{
user: "{{agentName}}",
content: {
text: "Your zUSDT has been swapped in exchange for 117.25 gZIL which are now in your wallet.",
action: "GET_SWAP_QUOTE",
},
},
],
[
{
user: "{{user1}}",
content: {
text: "Within the next ten minutes, please swap 200 of my WZIL directly in exchange for at least 15 SEED.",
},
},
{
user: "{{agentName}}",
content: {
text: "Your WZIL has been swapped in exchange for 17.25 SEED which are now in your wallet.",
action: "GET_SWAP_QUOTE",
},
},
],
[
{
user: "{{user1}}",
content: {
text: "Trade 1500 of my HRSE via WZIL for a minimum of 300 FPS in the next five minutes.",
},
},
{
user: "{{agentName}}",
content: {
text: "Your HRSE was swapped to WZIL then to 318.5 FPS which you have now received.",
action: "GET_SWAP_QUOTE",
},
},
],
],
},
{
name: "WRAP_ZIL",
description:
"Wrap native ZIL in the WZIL ERC-20 contract so that it may be traded on PlunderSwap.",
similes: [],
validate: async () => true,
examples: [
[
{
user: "{{user1}}",
content: {
text: "Please wrap 200 of my native ZIL so I can trade it.",
},
},
{
user: "{{agentName}}",
content: {
text: "You have now used 200 ZIL to give you 200 more WZIL.",
action: "WRAP_ZIL",
},
},
],
[
{
user: "{{user1}}",
content: {
text: "Please convert 500 ZIL to WZIL ready for swapping.",
},
},
{
user: "{{agentName}}",
content: {
text: "You have now spent 500 of your native ZIL to buy 500 WZIL.",
action: "WRAP_ZIL",
},
},
],
],
},
{
name: "UNWRAP_ZIL",
description:
"Unwrap WZIL from its ERC-20 contract to obtain native ZIL back.",
similes: [],
validate: async () => true,
examples: [
[
{
user: "{{user1}}",
content: {
text: "Please unwrap 250 of my ZIL, I don't need to trade it any more.",
},
},
{
user: "{{agentName}}",
content: {
text: "250 of your WZIL has been converted and received in your wallet as native ZIL.",
action: "UNWRAP_ZIL",
},
},
],
[
{
user: "{{user1}}",
content: {
text: "Please turn 100 of my WZIL back to native ZIL.",
},
},
{
user: "{{agentName}}",
content: {
text: "You have now exchanged 100 WZIL for the same amount of native ZIL.",
action: "UNWRAP_ZIL",
},
},
],
],
},
];

const tools = await getOnChainTools({
wallet: evmWallet,
// 2. Configure the plugins you need to perform those actions
Expand All @@ -142,12 +393,23 @@ export async function getOnChainActions(
plugins: [zilliqa()],
});

const zilEvmTools = await getOnChainTools({
wallet: zilliqaEvmWallet,
plugins: [plunderswap()],
});

const allTools = { ...zilTools, ...tools };
const allEvmTools = { ...zilEvmTools };
// 3. Let GOAT handle all the actions
return actionsWithoutHandler.map((action) => ({
...action,
handler: getActionHandler(action.name, action.description, allTools),
}));
return [
... actionsWithoutHandler.map((action) => ({
...action,
handler: getActionHandler(action.name, action.description, allTools),
})),
...actionsEvmWithoutHandler.map((action) => ({
...action,
handler: getActionHandler(action.name, action.description, allEvmTools),
}))];
}

function getActionHandler(
Expand Down
7 changes: 4 additions & 3 deletions packages/plugin-zilliqa/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import type { Plugin } from "@elizaos/core";
import { getOnChainActions } from "./actions";
import { getZilliqaWalletClient, getWalletProviders } from "./wallet";
import { getZilliqaWalletClient, getWalletProviders, getZilliqaViemWalletClient } from "./wallet";

export async function zilliqaPlugin(
getSetting: (key: string) => string | undefined
): Promise<Plugin> {
const zilliqaWalletClient = await getZilliqaWalletClient(getSetting);
if (!zilliqaWalletClient) {
const zilliqaViemWalletClient = await getZilliqaViemWalletClient(getSetting);
if (!(zilliqaWalletClient && zilliqaViemWalletClient)) {
throw new Error("Zilliqa wallet client initialization failed. Ensure that ZILLIQA_PRIVATE_KEY and ZILLIQA_PROVIDER_URL are configured.");
}
const walletClient = zilliqaWalletClient!.getEVM();
const actions = await getOnChainActions(walletClient, zilliqaWalletClient!);
const actions = await getOnChainActions(walletClient, zilliqaWalletClient, zilliqaViemWalletClient);

return {
name: "[ZILLIQA] Onchain Actions",
Expand Down
Loading

0 comments on commit e2be2ba

Please sign in to comment.