-
Notifications
You must be signed in to change notification settings - Fork 184
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
Add swapToRatio endpoint #8
Conversation
|
||
const SUPPORTED_CHAINS: ChainId[] = [ ChainId.MAINNET, ChainId.RINKEBY ]; | ||
|
||
const DEFAULT_TOKEN_LIST = 'https://gateway.ipfs.io/ipns/tokens.uniswap.org'; |
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.
assuming it's the same list, this could be import DEFAULT_TOKEN_LIST from '@uniswap/default-token-list';
so it's more future-proof
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.
Fine with this since the list is never really updated
import { StaticGasPriceProvider } from '../router-entities/static-gas-price-provider'; | ||
import { QuoteToRatioQueryParams } from './schema/quote-to-ratio-schema'; | ||
|
||
const SUPPORTED_CHAINS: ChainId[] = [ ChainId.MAINNET, ChainId.RINKEBY ]; |
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.
is this intentially supporting a different set of chains than the quoter?
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.
so a lot of my PR right now is copy-paste, to get the endpoint working, then I will DRY all the shared code. I'm a bit fire-hosed right now, but a lot of this is actually @willpote 's code, so he might be able to answer these questions better.
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 quote endpoint on the API also only supports mainnet and rinkeby right now. We have a task to do arbitrary network support that will come soon
const DEFAULT_TOKEN_LIST = 'https://gateway.ipfs.io/ipns/tokens.uniswap.org'; | ||
|
||
export type ContainerDependencies = { | ||
provider: ethers.providers.JsonRpcProvider; |
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.
does the provider need to be a jsonrpc as opposed to baseprovider
?
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.
It does since we are using the EIP1559 gas provider that uses eth_feehistory
package.json
Outdated
@@ -76,7 +76,7 @@ | |||
"@types/qs": "^6.9.7", | |||
"@types/stats-lite": "^2.2.0", | |||
"@uniswap/sdk-core": "^3.0.1", | |||
"@uniswap/smart-order-router": "1.44.1", | |||
"@uniswap/smart-order-router": "1.46.3", |
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.
this should probably be ^1.46.3
so dependencies can be resolved better next time there's a conflict
tsconfig.json
Outdated
@@ -12,7 +12,7 @@ | |||
"strictNullChecks": true, | |||
"noImplicitThis": true, | |||
"alwaysStrict": true, | |||
"noUnusedLocals": true /* Report errors on unused locals. */, | |||
"noUnusedLocals": false /* Report errors on unused locals. */, |
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.
TODO: don't merge this
lib/handlers/handler.ts
Outdated
log.info('event information', { | ||
event | ||
}) | ||
|
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.
Still need these logs?
import { StaticGasPriceProvider } from '../router-entities/static-gas-price-provider'; | ||
import { QuoteToRatioQueryParams } from './schema/quote-to-ratio-schema'; | ||
|
||
const SUPPORTED_CHAINS: ChainId[] = [ ChainId.MAINNET, ChainId.RINKEBY ]; |
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 quote endpoint on the API also only supports mainnet and rinkeby right now. We have a task to do arbitrary network support that will come soon
const DEFAULT_TOKEN_LIST = 'https://gateway.ipfs.io/ipns/tokens.uniswap.org'; | ||
|
||
export type ContainerDependencies = { | ||
provider: ethers.providers.JsonRpcProvider; |
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.
It does since we are using the EIP1559 gas provider that uses eth_feehistory
|
||
const SUPPORTED_CHAINS: ChainId[] = [ ChainId.MAINNET, ChainId.RINKEBY ]; | ||
|
||
const DEFAULT_TOKEN_LIST = 'https://gateway.ipfs.io/ipns/tokens.uniswap.org'; |
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.
Fine with this since the list is never really updated
test/integ/quote-to-ratio.test.ts
Outdated
deadline: '360', | ||
errorTolerance: 1, | ||
maxIterations: 6, |
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.
Indenting looks dodgy
@@ -0,0 +1,66 @@ | |||
import { AlphaRouterConfig } from '@uniswap/smart-order-router'; |
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.
mmm open to ideas on where to put the two shared items below
successfully outsourced a few types to a shared injector create injectorSOR get rid of unused dependencies DRYing handlers
token0.wrapped, | ||
token1.wrapped, | ||
feeAmount | ||
) as unknown as Pool |
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 casting shouldn't be needed, any idea whats going on?
log.error( | ||
`Could not find pool.` | ||
); | ||
return { statusCode: 400, errorCode: 'Could not find pool.' }; |
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.
Error code should be something like POOL_NOT_FOUND
. You can use detail
field for 'Could not find pool'
pool, | ||
tickLower, | ||
tickUpper, | ||
liquidity: 1, |
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.
Does setting liquidity to 1 effect anything?
In any case, feels like we should probably use: https://github.com/Uniswap/v3-sdk/blob/main/src/entities/position.ts#L312-L392
to get the correct liquidity value
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.
hmm...it's weird because liquidity plays absolutely no role in any calculation. we define a position to get the bounds, in many cases this will be a hypothetical position with no liquidity yet (which we would then go to create with the swap and add amounts which we don't know yet.
log.info( | ||
{ | ||
token0: token0.symbol, | ||
token1: token1.symbol, | ||
routingConfig: routingConfig, | ||
}, | ||
`Swap to ratio - token0: ${ | ||
token0.symbol | ||
}, token0Balance: ${ | ||
token0Balance | ||
}, token1: ${ | ||
token1.symbol | ||
}. token1Balance: ${ | ||
token1Balance | ||
}, Chain: ${chainId}` | ||
); | ||
|
||
log.info({ | ||
token0Balance, | ||
token1Balance, | ||
position, | ||
maxIterations, | ||
errorTolerance: errorToleranceFraction.toFixed(4), | ||
swapParams, | ||
routingConfig, | ||
}, 'more useful logs') |
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.
Could you merge these? Best to minimize number of logs
numerator: Joi.string(), | ||
denominator: Joi.string(), | ||
}).required(), | ||
postSwapTargetPool: Joi.any().required(), |
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.
Missing validation
import { QuoteToRatioQueryParams } from './schema/quote-to-ratio-schema'; | ||
import { ContainerInjected, InjectorSOR, RequestInjected } from '../injector-sor'; | ||
|
||
export class QuoteToRatioHandlerInjector extends InjectorSOR<ISwapToRatio<any, any>, QuoteToRatioQueryParams> { |
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.
At this point I think it makes sense to tie down our types to specific implementations i.e. ISwapToRatio<RoutingConfig, SwapAndAddConfig>
This way we can get type checking in this file
|
||
export class QuoteToRatioHandler extends APIGLambdaHandler< | ||
ContainerInjected, | ||
RequestInjected<ISwapToRatio<any, any>>, |
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.
Same as above
lib/handlers/quote/quote.ts
Outdated
|
||
export class QuoteHandler extends APIGLambdaHandler< | ||
ContainerInjected, | ||
RequestInjected, | ||
RequestInjected<IRouter<any>>, |
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.
Same as above
return new Fraction(JSBI.BigInt(fraction.numerator), JSBI.BigInt(fraction.denominator)) | ||
} | ||
|
||
describe('quote-to-ratio', () => { |
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.
Can we get some 4xx failure case tests?
Good to test some of the basic validation cases just to check that validation is hooked up (e.g. incorrect address returns 4xx etc.), as well as cases where we don't find a swap? (maybe we can set maxIterations to 0 or 1? or maybe passing in token0Balance = 0 and token1Balance = 0?)
Also what happens if I want to do a range order, but I already have 100% of the token required and 0 of the token not required? Do we still recommend a swap? Do we return 200 or 4xx in that case?
This PR includes some npm package updates to match smart-order-router sdk versions. I'm thinking to push those first in their own PR to separate out those updates from this work.
Todo: