add sample code for waas market
This commit is contained in:
parent
06f589258a
commit
e2b5006443
51
src/controllers/okxmarket.controller.ts
Normal file
51
src/controllers/okxmarket.controller.ts
Normal file
@ -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 {}
|
||||
}
|
||||
}
|
@ -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
|
||||
|
80
src/service/okxmarket.svr.ts
Normal file
80
src/service/okxmarket.svr.ts
Normal file
@ -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)
|
||||
}
|
@ -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');
|
||||
}});
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user