Skip to content

Commit

Permalink
Create OwnerContext type
Browse files Browse the repository at this point in the history
  • Loading branch information
anxolin committed Aug 30, 2023
1 parent 0e31b9f commit 4a11e0f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
17 changes: 10 additions & 7 deletions src/composable/ConditionalOrder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BigNumber, ethers, utils, providers } from 'ethers'
import { BigNumber, ethers, utils } from 'ethers'
import { IConditionalOrder } from './generated/ComposableCoW'

import { decodeParams, encodeParams } from './utils'
Expand All @@ -7,12 +7,12 @@ import {
ConditionalOrderParams,
ContextFactory,
IsValidResult,
OwnerContext,
PollParams,
PollResult,
PollResultCode,
PollResultErrors,
} from './types'
import { SupportedChainId } from '../common'
import { getComposableCow, getComposableCowInterface } from './contracts'

/**
Expand Down Expand Up @@ -253,7 +253,7 @@ export abstract class ConditionalOrder<D, S> {
}

// Check if the owner authorised the order
const isAuthorized = await this.isAuthorized(owner, chainId, provider)
const isAuthorized = await this.isAuthorized(params)
if (!isAuthorized) {
return {
result: PollResultCode.DONT_TRY_AGAIN,
Expand Down Expand Up @@ -290,8 +290,9 @@ export abstract class ConditionalOrder<D, S> {
* @param provider An RPC provider for the chain.
* @returns true if the owner authorized the order, false otherwise.
*/
public isAuthorized(owner: string, chain: SupportedChainId, provider: providers.Provider): Promise<boolean> {
const composableCow = getComposableCow(chain, provider)
public isAuthorized(params: OwnerContext): Promise<boolean> {
const { chainId, owner, provider } = params
const composableCow = getComposableCow(chainId, provider)
return composableCow.callStatic.singleOrders(owner, this.id)
}

Expand All @@ -303,8 +304,10 @@ export abstract class ConditionalOrder<D, S> {
* @param provider An RPC provider for the chain.
* @returns true if the owner authorized the order, false otherwise.
*/
public cabinet(owner: string, chain: SupportedChainId, provider: providers.Provider): Promise<string> {
const composableCow = getComposableCow(chain, provider)
public cabinet(params: OwnerContext): Promise<string> {
const { chainId, owner, provider } = params

const composableCow = getComposableCow(chainId, provider)
return composableCow.callStatic.cabinet(owner, this.id)
}

Expand Down
12 changes: 6 additions & 6 deletions src/composable/orderTypes/Twap.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BigNumber, constants, providers } from 'ethers'
import { BigNumber, constants } from 'ethers'

import { ConditionalOrder } from '../ConditionalOrder'
import {
Expand All @@ -7,12 +7,12 @@ import {
ContextFactory,
IsNotValid,
IsValid,
OwnerContext,
PollParams,
PollResultCode,
PollResultErrors,
} from '../types'
import { encodeParams, formatEpoch, getBlockInfo, isValidAbi } from '../utils'
import { SupportedChainId } from '../../common'

// The type of Conditional Order
const TWAP_ORDER_TYPE = 'twap'
Expand Down Expand Up @@ -265,14 +265,14 @@ export class Twap extends ConditionalOrder<TwapData, TwapStruct> {
return error ? { isValid: false, reason: error } : { isValid: true }
}

private async startTimestamp(owner: string, chain: SupportedChainId, provider: providers.Provider): Promise<number> {
private async startTimestamp(params: OwnerContext): Promise<number> {
const { startTime } = this.data

if (startTime?.startType === StartTimeValue.AT_EPOC) {
return startTime.epoch.toNumber()
}

const cabinet = await this.cabinet(owner, chain, provider)
const cabinet = await this.cabinet(params)
return parseInt(cabinet, 16)
}

Expand All @@ -285,11 +285,11 @@ export class Twap extends ConditionalOrder<TwapData, TwapStruct> {
* @returns true if the owner authorized the order, false otherwise.
*/
protected async pollValidate(params: PollParams): Promise<PollResultErrors | undefined> {
const { blockInfo = await getBlockInfo(params.provider), owner, chainId, provider } = params
const { blockInfo = await getBlockInfo(params.provider) } = params
const { blockTimestamp } = blockInfo
const { numberOfParts, timeBetweenParts } = this.data

const startTimestamp = await this.startTimestamp(owner, chainId, provider)
const startTimestamp = await this.startTimestamp(params)

if (startTimestamp > blockTimestamp) {
// The start time hasn't started
Expand Down
4 changes: 3 additions & 1 deletion src/composable/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,13 @@ export type ProofWithParams = {
params: ConditionalOrderParams
}

export type PollParams = {
export type OwnerContext = {
owner: string
chainId: SupportedChainId
provider: providers.Provider
}

export type PollParams = OwnerContext & {
/**
* If present, it can be used for custom conditional order validations. If not present, the orders will need to get the block info themselves
*/
Expand Down

0 comments on commit 4a11e0f

Please sign in to comment.