Skip to content

Commit

Permalink
wasm, core: add action to request atomic matches
Browse files Browse the repository at this point in the history
  • Loading branch information
sehyunc committed Oct 30, 2024
1 parent a37de4c commit c63a240
Show file tree
Hide file tree
Showing 21 changed files with 270 additions and 71 deletions.
11 changes: 6 additions & 5 deletions packages/core/src/actions/cancelOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import invariant from 'tiny-invariant'
import { CANCEL_ORDER_ROUTE } from '../constants.js'
import type { Config } from '../createConfig.js'
import type { BaseErrorType } from '../errors/base.js'
import { getSymmetricKey } from '../exports/index.js'
import { stringifyForWasm } from '../utils/bigJSON.js'
import { postRelayerWithAuth } from '../utils/http.js'
import { getBackOfQueueWallet } from './getBackOfQueueWallet.js'
Expand All @@ -21,23 +22,23 @@ export async function cancelOrder(
): Promise<CancelOrderReturnType> {
const { id } = parameters
const {
getRelayerBaseUrl,
utils,
state: { seed },
} = config
invariant(seed, 'Seed is required')

const symmetricKey = getSymmetricKey(config)
const walletId = getWalletId(config)
const wallet = await getBackOfQueueWallet(config)

const body = utils.cancel_order(seed, stringifyForWasm(wallet), id)

try {
const res = await postRelayerWithAuth(
config,
getRelayerBaseUrl(CANCEL_ORDER_ROUTE(walletId, id)),
const res = await postRelayerWithAuth(config, {
url: config.getRelayerBaseUrl(CANCEL_ORDER_ROUTE(walletId, id)),
body,
)
key: symmetricKey,
})
console.log(`task update-wallet(${res.task_id}): ${walletId}`)
return { taskId: res.task_id }
} catch (error) {
Expand Down
14 changes: 7 additions & 7 deletions packages/core/src/actions/cancelOrderRequest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CANCEL_ORDER_ROUTE } from '../constants.js'
import type { Config } from '../createConfig.js'
import type { BaseErrorType } from '../errors/base.js'
import { getSymmetricKey } from '../exports/index.js'
import { postRelayerWithAuth } from '../utils/http.js'
import { getWalletId } from './getWalletId.js'

Expand All @@ -15,16 +16,15 @@ export async function cancelOrderRequest(
parameters: CancelOrderRequestParameters,
): Promise<CancelOrderRequestReturnType> {
const { request, id } = parameters
const { getRelayerBaseUrl } = config

const symmetricKey = getSymmetricKey(config)
const walletId = getWalletId(config)

try {
const res = await postRelayerWithAuth(
config,
getRelayerBaseUrl(CANCEL_ORDER_ROUTE(walletId, id)),
request,
)
const res = await postRelayerWithAuth(config, {
url: config.getRelayerBaseUrl(CANCEL_ORDER_ROUTE(walletId, id)),
body: request,
key: symmetricKey,
})
console.log(`task update-wallet(${res.task_id}): ${walletId}`)
return { taskId: res.task_id }
} catch (error) {
Expand Down
11 changes: 6 additions & 5 deletions packages/core/src/actions/createOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { type Address, toHex } from 'viem'
import { WALLET_ORDERS_ROUTE } from '../constants.js'
import type { Config } from '../createConfig.js'
import type { BaseErrorType } from '../errors/base.js'
import { getSymmetricKey } from '../exports/index.js'
import { stringifyForWasm } from '../utils/bigJSON.js'
import { postRelayerWithAuth } from '../utils/http.js'
import { getBackOfQueueWallet } from './getBackOfQueueWallet.js'
Expand Down Expand Up @@ -38,12 +39,12 @@ export async function createOrder(
allowExternalMatches = false,
} = parameters
const {
getRelayerBaseUrl,
utils,
state: { seed },
} = config
invariant(seed, 'Seed is required')

const symmetricKey = getSymmetricKey(config)
const walletId = getWalletId(config)
const wallet = await getBackOfQueueWallet(config)

Expand All @@ -61,11 +62,11 @@ export async function createOrder(
)

try {
const res = await postRelayerWithAuth(
config,
getRelayerBaseUrl(WALLET_ORDERS_ROUTE(walletId)),
const res = await postRelayerWithAuth(config, {
url: config.getRelayerBaseUrl(WALLET_ORDERS_ROUTE(walletId)),
body,
)
key: symmetricKey,
})
console.log(`task update-wallet(${res.task_id}): ${walletId}`)
return { taskId: res.task_id }
} catch (error) {
Expand Down
13 changes: 7 additions & 6 deletions packages/core/src/actions/createOrderRequest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { WALLET_ORDERS_ROUTE } from '../constants.js'
import type { Config } from '../createConfig.js'
import type { BaseErrorType } from '../errors/base.js'
import { getSymmetricKey } from '../exports/actions.js'
import { postRelayerWithAuth } from '../utils/http.js'
import { getWalletId } from './getWalletId.js'

Expand All @@ -15,16 +16,16 @@ export async function createOrderRequest(
parameters: CreateOrderRequestParameters,
): Promise<CreateOrderRequestReturnType> {
const { request } = parameters
const { getRelayerBaseUrl } = config

const symmetricKey = getSymmetricKey(config)
const walletId = getWalletId(config)

try {
const res = await postRelayerWithAuth(
config,
getRelayerBaseUrl(WALLET_ORDERS_ROUTE(walletId)),
request,
)
const res = await postRelayerWithAuth(config, {
url: config.getRelayerBaseUrl(WALLET_ORDERS_ROUTE(walletId)),
body: request,
key: symmetricKey,
})
console.log(`task update-wallet(${res.task_id}): ${walletId}`)
return { taskId: res.task_id }
} catch (error) {
Expand Down
11 changes: 6 additions & 5 deletions packages/core/src/actions/deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { type Address, toHex } from 'viem'
import { DEPOSIT_BALANCE_ROUTE } from '../constants.js'
import type { Config } from '../createConfig.js'
import type { BaseErrorType } from '../errors/base.js'
import { getSymmetricKey } from '../exports/actions.js'
import { Token } from '../types/token.js'
import { stringifyForWasm } from '../utils/bigJSON.js'
import { postRelayerWithAuth } from '../utils/http.js'
Expand All @@ -29,7 +30,6 @@ export async function deposit(
const { fromAddr, mint, amount, permitNonce, permitDeadline, permit } =
parameters
const {
getRelayerBaseUrl,
utils,
state: { seed },
} = config
Expand All @@ -38,6 +38,7 @@ export async function deposit(
const token = Token.findByAddress(mint)
invariant(token, 'Token not found')

const symmetricKey = getSymmetricKey(config)
const walletId = getWalletId(config)
const wallet = await getBackOfQueueWallet(config)
const walletStr = stringifyForWasm(wallet)
Expand All @@ -54,11 +55,11 @@ export async function deposit(
)

try {
const res = await postRelayerWithAuth(
config,
getRelayerBaseUrl(DEPOSIT_BALANCE_ROUTE(walletId)),
const res = await postRelayerWithAuth(config, {
url: config.getRelayerBaseUrl(DEPOSIT_BALANCE_ROUTE(walletId)),
body,
)
key: symmetricKey,
})
console.log(`task update-wallet(${res.task_id}): ${walletId}`)
return { taskId: res.task_id }
} catch (error) {
Expand Down
14 changes: 7 additions & 7 deletions packages/core/src/actions/depositRequest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { DEPOSIT_BALANCE_ROUTE } from '../constants.js'
import type { Config } from '../createConfig.js'
import type { BaseErrorType } from '../errors/base.js'
import { getSymmetricKey } from '../exports/actions.js'
import { postRelayerWithAuth } from '../utils/http.js'
import { getWalletId } from './getWalletId.js'

Expand All @@ -15,16 +16,15 @@ export async function depositRequest(
parameters: DepositRequestParameters,
): Promise<DepositRequestReturnType> {
const { request } = parameters
const { getRelayerBaseUrl } = config

const symmetricKey = getSymmetricKey(config)
const walletId = getWalletId(config)

try {
const res = await postRelayerWithAuth(
config,
getRelayerBaseUrl(DEPOSIT_BALANCE_ROUTE(walletId)),
request,
)
const res = await postRelayerWithAuth(config, {
url: config.getRelayerBaseUrl(DEPOSIT_BALANCE_ROUTE(walletId)),
body: request,
key: symmetricKey,
})
console.log(`task update-wallet(${res.task_id}): ${walletId}`)
return { taskId: res.task_id }
} catch (error) {
Expand Down
54 changes: 54 additions & 0 deletions packages/core/src/actions/getAtomicMatchBundle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import invariant from 'tiny-invariant'
import { toHex } from 'viem'
import {
RENEGADE_API_KEY_HEADER,
REQUEST_EXTERNAL_MATCH_ROUTE,
} from '../constants.js'
import type { Config } from '../createConfig.js'
import { BaseError, type BaseErrorType } from '../errors/base.js'
import type { ExternalMatchResponse } from '../types/externalOrder.js'
import { postRelayerWithAuth } from '../utils/http.js'

export type GetAtomicMatchBundleParameters = {
base: `0x${string}`
quote: `0x${string}`
side: 'buy' | 'sell'
amount: bigint
minFillSize?: bigint
}

export type GetAtomicMatchBundleReturnType = ExternalMatchResponse

export type GetAtomicMatchBundleErrorType = BaseErrorType

export async function getAtomicMatchBundle(
config: Config,
parameters: GetAtomicMatchBundleParameters,
): Promise<GetAtomicMatchBundleReturnType> {
const { base, quote, side, amount, minFillSize = BigInt(0) } = parameters
const { apiSecret, apiKey } = config
invariant(apiSecret, 'API secret not specified in config')
invariant(apiKey, 'API key not specified in config')
const symmetricKey = config.utils.b64_to_hex_hmac_key(apiSecret)

const body = config.utils.new_external_order(
base,
quote,
side,
toHex(amount),
toHex(minFillSize),
)

const res = await postRelayerWithAuth(config, {
url: config.getAuthServerUrl(REQUEST_EXTERNAL_MATCH_ROUTE),
body,
key: symmetricKey,
headers: {
[RENEGADE_API_KEY_HEADER]: apiKey,
},
})
if (!res.match_bundle) {
throw new BaseError('No match bundle found')
}
return res.match_bundle
}
11 changes: 6 additions & 5 deletions packages/core/src/actions/payFees.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { PAY_FEES_ROUTE } from '../constants.js'
import type { Config } from '../createConfig.js'
import type { BaseError } from '../errors/base.js'
import { getSymmetricKey } from '../exports/actions.js'
import { postRelayerWithAuth } from '../utils/http.js'
import { getWalletId } from './getWalletId.js'

Expand All @@ -9,14 +10,14 @@ export type PayFeesReturnType = { taskIds: string[] }
export type PayFeesErrorType = BaseError

export async function payFees(config: Config): Promise<PayFeesReturnType> {
const { getRelayerBaseUrl } = config
const symmetricKey = getSymmetricKey(config)
const walletId = getWalletId(config)

try {
const res = await postRelayerWithAuth(
config,
getRelayerBaseUrl(PAY_FEES_ROUTE(walletId)),
)
const res = await postRelayerWithAuth(config, {
url: config.getRelayerBaseUrl(PAY_FEES_ROUTE(walletId)),
key: symmetricKey,
})
if (res?.task_ids) {
res.task_ids.map((id: string) => {
console.log(`task pay-fees(${id}): ${walletId}`)
Expand Down
11 changes: 6 additions & 5 deletions packages/core/src/actions/refreshWallet.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { REFRESH_WALLET_ROUTE } from '../constants.js'
import type { Config } from '../createConfig.js'
import { getSymmetricKey } from '../exports/actions.js'
import { postRelayerWithAuth } from '../utils/http.js'
import { getWalletId } from './getWalletId.js'

export type RefreshWalletReturnType = Promise<{ taskId: string }>

export async function refreshWallet(config: Config): RefreshWalletReturnType {
const { getRelayerBaseUrl } = config
const symmetricKey = getSymmetricKey(config)
const walletId = getWalletId(config)

try {
const res = await postRelayerWithAuth(
config,
getRelayerBaseUrl(REFRESH_WALLET_ROUTE(walletId)),
)
const res = await postRelayerWithAuth(config, {
url: config.getRelayerBaseUrl(REFRESH_WALLET_ROUTE(walletId)),
key: symmetricKey,
})
if (res?.task_id) {
console.log(`task refresh-wallet(${res.task_id}): ${walletId}`)
}
Expand Down
11 changes: 6 additions & 5 deletions packages/core/src/actions/updateOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import invariant from 'tiny-invariant'
import { type Address, toHex } from 'viem'
import { UPDATE_ORDER_ROUTE } from '../constants.js'
import type { Config } from '../createConfig.js'
import { getSymmetricKey } from '../exports/actions.js'
import { stringifyForWasm } from '../utils/bigJSON.js'
import { postRelayerWithAuth } from '../utils/http.js'
import { getBackOfQueueWallet } from './getBackOfQueueWallet.js'
Expand Down Expand Up @@ -35,12 +36,12 @@ export async function updateOrder(
allowExternalMatches = false,
} = parameters
const {
getRelayerBaseUrl,
utils,
state: { seed },
} = config
invariant(seed, 'Seed is required')

const symmetricKey = getSymmetricKey(config)
const walletId = getWalletId(config)
const wallet = await getBackOfQueueWallet(config)

Expand All @@ -58,11 +59,11 @@ export async function updateOrder(
)

try {
const res = await postRelayerWithAuth(
config,
getRelayerBaseUrl(UPDATE_ORDER_ROUTE(walletId, id)),
const res = await postRelayerWithAuth(config, {
url: config.getRelayerBaseUrl(UPDATE_ORDER_ROUTE(walletId, id)),
body,
)
key: symmetricKey,
})
console.log(`task update-wallet(${res.task_id}): ${walletId}`)
return { taskId: res.task_id }
} catch (error) {
Expand Down
11 changes: 6 additions & 5 deletions packages/core/src/actions/withdraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import invariant from 'tiny-invariant'
import { type Address, toHex } from 'viem'
import { WITHDRAW_BALANCE_ROUTE } from '../constants.js'
import type { Config } from '../createConfig.js'
import { getSymmetricKey } from '../exports/actions.js'
import { stringifyForWasm } from '../utils/bigJSON.js'
import { postRelayerWithAuth } from '../utils/http.js'
import { getBackOfQueueWallet } from './getBackOfQueueWallet.js'
Expand All @@ -21,12 +22,12 @@ export async function withdraw(
): WithdrawReturnType {
const { mint, amount, destinationAddr } = parameters
const {
getRelayerBaseUrl,
utils,
state: { seed },
} = config
invariant(seed, 'Seed is required')

const symmetricKey = getSymmetricKey(config)
const walletId = getWalletId(config)
const wallet = await getBackOfQueueWallet(config)

Expand All @@ -40,11 +41,11 @@ export async function withdraw(
)

try {
const res = await postRelayerWithAuth(
config,
getRelayerBaseUrl(WITHDRAW_BALANCE_ROUTE(walletId, mint)),
const res = await postRelayerWithAuth(config, {
url: config.getRelayerBaseUrl(WITHDRAW_BALANCE_ROUTE(walletId, mint)),
body,
)
key: symmetricKey,
})
console.log(`task update-wallet(${res.task_id}): ${walletId}`)
return { taskId: res.task_id }
} catch (error) {
Expand Down
Loading

0 comments on commit c63a240

Please sign in to comment.