From c63a2404f862e3b52d9a5680121e7a2332e76939 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sehyun=20Chung=20=E2=9C=8C=EF=B8=8E?= <41171808+sehyunc@users.noreply.github.com> Date: Tue, 29 Oct 2024 17:06:21 -0700 Subject: [PATCH] wasm, core: add action to request atomic matches --- packages/core/src/actions/cancelOrder.ts | 11 ++-- .../core/src/actions/cancelOrderRequest.ts | 14 ++-- packages/core/src/actions/createOrder.ts | 11 ++-- .../core/src/actions/createOrderRequest.ts | 13 ++-- packages/core/src/actions/deposit.ts | 11 ++-- packages/core/src/actions/depositRequest.ts | 14 ++-- .../core/src/actions/getAtomicMatchBundle.ts | 54 ++++++++++++++++ packages/core/src/actions/payFees.ts | 11 ++-- packages/core/src/actions/refreshWallet.ts | 11 ++-- packages/core/src/actions/updateOrder.ts | 11 ++-- packages/core/src/actions/withdraw.ts | 11 ++-- packages/core/src/actions/withdrawRequest.ts | 13 ++-- packages/core/src/constants.ts | 9 +++ packages/core/src/createConfig.ts | 16 +++++ packages/core/src/exports/actions.ts | 7 ++ packages/core/src/exports/index.ts | 7 ++ packages/core/src/types/externalOrder.ts | 22 +++++++ packages/core/src/utils.d.ts | 9 +++ packages/core/src/utils/http.ts | 21 +++--- wasm/src/external_api/auth/auth_helpers.rs | 1 - wasm/src/external_api/http.rs | 64 +++++++++++++++++++ 21 files changed, 270 insertions(+), 71 deletions(-) create mode 100644 packages/core/src/actions/getAtomicMatchBundle.ts create mode 100644 packages/core/src/types/externalOrder.ts diff --git a/packages/core/src/actions/cancelOrder.ts b/packages/core/src/actions/cancelOrder.ts index cee536bc..09aa1cc3 100644 --- a/packages/core/src/actions/cancelOrder.ts +++ b/packages/core/src/actions/cancelOrder.ts @@ -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' @@ -21,23 +22,23 @@ export async function cancelOrder( ): Promise { 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) { diff --git a/packages/core/src/actions/cancelOrderRequest.ts b/packages/core/src/actions/cancelOrderRequest.ts index 74d12d64..d29bcb17 100644 --- a/packages/core/src/actions/cancelOrderRequest.ts +++ b/packages/core/src/actions/cancelOrderRequest.ts @@ -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' @@ -15,16 +16,15 @@ export async function cancelOrderRequest( parameters: CancelOrderRequestParameters, ): Promise { 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) { diff --git a/packages/core/src/actions/createOrder.ts b/packages/core/src/actions/createOrder.ts index af05538e..1b02a057 100644 --- a/packages/core/src/actions/createOrder.ts +++ b/packages/core/src/actions/createOrder.ts @@ -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' @@ -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) @@ -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) { diff --git a/packages/core/src/actions/createOrderRequest.ts b/packages/core/src/actions/createOrderRequest.ts index 44e32ab0..3abfb121 100644 --- a/packages/core/src/actions/createOrderRequest.ts +++ b/packages/core/src/actions/createOrderRequest.ts @@ -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' @@ -15,16 +16,16 @@ export async function createOrderRequest( parameters: CreateOrderRequestParameters, ): Promise { 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) { diff --git a/packages/core/src/actions/deposit.ts b/packages/core/src/actions/deposit.ts index 186ddaa3..ce2cdaca 100644 --- a/packages/core/src/actions/deposit.ts +++ b/packages/core/src/actions/deposit.ts @@ -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' @@ -29,7 +30,6 @@ export async function deposit( const { fromAddr, mint, amount, permitNonce, permitDeadline, permit } = parameters const { - getRelayerBaseUrl, utils, state: { seed }, } = config @@ -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) @@ -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) { diff --git a/packages/core/src/actions/depositRequest.ts b/packages/core/src/actions/depositRequest.ts index 088783b7..ca47a8a3 100644 --- a/packages/core/src/actions/depositRequest.ts +++ b/packages/core/src/actions/depositRequest.ts @@ -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' @@ -15,16 +16,15 @@ export async function depositRequest( parameters: DepositRequestParameters, ): Promise { 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) { diff --git a/packages/core/src/actions/getAtomicMatchBundle.ts b/packages/core/src/actions/getAtomicMatchBundle.ts new file mode 100644 index 00000000..d465cb95 --- /dev/null +++ b/packages/core/src/actions/getAtomicMatchBundle.ts @@ -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 { + 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 +} diff --git a/packages/core/src/actions/payFees.ts b/packages/core/src/actions/payFees.ts index 79c55905..4715107b 100644 --- a/packages/core/src/actions/payFees.ts +++ b/packages/core/src/actions/payFees.ts @@ -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' @@ -9,14 +10,14 @@ export type PayFeesReturnType = { taskIds: string[] } export type PayFeesErrorType = BaseError export async function payFees(config: Config): Promise { - 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}`) diff --git a/packages/core/src/actions/refreshWallet.ts b/packages/core/src/actions/refreshWallet.ts index 821c08f7..fe3efa13 100644 --- a/packages/core/src/actions/refreshWallet.ts +++ b/packages/core/src/actions/refreshWallet.ts @@ -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}`) } diff --git a/packages/core/src/actions/updateOrder.ts b/packages/core/src/actions/updateOrder.ts index 28382ffe..e2cf139c 100644 --- a/packages/core/src/actions/updateOrder.ts +++ b/packages/core/src/actions/updateOrder.ts @@ -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' @@ -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) @@ -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) { diff --git a/packages/core/src/actions/withdraw.ts b/packages/core/src/actions/withdraw.ts index f6963e70..ee4e6b34 100644 --- a/packages/core/src/actions/withdraw.ts +++ b/packages/core/src/actions/withdraw.ts @@ -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' @@ -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) @@ -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) { diff --git a/packages/core/src/actions/withdrawRequest.ts b/packages/core/src/actions/withdrawRequest.ts index 69b32855..a927c489 100644 --- a/packages/core/src/actions/withdrawRequest.ts +++ b/packages/core/src/actions/withdrawRequest.ts @@ -1,6 +1,7 @@ import { WITHDRAW_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' @@ -15,16 +16,16 @@ export async function withdrawRequest( parameters: WithdrawRequestParameters, ): Promise { const { mint, request } = parameters - const { getRelayerBaseUrl } = config + const symmetricKey = getSymmetricKey(config) const walletId = getWalletId(config) try { - const res = await postRelayerWithAuth( - config, - getRelayerBaseUrl(WITHDRAW_BALANCE_ROUTE(walletId, mint)), - request, - ) + const res = await postRelayerWithAuth(config, { + url: config.getRelayerBaseUrl(WITHDRAW_BALANCE_ROUTE(walletId, mint)), + body: request, + key: symmetricKey, + }) console.log(`task update-wallet(${res.task_id}): ${walletId}`) return { taskId: res.task_id } } catch (error) { diff --git a/packages/core/src/constants.ts b/packages/core/src/constants.ts index 056b3dd1..0eb9999c 100644 --- a/packages/core/src/constants.ts +++ b/packages/core/src/constants.ts @@ -5,6 +5,8 @@ import type { Address } from 'viem' export const RENEGADE_AUTH_HEADER_NAME = 'x-renegade-auth' /// Header name for the expiration timestamp of a signature export const RENEGADE_SIG_EXPIRATION_HEADER_NAME = 'x-renegade-auth-expiration' +/// The Renegade API key header +export const RENEGADE_API_KEY_HEADER = 'x-renegade-api-key' /// The message used to derive the wallet's root key export const ROOT_KEY_MESSAGE_PREFIX = 'Unlock your Renegade Wallet on chain ID:' @@ -200,6 +202,13 @@ export const PRICE_REPORTER_ROUTE = ( quote: Address, ) => `/price/${PRICE_REPORTER_TOPIC(exchange, base, quote)}` +//////////////////////////////////////////////////////////////////////////////// +// Atomic Match +//////////////////////////////////////////////////////////////////////////////// +/// The route for requesting an atomic match +export const REQUEST_EXTERNAL_MATCH_ROUTE = + '/v0/matching-engine/request-external-match' + //////////////////////////////////////////////////////////////////////////////// // Token //////////////////////////////////////////////////////////////////////////////// diff --git a/packages/core/src/createConfig.ts b/packages/core/src/createConfig.ts index 26dd0d36..e8777cd1 100644 --- a/packages/core/src/createConfig.ts +++ b/packages/core/src/createConfig.ts @@ -26,6 +26,9 @@ export type CreateConfigParameters = { websocketPort?: number viemClient?: PublicClient adminKey?: string + apiSecret?: string + apiKey?: string + authServerUrl?: string } export function createConfig(parameters: CreateConfigParameters): Config { @@ -48,6 +51,9 @@ export function createConfig(parameters: CreateConfigParameters): Config { }), websocketPort = 4000, adminKey, + apiKey, + apiSecret, + authServerUrl, } = parameters invariant( @@ -156,6 +162,13 @@ export function createConfig(parameters: CreateConfigParameters): Config { }, viemClient, adminKey, + apiKey, + apiSecret, + getAuthServerUrl: (route = '') => { + invariant(authServerUrl, 'Auth server URL not specified in config') + const formattedRoute = route.startsWith('/') ? route : `/${route}` + return `${authServerUrl}${formattedRoute}` + }, _internal: { store, ssr: Boolean(ssr), @@ -188,6 +201,9 @@ export type Config = { utils: typeof rustUtils viemClient: PublicClient adminKey?: string + apiSecret?: string + apiKey?: string + getAuthServerUrl: (route?: string) => string /** * Not part of versioned API, proceed with caution. * @internal diff --git a/packages/core/src/exports/actions.ts b/packages/core/src/exports/actions.ts index 2be8af0c..1939fb26 100644 --- a/packages/core/src/exports/actions.ts +++ b/packages/core/src/exports/actions.ts @@ -49,6 +49,13 @@ export { export { type DisconnectReturnType, disconnect } from '../actions/disconnect.js' +export { + type GetAtomicMatchBundleParameters, + type GetAtomicMatchBundleReturnType, + type GetAtomicMatchBundleErrorType, + getAtomicMatchBundle, +} from '../actions/getAtomicMatchBundle.js' + export { type GetBalancesReturnType, getBalances, diff --git a/packages/core/src/exports/index.ts b/packages/core/src/exports/index.ts index bd481296..4713e1c4 100644 --- a/packages/core/src/exports/index.ts +++ b/packages/core/src/exports/index.ts @@ -53,6 +53,13 @@ export { disconnect, } from '../actions/disconnect.js' +export { + type GetAtomicMatchBundleParameters, + type GetAtomicMatchBundleReturnType, + type GetAtomicMatchBundleErrorType, + getAtomicMatchBundle, +} from '../actions/getAtomicMatchBundle.js' + export { type GetBalancesReturnType, getBalances, diff --git a/packages/core/src/types/externalOrder.ts b/packages/core/src/types/externalOrder.ts new file mode 100644 index 00000000..884ae16e --- /dev/null +++ b/packages/core/src/types/externalOrder.ts @@ -0,0 +1,22 @@ +import type { AccessList } from 'viem' + +export type ExternalMatchResult = { + quote_mint: `0x${string}` + base_mint: `0x${string}` + direction: 'Buy' | 'Sell' + quote_amount: bigint + base_amount: bigint + min_fill_size: bigint +} + +export type ExternalSettlementTx = { + type: `0x${string}` + to: `0x${string}` + data: `0x${string}` + accessList: AccessList +} + +export type ExternalMatchResponse = { + match_result: ExternalMatchResult + settlement_tx: ExternalSettlementTx +} diff --git a/packages/core/src/utils.d.ts b/packages/core/src/utils.d.ts index 9b0cca1b..a12d7b02 100644 --- a/packages/core/src/utils.d.ts +++ b/packages/core/src/utils.d.ts @@ -92,6 +92,15 @@ export function cancel_order(seed: string, wallet_str: string, order_id: string) */ export function update_order(seed: string, wallet_str: string, id: string, base_mint: string, quote_mint: string, side: string, amount: string, worst_case_price: string, min_fill_size: string, allow_external_matches: boolean): any; /** +* @param {string} base_mint +* @param {string} quote_mint +* @param {string} side +* @param {string} amount +* @param {string} min_fill_size +* @returns {any} +*/ +export function new_external_order(base_mint: string, quote_mint: string, side: string, amount: string, min_fill_size: string): any; +/** * @param {string} path * @param {any} headers * @param {string} body diff --git a/packages/core/src/utils/http.ts b/packages/core/src/utils/http.ts index 800766c0..5fb0b51b 100644 --- a/packages/core/src/utils/http.ts +++ b/packages/core/src/utils/http.ts @@ -103,22 +103,25 @@ export async function getRelayerRaw(url: string, headers = {}) { export async function postRelayerWithAuth( config: Config, - url: string, - body?: string, + { + body, + headers = {}, + key, + url, + }: { + body?: string + headers?: Record + key: string + url: string + }, ) { - const symmetricKey = getSymmetricKey(config) - invariant(symmetricKey, 'Failed to derive symmetric key') - const path = getPathFromUrl(url) - const headers = { - 'Content-Type': 'application/json', - } const headersWithAuth = addExpiringAuthToHeaders( config, path, headers, body ?? '', - symmetricKey, + key, SIG_EXPIRATION_BUFFER_MS, ) return await postRelayerRaw(url, body, headersWithAuth) diff --git a/wasm/src/external_api/auth/auth_helpers.rs b/wasm/src/external_api/auth/auth_helpers.rs index a900d651..8551a058 100644 --- a/wasm/src/external_api/auth/auth_helpers.rs +++ b/wasm/src/external_api/auth/auth_helpers.rs @@ -19,7 +19,6 @@ pub fn create_request_signature( let headers: HashMap = serde_wasm_bindgen::from_value(headers) .map_err(|e| JsError::new(&format!("Failed to deserialize headers: {}", e)))?; - let mac = _create_request_signature(path, &headers, body_bytes, &key); Ok(b64_general_purpose::STANDARD_NO_PAD.encode(mac)) } diff --git a/wasm/src/external_api/http.rs b/wasm/src/external_api/http.rs index 6e095005..1cb6f282 100644 --- a/wasm/src/external_api/http.rs +++ b/wasm/src/external_api/http.rs @@ -620,3 +620,67 @@ pub fn update_order( }; Ok(JsValue::from_str(&serde_json::to_string(&req).unwrap())) } + +/// The request type for requesting an external match +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ExternalMatchRequest { + /// The external order + pub external_order: ExternalOrder, +} + +/// An external order +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ExternalOrder { + /// The mint (erc20 address) of the quote token + #[serde( + serialize_with = "serialize_biguint_to_hex_string", + deserialize_with = "deserialize_biguint_from_hex_string" + )] + pub quote_mint: BigUint, + /// The mint (erc20 address) of the base token + #[serde( + serialize_with = "serialize_biguint_to_hex_string", + deserialize_with = "deserialize_biguint_from_hex_string" + )] + pub base_mint: BigUint, + /// The side of the market this order is on + pub side: OrderSide, + /// The amount of the order + pub amount: Amount, + /// The minimum fill size for the order + #[serde(default)] + pub min_fill_size: Amount, +} + +#[wasm_bindgen] +pub fn new_external_order( + base_mint: &str, + quote_mint: &str, + side: &str, + amount: &str, + min_fill_size: &str, +) -> Result { + let side = match side.to_lowercase().as_str() { + "sell" => OrderSide::Sell, + "buy" => OrderSide::Buy, + _ => return Err(JsError::new("Invalid order side")), + }; + let amount = wrap_eyre!(biguint_from_hex_string(amount)) + .unwrap() + .to_u128() + .unwrap(); + + let min_fill_size = wrap_eyre!(biguint_from_hex_string(min_fill_size)) + .unwrap() + .to_u128() + .unwrap(); + let external_order = ExternalOrder { + base_mint: biguint_from_hex_string(base_mint).unwrap(), + quote_mint: biguint_from_hex_string(quote_mint).unwrap(), + side, + amount, + min_fill_size, + }; + let req = ExternalMatchRequest { external_order }; + Ok(JsValue::from_str(&serde_json::to_string(&req).unwrap())) +}