Skip to content

Commit

Permalink
chore: rename postSellNativeCurrencyOrder
Browse files Browse the repository at this point in the history
  • Loading branch information
shoom3301 committed Dec 5, 2024
1 parent b21a9f8 commit 6fc34fd
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 28 deletions.
8 changes: 4 additions & 4 deletions src/trading/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ The parameters required are:
- `buyTokenDecimals` - the buy token decimals
- `amount` - the amount to sell/buy in atoms

> When sell token is a blockchain native token (ETH for Ethereum), then order will be created as an on-chain transaction. See [postSellNativeCurrencyTrade](#postSellNativeCurrencyTrade)
> When sell token is a blockchain native token (ETH for Ethereum), then order will be created as an on-chain transaction. See [postSellNativeCurrencyOrder](#postSellNativeCurrencyOrder)
#### Example

Expand Down Expand Up @@ -230,12 +230,12 @@ const orderId = await sdk.postLimitOrder(limitOrderParameters)
console.log('Order created, id: ', orderId)
```
### postSellNativeCurrencyTrade
### postSellNativeCurrencyOrder
CoW Protocol supports on-chain trades for selling blockchain native tokens (ETH for Ethereum).
In this case, the order is created as an on-chain transaction.
You don't have to think about the case when you use `postSwapOrder` function, it will be handled automatically.
But if you need more flexible way to create an order to sell native token, you can use the `postSellNativeCurrencyTrade` function.
But if you need more flexible way to create an order to sell native token, you can use the `postSellNativeCurrencyOrder` function.
> We consider the order as native token selling order if the sell token has '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee' address.
Expand All @@ -262,7 +262,7 @@ const parameters: TradeParameters = {
amount: '120000000000000000'
}

const orderId = await sdk.postSellNativeCurrencyTrade(parameters)
const orderId = await sdk.postSellNativeCurrencyOrder(parameters)

console.log('Order created, id: ', orderId)
```
Expand Down
2 changes: 1 addition & 1 deletion src/trading/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export { postSwapOrder, postSwapOrderFromQuote } from './postSwapOrder'
export { postLimitOrder } from './postLimitOrder'
export { postCoWProtocolTrade } from './postCoWProtocolTrade'
export { getOrderToSign } from './getOrderToSign'
export { postSellNativeCurrencyTrade } from './postSellNativeCurrencyTrade'
export { postSellNativeCurrencyOrder } from './postSellNativeCurrencyOrder'
export { getEthFlowTransaction } from './getEthFlowTransaction'
export { getPreSignTransaction } from './getPreSignTransaction'

Expand Down
22 changes: 11 additions & 11 deletions src/trading/postCoWProtocolTrade.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { postCoWProtocolTrade } from './postCoWProtocolTrade'

jest.mock('cross-fetch', () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const fetchMock = require('jest-fetch-mock')
Expand All @@ -20,20 +18,22 @@ jest.mock('../order-signing', () => {
}
})

jest.mock('./postSellNativeCurrencyTrade', () => {
jest.mock('./postSellNativeCurrencyOrder', () => {
return {
postSellNativeCurrencyTrade: jest.fn(),
}
})

import { postSellNativeCurrencyTrade } from './postSellNativeCurrencyTrade'
import { postSellNativeCurrencyOrder } from './postSellNativeCurrencyOrder'

import { AppDataInfo, LimitOrderParameters } from './types'
import { ETH_ADDRESS, SupportedChainId } from '../common'
import { OrderBookApi, OrderKind } from '../order-book'
import { OrderSigningUtils as OrderSigningUtilsMock } from '../order-signing'
import { VoidSigner } from '@ethersproject/abstract-signer'

const quoteId = 31

const defaultOrderParams: LimitOrderParameters = {
chainId: SupportedChainId.GNOSIS_CHAIN,
signer: '0x006',
Expand All @@ -45,7 +45,7 @@ const defaultOrderParams: LimitOrderParameters = {
sellAmount: '1000000000000000000',
buyAmount: '2000000000000000000',
kind: OrderKind.SELL,
quoteId: 31,
quoteId,
slippageBps: 50,
}

Expand All @@ -66,13 +66,13 @@ const appDataMock = {
'{\\"appCode\\":\\"CoW Swap\\",\\"environment\\":\\"barn\\",\\"metadata\\":{\\"orderClass\\":{\\"orderClass\\":\\"market\\"},\\"quote\\":{\\"slippageBips\\":201,\\"smartSlippage\\":true}},\\"version\\":\\"1.3.0\\"}',
} as unknown as AppDataInfo

describe('postCoWProtocolTrade', () => {
describe('postSellNativeCurrencyOrder', () => {
let signOrderMock: jest.SpyInstance
let postSellNativeCurrencyTradeMock: jest.SpyInstance

beforeAll(() => {
signOrderMock = OrderSigningUtilsMock.signOrder as unknown as jest.SpyInstance
postSellNativeCurrencyTradeMock = postSellNativeCurrencyTrade as unknown as jest.SpyInstance
postSellNativeCurrencyTradeMock = postSellNativeCurrencyOrder as unknown as jest.SpyInstance
})

beforeEach(() => {
Expand All @@ -89,8 +89,8 @@ describe('postCoWProtocolTrade', () => {
it('When sell token is native, then should post on-chain order', async () => {
postSellNativeCurrencyTradeMock.mockResolvedValue({ orderId: '0x01' })

const order = { ...defaultOrderParams, sellToken: ETH_ADDRESS }
await postCoWProtocolTrade(orderBookApiMock, signer, appDataMock, order)
const order = { ...defaultOrderParams, sellToken: ETH_ADDRESS, quoteId }
await postSellNativeCurrencyOrder(orderBookApiMock, signer, appDataMock, order)

expect(postSellNativeCurrencyTradeMock).toHaveBeenCalledTimes(1)
expect(postSellNativeCurrencyTradeMock).toHaveBeenCalledWith(orderBookApiMock, signer, appDataMock, order, '0')
Expand All @@ -99,8 +99,8 @@ describe('postCoWProtocolTrade', () => {
it('API request should contain all specified parameters', async () => {
sendOrderMock.mockResolvedValue('0x02')

const order = { ...defaultOrderParams }
await postCoWProtocolTrade(orderBookApiMock, signer, appDataMock, order)
const order = { ...defaultOrderParams, quoteId }
await postSellNativeCurrencyOrder(orderBookApiMock, signer, appDataMock, order)

const callBody = sendOrderMock.mock.calls[0][0]

Expand Down
4 changes: 2 additions & 2 deletions src/trading/postCoWProtocolTrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { AppDataInfo, LimitTradeParameters } from './types'
import { log, SIGN_SCHEME_MAP } from './consts'
import { OrderSigningUtils } from '../order-signing'
import { getOrderToSign } from './getOrderToSign'
import { postSellNativeCurrencyTrade } from './postSellNativeCurrencyTrade'
import { postSellNativeCurrencyOrder } from './postSellNativeCurrencyOrder'
import { getIsEthFlowOrder } from './utils'

export async function postCoWProtocolTrade(
Expand All @@ -18,7 +18,7 @@ export async function postCoWProtocolTrade(
const quoteId = params.quoteId

if (typeof quoteId === 'number') {
const { orderId } = await postSellNativeCurrencyTrade(
const { orderId } = await postSellNativeCurrencyOrder(
orderBookApi,
signer,
appData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { VoidSigner } from '@ethersproject/abstract-signer'
import { AppDataInfo, LimitOrderParameters } from './types'
import { SupportedChainId, WRAPPED_NATIVE_CURRENCIES } from '../common'
import { OrderBookApi, OrderKind } from '../order-book'
import { postSellNativeCurrencyTrade } from './postSellNativeCurrencyTrade'
import { postSellNativeCurrencyOrder } from './postSellNativeCurrencyOrder'

jest.mock('cross-fetch', () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
Expand Down Expand Up @@ -97,7 +97,7 @@ describe('postSellNativeCurrencyTrade', () => {
it('Should call checkEthFlowOrderExists if it is set', async () => {
const checkEthFlowOrderExists = jest.fn().mockResolvedValue(false)

await postSellNativeCurrencyTrade(
await postSellNativeCurrencyOrder(
orderBookApiMock,
signer,
appDataMock,
Expand All @@ -110,23 +110,23 @@ describe('postSellNativeCurrencyTrade', () => {
})

it('Should upload appData', async () => {
await postSellNativeCurrencyTrade(orderBookApiMock, signer, appDataMock, defaultOrderParams)
await postSellNativeCurrencyOrder(orderBookApiMock, signer, appDataMock, defaultOrderParams)

expect(uploadAppDataMock).toHaveBeenCalledWith(appDataMock.appDataKeccak256, appDataMock.fullAppData)
})

it('When transaction gas estimation is failed, then should use fallback value + 20%', async () => {
ethFlowContractMock.estimateGas.createOrder.mockRejectedValue(new Error('Estimation failed'))

await postSellNativeCurrencyTrade(orderBookApiMock, signer, appDataMock, defaultOrderParams)
await postSellNativeCurrencyOrder(orderBookApiMock, signer, appDataMock, defaultOrderParams)

const call = (signer.sendTransaction as jest.Mock).mock.calls[0][0]

expect(+call.gas).toBe(180000) // 150000 by default + 20%
})

it('Should create an on-chain transaction with all specified parameters', async () => {
await postSellNativeCurrencyTrade(orderBookApiMock, signer, appDataMock, defaultOrderParams)
await postSellNativeCurrencyOrder(orderBookApiMock, signer, appDataMock, defaultOrderParams)

expect(ethFlowContractMock.interface.encodeFunctionData).toHaveBeenCalledTimes(1)
expect(ethFlowContractMock.interface.encodeFunctionData).toHaveBeenCalledWith('createOrder', [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { log } from './consts'
import { OrderBookApi } from '../order-book'
import { getEthFlowTransaction } from './getEthFlowTransaction'

export async function postSellNativeCurrencyTrade(
export async function postSellNativeCurrencyOrder(
orderBookApi: OrderBookApi,
signer: Signer,
appData: Pick<AppDataInfo, 'fullAppData' | 'appDataKeccak256'>,
Expand Down
8 changes: 4 additions & 4 deletions src/trading/tradingSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { postSwapOrder, postSwapOrderFromQuote } from './postSwapOrder'
import { postLimitOrder } from './postLimitOrder'
import { getQuoteWithSigner } from './getQuote'
import { postSellNativeCurrencyTrade } from './postSellNativeCurrencyTrade'
import { postSellNativeCurrencyOrder } from './postSellNativeCurrencyOrder'
import { getSigner, swapParamsToLimitOrderParams } from './utils'
import { getPreSignTransaction } from './getPreSignTransaction'

Expand All @@ -33,14 +33,14 @@ export class TradingSdk {
return postLimitOrder(this.mergeParams(params), advancedSettings)
}

async postSellNativeCurrencyTrade(
async postSellNativeCurrencyOrder(
params: TradeParameters,
advancedSettings?: SwapAdvancedSettings
): Promise<ReturnType<typeof postSellNativeCurrencyTrade>> {
): Promise<ReturnType<typeof postSellNativeCurrencyOrder>> {
const quoteResults = await getQuoteWithSigner(this.mergeParams(params), advancedSettings)

const { tradeParameters, quoteResponse, amountsAndCosts } = quoteResults.result
return postSellNativeCurrencyTrade(
return postSellNativeCurrencyOrder(
quoteResults.orderBookApi,
quoteResults.result.signer,
quoteResults.result.appDataInfo,
Expand Down

0 comments on commit 6fc34fd

Please sign in to comment.