-
Notifications
You must be signed in to change notification settings - Fork 532
security: fix swapped transfer amounts + broken invariant check in token-swap #690
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -92,4 +92,36 @@ describe('Swap', () => { | |||||||||||||||||||||||||
| values.defaultSupply.sub(values.depositAmountB).add(input).toNumber(), | ||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| it('Swap from B to A', async () => { | ||||||||||||||||||||||||||
| const input = new anchor.BN(3 * 10 ** 6); | ||||||||||||||||||||||||||
| await program.methods | ||||||||||||||||||||||||||
| .swapExactTokensForTokens(false, input, new anchor.BN(100)) | ||||||||||||||||||||||||||
| .accountsPartial({ | ||||||||||||||||||||||||||
| amm: values.ammKey, | ||||||||||||||||||||||||||
| pool: values.poolKey, | ||||||||||||||||||||||||||
| poolAuthority: values.poolAuthority, | ||||||||||||||||||||||||||
| trader: values.admin.publicKey, | ||||||||||||||||||||||||||
| mintA: values.mintAKeypair.publicKey, | ||||||||||||||||||||||||||
| mintB: values.mintBKeypair.publicKey, | ||||||||||||||||||||||||||
| poolAccountA: values.poolAccountA, | ||||||||||||||||||||||||||
| poolAccountB: values.poolAccountB, | ||||||||||||||||||||||||||
| traderAccountA: values.holderAccountA, | ||||||||||||||||||||||||||
| traderAccountB: values.holderAccountB, | ||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||
| .signers([values.admin]) | ||||||||||||||||||||||||||
| .rpc({ skipPreflight: true }); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const traderTokenAccountA = await connection.getTokenAccountBalance(values.holderAccountA); | ||||||||||||||||||||||||||
| const traderTokenAccountB = await connection.getTokenAccountBalance(values.holderAccountB); | ||||||||||||||||||||||||||
| expect(traderTokenAccountB.value.amount).to.equal( | ||||||||||||||||||||||||||
| values.defaultSupply.sub(values.depositAmountB).sub(input).toString(), | ||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||
| expect(Number(traderTokenAccountA.value.amount)).to.be.greaterThan( | ||||||||||||||||||||||||||
| values.defaultSupply.sub(values.depositAmountA).toNumber(), | ||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||
| expect(Number(traderTokenAccountA.value.amount)).to.be.lessThan( | ||||||||||||||||||||||||||
| values.defaultSupply.sub(values.depositAmountA).add(input).toNumber(), | ||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The broad balance range accepts any positive A output below
Suggested change
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
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.
should use checked_ math for better handling of overflow