New fetchWithValidation / introducing simpleFetch

This commit is contained in:
Aleksandr Kraiz
2022-05-10 11:44:09 +04:00
parent edc91ecefe
commit 883bcf928f
16 changed files with 350 additions and 117 deletions

View File

@@ -1,6 +1,6 @@
import BigNumber from 'bignumber.js';
import { z } from 'zod';
import { fetchJsonWithValidation } from '../../fetchWithValidation';
import fetchWithValidation from '../../fetchWithValidation';
import swapInfoSchema from './schemas/swapInfoSchema';
import exchangeInfoSchema from './schemas/exchangeInfoSchema';
import cancelOrderSchema from './schemas/cancelOrderSchema';
@@ -8,7 +8,7 @@ import orderBenefitsSchema from './schemas/orderBenefitsSchema';
import errorSchema from './errorSchema';
import placeAtomicSwapSchema from './schemas/placeAtomicSwapSchema';
import { OrionAggregatorWS } from './ws';
import atomicSwapHistorySchema from './schemas/atomicSwapHistorySchema';
import { atomicSwapHistorySchema } from './schemas/atomicSwapHistorySchema';
import { SignedCancelOrderRequest, SignedOrder, SupportedChainId } from '../../types';
import { pairConfigSchema } from './schemas';
@@ -20,6 +20,18 @@ class OrionAggregator {
constructor(apiUrl: string, chainId: SupportedChainId) {
this.apiUrl = apiUrl;
this.ws = new OrionAggregatorWS(this.aggregatorWSUrl, chainId);
this.getHistoryAtomicSwaps = this.getHistoryAtomicSwaps.bind(this);
this.getPairConfig = this.getPairConfig.bind(this);
this.getPairConfigs = this.getPairConfigs.bind(this);
this.getPairsList = this.getPairsList.bind(this);
this.getSwapInfo = this.getSwapInfo.bind(this);
this.getTradeProfits = this.getTradeProfits.bind(this);
this.placeAtomicSwap = this.placeAtomicSwap.bind(this);
this.placeOrder = this.placeOrder.bind(this);
this.cancelOrder = this.cancelOrder.bind(this);
this.checkWhitelisted = this.checkWhitelisted.bind(this);
this.getLockedBalance = this.getLockedBalance.bind(this);
}
get aggregatorWSUrl() { return `wss://${this.apiUrl}/v1`; }
@@ -29,14 +41,14 @@ class OrionAggregator {
}
getPairsList() {
return fetchJsonWithValidation(
return fetchWithValidation(
`${this.aggregatorUrl}/api/v1/pairs/list`,
z.array(z.string()),
);
}
getPairConfigs() {
return fetchJsonWithValidation(
return fetchWithValidation(
`${this.aggregatorUrl}/api/v1/pairs/exchangeInfo`,
exchangeInfoSchema,
undefined,
@@ -45,7 +57,7 @@ class OrionAggregator {
}
getPairConfig(assetPair: string) {
return fetchJsonWithValidation(
return fetchWithValidation(
`${this.aggregatorUrl}/api/v1/pairs/exchangeInfo/${assetPair}`,
pairConfigSchema,
undefined,
@@ -54,7 +66,7 @@ class OrionAggregator {
}
checkWhitelisted(address: string) {
return fetchJsonWithValidation(
return fetchWithValidation(
`${this.aggregatorUrl}/api/v1/whitelist/check?address=${address}`,
z.boolean(),
undefined,
@@ -73,7 +85,7 @@ class OrionAggregator {
...partnerId && { 'X-Partner-Id': partnerId },
};
return fetchJsonWithValidation(
return fetchWithValidation(
`${this.aggregatorUrl}/api/v1/order/${isCreateInternalOrder ? 'internal' : ''}`,
z.object({
orderId: z.string(),
@@ -95,7 +107,7 @@ class OrionAggregator {
}
cancelOrder(signedCancelOrderRequest: SignedCancelOrderRequest) {
return fetchJsonWithValidation(
return fetchWithValidation(
`${this.aggregatorUrl}/api/v1/order`,
cancelOrderSchema,
{
@@ -128,7 +140,7 @@ class OrionAggregator {
url.searchParams.append('amountOut', amount);
}
return fetchJsonWithValidation(
return fetchWithValidation(
url.toString(),
swapInfoSchema,
undefined,
@@ -139,7 +151,7 @@ class OrionAggregator {
getLockedBalance(address: string, currency: string) {
const url = new URL(`${this.aggregatorUrl}/api/v1/address/balance/reserved/${currency}`);
url.searchParams.append('address', address);
return fetchJsonWithValidation(
return fetchWithValidation(
url.toString(),
z.object({
[currency]: z.number(),
@@ -159,7 +171,7 @@ class OrionAggregator {
url.searchParams.append('amount', amount.toString());
url.searchParams.append('side', isBuy ? 'buy' : 'sell');
return fetchJsonWithValidation(
return fetchWithValidation(
url.toString(),
orderBenefitsSchema,
undefined,
@@ -177,7 +189,7 @@ class OrionAggregator {
secretHash: string,
sourceNetworkCode: string,
) {
return fetchJsonWithValidation(
return fetchWithValidation(
`${this.aggregatorUrl}/api/v1/atomic-swap`,
placeAtomicSwapSchema,
{
@@ -204,7 +216,7 @@ class OrionAggregator {
const url = new URL(`${this.aggregatorUrl}/api/v1/atomic-swap/history/all`);
url.searchParams.append('sender', sender);
url.searchParams.append('limit', limit.toString());
return fetchJsonWithValidation(url.toString(), atomicSwapHistorySchema);
return fetchWithValidation(url.toString(), atomicSwapHistorySchema);
}
}
export * as schemas from './schemas';

View File

@@ -1,5 +1,5 @@
import { z } from 'zod';
import { fetchJsonWithValidation } from '../../fetchWithValidation';
import fetchWithValidation from '../../fetchWithValidation';
import { PairStatusEnum, pairStatusSchema } from './schemas/adminPoolsListSchema';
import {
IDOSchema, atomicHistorySchema,
@@ -56,6 +56,32 @@ class OrionBlockchain {
constructor(apiUrl: string) {
this.apiUrl = apiUrl;
this.ws = new OrionBlockchainSocketIO(`https://${apiUrl}/`);
this.getAtomicSwapAssets = this.getAtomicSwapAssets.bind(this);
this.getAtomicSwapHistory = this.getAtomicSwapHistory.bind(this);
this.getAuthToken = this.getAuthToken.bind(this);
this.getCirculatingSupply = this.getCirculatingSupply.bind(this);
this.getInfo = this.getInfo.bind(this);
this.getPoolsConfig = this.getPoolsConfig.bind(this);
this.getPoolsInfo = this.getPoolsInfo.bind(this);
this.getHistory = this.getHistory.bind(this);
this.getPrices = this.getPrices.bind(this);
this.getTokensFee = this.getTokensFee.bind(this);
this.getGasPriceWei = this.getGasPriceWei.bind(this);
this.checkFreeRedeemAvailable = this.checkFreeRedeemAvailable.bind(this);
this.redeemAtomicSwap = this.redeemAtomicSwap.bind(this);
this.redeem2AtomicSwaps = this.redeem2AtomicSwaps.bind(this);
this.checkRedeem = this.checkRedeem.bind(this);
this.checkRedeem2Atomics = this.checkRedeem2Atomics.bind(this);
this.getIDOInfo = this.getIDOInfo.bind(this);
this.checkAuth = this.checkAuth.bind(this);
this.addPool = this.addPool.bind(this);
this.editPool = this.editPool.bind(this);
this.getPoolsList = this.getPoolsList.bind(this);
this.getSourceAtomicSwapHistory = this.getSourceAtomicSwapHistory.bind(this);
this.getTargetAtomicSwapHistory = this.getTargetAtomicSwapHistory.bind(this);
this.checkPoolInformation = this.checkPoolInformation.bind(this);
this.checkIfHashUsed = this.checkIfHashUsed.bind(this);
}
get orionBlockchainWsUrl() {
@@ -63,43 +89,43 @@ class OrionBlockchain {
}
getAuthToken() {
return fetchJsonWithValidation(`https://${this.apiUrl}/api/auth/token`, z.object({ token: z.string() }));
return fetchWithValidation(`https://${this.apiUrl}/api/auth/token`, z.object({ token: z.string() }));
}
getCirculatingSupply() {
return fetchJsonWithValidation(`https://${this.apiUrl}/api/circulating-supply`, z.number());
return fetchWithValidation(`https://${this.apiUrl}/api/circulating-supply`, z.number());
}
getInfo() {
return fetchJsonWithValidation(`https://${this.apiUrl}/api/info`, infoSchema);
return fetchWithValidation(`https://${this.apiUrl}/api/info`, infoSchema);
}
getPoolsConfig() {
return fetchJsonWithValidation(`https://${this.apiUrl}/api/pools/config`, poolsConfigSchema);
return fetchWithValidation(`https://${this.apiUrl}/api/pools/config`, poolsConfigSchema);
}
getPoolsInfo() {
return fetchJsonWithValidation(`https://${this.apiUrl}/api/pools/info`, poolsInfoSchema);
return fetchWithValidation(`https://${this.apiUrl}/api/pools/info`, poolsInfoSchema);
}
getHistory(address: string) {
return fetchJsonWithValidation(`https://${this.apiUrl}/api/history/${address}`, historySchema);
return fetchWithValidation(`https://${this.apiUrl}/api/history/${address}`, historySchema);
}
getPrices() {
return fetchJsonWithValidation(`https://${this.apiUrl}/api/prices`, z.record(z.string()).transform(utils.makePartial));
return fetchWithValidation(`https://${this.apiUrl}/api/prices`, z.record(z.string()).transform(utils.makePartial));
}
getTokensFee() {
return fetchJsonWithValidation(`https://${this.apiUrl}/api/tokensFee`, z.record(z.string()).transform(utils.makePartial));
return fetchWithValidation(`https://${this.apiUrl}/api/tokensFee`, z.record(z.string()).transform(utils.makePartial));
}
getGasPriceWei() {
return fetchJsonWithValidation(`https://${this.apiUrl}/api/gasPrice`, z.string());
return fetchWithValidation(`https://${this.apiUrl}/api/gasPrice`, z.string());
}
checkFreeRedeemAvailable(walletAddress: string) {
return fetchJsonWithValidation(`https://${this.apiUrl}/api/atomic/has-free-redeem/${walletAddress}`, z.boolean());
return fetchWithValidation(`https://${this.apiUrl}/api/atomic/has-free-redeem/${walletAddress}`, z.boolean());
}
redeemAtomicSwap(
@@ -107,7 +133,7 @@ class OrionBlockchain {
secret: string,
sourceNetwork: string,
) {
return fetchJsonWithValidation(
return fetchWithValidation(
`https://${this.apiUrl}/api/atomic/matcher-redeem`,
z.string(),
{
@@ -131,7 +157,7 @@ class OrionBlockchain {
secret2: string,
sourceNetwork: string,
) {
return fetchJsonWithValidation(
return fetchWithValidation(
`https://${this.apiUrl}/api/atomic/matcher-redeem2atomics`,
z.string(),
{
@@ -151,31 +177,31 @@ class OrionBlockchain {
}
checkRedeem(secretHash: string) {
return fetchJsonWithValidation(
return fetchWithValidation(
`https://${this.apiUrl}/api/atomic/matcher-redeem/${secretHash}`,
z.enum(['OK', 'FAIL']).nullable(),
);
}
checkRedeem2Atomics(firstSecretHash: string, secondSecretHash: string) {
return fetchJsonWithValidation(
return fetchWithValidation(
`https://${this.apiUrl}/api/atomic/matcher-redeem/${firstSecretHash}-${secondSecretHash}`,
z.enum(['OK', 'FAIL']).nullable(),
);
}
getIDOInfo() {
return fetchJsonWithValidation(`https://${this.apiUrl}/api/solarflare`, IDOSchema);
return fetchWithValidation(`https://${this.apiUrl}/api/solarflare`, IDOSchema);
}
checkAuth(headers: IAdminAuthHeaders) {
return fetchJsonWithValidation(`https://${this.apiUrl}/api/auth/check`, z.object({
return fetchWithValidation(`https://${this.apiUrl}/api/auth/check`, z.object({
auth: z.boolean(),
}), { headers });
}
getPoolsList(headers: IAdminAuthHeaders) {
return fetchJsonWithValidation(
return fetchWithValidation(
`https://${this.apiUrl}/api/pools/list`,
adminPoolsListSchema,
{ headers },
@@ -183,7 +209,7 @@ class OrionBlockchain {
}
editPool(address: string, data: IEditPool, headers: IAdminAuthHeaders) {
return fetchJsonWithValidation(
return fetchWithValidation(
`https://${this.apiUrl}/api/pools/edit/${address}`,
pairStatusSchema,
{
@@ -198,22 +224,27 @@ class OrionBlockchain {
}
addPool(data: z.infer<typeof addPoolSchema>) {
return fetchJsonWithValidation(`https://${this.apiUrl}/api/pools/add`, z.number(), {
method: 'POST',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
return fetchWithValidation(
`https://${this.apiUrl}/api/pools/add`,
z.number(),
{
method: 'POST',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
},
});
z.string(),
);
}
checkPoolInformation(poolAddress: string) {
return fetchJsonWithValidation(`https://${this.apiUrl}/api/pools/check/${poolAddress}`, pairStatusSchema);
return fetchWithValidation(`https://${this.apiUrl}/api/pools/check/${poolAddress}`, pairStatusSchema);
}
getAtomicSwapAssets() {
return fetchJsonWithValidation(`https://${this.apiUrl}/api/atomic/swap-assets`, z.array(z.string()));
return fetchWithValidation(`https://${this.apiUrl}/api/atomic/swap-assets`, z.array(z.string()));
}
/**
@@ -226,7 +257,7 @@ class OrionBlockchain {
Object.entries(query)
.forEach(([key, value]) => url.searchParams.append(key, value.toString()));
return fetchJsonWithValidation(url.toString(), atomicHistorySchema);
return fetchWithValidation(url.toString(), atomicHistorySchema);
}
getSourceAtomicSwapHistory(query: AtomicSwapHistorySourceQuery) {
@@ -237,7 +268,7 @@ class OrionBlockchain {
if (!query.type) url.searchParams.append('type', 'source');
return fetchJsonWithValidation(url.toString(), sourceAtomicHistorySchema);
return fetchWithValidation(url.toString(), sourceAtomicHistorySchema);
}
getTargetAtomicSwapHistory(query: AtomicSwapHistoryTargetQuery) {
@@ -248,11 +279,11 @@ class OrionBlockchain {
if (!query.type) url.searchParams.append('type', 'target');
return fetchJsonWithValidation(url.toString(), targetAtomicHistorySchema);
return fetchWithValidation(url.toString(), targetAtomicHistorySchema);
}
checkIfHashUsed(secretHashes: string[]) {
return fetchJsonWithValidation(
return fetchWithValidation(
`https://${this.apiUrl}/api/atomic/is-hash-used`,
z.record(z.boolean()).transform(utils.makePartial),
{

View File

@@ -1,4 +1,4 @@
import { fetchJsonWithValidation } from '../../fetchWithValidation';
import fetchWithValidation from '../../fetchWithValidation';
import candlesSchema from './schemas/candlesSchema';
class PriceFeed {
@@ -6,6 +6,8 @@ class PriceFeed {
constructor(apiUrl: string) {
this.apiUrl = apiUrl;
this.getCandles = this.getCandles.bind(this);
}
getCandles(
@@ -22,7 +24,7 @@ class PriceFeed {
url.searchParams.append('interval', interval);
url.searchParams.append('exchange', exchange);
return fetchJsonWithValidation(
return fetchWithValidation(
url.toString(),
candlesSchema,
);