Skip to content

Commit f6b9d28

Browse files
authored
Merge pull request #34 from FuelLabs/dp/handle-errors-gracefully
add better error handling
2 parents 70132de + e5cec63 commit f6b9d28

File tree

11 files changed

+415
-329
lines changed

11 files changed

+415
-329
lines changed

src/FuelAgent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export interface FuelAgentConfig {
2222
googleGeminiApiKey?: string;
2323
}
2424

25-
export class FuelAgent {
25+
export class FuelAgent {
2626
private walletPrivateKey: string;
2727
private agentExecutor: AgentExecutor;
2828
private model: keyof typeof modelMapping;

src/agent.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ const systemMessage = new SystemMessage(
1111
` You are an AI agent on Fuel network capable of executing all kinds of transactions and interacting with the Fuel blockchain.
1212
You are able to execute transactions on behalf of the user.
1313
14-
Always return the response in the following format:
15-
The transaction was successful/failed. The explorer link is: https://app.fuel.network/tx/0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef/simple
14+
If the transaction was successful, return the response in the following format:
15+
The transaction was successful. The explorer link is: https://app.fuel.network/tx/0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef/simple
16+
17+
If the transaction was unsuccessful, return the response in the following format, followed by an explanation if any known:
18+
The transaction failed.
1619
`,
1720
);
1821

src/mira/addLiquidity.ts

Lines changed: 91 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -20,93 +20,101 @@ export const addLiquidity = async (
2020
params: AddLiquidityParams,
2121
privateKey: string,
2222
) => {
23-
const { wallet, provider } = await setupWallet(privateKey);
24-
const assets = await getAllVerifiedFuelAssets();
25-
26-
const asset0 = assets.find((asset) => asset.symbol === params.asset0Symbol);
27-
const asset1 = assets.find((asset) => asset.symbol === params.asset1Symbol);
28-
29-
if (!asset0 || !asset1) {
30-
throw new Error(
31-
`Asset ${params.asset0Symbol} or ${params.asset1Symbol} not found`,
23+
try {
24+
const { wallet, provider } = await setupWallet(privateKey);
25+
const assets = await getAllVerifiedFuelAssets();
26+
27+
const asset0 = assets.find((asset) => asset.symbol === params.asset0Symbol);
28+
const asset1 = assets.find((asset) => asset.symbol === params.asset1Symbol);
29+
30+
if (!asset0 || !asset1) {
31+
throw new Error(
32+
`Asset ${params.asset0Symbol} or ${params.asset1Symbol} not found`,
33+
);
34+
}
35+
36+
let isStable =
37+
asset0.symbol.includes('USD') && asset1.symbol.includes('USD');
38+
const asset0Id = asset0.assetId;
39+
const asset1Id = asset1.assetId;
40+
41+
if (!asset0Id || !asset1Id) {
42+
throw new Error('Invalid asset IDs');
43+
}
44+
45+
const asset0Decimals = asset0.decimals;
46+
const asset1Decimals = asset1.decimals;
47+
48+
if (
49+
typeof asset0Decimals !== 'number' ||
50+
typeof asset1Decimals !== 'number'
51+
) {
52+
throw new Error('Invalid asset decimals');
53+
}
54+
55+
const amount0InWei = bn.parseUnits(params.amount0, asset0Decimals);
56+
57+
const poolId = buildPoolId(asset0Id, asset1Id, isStable);
58+
59+
const miraAmm = new MiraAmm(wallet);
60+
const miraAmmReader = new ReadonlyMiraAmm(provider);
61+
const deadline = await futureDeadline(provider);
62+
63+
// Calculate the required amount1 based on amount0
64+
const result = await miraAmmReader.getAmountsOut(
65+
{ bits: asset0Id },
66+
amount0InWei,
67+
[poolId],
3268
);
33-
}
34-
35-
let isStable = asset0.symbol.includes('USD') && asset1.symbol.includes('USD');
36-
const asset0Id = asset0.assetId;
37-
const asset1Id = asset1.assetId;
38-
39-
if (!asset0Id || !asset1Id) {
40-
throw new Error('Invalid asset IDs');
41-
}
42-
43-
const asset0Decimals = asset0.decimals;
44-
const asset1Decimals = asset1.decimals;
45-
46-
if (
47-
typeof asset0Decimals !== 'number' ||
48-
typeof asset1Decimals !== 'number'
49-
) {
50-
throw new Error('Invalid asset decimals');
51-
}
52-
53-
const amount0InWei = bn.parseUnits(params.amount0, asset0Decimals);
54-
55-
const poolId = buildPoolId(asset0Id, asset1Id, isStable);
56-
57-
const miraAmm = new MiraAmm(wallet);
58-
const miraAmmReader = new ReadonlyMiraAmm(provider);
59-
const deadline = await futureDeadline(provider);
6069

61-
// Calculate the required amount1 based on amount0
62-
const result = await miraAmmReader.getAmountsOut(
63-
{ bits: asset0Id },
64-
amount0InWei,
65-
[poolId],
66-
);
70+
if (!result || result.length === 0 || !result[0] || result[0].length < 2) {
71+
throw new Error('Failed to calculate amount1');
72+
}
73+
74+
let amount1InWei;
75+
if (result[1] && result[1][0].bits === asset1Id) {
76+
amount1InWei = result[1][1];
77+
} else if (result[0][0].bits === asset1Id) {
78+
amount1InWei = result[0][1];
79+
}
80+
81+
if (!amount1InWei) {
82+
throw new Error('Failed to calculate amount1');
83+
}
84+
85+
// Calculate minimum amounts with slippage
86+
const minAmount0 = amount0InWei
87+
.mul(bn(100 - Math.floor((params.slippage || DEFAULT_SLIPPAGE) * 100)))
88+
.div(bn(100));
89+
const minAmount1 = amount1InWei
90+
.mul(bn(100 - Math.floor((params.slippage || DEFAULT_SLIPPAGE) * 100)))
91+
.div(bn(100));
92+
93+
const req = await miraAmm.addLiquidity(
94+
poolId,
95+
amount0InWei,
96+
amount1InWei,
97+
minAmount0,
98+
minAmount1,
99+
deadline,
100+
{
101+
gasLimit: 1000000,
102+
maxFee: 1000000,
103+
},
104+
);
67105

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

72-
let amount1InWei;
73-
if (result[1] && result[1][0].bits === asset1Id) {
74-
amount1InWei = result[1][1];
75-
} else if (result[0][0].bits === asset1Id) {
76-
amount1InWei = result[0][1];
77-
}
108+
const { id, status } = await tx.waitForResult();
78109

79-
if (!amount1InWei) {
80-
throw new Error('Failed to calculate amount1');
110+
return {
111+
status,
112+
id,
113+
};
114+
} catch (error) {
115+
return JSON.stringify({
116+
status: 'failure',
117+
error: error instanceof Error ? error.message : 'Unknown error',
118+
});
81119
}
82-
83-
// Calculate minimum amounts with slippage
84-
const minAmount0 = amount0InWei
85-
.mul(bn(100 - Math.floor((params.slippage || DEFAULT_SLIPPAGE) * 100)))
86-
.div(bn(100));
87-
const minAmount1 = amount1InWei
88-
.mul(bn(100 - Math.floor((params.slippage || DEFAULT_SLIPPAGE) * 100)))
89-
.div(bn(100));
90-
91-
const req = await miraAmm.addLiquidity(
92-
poolId,
93-
amount0InWei,
94-
amount1InWei,
95-
minAmount0,
96-
minAmount1,
97-
deadline,
98-
{
99-
gasLimit: 1000000,
100-
maxFee: 1000000,
101-
},
102-
);
103-
104-
const tx = await wallet.sendTransaction(req);
105-
106-
const { id, status } = await tx.waitForResult();
107-
108-
return {
109-
status,
110-
id,
111-
};
112120
};

0 commit comments

Comments
 (0)