orderbuild fixes; tranche table work; ordershapes still broken

This commit is contained in:
tim
2024-09-20 04:12:23 -04:00
parent ce54b943ea
commit e5d5c9c0d8
10 changed files with 111 additions and 48 deletions

View File

@@ -1,4 +1,4 @@
import {ethers} from "ethers";
import {ethers, FixedNumber} from "ethers";
const UNISWAPV3_POOL_INIT_CODE_HASH = '0xe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54'
const uniswapV3Addresses = {
@@ -26,4 +26,14 @@ export function uniswapV3PoolAddress(chainId, tokenAddrA, tokenAddrB, fee) {
return null
}
return ethers.getCreate2Address(factory, salt, UNISWAPV3_POOL_INIT_CODE_HASH)
}
export function uniswapV3AveragePrice(amountIn, amountOut, fee) {
if (amountIn === 0n || amountOut === 0n) return null
const fmtX18 = {decimals: 18, width: 256, signed: false};
let result = FixedNumber.fromValue(amountOut, 0, fmtX18)
.div(FixedNumber.fromValue(amountIn, 0, fmtX18)).toUnsafeFloat()
result /= (1 - fee / 1_000_000) // adjust for pool fee
return result
}