forked from elizaOS/eliza
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ef85bda
commit 596be47
Showing
7 changed files
with
381 additions
and
3 deletions.
There are no files selected for viewing
181 changes: 181 additions & 0 deletions
181
packages/plugin-cosmos/src/actions/fetch-balance/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
import { HandlerCallback, IAgentRuntime, Memory, State } from "@elizaos/core"; | ||
import { initWalletChainsData } from "../../providers/wallet/utils"; | ||
import { cosmosTransferTemplate } from "../../templates"; | ||
import type { | ||
ICosmosPluginOptions, | ||
ICosmosWalletChains, | ||
} from "../../shared/interfaces"; | ||
import { FetchBalancesActionService } from "./services/fetch-balance"; | ||
|
||
export const fetchBalancesAction = (pluginOptions: ICosmosPluginOptions) => ({ | ||
name: "FETCH_BALANCES", | ||
description: "Fetch balances for all connected chains", | ||
handler: async ( | ||
_runtime: IAgentRuntime, | ||
_message: Memory, | ||
state: State, | ||
_options: { [key: string]: unknown }, | ||
_callback?: HandlerCallback | ||
) => { | ||
try { | ||
const walletProvider: ICosmosWalletChains = | ||
await initWalletChainsData(_runtime); | ||
|
||
const action = new FetchBalancesActionService(walletProvider); | ||
|
||
const transferResp = await action.execute( | ||
pluginOptions?.customChainData | ||
); | ||
|
||
console.log(transferResp); | ||
|
||
if (_callback) { | ||
await _callback({ | ||
text: `Successfully fetched balances`, | ||
content: { | ||
success: true, | ||
}, | ||
}); | ||
|
||
const newMemory: Memory = { | ||
userId: _message.agentId, | ||
agentId: _message.agentId, | ||
roomId: _message.roomId, | ||
content: { | ||
text: `Successfully fetched balances`, | ||
}, | ||
}; | ||
|
||
await _runtime.messageManager.createMemory(newMemory); | ||
} | ||
return true; | ||
} catch (error) { | ||
console.error("Error during token transfer:", error); | ||
|
||
if (_callback) { | ||
await _callback({ | ||
text: `Error while fetching balance: ${error.message}`, | ||
content: { error: error.message }, | ||
}); | ||
} | ||
|
||
const newMemory: Memory = { | ||
userId: _message.agentId, | ||
agentId: _message.agentId, | ||
roomId: _message.roomId, | ||
content: { | ||
text: `Balance fetch failed`, | ||
}, | ||
}; | ||
|
||
await _runtime.messageManager.createMemory(newMemory); | ||
|
||
return false; | ||
} | ||
}, | ||
template: cosmosTransferTemplate, | ||
validate: async (runtime: IAgentRuntime) => { | ||
const mnemonic = runtime.getSetting("COSMOS_RECOVERY_PHRASE"); | ||
const availableChains = runtime.getSetting("COSMOS_AVAILABLE_CHAINS"); | ||
const availableChainsArray = availableChains?.split(","); | ||
|
||
return !(mnemonic && availableChains && availableChainsArray.length); | ||
}, | ||
examples: [ | ||
[ | ||
{ | ||
user: "{{user1}}", | ||
content: { | ||
text: "Fetch balances", | ||
action: "FETCH_BALANCES", | ||
}, | ||
}, | ||
{ | ||
user: "{{user2}}", | ||
content: { | ||
text: "Do you confirm the transfer action?", | ||
action: "FETCH_BALANCES", | ||
}, | ||
}, | ||
{ | ||
user: "{{user1}}", | ||
content: { | ||
text: "Yes", | ||
action: "FETCH_BALANCES", | ||
}, | ||
}, | ||
{ | ||
user: "{{user2}}", | ||
content: { | ||
text: "", | ||
action: "FETCH_BALANCES", | ||
}, | ||
}, | ||
], | ||
[ | ||
{ | ||
user: "{{user1}}", | ||
content: { | ||
text: "Display my wallet balances", | ||
action: "FETCH_BALANCES", | ||
}, | ||
}, | ||
{ | ||
user: "{{user2}}", | ||
content: { | ||
text: "Do you confirm the transfer action?", | ||
action: "FETCH_BALANCES", | ||
}, | ||
}, | ||
{ | ||
user: "{{user1}}", | ||
content: { | ||
text: "Yes", | ||
action: "FETCH_BALANCES", | ||
}, | ||
}, | ||
{ | ||
user: "{{user2}}", | ||
content: { | ||
text: "", | ||
action: "FETCH_BALANCES", | ||
}, | ||
}, | ||
], | ||
[ | ||
{ | ||
user: "{{user1}}", | ||
content: { | ||
text: "Show me my wallet balances", | ||
action: "FETCH_BALANCES", | ||
}, | ||
}, | ||
{ | ||
user: "{{user2}}", | ||
content: { | ||
text: "Do you confirm the transfer action?", | ||
action: "FETCH_BALANCES", | ||
}, | ||
}, | ||
{ | ||
user: "{{user1}}", | ||
content: { | ||
text: "Yes", | ||
action: "FETCH_BALANCES", | ||
}, | ||
}, | ||
{ | ||
user: "{{user2}}", | ||
content: { | ||
text: "", | ||
action: "FETCH_BALANCES", | ||
}, | ||
}, | ||
], | ||
], | ||
similes: [ | ||
"COSMOS_FETCH_ACCOUNT_BALANCE", | ||
"COSMOS_GET_BALANCE", | ||
"COSMOS_GET_WALLET_BALANCE", | ||
], | ||
}); |
56 changes: 56 additions & 0 deletions
56
packages/plugin-cosmos/src/actions/fetch-balance/services/fetch-balance.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { getChainByChainName } from "@chain-registry/utils"; | ||
import type { | ||
ICosmosActionService, | ||
ICosmosPluginCustomChainData, | ||
ICosmosWalletChains, | ||
} from "../../../shared/interfaces.ts"; | ||
import { assets, chains } from "chain-registry"; | ||
import { balanceFetcher } from "../../../shared/services/balance-fetcher.ts"; | ||
import { Coin } from "@cosmjs/proto-signing"; | ||
|
||
export class FetchBalancesActionService implements ICosmosActionService { | ||
constructor(private cosmosWalletChains: ICosmosWalletChains) { | ||
this.cosmosWalletChains = cosmosWalletChains; | ||
} | ||
|
||
async execute( | ||
customChains?: ICosmosPluginCustomChainData[] | ||
): Promise<{ chainName: string; balances: Coin[] }[]> { | ||
const chainRegisteryChainsWithCustomChains = [...chains]; | ||
const chainRegisteryAssetsWithCustomAssets = [...assets]; | ||
|
||
if (customChains) { | ||
chainRegisteryChainsWithCustomChains.push( | ||
...customChains.map(({ chainData }) => chainData) | ||
); | ||
chainRegisteryAssetsWithCustomAssets.push( | ||
...customChains.map(({ assets }) => assets) | ||
); | ||
} | ||
|
||
const chainsDetails = Object.keys( | ||
this.cosmosWalletChains.walletChainsData | ||
) | ||
.map((chainName) => getChainByChainName(chains, chainName)) | ||
.filter(Boolean); | ||
|
||
return Promise.all( | ||
chainsDetails.map(async (chainDetails) => { | ||
const addressForGivenChain = | ||
await this.cosmosWalletChains.getWalletAddress( | ||
chainDetails.chain_name | ||
); | ||
|
||
const balanceForGivenChain = await balanceFetcher( | ||
chainDetails.apis.rpc[0].address, | ||
addressForGivenChain | ||
); | ||
|
||
return { | ||
chainName: chainDetails.chain_name, | ||
balances: balanceForGivenChain, | ||
}; | ||
}) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
packages/plugin-cosmos/src/shared/services/balance-fetcher.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { cosmos } from "interchain"; | ||
import type { Coin } from "@cosmjs/proto-signing"; | ||
|
||
export const balanceFetcher = async (rpcEndpoint: string, address: string) => { | ||
const client = await cosmos.ClientFactory.createRPCQueryClient({ | ||
rpcEndpoint, | ||
}); | ||
|
||
const allBalances = await client.cosmos.bank.v1beta1.allBalances({ | ||
address, | ||
}); | ||
|
||
return allBalances.balances as Coin[]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.