Skip to content

Commit

Permalink
fix(referralId): rename partnerId to referralId
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonKozAllB committed Aug 7, 2024
1 parent ee1cb31 commit 23fedf4
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 79 deletions.
12 changes: 6 additions & 6 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
import { formatAddress } from "../services/bridge/utils";

import { convertFloatAmountToInt, convertIntAmountToFloat, getFeePercent } from "../utils/calculation";
import { addPartnerIdToValue } from "../utils/utils";
import { addReferralIdToValue } from "../utils/utils";
import tokensGroupedByChain from "./data/tokens-info/ChainDetailsMap.json";
import tokenInfoWithChainDetailsGrl from "./data/tokens-info/TokenInfoWithChainDetails-GRL.json";
import tokenInfoWithChainDetailsTrx from "./data/tokens-info/TokenInfoWithChainDetails-TRX.json";
Expand Down Expand Up @@ -717,7 +717,7 @@ describe("SDK", () => {
expect(methodSendRawTransactionSpy).toHaveBeenCalledWith({
from: fromAccountAddress,
to: grlChainToken.bridgeAddress,
value: addPartnerIdToValue(fee),
value: addReferralIdToValue(fee),
data: expectedData,
});

Expand Down Expand Up @@ -797,7 +797,7 @@ describe("SDK", () => {
expect(methodTriggerSmartContractMock).toBeCalledWith(
trxChainToken.bridgeAddress,
"swapAndBridge(bytes32,uint256,bytes32,uint256,bytes32,uint256,uint8,uint256)",
{ callValue: addPartnerIdToValue(fee) },
{ callValue: addReferralIdToValue(fee) },
[
{
type: "bytes32",
Expand Down Expand Up @@ -893,7 +893,7 @@ describe("SDK", () => {
expect(methodSendRawTransactionSpy).toHaveBeenCalledWith({
from: fromAccountAddress,
to: grlChainToken.bridgeAddress,
value: addPartnerIdToValue("0"),
value: addReferralIdToValue("0"),
data: expectedData,
});

Expand Down Expand Up @@ -936,7 +936,7 @@ describe("SDK", () => {
data: "0x331838b20000000000000000000000000000000000000000000000008ac7230489e80000000000000000000000000000ddac3cb57dea3fbeff4997d78215535eb5787117000000000000000000000000c7dbc4a896b34b7a10dda2ef72052145a9122f4300000000000000000000000068d7ed9cf9881427f1db299b90fd63ef805dd10d0000000000000000000000000000000000000000000000006124fee993bc0000",
from: "0x68D7ed9cf9881427F1dB299B90Fd63ef805dd10d",
to: "0xba285A8F52601EabCc769706FcBDe2645aa0AF18",
value: addPartnerIdToValue("0"),
value: addReferralIdToValue("0"),
});
});
test("rawTxBuilder should build swapTx correctly for trx", async () => {
Expand Down Expand Up @@ -969,7 +969,7 @@ describe("SDK", () => {
expect(methodTriggerSmartContractMock).toBeCalledWith(
trxChainToken.bridgeAddress,
"swap(uint256,bytes32,bytes32,address,uint256)",
{ callValue: addPartnerIdToValue("0") },
{ callValue: addReferralIdToValue("0") },
[
{
type: "uint256",
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/services/bridge/evm/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { FeePaymentMethod, SendParams, TokenWithChainDetails } from "../../../..
import { NodeRpcUrlsConfig } from "../../../../services";
import { EvmBridgeService } from "../../../../services/bridge/evm";
import { ChainDetailsMapWithFlags } from "../../../../tokens-info";
import { addPartnerIdToValue } from "../../../../utils/utils";
import { addReferralIdToValue } from "../../../../utils/utils";
import tokensGroupedByChain from "../../../data/tokens-info/ChainDetailsMap-ETH-USDT.json";
import { mockNonce } from "../../../mock/bridge/utils";
import { initChainsWithTestnet } from "../../../mock/utils";
Expand Down Expand Up @@ -65,7 +65,7 @@ describe("EvmBridge", () => {
expect(actual).toEqual({
from: from,
to: bridgeAddress,
value: addPartnerIdToValue(gasFee),
value: addReferralIdToValue(gasFee),
data: "0x4cd480bd000000000000000000000000c7dbc4a896b34b7a10dda2ef72052145a9122f4300000000000000000000000000000000000000000000000012751bf40f450000000000000000000000000000ba285a8f52601eabcc769706fcbde2645aa0af180000000000000000000000000000000000000000000000000000000000000004000000000000000000000000b10388f04f8331b59a02732cc1b6ac0d7045574b3b1200153e110000001b006132000000000000000000362600611e000000070c00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000",
});
});
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/services/bridge/trx/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ChainSymbol, FeePaymentMethod } from "../../../../models";
import { TxSendParams } from "../../../../services/bridge/models";
import { TronBridgeService } from "../../../../services/bridge/trx";
import { formatAddress } from "../../../../services/bridge/utils";
import { addPartnerIdToValue } from "../../../../utils/utils";
import { addReferralIdToValue } from "../../../../utils/utils";
import { mockNonce } from "../../../mock/bridge/utils";
import triggerSmartContractSendResponse from "../../../mock/tron-web/trigger-smart-contract-send.json";

Expand Down Expand Up @@ -74,7 +74,7 @@ describe("TrxBridge", () => {
expect(tronWebMock.transactionBuilder.triggerSmartContract).toHaveBeenCalledWith(
bridgeAddress,
"swapAndBridge(bytes32,uint256,bytes32,uint256,bytes32,uint256,uint8,uint256)",
{ callValue: addPartnerIdToValue(gasFee) },
{ callValue: addReferralIdToValue(gasFee) },
[
{
type: "bytes32",
Expand Down
76 changes: 38 additions & 38 deletions src/__tests__/utils/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import { InvalidAmountError } from "../../exceptions";
import { addPartnerIdToValue, validatePartnerId } from "../../utils/utils";
import { addReferralIdToValue, validateReferralId } from "../../utils/utils";

describe("validatePartnerId", () => {
it("should not throw an error if partnerId is not provided", () => {
expect(() => validatePartnerId()).not.toThrow();
describe("validateReferralId", () => {
it("should not throw an error if referralId is not provided", () => {
expect(() => validateReferralId()).not.toThrow();
});

it("should not throw an error if partnerId is within valid range", () => {
expect(() => validatePartnerId(1)).not.toThrow();
expect(() => validatePartnerId(65535)).not.toThrow();
expect(() => validatePartnerId(0xffff)).not.toThrow();
it("should not throw an error if referralId is within valid range", () => {
expect(() => validateReferralId(1)).not.toThrow();
expect(() => validateReferralId(65535)).not.toThrow();
expect(() => validateReferralId(0xffff)).not.toThrow();
});

it("should throw an error if partnerId is less than 0", () => {
expect(() => validatePartnerId(-1)).toThrow(InvalidAmountError);
expect(() => validatePartnerId(-1)).toThrow("Partner Id must be > 0");
expect(() => validatePartnerId(0)).toThrow("Partner Id must be > 0");
it("should throw an error if referralId is less than 0", () => {
expect(() => validateReferralId(-1)).toThrow(InvalidAmountError);
expect(() => validateReferralId(-1)).toThrow("Referral Id must be > 0");
expect(() => validateReferralId(0)).toThrow("Referral Id must be > 0");
});

it("should throw an error if partnerId is greater than 65535", () => {
expect(() => validatePartnerId(65536)).toThrow(InvalidAmountError);
expect(() => validatePartnerId(65536)).toThrow("Partner Id must be < 0xffff (65535)");
expect(() => validatePartnerId(0x10000)).toThrow("Partner Id must be < 0xffff (65535)");
it("should throw an error if referralId is greater than 65535", () => {
expect(() => validateReferralId(65536)).toThrow(InvalidAmountError);
expect(() => validateReferralId(65536)).toThrow("Referral Id must be <= 0xffff (65535)");
expect(() => validateReferralId(0x10000)).toThrow("Referral Id must be <= 0xffff (65535)");
});
});

describe("addPartnerIdToValue", () => {
describe("no partnerId", () => {
describe("addReferralIdToValue", () => {
describe("no referralId", () => {
const testCases = [
{ input: "0", expected: "0x0" },
{ input: "100", expected: "0x0" },
Expand All @@ -36,32 +36,32 @@ describe("addPartnerIdToValue", () => {
{ input: "6175000", expected: "0x5e0000" },
];

test.each(testCases)("no partnerId $input", ({ input, expected }) => {
const result = addPartnerIdToValue(input);
test.each(testCases)("no referralId $input", ({ input, expected }) => {
const result = addReferralIdToValue(input);
expect(result).toEqual(expected);
});
});
describe("with partnerId", () => {
describe("with referralId", () => {
const testCases = [
{ input: "0", expected: "0x1", partnerId: 0x1 },
{ input: "0", expected: "0x123", partnerId: 0x123 },
{ input: "0", expected: "0x1234", partnerId: 0x1234 },
{ input: "0", expected: "0xffff", partnerId: 65535 },
{ input: "100", expected: "0x1", partnerId: 0x1 },
{ input: "55000", expected: "0x1", partnerId: 0x1 },
{ input: "55000", expected: "0x123", partnerId: 0x123 },
{ input: "55000", expected: "0x1234", partnerId: 0x1234 },
{ input: "75000", expected: "0x10123", partnerId: 0x123 },
{ input: "175000", expected: "0x21234", partnerId: 0x1234 },
{ input: "175000", expected: "0x2ffff", partnerId: 65535 },
{ input: "6175123", expected: "0x5e0001", partnerId: 0x1 },
{ input: "6175123", expected: "0x5e0123", partnerId: 0x123 },
{ input: "6175123", expected: "0x5e1234", partnerId: 0x1234 },
{ input: "6175123", expected: "0x5effff", partnerId: 65535 },
{ input: "0", expected: "0x1", referralId: 0x1 },
{ input: "0", expected: "0x123", referralId: 0x123 },
{ input: "0", expected: "0x1234", referralId: 0x1234 },
{ input: "0", expected: "0xffff", referralId: 65535 },
{ input: "100", expected: "0x1", referralId: 0x1 },
{ input: "55000", expected: "0x1", referralId: 0x1 },
{ input: "55000", expected: "0x123", referralId: 0x123 },
{ input: "55000", expected: "0x1234", referralId: 0x1234 },
{ input: "75000", expected: "0x10123", referralId: 0x123 },
{ input: "175000", expected: "0x21234", referralId: 0x1234 },
{ input: "175000", expected: "0x2ffff", referralId: 65535 },
{ input: "6175123", expected: "0x5e0001", referralId: 0x1 },
{ input: "6175123", expected: "0x5e0123", referralId: 0x123 },
{ input: "6175123", expected: "0x5e1234", referralId: 0x1234 },
{ input: "6175123", expected: "0x5effff", referralId: 65535 },
];

test.each(testCases)("partnerId $partnerId input $input", ({ input, expected, partnerId }) => {
const result = addPartnerIdToValue(input, partnerId);
test.each(testCases)("referralId $referralId input $input", ({ input, expected, referralId }) => {
const result = addReferralIdToValue(input, referralId);
expect(result).toEqual(expected);
});
});
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ export interface AllbridgeCoreSdkOptions {
additionalChainsProperties?: Record<string, BasicChainProperties>;

/**
* Represents an optional identifier for a partner.
* Represents an optional identifier for a referral.
*
* @property {number} [partnerId] - The partner ID. This number should be greater than 0 and less than 0xffff (65535).
* @property {number} [referral] - The referral ID. This number should be greater than 0 and less than 0xffff (65535).
*/
partnerId?: number;
referralId?: number;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/services/bridge/evm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
SwapParams,
TransactionResponse,
} from "../../../models";
import { addPartnerIdToValue } from "../../../utils/utils";
import { addReferralIdToValue } from "../../../utils/utils";
import { NodeRpcUrlsConfig } from "../../index";
import { RawTransaction } from "../../models";
import bridgeAbi from "../../models/abi/Bridge.json";
Expand Down Expand Up @@ -76,7 +76,7 @@ export class EvmBridgeService extends ChainBridgeService {
return Promise.resolve({
from: fromAccountAddress,
to: contractAddress,
value: addPartnerIdToValue("0", this.sdkOptions.partnerId),
value: addReferralIdToValue("0", this.sdkOptions.referralId),
data: swapMethod.encodeABI(),
});
}
Expand Down Expand Up @@ -141,7 +141,7 @@ export class EvmBridgeService extends ChainBridgeService {
return Promise.resolve({
from: fromAccountAddress,
to: contractAddress,
value: addPartnerIdToValue(value, this.sdkOptions.partnerId),
value: addReferralIdToValue(value, this.sdkOptions.referralId),
data: sendMethod.encodeABI(),
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/services/bridge/trx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { AllbridgeCoreClient } from "../../../client/core-api/core-client-base";
import { SdkError } from "../../../exceptions";
import { AllbridgeCoreSdkOptions } from "../../../index";
import { FeePaymentMethod, Messenger, SwapParams, TransactionResponse } from "../../../models";
import { addPartnerIdToValue } from "../../../utils/utils";
import { addReferralIdToValue } from "../../../utils/utils";
import { RawTransaction, SmartContractMethodParameter } from "../../models";
import { sendRawTransaction } from "../../utils/trx";
import { SendParams, TxSendParams, TxSwapParams } from "../models";
Expand Down Expand Up @@ -147,7 +147,7 @@ export class TronBridgeService extends ChainBridgeService {
contractAddress,
methodSignature,
{
callValue: addPartnerIdToValue(value, this.sdkOptions.partnerId),
callValue: addReferralIdToValue(value, this.sdkOptions.referralId),
},
parameters,
fromAddress
Expand Down
4 changes: 2 additions & 2 deletions src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import {
swapAndBridgeFeeCalculation,
swapAndBridgeFeeCalculationReverse,
} from "../utils/calculation/swap-and-bridge-fee-calc";
import { getPoolInfoByToken, validateAmountDecimals, validateAmountGtZero, validatePartnerId } from "../utils/utils";
import { getPoolInfoByToken, validateAmountDecimals, validateAmountGtZero, validateReferralId } from "../utils/utils";
import { BridgeService, DefaultBridgeService } from "./bridge";
import { GetNativeTokenBalanceParams } from "./bridge/models";
import { getExtraGasMaxLimits, getGasFeeOptions } from "./bridge/utils";
Expand Down Expand Up @@ -81,7 +81,7 @@ export class AllbridgeCoreSdkService {
pool: LiquidityPoolService;

constructor(nodeRpcUrlsConfig: NodeRpcUrlsConfig, params: AllbridgeCoreSdkOptions = mainnet) {
validatePartnerId(params.partnerId);
validateReferralId(params.referralId);
Chains.addChainsProperties(params.additionalChainsProperties);
const apiClient = new ApiClientImpl(params);
const apiClientCaching = new ApiClientCaching(apiClient);
Expand Down
37 changes: 16 additions & 21 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,36 @@
import {Big, BigSource} from "big.js";
import { Big, BigSource } from "big.js";
import BN from "bn.js";
import {AllbridgeCoreClientPoolsExt} from "../client/core-api/core-client-pool-info-caching";
import {ArgumentInvalidDecimalsError, InvalidAmountError, TimeoutError} from "../exceptions";
import {PoolInfo, TokenWithChainDetails} from "../tokens-info";

export function validatePartnerId(partnerId?: number): void {
if (partnerId || partnerId === 0) {
if (partnerId <= 0) {
throw new InvalidAmountError("Partner Id must be > 0");
import { AllbridgeCoreClientPoolsExt } from "../client/core-api/core-client-pool-info-caching";
import { ArgumentInvalidDecimalsError, InvalidAmountError, TimeoutError } from "../exceptions";
import { PoolInfo, TokenWithChainDetails } from "../tokens-info";

export function validateReferralId(referralId?: number): void {
if (referralId || referralId === 0) {
if (referralId <= 0) {
throw new InvalidAmountError("Referral Id must be > 0");
}
if (partnerId > 65535) {
throw new InvalidAmountError("Partner Id must be <= 0xffff (65535)");
if (referralId > 65535) {
throw new InvalidAmountError("Referral Id must be <= 0xffff (65535)");
}
}
}

export function addPartnerIdToValue(value: string, partnerId?: number): string {
export function addReferralIdToValue(value: string, referralId?: number): string {
let number = new BN(value, 10);
let hexString = number.toString("hex").padStart(6, "0");

hexString = hexString.slice(0, -4);

if (partnerId) {
const partnerIdHex = new BN(partnerId).toString(16).padStart(4, "0");
hexString += partnerIdHex.slice(0, 2) + partnerIdHex.slice(2);
if (referralId) {
const referralIdHex = new BN(referralId).toString(16).padStart(4, "0");
hexString += referralIdHex.slice(0, 2) + referralIdHex.slice(2);
} else {
hexString += "0000";
}

hexString = hexString.replace(/^0+/, "");
number = new BN(hexString, "hex");
const result = "0x" + number.toString("hex").padStart(hexString.length, "0");
console.log(`addPartnerIdToValue ${value} -> ${result} (${new BN(
result.replace(/^0x+/, ""),
"hex"
).toString(10)}))`)
return result;
return "0x" + number.toString("hex").padStart(hexString.length, "0");
}

export async function getPoolInfoByToken(
Expand Down

0 comments on commit 23fedf4

Please sign in to comment.