Skip to content

Commit

Permalink
uniswap LA build bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
spacesailor24 committed Dec 19, 2024
1 parent 4066028 commit 78b4546
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 26 deletions.
44 changes: 19 additions & 25 deletions packages/lit-agent-tool-uniswap/src/litAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ export default async () => {
tokenIn = ethers.utils.getAddress(params.tokenIn);
tokenOut = ethers.utils.getAddress(params.tokenOut);
} catch (error) {
throw new Error(`Invalid token address: ${error.message}`);
throw new Error(
`Invalid token address: ${error instanceof Error ? error.message : String(error)}`
);
}

const UNISWAP_V3_ROUTER = "0x2626664c2603336E57B271c5C0b26F421741e481";
Expand Down Expand Up @@ -130,7 +132,7 @@ export default async () => {
LitActions.setResponse({
response: JSON.stringify({
status: "error",
error: `Failed to decode policy: ${error.message}`,
error: `Failed to decode policy: ${error instanceof Error ? error.message : String(error)}`,
}),
});
throw error;
Expand Down Expand Up @@ -197,23 +199,15 @@ export default async () => {

const amountOutMin = ethers.BigNumber.from(0);

// Get fee data for EIP-1559 transaction
const feeData = await ethersProvider.getFeeData();
if (!feeData.maxFeePerGas || !feeData.maxPriorityFeePerGas) {
throw new Error("Failed to get fee data from provider");
}

const approvalTx = {
type: 2, // EIP-1559
to: normalizedTokenIn,
data: tokenInterface.encodeFunctionData("approve", [
UNISWAP_V3_ROUTER,
amountIn,
]),
value: "0x0",
gasLimit: ethers.utils.hexlify(100000),
maxFeePerGas: feeData.maxFeePerGas.mul(2).toHexString(), // Double the suggested fee
maxPriorityFeePerGas: feeData.maxPriorityFeePerGas.mul(2).toHexString(), // Double the suggested priority fee
gasPrice: (await ethersProvider.getGasPrice()).mul(2).toHexString(),
nonce: await ethersProvider.getTransactionCount(pkpEthAddress),
chainId: chainInfo.chainId,
};
Expand All @@ -228,7 +222,7 @@ export default async () => {
LitActions.setResponse({
response: JSON.stringify({
status: "error",
error: `Invalid approval transaction: ${error.message}`,
error: `Invalid approval transaction: ${error instanceof Error ? error.message : String(error)}`,
}),
});
throw error;
Expand All @@ -248,7 +242,7 @@ export default async () => {
LitActions.setResponse({
response: JSON.stringify({
status: "error",
error: `Error signing approval transaction: ${error.message}`,
error: `Error signing approval transaction: ${error instanceof Error ? error.message : String(error)}`,
}),
});
throw error;
Expand All @@ -270,7 +264,7 @@ export default async () => {
LitActions.setResponse({
response: JSON.stringify({
status: "error",
error: `Error serializing signed approval transaction: ${error.message}`,
error: `Error serializing signed approval transaction: ${error instanceof Error ? error.message : String(error)}`,
}),
});
throw error;
Expand All @@ -294,7 +288,7 @@ export default async () => {
} catch (error) {
console.log("Error sending approval transaction:", error);
throw new Error(
`Error sending approval transaction: ${error.message}`
`Error sending approval transaction: ${error instanceof Error ? error.message : String(error)}`
);
}
}
Expand All @@ -308,7 +302,7 @@ export default async () => {
LitActions.setResponse({
response: JSON.stringify({
status: "error",
error: `Error in approval transaction: ${error.message}`,
error: `Error in approval transaction: ${error instanceof Error ? error.message : String(error)}`,
}),
});
throw error;
Expand All @@ -328,7 +322,7 @@ export default async () => {
LitActions.setResponse({
response: JSON.stringify({
status: "error",
error: `Error confirming approval transaction: ${error.message}`,
error: `Error confirming approval transaction: ${error instanceof Error ? error.message : String(error)}`,
}),
});
throw error;
Expand All @@ -349,15 +343,13 @@ export default async () => {
];

const swapTx = {
type: 2, // EIP-1559
to: UNISWAP_V3_ROUTER,
data: routerInterface.encodeFunctionData("exactInputSingle", [
swapParamsArray,
]),
value: "0x0",
gasLimit: ethers.utils.hexlify(200000),
maxFeePerGas: feeData.maxFeePerGas.mul(2).toHexString(), // Double the suggested fee
maxPriorityFeePerGas: feeData.maxPriorityFeePerGas.mul(2).toHexString(), // Double the suggested priority fee
gasPrice: (await ethersProvider.getGasPrice()).mul(2).toHexString(),
nonce: await ethersProvider.getTransactionCount(pkpEthAddress),
chainId: chainInfo.chainId,
};
Expand All @@ -370,7 +362,7 @@ export default async () => {
LitActions.setResponse({
response: JSON.stringify({
status: "error",
error: `Invalid swap transaction: ${error.message}`,
error: `Invalid swap transaction: ${error instanceof Error ? error.message : String(error)}`,
}),
});
throw error;
Expand All @@ -390,7 +382,7 @@ export default async () => {
LitActions.setResponse({
response: JSON.stringify({
status: "error",
error: `Error signing swap transaction: ${error.message}`,
error: `Error signing swap transaction: ${error instanceof Error ? error.message : String(error)}`,
}),
});
throw error;
Expand All @@ -412,7 +404,7 @@ export default async () => {
LitActions.setResponse({
response: JSON.stringify({
status: "error",
error: `Error serializing signed swap transaction: ${error.message}`,
error: `Error serializing signed swap transaction: ${error instanceof Error ? error.message : String(error)}`,
}),
});
throw error;
Expand All @@ -434,7 +426,9 @@ export default async () => {
return swapReceipt.hash;
} catch (error) {
console.log("Error sending swap transaction:", error);
throw new Error(`Error sending swap transaction: ${error.message}`);
throw new Error(
`Error sending swap transaction: ${error instanceof Error ? error.message : String(error)}`
);
}
}
);
Expand All @@ -447,7 +441,7 @@ export default async () => {
LitActions.setResponse({
response: JSON.stringify({
status: "error",
error: `Error in swap transaction: ${error.message}`,
error: `Error in swap transaction: ${error instanceof Error ? error.message : String(error)}`,
}),
});
throw error;
Expand Down
2 changes: 1 addition & 1 deletion packages/lit-agent-tool-uniswap/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
"declaration": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist", "src/litAction.ts"]
"exclude": ["node_modules", "dist"]
}

0 comments on commit 78b4546

Please sign in to comment.