Skip to content

Commit

Permalink
Merge pull request #34 from FuelLabs/dp/handle-errors-gracefully
Browse files Browse the repository at this point in the history
add better error handling
  • Loading branch information
Dhaiwat10 authored Dec 29, 2024
2 parents 70132de + e5cec63 commit f6b9d28
Show file tree
Hide file tree
Showing 11 changed files with 415 additions and 329 deletions.
2 changes: 1 addition & 1 deletion src/FuelAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface FuelAgentConfig {
googleGeminiApiKey?: string;
}

export class FuelAgent {
export class FuelAgent {
private walletPrivateKey: string;
private agentExecutor: AgentExecutor;
private model: keyof typeof modelMapping;
Expand Down
7 changes: 5 additions & 2 deletions src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ const systemMessage = new SystemMessage(
` You are an AI agent on Fuel network capable of executing all kinds of transactions and interacting with the Fuel blockchain.
You are able to execute transactions on behalf of the user.
Always return the response in the following format:
The transaction was successful/failed. The explorer link is: https://app.fuel.network/tx/0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef/simple
If the transaction was successful, return the response in the following format:
The transaction was successful. The explorer link is: https://app.fuel.network/tx/0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef/simple
If the transaction was unsuccessful, return the response in the following format, followed by an explanation if any known:
The transaction failed.
`,
);

Expand Down
174 changes: 91 additions & 83 deletions src/mira/addLiquidity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,93 +20,101 @@ export const addLiquidity = async (
params: AddLiquidityParams,
privateKey: string,
) => {
const { wallet, provider } = await setupWallet(privateKey);
const assets = await getAllVerifiedFuelAssets();

const asset0 = assets.find((asset) => asset.symbol === params.asset0Symbol);
const asset1 = assets.find((asset) => asset.symbol === params.asset1Symbol);

if (!asset0 || !asset1) {
throw new Error(
`Asset ${params.asset0Symbol} or ${params.asset1Symbol} not found`,
try {
const { wallet, provider } = await setupWallet(privateKey);
const assets = await getAllVerifiedFuelAssets();

const asset0 = assets.find((asset) => asset.symbol === params.asset0Symbol);
const asset1 = assets.find((asset) => asset.symbol === params.asset1Symbol);

if (!asset0 || !asset1) {
throw new Error(
`Asset ${params.asset0Symbol} or ${params.asset1Symbol} not found`,
);
}

let isStable =
asset0.symbol.includes('USD') && asset1.symbol.includes('USD');
const asset0Id = asset0.assetId;
const asset1Id = asset1.assetId;

if (!asset0Id || !asset1Id) {
throw new Error('Invalid asset IDs');
}

const asset0Decimals = asset0.decimals;
const asset1Decimals = asset1.decimals;

if (
typeof asset0Decimals !== 'number' ||
typeof asset1Decimals !== 'number'
) {
throw new Error('Invalid asset decimals');
}

const amount0InWei = bn.parseUnits(params.amount0, asset0Decimals);

const poolId = buildPoolId(asset0Id, asset1Id, isStable);

const miraAmm = new MiraAmm(wallet);
const miraAmmReader = new ReadonlyMiraAmm(provider);
const deadline = await futureDeadline(provider);

// Calculate the required amount1 based on amount0
const result = await miraAmmReader.getAmountsOut(
{ bits: asset0Id },
amount0InWei,
[poolId],
);
}

let isStable = asset0.symbol.includes('USD') && asset1.symbol.includes('USD');
const asset0Id = asset0.assetId;
const asset1Id = asset1.assetId;

if (!asset0Id || !asset1Id) {
throw new Error('Invalid asset IDs');
}

const asset0Decimals = asset0.decimals;
const asset1Decimals = asset1.decimals;

if (
typeof asset0Decimals !== 'number' ||
typeof asset1Decimals !== 'number'
) {
throw new Error('Invalid asset decimals');
}

const amount0InWei = bn.parseUnits(params.amount0, asset0Decimals);

const poolId = buildPoolId(asset0Id, asset1Id, isStable);

const miraAmm = new MiraAmm(wallet);
const miraAmmReader = new ReadonlyMiraAmm(provider);
const deadline = await futureDeadline(provider);

// Calculate the required amount1 based on amount0
const result = await miraAmmReader.getAmountsOut(
{ bits: asset0Id },
amount0InWei,
[poolId],
);
if (!result || result.length === 0 || !result[0] || result[0].length < 2) {
throw new Error('Failed to calculate amount1');
}

let amount1InWei;
if (result[1] && result[1][0].bits === asset1Id) {
amount1InWei = result[1][1];
} else if (result[0][0].bits === asset1Id) {
amount1InWei = result[0][1];
}

if (!amount1InWei) {
throw new Error('Failed to calculate amount1');
}

// Calculate minimum amounts with slippage
const minAmount0 = amount0InWei
.mul(bn(100 - Math.floor((params.slippage || DEFAULT_SLIPPAGE) * 100)))
.div(bn(100));
const minAmount1 = amount1InWei
.mul(bn(100 - Math.floor((params.slippage || DEFAULT_SLIPPAGE) * 100)))
.div(bn(100));

const req = await miraAmm.addLiquidity(
poolId,
amount0InWei,
amount1InWei,
minAmount0,
minAmount1,
deadline,
{
gasLimit: 1000000,
maxFee: 1000000,
},
);

if (!result || result.length === 0 || !result[0] || result[0].length < 2) {
throw new Error('Failed to calculate amount1');
}
const tx = await wallet.sendTransaction(req);

let amount1InWei;
if (result[1] && result[1][0].bits === asset1Id) {
amount1InWei = result[1][1];
} else if (result[0][0].bits === asset1Id) {
amount1InWei = result[0][1];
}
const { id, status } = await tx.waitForResult();

if (!amount1InWei) {
throw new Error('Failed to calculate amount1');
return {
status,
id,
};
} catch (error) {
return JSON.stringify({
status: 'failure',
error: error instanceof Error ? error.message : 'Unknown error',
});
}

// Calculate minimum amounts with slippage
const minAmount0 = amount0InWei
.mul(bn(100 - Math.floor((params.slippage || DEFAULT_SLIPPAGE) * 100)))
.div(bn(100));
const minAmount1 = amount1InWei
.mul(bn(100 - Math.floor((params.slippage || DEFAULT_SLIPPAGE) * 100)))
.div(bn(100));

const req = await miraAmm.addLiquidity(
poolId,
amount0InWei,
amount1InWei,
minAmount0,
minAmount1,
deadline,
{
gasLimit: 1000000,
maxFee: 1000000,
},
);

const tx = await wallet.sendTransaction(req);

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

return {
status,
id,
};
};
Loading

0 comments on commit f6b9d28

Please sign in to comment.