From 55116f7ce06547769d9634ca83dd0babdb4d7229 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Wed, 13 Mar 2024 20:48:04 +0300 Subject: [PATCH] feat: add useful getters (#59) --- src/auction-calculator/auction-calculator.ts | 4 +- src/fusion-order/fusion-order.ts | 57 ++++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/src/auction-calculator/auction-calculator.ts b/src/auction-calculator/auction-calculator.ts index 180bbe7..72e5bde 100644 --- a/src/auction-calculator/auction-calculator.ts +++ b/src/auction-calculator/auction-calculator.ts @@ -45,7 +45,7 @@ export class AuctionCalculator { /** * @see https://github.com/1inch/limit-order-settlement/blob/1b6757eecb2574953b543821db6f7bbff5afee48/contracts/extensions/BaseExtension.sol#L56 */ - calcAuctionTakingAmount(takingAmount: bigint, rate: number): bigint { + public calcAuctionTakingAmount(takingAmount: bigint, rate: number): bigint { const auctionTakingAmount = (BigInt(takingAmount) * (BigInt(rate) + RATE_BUMP_DENOMINATOR)) / RATE_BUMP_DENOMINATOR @@ -62,7 +62,7 @@ export class AuctionCalculator { * @param time auction timestamp in seconds * @param blockBaseFee blockBaseFee in Wei, if passed, then rate will be calculated as if order executed in block with `blockBaseFee` */ - calcRateBump(time: bigint, blockBaseFee = 0n): number { + public calcRateBump(time: bigint, blockBaseFee = 0n): number { const gasBump = this.getGasPriceBump(blockBaseFee) const auctionBump = this.getAuctionBump(time) diff --git a/src/fusion-order/fusion-order.ts b/src/fusion-order/fusion-order.ts index 93d9cfe..f95fb0b 100644 --- a/src/fusion-order/fusion-order.ts +++ b/src/fusion-order/fusion-order.ts @@ -19,6 +19,7 @@ import { import {AuctionCalculator} from '../auction-calculator' import {add0x} from '../utils' import {ZX} from '../constants' +import {calcTakingAmount} from '../utils/amounts' export class FusionOrder { private static defaultExtra = { @@ -136,6 +137,37 @@ export class FusionOrder { return this.inner.extension } + get maker(): Address { + return this.inner.maker + } + + get takerAsset(): Address { + return this.inner.takerAsset + } + + get makerAsset(): Address { + return this.inner.makerAsset + } + + get takingAmount(): bigint { + return this.inner.takingAmount + } + + get makingAmount(): bigint { + return this.inner.makingAmount + } + + get receiver(): Address { + return this.inner.receiver + } + + /** + * Timestamp in sec + */ + get deadline(): bigint { + return this.inner.makerTraits.expiration() || 0n + } + static new( /** * Fusion extension address @@ -275,4 +307,29 @@ export class FusionOrder { this.fusionExtension.details ) } + + /** + * Calculates required taking amount for passed `makingAmount` at block time `time` + * + * @param makingAmount maker swap amount + * @param time execution time in sec + * @param blockBaseFee block fee in wei. + * */ + public calcTakingAmount( + makingAmount: bigint, + time: bigint, + blockBaseFee = 0n + ): bigint { + const takingAmount = calcTakingAmount( + makingAmount, + this.makingAmount, + this.takingAmount + ) + + const calculator = this.getCalculator() + + const bump = calculator.calcRateBump(time, blockBaseFee) + + return calculator.calcAuctionTakingAmount(takingAmount, bump) + } }