Skip to content

Commit

Permalink
feat(PARTNER_ID): add PARTNER_ID
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonKozAllB committed Aug 6, 2024
1 parent d018b3b commit f676cd9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ The Allbridge Core REST API requires the following environment variables. You ca
- `https://api.trongrid.io/jsonrpc`
- `JUPITER_URL` - The URL of the Jupiter API. For example:
- `https://quote-api.jup.ag/v6`
- `PARTNER_ID` - Identifier for a partner. Greater than 0 and less than 0xffff (65535). For example:
- `17` or `0x11`

## How to get

Expand Down
38 changes: 35 additions & 3 deletions rest-api/src/service/sdk.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
AmountFormatted,
ChainDetailsMap,
ChainSymbol,
CheckAddressResponse,
CheckAllowanceParams,
ExtraGasMaxLimitResponse,
GasBalanceResponse,
Expand All @@ -30,7 +29,7 @@ import {
UserBalanceInfo,
} from '@allbridge/bridge-core-sdk';

import { Injectable } from '@nestjs/common';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import Big from 'big.js';
import { getLogger } from '../utils/logger-factory';
import { ConfigService } from './config.service';
Expand Down Expand Up @@ -76,19 +75,22 @@ export class SDKServiceBuilder {
const jupiterUrl = headers[`JUPITER_URL`] || headers[`jupiter_url`];
const coreApiHeaders = headers[`HEADERS`] || headers[`headers`];
const coreApiUrl = headers[`CORE_API_URL`] || headers[`core_api_url`];
const partnerId = headers[`PARTNER_ID`] || headers[`partner_id`];
if (
nodeRpcUrls ||
tronJsonRpc ||
jupiterUrl ||
coreApiHeaders ||
coreApiUrl
coreApiUrl ||
partnerId
) {
return new SDKService({
nodeRpcUrls,
tronJsonRpc,
jupiterUrl,
coreApiHeaders,
coreApiUrl,
partnerId,
});
}
}
Expand All @@ -113,6 +115,32 @@ export class SDKServiceBuilder {
}
}

export function validatePartnerId(partnerIdstr: string): number {
const decimalPattern = /^\d+$/;
const hexPattern = /^0x[0-9a-fA-F]+$/;
let partnerId: number;
if (decimalPattern.test(partnerIdstr)) {
partnerId = parseInt(partnerIdstr, 10); // Преобразуем из десятичной системы
} else if (hexPattern.test(partnerIdstr)) {
partnerId = parseInt(partnerIdstr, 16); // Преобразуем из шестнадцатеричной системы
} else {
throw new HttpException(
'Partner Id is not a number',
HttpStatus.BAD_REQUEST,
);
}
if (partnerId <= 0) {
throw new HttpException('Partner Id must be > 0', HttpStatus.BAD_REQUEST);
}
if (partnerId > 65535) {
throw new HttpException(
'Partner Id must be < 0xffff (65535)',
HttpStatus.BAD_REQUEST,
);
}
return partnerId;
}

export class SDKService {
sdk: AllbridgeCoreSdk;
logger = getLogger(`SDKService`);
Expand All @@ -124,6 +152,7 @@ export class SDKService {
jupiterUrl: string | undefined;
coreApiHeaders: string | undefined;
coreApiUrl: string | undefined;
partnerId: string | undefined;
}) {
this.sdk = new AllbridgeCoreSdk(
{
Expand All @@ -139,6 +168,9 @@ export class SDKService {
jupiterUrl: param?.jupiterUrl || ConfigService.getJupiterUrl(),
tronJsonRpc: param?.tronJsonRpc || ConfigService.getTronJsonRpc(),
coreApiUrl: param?.coreApiUrl || mainnet.coreApiUrl,
partnerId: param?.partnerId
? validatePartnerId(param.partnerId)
: undefined,
},
);
}
Expand Down

0 comments on commit f676cd9

Please sign in to comment.