Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add mira exact input swap #4

Merged
merged 2 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 71 additions & 21 deletions src/agent.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { ChatPromptTemplate } from '@langchain/core/prompts';
import { ChatOpenAI } from '@langchain/openai';
import { transferTool, transferToWallet } from './tools.js';
import {
borrowAsset,
supplyCollateral,
transferTool,
transferToWallet,
} from './tools.js';
import { AIMessage } from '@langchain/core/messages';
import { createToolCallingAgent, AgentExecutor } from 'langchain/agents';
import { tools } from './tools.js';
import { swapExactInput } from './mira/swap.js';

export const prompt = ChatPromptTemplate.fromMessages([
[
Expand Down Expand Up @@ -36,28 +42,72 @@ export async function handleModelResponse(response: AIMessage) {
}

// Verify we have the expected tool and args
if (
toolCall.name === 'fuel_transfer' &&
toolCall.args &&
toolCall.args.to &&
toolCall.args.amount
) {
try {
// Execute the actual tool function
// You'll need to implement or import this function
const transferResult = await transferToWallet({
to: toolCall.args.to,
amount: toolCall.args.amount,
});
switch (toolCall.name) {
case 'fuel_transfer':
if (
toolCall.args?.to &&
toolCall.args?.amount &&
toolCall.args?.symbol
) {
try {
return await transferToWallet({
to: toolCall.args.to,
amount: toolCall.args.amount,
symbol: toolCall.args.symbol,
});
} catch (error) {
console.error('Error executing fuel transfer:', error);
throw error;
}
}
break;

return transferResult;
} catch (error) {
console.error('Error executing fuel transfer:', error);
throw error;
}
} else {
throw new Error('Invalid or unexpected tool call format');
case 'swap_exact_input':
if (
toolCall.args?.amount &&
toolCall.args?.fromSymbol &&
toolCall.args?.toSymbol
) {
try {
return await swapExactInput({
amount: toolCall.args.amount,
fromSymbol: toolCall.args.fromSymbol,
toSymbol: toolCall.args.toSymbol,
});
} catch (error) {
console.error('Error executing swap:', error);
throw error;
}
}
break;

case 'supply_collateral':
if (toolCall.args?.amount && toolCall.args?.symbol) {
try {
return await supplyCollateral({
amount: toolCall.args.amount,
symbol: toolCall.args.symbol,
});
} catch (error) {
console.error('Error supplying collateral:', error);
throw error;
}
}
break;

case 'borrow_asset':
if (toolCall.args?.amount) {
try {
return await borrowAsset({
amount: toolCall.args.amount,
});
} catch (error) {
console.error('Error borrowing asset:', error);
throw error;
}
}
}
throw new Error('Invalid or unexpected tool call format');
} else {
throw new Error('No tool calls found in response');
}
Expand Down
93 changes: 93 additions & 0 deletions src/mira/swap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { bn, Provider, Wallet } from 'fuels';
import { getAllVerifiedFuelAssets } from '../utils/assets.js';
import { buildPoolId, MiraAmm } from 'mira-dex-ts';

async function futureDeadline(provider: Provider) {
const block = await provider.getBlock('latest');
return block?.height.add(1000) ?? 1000000000;
}

export const swapExactInput = async ({
amount,
fromSymbol,
toSymbol,
}: {
amount: number;
fromSymbol: string;
toSymbol: string;
}) => {
const assets = await getAllVerifiedFuelAssets();

const fromAsset = assets.find((asset) => asset.symbol === fromSymbol);
const toAsset = assets.find((asset) => asset.symbol === toSymbol);

if (!fromAsset) {
throw new Error(`Asset ${fromSymbol} not found`);
}

if (!toAsset) {
throw new Error(`Asset ${toSymbol} not found`);
}

const fromAssetId = fromAsset?.assetId;
const toAssetId = toAsset?.assetId;

if (!fromAssetId) {
throw new Error(`Asset ${fromSymbol} not found`);
}

if (!toAssetId) {
throw new Error(`Asset ${toSymbol} not found`);
}

const fromAssetDecimals = fromAsset?.decimals;
const toAssetDecimals = toAsset?.decimals;

if (!fromAssetDecimals) {
throw new Error(`Asset ${fromSymbol} not found`);
}

if (!toAssetDecimals) {
throw new Error(`Asset ${toSymbol} not found`);
}

const provider = await Provider.create(
'https://mainnet.fuel.network/v1/graphql',
);

const wallet = Wallet.fromPrivateKey(
process.env.FUEL_WALLET_PRIVATE_KEY as string,
provider,
);

const amountInWei = bn.parseUnits(amount.toString(), fromAssetDecimals);

const poolId = buildPoolId(fromAssetId, toAssetId, true);

const miraAmm = new MiraAmm(wallet);

const deadline = await futureDeadline(provider);

const req = await miraAmm.swapExactInput(
amountInWei,
{
bits: fromAssetId,
},
0,
[poolId],
deadline,
{
gasLimit: 1000000,
maxFee: 1000000,
},
);

const tx = await wallet.sendTransaction(req);

const { id, status } = await tx.waitForResult();

return {
status,
id,
};
};
70 changes: 16 additions & 54 deletions src/tools.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { tool } from '@langchain/core/tools';
import { bn, Provider, Wallet, arrayify, DateTime } from 'fuels';
import { MiraAmm, type PoolId } from 'mira-dex-ts';
import { z } from 'zod';
import { getAllVerifiedFuelAssets } from './utils/assets.js';
import { getTxExplorerUrl } from './utils/explorer.js';
import { Market, type PriceDataUpdateInput } from './types/Market.js';
import { HermesClient } from '@pythnetwork/hermes-client';
import { PythContract } from '@pythnetwork/pyth-fuel-js';
import { swapExactInput } from './mira/swap.js';

export const transferToWallet = async ({
to,
Expand Down Expand Up @@ -57,57 +57,15 @@ export const transferTool = tool(transferToWallet, {
}),
});

export const swapEthForUSDC = async ({ amount }: { amount: string }) => {
const provider = await Provider.create(
'https://mainnet.fuel.network/v1/graphql',
);
const wallet = Wallet.fromPrivateKey(
process.env.FUEL_WALLET_PRIVATE_KEY as string,
provider,
);

const amountInWei = bn.parseUnits(amount);

const pools: PoolId[] = [
[
{
bits: provider.getBaseAssetId(),
},
{
bits: '0x286c479da40dc953bddc3bb4c453b608bba2e0ac483b077bd475174115395e6b', // USDC
},
true,
],
];

const miraAmm = new MiraAmm(wallet);

const response = await miraAmm.swapExactInput(
amountInWei,
{
bits: provider.getBaseAssetId(),
},
0,
pools,
// 7 days from now in milliseconds
bn(Math.floor(Date.now() / 1000) + 60 * 60 * 24 * 7),
);

const res = await wallet.sendTransaction(response);

const { id, status } = await res.waitForResult();

return {
status,
id,
};
};

export const swapEthForUSDCTool = tool(swapEthForUSDC, {
name: 'swap_eth_for_usdc',
description: 'Swap ETH for USDC on Mira',
export const swapExactInputTool = tool(swapExactInput, {
name: 'swap_exact_input',
description: 'Swap exact input on Mira',
schema: z.object({
amount: z.string().describe('The ETH amount to swap'),
amount: z.number().describe('The amount to swap'),
fromSymbol: z
.string()
.describe('The asset symbol to swap from. eg. USDC, ETH'),
toSymbol: z.string().describe('The asset symbol to swap to. eg. USDC, ETH'),
}),
});

Expand Down Expand Up @@ -172,7 +130,6 @@ export const borrowAsset = async ({ amount }: { amount: number }) => {
provider,
);


const marketContractId =
'0x657ab45a6eb98a4893a99fd104347179151e8b3828fd8f2a108cc09770d1ebae';
const marketContract: Market = new Market(marketContractId, wallet);
Expand Down Expand Up @@ -219,7 +176,7 @@ export const borrowAsset = async ({ amount }: { amount: number }) => {
'0x1c86fdd9e0e7bc0d2ae1bf6817ef4834ffa7247655701ee1b031b52a24c523da',
wallet,
);

// before initiating the borrow make sure the wallet has some small amount of USDC for the oracle fee
const priceUpdateData: PriceDataUpdateInput = {
update_fee: 0,
Expand Down Expand Up @@ -256,4 +213,9 @@ export const borrowAssetTool = tool(borrowAsset, {
}),
});

export const tools = [transferTool, swapEthForUSDCTool, supplyCollateralTool];
export const tools = [
transferTool,
swapExactInputTool,
supplyCollateralTool,
borrowAssetTool,
];
12 changes: 9 additions & 3 deletions test/swap.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { test } from 'vitest';
import { FuelAgent } from '../src/FuelAgent.js';
import { swapExactInput } from '../src/mira/swap.js';

test(
'swap eth for usdc',
'swap exact input',
async () => {
const agent = new FuelAgent();
console.log(await agent.execute('swap 0.005 eth for usdc'));
console.log(
await swapExactInput({
amount: 5,
fromSymbol: 'USDT',
toSymbol: 'ETH',
}),
);
},
{
timeout: 30000,
Expand Down
Loading