Skip to content

Commit

Permalink
feat(REFERRAL_ID): add REFERRAL_ID;
Browse files Browse the repository at this point in the history
sdk update to 3.18.0-beta.5
  • Loading branch information
AntonKozAllB committed Aug 8, 2024
1 parent 15c3c1a commit d4e0265
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 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`
- `REFERRAL_ID` - Identifier for a referral. Greater than 0 and less than 0xffff (65535). For example:
- `17` or `0x11`

## How to get

Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 referralId = headers[`REFERRAL_ID`] || headers[`referral_id`];
if (
nodeRpcUrls ||
tronJsonRpc ||
jupiterUrl ||
coreApiHeaders ||
coreApiUrl
coreApiUrl ||
referralId
) {
return new SDKService({
nodeRpcUrls,
tronJsonRpc,
jupiterUrl,
coreApiHeaders,
coreApiUrl,
referralId,
});
}
}
Expand All @@ -113,6 +115,32 @@ export class SDKServiceBuilder {
}
}

export function validateReferralId(referralIdstr: string): number {
const decimalPattern = /^\d+$/;
const hexPattern = /^0x[0-9a-fA-F]+$/;
let referralId: number;
if (decimalPattern.test(referralIdstr)) {
referralId = parseInt(referralIdstr, 10);
} else if (hexPattern.test(referralIdstr)) {
referralId = parseInt(referralIdstr, 16);
} else {
throw new HttpException(
'Referral Id is not a number',
HttpStatus.BAD_REQUEST,
);
}
if (referralId <= 0) {
throw new HttpException('Referral Id must be > 0', HttpStatus.BAD_REQUEST);
}
if (referralId > 65535) {
throw new HttpException(
'Referral Id must be < 0xffff (65535)',
HttpStatus.BAD_REQUEST,
);
}
return referralId;
}

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;
referralId: 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,
referralId: param?.referralId
? validateReferralId(param.referralId)
: undefined,
},
);
}
Expand Down

0 comments on commit d4e0265

Please sign in to comment.