UNPKG

@coinbase/agentkit

Version:

Coinbase AgentKit core primitives

77 lines (76 loc) 2.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getVaultsLink = getVaultsLink; exports.executeActions = executeActions; exports.transformApyObject = transformApyObject; exports.transformApy = transformApy; exports.transformVault = transformVault; /** * Get the link to the vaults.fyi page for a vault * * @param vault - The vault * @returns The link to the vaults.fyi page */ function getVaultsLink(vault) { return `https://app.vaults.fyi/opportunity/${vault.network.name}/${vault.address}`; } /** * Execute a list of actions * * @param wallet - The wallet provider * @param actions - The list of actions to execute * @returns nothing */ async function executeActions(wallet, actions) { for (let i = actions.currentActionIndex; i < actions.actions.length; i++) { const action = actions.actions[i]; const txHash = await wallet.sendTransaction({ to: action.tx.to, data: action.tx.data, value: action.tx.value ? BigInt(action.tx.value) : undefined, }); await wallet.waitForTransactionReceipt(txHash); } } /** * Transform the apy object to a more readable format * * @param apy - The apy * @returns The transformed apy */ function transformApyObject(apy) { return ["1day", "7day", "30day", "1hour"].reduce((acc, curr) => { if (!(curr in apy)) { return acc; } acc[curr] = transformApy(apy[curr]); return acc; }, {}); } /** * Transform the apy to a more readable format * * @param apy - The apy * @returns The transformed apy */ function transformApy(apy) { return { base: `${(apy.base * 100).toFixed(2)}%`, reward: `${(apy.reward * 100).toFixed(2)}%`, total: `${(apy.total * 100).toFixed(2)}%`, }; } /** * Transform the vault to a more readable format * * @param vault - The vault * @returns The transformed vault */ function transformVault(vault) { return { ...vault, apy: transformApyObject(vault.apy), link: getVaultsLink(vault), lastUpdateTimestamp: new Date(vault.lastUpdateTimestamp * 1000).toISOString(), }; }