From d4e0265f0a4af507f0514a93f53f2b32f5023320 Mon Sep 17 00:00:00 2001 From: Kozer4 Date: Tue, 6 Aug 2024 13:03:12 +0300 Subject: [PATCH] feat(REFERRAL_ID): add REFERRAL_ID; sdk update to 3.18.0-beta.5 --- README.md | 2 ++ pnpm-lock.yaml | 8 +++--- rest-api/src/service/sdk.service.ts | 38 ++++++++++++++++++++++++++--- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 9dd96af..fc83ea5 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2a4815a..f24efce 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -74,7 +74,7 @@ importers: dependencies: '@allbridge/bridge-core-sdk': specifier: '*' - version: 3.18.0-beta.4(bufferutil@4.0.8)(fastestsmallesttextencoderdecoder@1.0.22)(utf-8-validate@5.0.10) + version: 3.18.0-beta.5(bufferutil@4.0.8)(fastestsmallesttextencoderdecoder@1.0.22)(utf-8-validate@5.0.10) '@allbridge/logger': specifier: ^2.4.1 version: 2.4.1(@nestjs/common@10.3.3(class-transformer@0.5.1)(class-validator@0.14.1)(reflect-metadata@0.2.2)(rxjs@7.8.1)) @@ -211,8 +211,8 @@ importers: packages: - '@allbridge/bridge-core-sdk@3.18.0-beta.4': - resolution: {integrity: sha512-hvCRGRIjYVhGXOz1MnnYL7a7zKSKGFVwoyDxgZC3N1jjXYNfG4mpSdpkadoxHxrbPjfZpwqzqMLhmCEiLPruNg==} + '@allbridge/bridge-core-sdk@3.18.0-beta.5': + resolution: {integrity: sha512-MVDf3rNYW3i2cYKLurbrZVx6QGARsYnqOQiL7w8idHxFL56gHcgJDDCvTVdRpkIGXsxPN/mtFozJG5Qh4QQmoQ==} engines: {node: ^18.7.0 || >= 16, npm: ^8.16.0 || >= 8} '@allbridge/logger@2.4.1': @@ -5898,7 +5898,7 @@ packages: snapshots: - '@allbridge/bridge-core-sdk@3.18.0-beta.4(bufferutil@4.0.8)(fastestsmallesttextencoderdecoder@1.0.22)(utf-8-validate@5.0.10)': + '@allbridge/bridge-core-sdk@3.18.0-beta.5(bufferutil@4.0.8)(fastestsmallesttextencoderdecoder@1.0.22)(utf-8-validate@5.0.10)': dependencies: '@project-serum/anchor': 0.25.0(bufferutil@4.0.8)(utf-8-validate@5.0.10) '@solana/spl-token': 0.3.11(@solana/web3.js@1.91.7(bufferutil@4.0.8)(utf-8-validate@5.0.10))(bufferutil@4.0.8)(fastestsmallesttextencoderdecoder@1.0.22)(utf-8-validate@5.0.10) diff --git a/rest-api/src/service/sdk.service.ts b/rest-api/src/service/sdk.service.ts index bdf860c..a6fd026 100644 --- a/rest-api/src/service/sdk.service.ts +++ b/rest-api/src/service/sdk.service.ts @@ -4,7 +4,6 @@ import { AmountFormatted, ChainDetailsMap, ChainSymbol, - CheckAddressResponse, CheckAllowanceParams, ExtraGasMaxLimitResponse, GasBalanceResponse, @@ -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'; @@ -76,12 +75,14 @@ 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, @@ -89,6 +90,7 @@ export class SDKServiceBuilder { jupiterUrl, coreApiHeaders, coreApiUrl, + referralId, }); } } @@ -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`); @@ -124,6 +152,7 @@ export class SDKService { jupiterUrl: string | undefined; coreApiHeaders: string | undefined; coreApiUrl: string | undefined; + referralId: string | undefined; }) { this.sdk = new AllbridgeCoreSdk( { @@ -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, }, ); }