Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock contracts and test cabinet #158

Merged
merged 1 commit into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 65 additions & 52 deletions src/composable/ConditionalOrder.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { TestConditionalOrder } from './orderTypes/test/TestConditionalOrder'
import { ConditionalOrder } from './ConditionalOrder'
import { IsValidResult, PollResultErrors } from './types'
import { Twap } from './orderTypes/Twap'
import { encodeParams } from './utils'

import { getComposableCow } from './contracts'
import { constants } from 'ethers'

jest.mock('./contracts')
const mockGetComposableCow = getComposableCow as jest.MockedFunction<typeof getComposableCow>

const TWAP_SERIALIZED = (salt?: string, handler?: string): string => {
return (
Expand All @@ -25,6 +30,20 @@ const TWAP_SERIALIZED = (salt?: string, handler?: string): string => {
)
}

const SINGLE_ORDER = new TestConditionalOrder(
'0x910d00a310f7Dc5B29FE73458F47f519be547D3d',
'0x9379a0bf532ff9a66ffde940f94b1a025d6f18803054c1aef52dc94b15255bbe',
'0x',
true
)

const MERKLE_ROOT_ORDER = new TestConditionalOrder(
'0x910d00a310f7Dc5B29FE73458F47f519be547D3d',
'0x9379a0bf532ff9a66ffde940f94b1a025d6f18803054c1aef52dc94b15255bbe',
'0x',
false
)

describe('Constructor', () => {
test('Create TestConditionalOrder', () => {
// bad address
Expand Down Expand Up @@ -79,11 +98,7 @@ describe('Serialize: Encode static input', () => {

describe('Compute orderUid', () => {
test('Returns correct id', () => {
const order = new TestConditionalOrder(
'0x910d00a310f7Dc5B29FE73458F47f519be547D3d',
'0x9379a0bf532ff9a66ffde940f94b1a025d6f18803054c1aef52dc94b15255bbe'
)
expect(order.id).toEqual('0x88ca0698d8c5500b31015d84fa0166272e1812320d9af8b60e29ae00153363b3')
expect(SINGLE_ORDER.id).toEqual('0x88ca0698d8c5500b31015d84fa0166272e1812320d9af8b60e29ae00153363b3')
})

test('Derive OrderId from leaf data', () => {
Expand All @@ -97,49 +112,47 @@ describe('Compute orderUid', () => {
})
})

class TestConditionalOrder extends ConditionalOrder<string, string> {
isSingleOrder = true
describe('cabinet', () => {
const owner = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045'
const cabinetValue = '000000000000000000000000000000000000000000000000000000064f0b353'
const mockCabinet = jest.fn()
const param = { owner } as any
beforeEach(() => {
jest.resetAllMocks()

mockGetComposableCow.mockReturnValue({
callStatic: {
cabinet: mockCabinet,
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any)

mockCabinet.mockReturnValue(cabinetValue)
})

constructor(address: string, salt?: string, data = '0x') {
super({
handler: address,
salt,
data,
})
}

get orderType(): string {
return 'TEST'
}

encodeStaticInput(): string {
return this.staticInput
}

testEncodeStaticInput(): string {
return super.encodeStaticInputHelper(['uint256'], this.staticInput)
}

transformStructToData(params: string): string {
return params
}

transformDataToStruct(params: string): string {
return params
}

protected pollValidate(): Promise<PollResultErrors | undefined> {
throw new Error('Method not implemented.')
}

isValid(): IsValidResult {
throw new Error('Method not implemented.')
}
serialize(): string {
return encodeParams(this.leaf)
}

toString(): string {
throw new Error('Method not implemented.')
}
}
test('Single orders call the contract with order id as the ctx', () => {
// GIVEN: a conditional order
// WHEN: we call cabinet
expect(SINGLE_ORDER.cabinet(param)).toEqual(cabinetValue)

// THEN: we expect to do a CALL using the ComposableCoW contract passing the UID of the order
expect(mockGetComposableCow).toHaveBeenCalledTimes(1)
expect(mockCabinet.mock.calls).toHaveLength(1)

// THEN: we expect the params to be the owner and the order id
expect(mockCabinet.mock.calls[0]).toEqual([owner, SINGLE_ORDER.id])
})

test('Merkle Root orders call the contract with the 0x0 as the ctx', () => {
// GIVEN: a merkle root order
// WHEN: we call cabinet
expect(MERKLE_ROOT_ORDER.cabinet(param)).toEqual(cabinetValue)

// THEN: we expect to do a CALL using the ComposableCoW contract
expect(mockGetComposableCow).toHaveBeenCalledTimes(1)
expect(mockCabinet.mock.calls).toHaveLength(1)

// THEN: we expect the params to be the owner and 0x0
expect(mockCabinet.mock.calls[0]).toEqual([owner, constants.HashZero])
})
})
2 changes: 1 addition & 1 deletion src/composable/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function getComposableCowInterface(): ComposableCoWInterface {
return composableCowInterfaceCache
}

export function getComposableCow(chain: SupportedChainId, provider: providers.Provider) {
export function getComposableCow(chain: SupportedChainId, provider: providers.Provider): ComposableCoW {
if (!composableCowContractCache) {
composableCowContractCache = ComposableCoW__factory.connect(COMPOSABLE_COW_CONTRACT_ADDRESS[chain], provider)
}
Expand Down
51 changes: 51 additions & 0 deletions src/composable/orderTypes/test/TestConditionalOrder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { ConditionalOrder } from '../../ConditionalOrder'
import { IsValidResult, PollResultErrors } from '../../types'
import { encodeParams } from '../../utils'

export class TestConditionalOrder extends ConditionalOrder<string, string> {
isSingleOrder: boolean

constructor(address: string, salt?: string, data = '0x', isSingleOrder = true) {
super({
handler: address,
salt,
data,
})
this.isSingleOrder = isSingleOrder
}

get orderType(): string {
return 'TEST'
}

encodeStaticInput(): string {
return this.staticInput
}

testEncodeStaticInput(): string {
return super.encodeStaticInputHelper(['uint256'], this.staticInput)
}

transformStructToData(params: string): string {
return params
}

transformDataToStruct(params: string): string {
return params
}

protected pollValidate(): Promise<PollResultErrors | undefined> {
throw new Error('Method not implemented.')
}

isValid(): IsValidResult {
throw new Error('Method not implemented.')
}
serialize(): string {
return encodeParams(this.leaf)
}

toString(): string {
throw new Error('Method not implemented.')
}
}