-
Notifications
You must be signed in to change notification settings - Fork 13
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
SOR - Assert behavior for tokens with 0 decimals #1498
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2cf910f
SOR - Assert behavior for tokens with 0 decimals
brunoguerios a9b607d
Attempt to fix build issue on CI
brunoguerios 5b2ffcb
Another attempt to fix build on CI
brunoguerios 4821240
Add extra integration test with return amount > 0
brunoguerios File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'backend': patch | ||
--- | ||
|
||
SOR - Assert behavior for tokens with 0 decimals |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
modules/sor/sorV2/lib/poolsV2/balancer-v2-sor.integration.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
// yarn vitest balancer-v2-sor.integration.test.ts | ||
|
||
import { ExactInQueryOutput, Swap, SwapKind, Token, Address } from '@balancer/sdk'; | ||
|
||
import { PathWithAmount } from '../path'; | ||
import { sorGetPathsWithPools } from '../static'; | ||
import { getOutputAmount } from '../utils/helpers'; | ||
import { chainToChainId as chainToIdMap } from '../../../../network/chain-id-to-chain'; | ||
|
||
import { ANVIL_NETWORKS, startFork } from '../../../../../test/anvil/anvil-global-setup'; | ||
import { prismaPoolDynamicDataFactory, prismaPoolFactory, prismaPoolTokenFactory } from '../../../../../test/factories'; | ||
import { createTestClient, Hex, http, TestClient } from 'viem'; | ||
import { gnosis } from 'viem/chains'; | ||
import { PrismaPoolAndHookWithDynamic } from '../../../../../prisma/prisma-types'; | ||
|
||
/** | ||
* Test Data: | ||
* | ||
* In order to properly compare SOR quotes vs SDK queries, we need to setup test data from a specific blockNumber. | ||
* Although the API does not provide that functionality, we can use subgraph to achieve it. | ||
* These tests run against [BalancerV2 subgraph](https://thegraph.com/explorer/subgraphs/C4ayEZP2yTXRAB8vSaTrgN4m9anTe9Mdm2ViyiAuV9TV?view=Query) | ||
* TODO: improve test data setup by creating a script that fetches all necessary data automatically for a given blockNumber. | ||
*/ | ||
|
||
const protocolVersion = 2; | ||
|
||
describe('Balancer V2 SOR Integration Tests', () => { | ||
let rpcUrl: string; | ||
let paths: PathWithAmount[]; | ||
let sdkSwap: Swap; | ||
let snapshot: Hex; | ||
let client: TestClient; | ||
|
||
beforeAll(async () => { | ||
// start fork to run queries against | ||
({ rpcUrl } = await startFork(ANVIL_NETWORKS.GNOSIS_CHAIN)); | ||
client = createTestClient({ | ||
mode: 'anvil', | ||
chain: gnosis, | ||
transport: http(rpcUrl), | ||
}); | ||
snapshot = await client.snapshot(); | ||
}); | ||
|
||
beforeEach(async () => { | ||
await client.revert({ | ||
id: snapshot, | ||
}); | ||
snapshot = await client.snapshot(); | ||
}); | ||
|
||
describe('Weighted Pool Path - Token with 0 decimals', () => { | ||
let prismaWeightedPool: PrismaPoolAndHookWithDynamic; | ||
let tIn: Token; | ||
let tOut: Token; | ||
|
||
beforeAll(async () => { | ||
// setup mock pool data | ||
const wxDAI = prismaPoolTokenFactory.build({ | ||
address: '0xe91d153e0b41518a2ce8dd3d7944fa863463a97d', | ||
balance: '2110.269380198644452506', | ||
weight: '0.5', | ||
}); | ||
const MPS = prismaPoolTokenFactory.build({ | ||
address: '0xfa57aa7beed63d03aaf85ffd1753f5f6242588fb', | ||
balance: '356', | ||
weight: '0.5', | ||
token: { | ||
decimals: 0, | ||
}, | ||
}); | ||
prismaWeightedPool = prismaPoolFactory.build({ | ||
id: '0x4bcf6b48906fa0f68bea1fc255869a41241d4851000200000000000000000021', | ||
address: '0x4bcf6b48906fa0f68bea1fc255869a41241d4851', | ||
type: 'WEIGHTED', | ||
protocolVersion, | ||
tokens: [wxDAI, MPS], | ||
dynamicData: prismaPoolDynamicDataFactory.build({ | ||
totalShares: '1584.613732317989225757', | ||
swapFee: '0.03', | ||
}), | ||
chain: 'GNOSIS', | ||
}); | ||
|
||
tIn = new Token(parseFloat(chainToIdMap['GNOSIS']), wxDAI.address as Address, wxDAI.token.decimals); | ||
tOut = new Token(parseFloat(chainToIdMap['GNOSIS']), MPS.address as Address, MPS.token.decimals); | ||
}); | ||
|
||
test('SOR quote should match swap query - below min', async () => { | ||
// get SOR paths | ||
const amountIn = BigInt(1e18); | ||
|
||
paths = (await sorGetPathsWithPools( | ||
tIn, | ||
tOut, | ||
SwapKind.GivenIn, | ||
amountIn, | ||
[prismaWeightedPool], | ||
protocolVersion, | ||
)) as PathWithAmount[]; | ||
|
||
// build SDK swap from SOR paths | ||
sdkSwap = new Swap({ | ||
chainId: parseFloat(chainToIdMap['GNOSIS']), | ||
paths: paths.map((path) => ({ | ||
protocolVersion, | ||
inputAmountRaw: path.inputAmount.amount, | ||
outputAmountRaw: path.outputAmount.amount, | ||
tokens: path.tokens.map((token) => ({ | ||
address: token.address, | ||
decimals: token.decimals, | ||
})), | ||
pools: path.pools.map((pool) => pool.id), | ||
})), | ||
swapKind: SwapKind.GivenIn, | ||
}); | ||
|
||
const returnAmountSOR = getOutputAmount(paths); | ||
const queryOutput = await sdkSwap.query(rpcUrl); | ||
const returnAmountQuery = (queryOutput as ExactInQueryOutput).expectedAmountOut; | ||
expect(returnAmountQuery.amount).toEqual(returnAmountSOR.amount); | ||
}); | ||
|
||
test('SOR quote should match swap query', async () => { | ||
// get SOR paths | ||
const amountIn = BigInt(10e18); | ||
|
||
paths = (await sorGetPathsWithPools( | ||
tIn, | ||
tOut, | ||
SwapKind.GivenIn, | ||
amountIn, | ||
[prismaWeightedPool], | ||
protocolVersion, | ||
)) as PathWithAmount[]; | ||
|
||
// build SDK swap from SOR paths | ||
sdkSwap = new Swap({ | ||
chainId: parseFloat(chainToIdMap['GNOSIS']), | ||
paths: paths.map((path) => ({ | ||
protocolVersion, | ||
inputAmountRaw: path.inputAmount.amount, | ||
outputAmountRaw: path.outputAmount.amount, | ||
tokens: path.tokens.map((token) => ({ | ||
address: token.address, | ||
decimals: token.decimals, | ||
})), | ||
pools: path.pools.map((pool) => pool.id), | ||
})), | ||
swapKind: SwapKind.GivenIn, | ||
}); | ||
|
||
const returnAmountSOR = getOutputAmount(paths); | ||
const queryOutput = await sdkSwap.query(rpcUrl); | ||
const returnAmountQuery = (queryOutput as ExactInQueryOutput).expectedAmountOut; | ||
expect(returnAmountQuery.amount).toEqual(returnAmountSOR.amount); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { stopAnvilForks } from './anvil/anvil-global-setup'; | ||
|
||
afterAll(async () => { | ||
await stopAnvilForks(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,8 @@ | |
"resolveJsonModule": true, | ||
"outDir": "./dist", | ||
"skipLibCheck": true, | ||
"sourceMap": true | ||
"sourceMap": true, | ||
"types": ["vitest/globals"], | ||
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. Makes working with tests easier. |
||
}, | ||
"exclude": ["node_modules", "debug", "**/*.spec.ts", "**/*.test.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Based on the issue description this is the test which ensures there is no issue in the sdk.