-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* implement okx 1% * relax test case * remove getLogger due to 'Dependency with key logger not found' when used from backend. * relax test case * added okx approve transaction * register loggers * remove registering logger in sdk, should be done in backend instead * review comments * change logger format * version * make logs human readable
- Loading branch information
1 parent
5b8eb78
commit 75986b4
Showing
9 changed files
with
372 additions
and
44 deletions.
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@aperture_finance/uniswap-v3-automation-sdk", | ||
"version": "3.10.0", | ||
"version": "3.10.1", | ||
"description": "SDK for Aperture's CLMM automation platform", | ||
"author": "Aperture Finance <[email protected]>", | ||
"license": "MIT", | ||
|
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,163 @@ | ||
import { ApertureSupportedChainId, getAMMInfo } from '@/index'; | ||
import axios from 'axios'; | ||
import { Address, Hex } from 'viem'; | ||
|
||
import { encodeOptimalSwapData } from '../automan'; | ||
import { limiter } from './common'; | ||
import { ISolver } from './types'; | ||
import { SwapRoute } from './types'; | ||
|
||
const ApiBaseUrl = 'https://okx-api.aperture.finance'; | ||
const headers = { | ||
Accept: 'application/json', | ||
}; | ||
|
||
export async function buildRequest(methodName: string, params: object) { | ||
return limiter.schedule(() => | ||
axios.get(apiRequestUrl(methodName), { | ||
headers, | ||
params, | ||
}), | ||
); | ||
} | ||
|
||
function apiRequestUrl(methodName: string) { | ||
const rv = new URL( | ||
`api/v5/dex/aggregator/${methodName}`, | ||
ApiBaseUrl, | ||
).toString(); | ||
console.log('apiRequestUrl', rv); | ||
return rv; | ||
} | ||
|
||
export async function getOkxApproveTarget( | ||
chainId: ApertureSupportedChainId, | ||
tokenContractAddress: string, | ||
approveAmount: string, | ||
): Promise<Address> { | ||
try { | ||
return ( | ||
await buildRequest('approve-transaction', { | ||
chainId, | ||
tokenContractAddress, | ||
approveAmount, | ||
}) | ||
).data.data[0].dexContractAddress; | ||
} catch (e) { | ||
console.error(e); | ||
throw e; | ||
} | ||
} | ||
|
||
export const getOkxSolver = (): ISolver => { | ||
return { | ||
optimalMint: async (props) => { | ||
const { | ||
chainId, | ||
amm, | ||
token0, | ||
token1, | ||
feeOrTickSpacing, | ||
tickLower, | ||
tickUpper, | ||
slippage, | ||
poolAmountIn, | ||
zeroForOne, | ||
} = props; | ||
|
||
const { optimalSwapRouter } = getAMMInfo(chainId, amm)!; | ||
if (!optimalSwapRouter) { | ||
throw new Error('Expected: Chain or AMM not support'); | ||
} | ||
|
||
const { tx, protocols } = await getOkxQuote( | ||
chainId, | ||
zeroForOne ? token0 : token1, | ||
zeroForOne ? token1 : token0, | ||
poolAmountIn.toString(), | ||
optimalSwapRouter, | ||
slippage * 100, | ||
); | ||
|
||
const approveTarget = await getOkxApproveTarget( | ||
chainId, | ||
zeroForOne ? token0 : token1, | ||
poolAmountIn.toString(), | ||
); | ||
return { | ||
swapData: encodeOptimalSwapData( | ||
chainId, | ||
amm, | ||
token0, | ||
token1, | ||
feeOrTickSpacing, | ||
tickLower, | ||
tickUpper, | ||
zeroForOne, | ||
approveTarget, | ||
tx.to, | ||
tx.data, | ||
), | ||
swapRoute: protocols, | ||
}; | ||
}, | ||
}; | ||
}; | ||
|
||
/** | ||
* Get a quote for a swap. | ||
* @param chainId The chain ID. | ||
* @param src Contract address of a token to sell | ||
* @param dst Contract address of a token to buy | ||
* @param amount Amount of a token to sell, set in minimal divisible units | ||
* @param from Address of a seller, make sure that this address has approved to spend src in needed amount | ||
* @param slippage Limit of price slippage you are willing to accept in percentage | ||
*/ | ||
export async function getOkxQuote( | ||
chainId: ApertureSupportedChainId, | ||
src: string, | ||
dst: string, | ||
amount: string, | ||
from: string, | ||
slippage: number, | ||
): Promise<{ | ||
toAmount: string; | ||
tx: { | ||
from: Address; | ||
to: Address; | ||
data: Hex; | ||
value: string; | ||
gas: string; | ||
gasPrice: string; | ||
}; | ||
protocols?: SwapRoute; | ||
}> { | ||
if (amount === '0') { | ||
throw new Error('amount should greater than 0'); | ||
} | ||
const swapParams = { | ||
chainId: chainId.toString(), | ||
fromTokenAddress: src, | ||
toTokenAddress: dst, | ||
amount, | ||
slippage: slippage.toString(), | ||
userWalletAddress: from, | ||
}; | ||
try { | ||
const swapData = ( | ||
await buildRequest('swap', new URLSearchParams(swapParams)) | ||
).data.data; | ||
if (swapData.length < 1) { | ||
throw new Error( | ||
`Error: No swap route found with swapParams=${JSON.stringify(swapParams)}`, | ||
); | ||
} | ||
return { | ||
toAmount: swapData[0].routerResult.toTokenAmount, | ||
tx: swapData[0].tx, | ||
}; | ||
} catch (e) { | ||
console.error(e); | ||
throw e; | ||
} | ||
} |
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
Oops, something went wrong.