From e2b500644325d245ec0dfb52ed675363e90d9fd0 Mon Sep 17 00:00:00 2001 From: CounterFire2023 <136581895+CounterFire2023@users.noreply.github.com> Date: Thu, 14 Dec 2023 10:08:14 +0800 Subject: [PATCH] add sample code for waas market --- src/controllers/okxmarket.controller.ts | 51 ++++++++++++++++ src/service/okx.svr.ts | 2 +- src/service/okxmarket.svr.ts | 80 +++++++++++++++++++++++++ src/utils/net.util.ts | 22 +++---- 4 files changed, 140 insertions(+), 15 deletions(-) create mode 100644 src/controllers/okxmarket.controller.ts create mode 100644 src/service/okxmarket.svr.ts diff --git a/src/controllers/okxmarket.controller.ts b/src/controllers/okxmarket.controller.ts new file mode 100644 index 0000000..010627b --- /dev/null +++ b/src/controllers/okxmarket.controller.ts @@ -0,0 +1,51 @@ +import { ZError } from 'common/ZError'; +import BaseController, { ROLE_ANON } from 'common/base.controller' +import {role, router} from 'decorators/router' +import {beginSell, buyOrder, submitOrder} from 'service/okxmarket.svr'; + +class OkxMarketController extends BaseController { + @role(ROLE_ANON) + @router('post /okx/market/presell') + async sellNFTPre(req, res) { + let {data} = req.params; + let result = await beginSell(data); + if (result.data.code) { + throw new ZError(result.data.code, result.data.message); + } + return result.data.data + } + + @role(ROLE_ANON) + @router('post /okx/market/sell') + async sellNFT(req, res) { + let {data} = req.params; + let result = await submitOrder(data); + if (result.data.code) { + throw new ZError(result.data.code, result.data.message); + } + return result.data.data + } + + @role(ROLE_ANON) + @router('post /okx/market/buy') + async buyNFT(req, res) { + let {data} = req.params + let result = await buyOrder(data); + if (result.data.code) { + throw new ZError(result.data.code, result.data.message); + } + return result.data.data + } + + @role(ROLE_ANON) + @router('post /okx/market/listings') + async queryListings(req, res) { + return {} + } + + @role(ROLE_ANON) + @router('post /okx/market/offers') + async queryOffers(req, res) { + return {} + } +} diff --git a/src/service/okx.svr.ts b/src/service/okx.svr.ts index aeeeee2..1ed8471 100644 --- a/src/service/okx.svr.ts +++ b/src/service/okx.svr.ts @@ -2,7 +2,7 @@ import axios, { AxiosResponse } from 'axios' import logger from 'logger/logger' import { prepareOkxReqCfg } from 'utils/okx.utils'; -const OKX_BASE = 'https://www.okx.com/api/v5/waas' +export const OKX_BASE = 'https://www.okx.com/api/v5/waas' export interface IOkxRes { code: number diff --git a/src/service/okxmarket.svr.ts b/src/service/okxmarket.svr.ts new file mode 100644 index 0000000..e266e41 --- /dev/null +++ b/src/service/okxmarket.svr.ts @@ -0,0 +1,80 @@ +import axios from 'axios' +import logger from 'logger/logger' +import {generateKVStr} from 'utils/net.util'; +import { prepareOkxReqCfg } from 'utils/okx.utils'; + +const OKX_BASE = 'https://www.okx.com/api/v5/mktplace'; + +/** + * https://www.okx.com/web3/build/docs/build-dapp/marketplace-create-a-listing + */ +export function beginSell(data: any) { + if (typeof data === 'object') { + data = JSON.stringify(data) + } + let config = { + method: 'post', + url: `${OKX_BASE}/nft/markets/create-listing`, + data + }; + config = prepareOkxReqCfg(config); + return axios.request(config) +} +export function submitOrder(data: any) { + if (typeof data === 'object') { + data = JSON.stringify(data) + } + let config = { + method: 'post', + url: `${OKX_BASE}/nft/markets/submit-listing`, + data + }; + config = prepareOkxReqCfg(config); + return axios.request(config) +} +/** + * https://www.okx.com/web3/build/docs/build-dapp/marketplace-buy-orders + */ +export function buyOrder(data: any) { + if (typeof data === 'object') { + data = JSON.stringify(data) + } + let config = { + method: 'post', + url: `${OKX_BASE}/nft/markets/buy`, + data + }; + config = prepareOkxReqCfg(config); + return axios.request(config) +} + +/** + * https://www.okx.com/web3/build/docs/build-dapp/marketplace-query-listing + */ +export function listings(data: any) { + let uri = `${OKX_BASE}/nft/markets/listings`; + if (data) { + uri = generateKVStr({data, uri}) + } + let config = { + method: 'get', + url: uri + }; + config = prepareOkxReqCfg(config); + return axios.request(config) +} +/** + * https://www.okx.com/web3/build/docs/build-dapp/marketplace-query-offer + */ +export function offers(data: any) { + let uri = `${OKX_BASE}/nft/markets/offers`; + if (data) { + uri = generateKVStr({data, uri}) + } + let config = { + method: 'get', + url: uri + }; + config = prepareOkxReqCfg(config); + return axios.request(config) +} diff --git a/src/utils/net.util.ts b/src/utils/net.util.ts index 6b05660..385eb6e 100644 --- a/src/utils/net.util.ts +++ b/src/utils/net.util.ts @@ -144,19 +144,13 @@ export function generateKVStr({ }) { const keys = Object.keys(data); sort && keys.sort(); - let result = ""; - let i = 0; - for (let key of keys) { - if (ignoreNull && !data[key]) { - continue; - } - if (i++ > 0) result += splitChar; - if (encode) { - result += `${key}${equalChar}${encodeURIComponent(data[key])}`; - } else { - result += `${key}${equalChar}${data[key]}`; - } - } + let result = keys + .filter((key) => !ignoreNull || data[key]) + .map((key) => { + const value = encode ? encodeURIComponent(data[key]) : data[key]; + return `${key}${equalChar}${value}`; + }) + .join(splitChar); if (uri) { const joinChar = uri.search(/\?/) === -1 ? "?" : "&"; result = uri + joinChar + result; @@ -191,4 +185,4 @@ export const checkParamsNeeded = (...args) => { args.forEach((arg) => {if (!arg) { throw new ZError(10, 'params mismatch'); }}); -} \ No newline at end of file +}