-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
feat(new-action): paraswap api based swap action for evm plugin #2820
base: develop
Are you sure you want to change the base?
Conversation
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @dxganta! Welcome to the elizaOS community. Thanks for submitting your first pull request; your efforts are helping us accelerate towards AGI. We'll review it shortly. You are now an elizaOS contributor!
const amount = ( | ||
Number(params.amount) * Math.pow(10, inputTokenDecimals) | ||
).toString(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using Number
for token amount calculations risks precision loss, particularly with large values or tokens with many decimals. Consider using BigInt
instead:
const amount = (BigInt(params.amount) * BigInt(10 ** inputTokenDecimals)).toString();
This ensures precise calculations across the full range of possible token amounts.
Spotted by Graphite Reviewer
Is this helpful? React 👍 or 👎 to let us know.
async approveTokenIfNeeded( | ||
amount: string, | ||
tokenAddress: string, | ||
spenderAddress: string, | ||
callback?: any | ||
): Promise<Hash | null> { | ||
try { | ||
const currentChain = this.walletProvider | ||
.getCurrentChain() | ||
.name.toLowerCase() as SupportedChain; | ||
const walletClient = | ||
this.walletProvider.getWalletClient(currentChain); | ||
const publicClient = | ||
this.walletProvider.getPublicClient(currentChain); | ||
const amountBigInt = BigInt(amount); | ||
|
||
const allowance = await publicClient.readContract({ | ||
address: tokenAddress as Address, | ||
abi: this.ERC20_ABI, | ||
functionName: "allowance", | ||
args: [ | ||
this.walletProvider.getAddress(), | ||
spenderAddress as Address, | ||
], | ||
}); | ||
|
||
if (allowance >= amountBigInt) { | ||
return null; | ||
} | ||
|
||
const hash = await walletClient.writeContract({ | ||
address: tokenAddress as Address, | ||
abi: this.ERC20_ABI, | ||
functionName: "approve", | ||
args: [spenderAddress as Address, amountBigInt], | ||
account: walletClient.account, | ||
chain: this.walletProvider.getCurrentChain(), | ||
}); | ||
|
||
if (callback) { | ||
callback({ | ||
text: `Successfully approved ${amount} tokens. Hash: ${hash}`, | ||
}); | ||
} | ||
} catch (error) { | ||
if (callback) { | ||
callback({ | ||
text: `Token Approval Error: ${error.message}`, | ||
}); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The approveTokenIfNeeded
method is declared to return Promise<Hash | null>
but doesn't return anything in the catch
block, which could lead to undefined
being returned instead. To maintain the declared return type, add return null
at the end of the catch
block:
catch (error) {
if (callback) {
callback({
text: `Token Approval Error: ${error.message}`
});
}
return null;
}
Spotted by Graphite Reviewer
Is this helpful? React 👍 or 👎 to let us know.
How to use the Graphite Merge QueueAdd either label to this PR to merge it via the merge queue:
You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. |
can you work within how this is implemented-
|
The swap action currently being used for the evm-plugin using LiFi is not working.
Link to Issue
Risks
Medium.
One of the main reasons someone will use the evm plugin is to swap tokens and if that is only not working what use is the plugin.
Background
I was trying out the evm plugin. Had a lot of bugs. Couldn't fix them.
What does this PR do?
It adds a new swap action page, which uses Paraswap API for swapping tokens. Supports swapping both native & ERC20 tokens. Also handles approval of tokens.
Before
After
What kind of change is this?
Features (non-breaking change which adds functionality)
Documentation changes needed?
My changes do not require a change to the project documentation.
Testing
Where should a reviewer start?
start the agent
ask the agent to swap tokens.
E.g: swap 2 usdc to dai
swap 0.01 eth to usdc
swap 2 usdc to weth