-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor clients and spokePool Validate fill (#6)
Co-authored-by: chrismaree <[email protected]> Co-authored-by: ubuntu <>
- Loading branch information
1 parent
043e711
commit b9a939a
Showing
19 changed files
with
499 additions
and
310 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
This file was deleted.
Oops, something went wrong.
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,67 @@ | ||
import { spreadEvent, assign, Contract, BaseContract, toBNWei, Block, BigNumber, toBN, utils } from "../utils"; | ||
import { Deposit, Fill, SpeedUp } from "../interfaces/SpokePool"; | ||
|
||
export class HubPoolClient { | ||
// l1Token -> destinationChainId -> destinationToken | ||
private l1TokensToDestinationTokens: { [l1Token: string]: { [destinationChainId: number]: string } } = {}; | ||
|
||
public firstBlockToSearch: number; | ||
|
||
constructor( | ||
readonly hubPool: Contract, | ||
readonly startingBlock: number = 0, | ||
readonly endingBlock: number | null = null | ||
) {} | ||
|
||
getDestinationTokenForDeposit(deposit: Deposit) { | ||
const l1Token = this.getL1TokenForDeposit(deposit); | ||
return this.getDestinationTokenForL1TokenAndDestinationChainId(l1Token, deposit.destinationChainId); | ||
} | ||
|
||
getL1TokensToDestinationTokens() { | ||
return this.l1TokensToDestinationTokens; | ||
} | ||
|
||
getL1TokenForDeposit(deposit: Deposit) { | ||
let l1Token = null; | ||
Object.keys(this.l1TokensToDestinationTokens).forEach((_l1Token) => { | ||
if (this.l1TokensToDestinationTokens[_l1Token][deposit.originChainId.toString()] === deposit.originToken) | ||
l1Token = _l1Token; | ||
}); | ||
return l1Token; | ||
} | ||
|
||
getDestinationTokenForL1TokenAndDestinationChainId(l1Token: string, destinationChainId: number) { | ||
return this.l1TokensToDestinationTokens[l1Token][destinationChainId]; | ||
} | ||
|
||
async getPoolUtilization(quoteBlockNumber: number, l1Token: string, amount: BigNumber) { | ||
const blockOffset = { blockTag: quoteBlockNumber }; | ||
const [liquidityUtilizationCurrent, liquidityUtilizationPostRelay] = await Promise.all([ | ||
this.hubPool.callStatic.liquidityUtilizationCurrent(l1Token, blockOffset), | ||
this.hubPool.callStatic.liquidityUtilizationPostRelay(l1Token, amount.toString(), blockOffset), | ||
]); | ||
return { liquidityUtilizationCurrent, liquidityUtilizationPostRelay }; | ||
} | ||
|
||
async update() { | ||
const searchConfig = [this.firstBlockToSearch, this.endingBlock || (await this.getBlockNumber())]; | ||
if (searchConfig[0] > searchConfig[1]) return; // If the starting block is greater than the ending block return. | ||
const [poolRebalanceRouteEvents] = await Promise.all([ | ||
this.hubPool.queryFilter(this.hubPool.filters.SetPoolRebalanceRoute(), ...searchConfig), | ||
]); | ||
|
||
for (const event of poolRebalanceRouteEvents) { | ||
const args = spreadEvent(event); | ||
assign(this.l1TokensToDestinationTokens, [args.l1Token, args.destinationChainId], args.destinationToken); | ||
} | ||
} | ||
|
||
async getBlockNumber(): Promise<number> { | ||
return await this.hubPool.provider.getBlockNumber(); | ||
} | ||
|
||
getProvider() { | ||
return this.hubPool.provider; | ||
} | ||
} |
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,70 @@ | ||
import { spreadEvent, assign, Contract, toBNWei, Block, BigNumber, toBN, utils } from "../utils"; | ||
import { Deposit, Fill } from "../interfaces/SpokePool"; | ||
import { lpFeeCalculator } from "@across-protocol/sdk-v2"; | ||
import { BlockFinder, across } from "@uma/sdk"; | ||
import { HubPoolClient } from "./HubPoolClient"; | ||
|
||
export class RateModelClient { | ||
private readonly blockFinder; | ||
|
||
private cumulativeRateModelEvents: across.rateModel.RateModelEvent[] = []; | ||
private rateModelDictionary: across.rateModel.RateModelDictionary; | ||
|
||
public firstBlockToSearch: number; | ||
|
||
constructor(readonly rateModelStore: Contract, readonly hubPoolClient: HubPoolClient) { | ||
this.blockFinder = new BlockFinder( | ||
this.hubPoolClient.getProvider().getBlock.bind(this.hubPoolClient.getProvider()) | ||
); | ||
this.rateModelDictionary = new across.rateModel.RateModelDictionary(); | ||
} | ||
|
||
async computeRealizedLpFeePct(deposit: Deposit, l1Token: string) { | ||
const quoteBlockNumber = (await this.blockFinder.getBlockForTimestamp(deposit.quoteTimestamp)).number; | ||
|
||
const rateModelForBlockNumber = this.getRateModelForBlockNumber(l1Token, quoteBlockNumber); | ||
|
||
const blockOffset = { blockTag: quoteBlockNumber }; | ||
const [liquidityUtilizationCurrent, liquidityUtilizationPostRelay] = await Promise.all([ | ||
this.hubPoolClient.hubPool.callStatic.liquidityUtilizationCurrent(l1Token, blockOffset), | ||
this.hubPoolClient.hubPool.callStatic.liquidityUtilizationPostRelay(l1Token, deposit.amount, blockOffset), | ||
]); | ||
|
||
const realizedLpFeePct = lpFeeCalculator.calculateRealizedLpFeePct( | ||
rateModelForBlockNumber, | ||
liquidityUtilizationCurrent, | ||
liquidityUtilizationPostRelay | ||
); | ||
|
||
return toBN(realizedLpFeePct); | ||
} | ||
|
||
getRateModelForBlockNumber(l1Token: string, blockNumber: number | undefined = undefined): across.constants.RateModel { | ||
return this.rateModelDictionary.getRateModelForBlockNumber(l1Token, blockNumber); | ||
} | ||
|
||
async validateRealizedLpFeePctForFill(fill: Fill, deposit: Deposit) { | ||
const expectedFee = await this.computeRealizedLpFeePct(deposit, this.hubPoolClient.getL1TokenForDeposit(deposit)); | ||
if (!expectedFee.eq(fill.realizedLpFeePct)) return false; | ||
} | ||
|
||
async update() { | ||
const searchConfig = [this.firstBlockToSearch, await this.hubPoolClient.getBlockNumber()]; | ||
if (searchConfig[0] > searchConfig[1]) return; // If the starting block is greater than the ending block return. | ||
const rateModelStoreEvents = await this.rateModelStore.queryFilter( | ||
this.rateModelStore.filters.UpdatedRateModel(), | ||
...searchConfig | ||
); | ||
|
||
for (const event of rateModelStoreEvents) { | ||
const args = { | ||
blockNumber: event.blockNumber, | ||
transactionIndex: event.transactionIndex, | ||
logIndex: event.logIndex, | ||
...spreadEvent(event), | ||
}; | ||
this.cumulativeRateModelEvents = [...this.cumulativeRateModelEvents, args]; | ||
} | ||
this.rateModelDictionary.updateWithEvents(this.cumulativeRateModelEvents); | ||
} | ||
} |
Oops, something went wrong.