Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert BigNumber changes #213

Merged
merged 3 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"packages": [
"packages/*"
],
"version": "7.0.0-alpha.11",
"version": "7.0.0-alpha.12",
"$schema": "node_modules/lerna/schemas/lerna-schema.json"
}
5 changes: 3 additions & 2 deletions packages/common/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@streamflow/common",
"version": "7.0.0-alpha.11",
"version": "7.0.0-alpha.12",
"description": "Common utilities and types used by streamflow packages.",
"homepage": "https://github.com/streamflow-finance/js-sdk/",
"main": "./dist/esm/index.js",
Expand Down Expand Up @@ -30,6 +30,7 @@
"gitHead": "a37306eba0e762af096db642fa22f07194014cfd",
"devDependencies": {
"@streamflow/eslint-config": "workspace:*",
"@types/bn.js": "5.1.1",
"date-fns": "2.28.0",
"typescript": "^4.9.5"
},
Expand All @@ -40,7 +41,7 @@
"@solana/wallet-adapter-base": "0.9.19",
"@solana/web3.js": "1.90.2",
"aptos": "1.21.0",
"bignumber.js": "^9.1.2",
"bn.js": "5.2.1",
"borsh": "^2.0.0",
"bs58": "5.0.0",
"p-queue": "^8.0.1"
Expand Down
4 changes: 2 additions & 2 deletions packages/common/solana/instructions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import {
createSyncNativeInstruction,
} from "@solana/spl-token";
import { Connection, PublicKey, SystemProgram, TransactionInstruction } from "@solana/web3.js";
import BigNumber from "bignumber.js";
import BN from "bn.js";

export const prepareWrappedAccount = async (
connection: Connection,
senderAddress: PublicKey,
amount: BigNumber,
amount: BN,
): Promise<TransactionInstruction[]> => {
const tokenAccount = await getAssociatedTokenAddress(NATIVE_MINT, senderAddress, true);

Expand Down
30 changes: 18 additions & 12 deletions packages/common/utils.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
import BigNumber from "bignumber.js";
import BN from "bn.js";

import { ContractError } from "./types.js";

/**
* Used for token amounts conversion from their Big Number representation to number.
* Get value in the highest units from BigNumber representation of the same value in the smallest units.
* @param {BigNumber} value - Big Number representation of value in the smallest units.
* Used for conversion of token amounts to their Big Number representation.
* Get Big Number representation in the smallest units from the same value in the highest units.
* @param {number} value - Number of tokens you want to convert to its BN representation.
* @param {number} decimals - Number of decimals the token has.
*/
export const getNumberFromBigNumber = (bigNum: BigNumber, decimals: number): number => {
return bigNum.div(BigNumber(10).pow(decimals)).toNumber();
export const getBN = (value: number, decimals: number): BN => {
const decimalPart = value - Math.trunc(value);
const integerPart = new BN(Math.trunc(value));

const decimalE = new BN(decimalPart * 1e9);

const sum = integerPart.mul(new BN(1e9)).add(decimalE);
const resultE = sum.mul(new BN(10).pow(new BN(decimals)));
return resultE.div(new BN(1e9));
};

/**
* Used for conversion of token amounts to their Big Number representation.
* Get Big Number representation in the smallest units from the same value in the highest units.
* @param {number} value - Number of tokens you want to convert to its BigNumber representation.
* Used for token amounts conversion from their Big Number representation to number.
* Get value in the highest units from BN representation of the same value in the smallest units.
* @param {BN} value - Big Number representation of value in the smallest units.
* @param {number} decimals - Number of decimals the token has.
*/
export const getScaledBigNumber = (value: number | string | BigNumber, decimals: number): BigNumber => {
return new BigNumber(value).times(new BigNumber(10).pow(decimals));
};
export const getNumberFromBN = (value: BN, decimals: number): number =>
value.gt(new BN(2 ** 53 - 1)) ? value.div(new BN(10 ** decimals)).toNumber() : value.toNumber() / 10 ** decimals;

/**
* Used to make on chain calls to the contract and wrap raised errors if any
Expand Down
6 changes: 3 additions & 3 deletions packages/distributor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const res = await client.create(
240, 192, 134, 208, 108, 246, 0, 191,
], // Merkle root
maxNumNodes: 4, // Number of recipients
maxTotalClaim: "4000000000", // Total amount to distribute
maxTotalClaim: new BN("4000000000"), // Total amount to distribute
unlockPeriod: 1, // Unlock period in seconds
startVestingTs: 0, // Timestamp when Airdrop starts
endVestingTs: now + 3600 * 24 * 7, // Timestamp when Airdrop ends
Expand Down Expand Up @@ -93,8 +93,8 @@ const claimRes = await client.claim(
43, 104, 75, 183, 12, 38, 37, 153,
],
], // Merkle Proof used to verify claim
amountUnlocked: "0", // Total amount unlocked for a Recipient
amountLocked: "1000000000", // Total amount locked for a Recipient
amountUnlocked: new BN("0"), // Total amount unlocked for a Recipient
amountLocked: new BN("1000000000"), // Total amount locked for a Recipient
},
solanaParams,
);
Expand Down
5 changes: 3 additions & 2 deletions packages/distributor/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@streamflow/distributor",
"version": "7.0.0-alpha.11",
"version": "7.0.0-alpha.12",
"description": "JavaScript SDK to interact with Streamflow Airdrop protocol.",
"homepage": "https://github.com/streamflow-finance/js-sdk/",
"main": "dist/esm/index.js",
Expand Down Expand Up @@ -29,6 +29,7 @@
"gitHead": "a37306eba0e762af096db642fa22f07194014cfd",
"devDependencies": {
"@streamflow/eslint-config": "workspace:*",
"@types/bn.js": "5.1.1",
"date-fns": "2.28.0",
"typescript": "^4.9.5"
},
Expand All @@ -39,7 +40,7 @@
"@solana/wallet-adapter-base": "0.9.19",
"@solana/web3.js": "1.90.2",
"@streamflow/common": "workspace:*",
"bignumber.js": "^9.1.2",
"bn.js": "5.2.1",
"borsh": "^2.0.0",
"bs58": "5.0.0",
"p-queue": "^8.0.1"
Expand Down
30 changes: 15 additions & 15 deletions packages/distributor/solana/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import bs58 from "bs58";
import BigNumber from "bignumber.js";
import BN from "bn.js";
import PQueue from "p-queue";
import {
ASSOCIATED_TOKEN_PROGRAM_ID,
Expand Down Expand Up @@ -140,14 +140,14 @@ export default class SolanaDistributorClient {
const senderTokens = await ata(mint, extParams.invoker.publicKey, tokenProgramId);

const args: NewDistributorArgs = {
version: BigNumber(data.version),
version: new BN(data.version),
root: data.root,
maxTotalClaim: BigNumber(data.maxTotalClaim),
maxNumNodes: BigNumber(data.maxNumNodes),
unlockPeriod: BigNumber(data.unlockPeriod),
startVestingTs: BigNumber(data.startVestingTs),
endVestingTs: BigNumber(data.endVestingTs),
clawbackStartTs: BigNumber(data.clawbackStartTs),
maxTotalClaim: new BN(data.maxTotalClaim),
maxNumNodes: new BN(data.maxNumNodes),
unlockPeriod: new BN(data.unlockPeriod),
startVestingTs: new BN(data.startVestingTs),
endVestingTs: new BN(data.endVestingTs),
clawbackStartTs: new BN(data.clawbackStartTs),
claimsClosable: data.claimsClosable,
};
const accounts: NewDistributorAccounts = {
Expand All @@ -163,14 +163,14 @@ export default class SolanaDistributorClient {

if (extParams.isNative) {
ixs.push(
...(await prepareWrappedAccount(this.connection, extParams.invoker.publicKey, BigNumber(data.maxTotalClaim))),
...(await prepareWrappedAccount(this.connection, extParams.invoker.publicKey, new BN(data.maxTotalClaim))),
);
}

const nowTs = BigNumber(Math.floor(Date.now() / 1000));
const nowTs = new BN(Math.floor(Date.now() / 1000));
const endVestingTs = args.endVestingTs.isZero() ? nowTs : args.endVestingTs;
const startVestingTs = args.startVestingTs.isZero() ? nowTs : args.startVestingTs;
if (endVestingTs.gt(startVestingTs) && endVestingTs.minus(startVestingTs).lt(args.unlockPeriod)) {
if (endVestingTs.gt(startVestingTs) && endVestingTs.sub(startVestingTs).lt(args.unlockPeriod)) {
throw new Error("The unlock period cannot be longer than the total vesting duration!");
}

Expand Down Expand Up @@ -282,17 +282,17 @@ export default class SolanaDistributorClient {

if (!claimStatus) {
const args: NewClaimArgs = {
amountLocked: BigNumber(data.amountLocked),
amountUnlocked: BigNumber(data.amountUnlocked),
amountLocked: new BN(data.amountLocked),
amountUnlocked: new BN(data.amountUnlocked),
proof: data.proof,
};
ixs.push(newClaim(args, accounts, this.programId));
}

const nowTs = BigNumber(Math.floor(Date.now() / 1000));
const nowTs = new BN(Math.floor(Date.now() / 1000));
if (
claimStatus ||
(BigNumber(data.amountLocked).gt(0) && nowTs.minus(distributor.startTs).gte(distributor.unlockPeriod))
(new BN(data.amountLocked).gtn(0) && nowTs.sub(distributor.startTs).gte(distributor.unlockPeriod))
) {
ixs.push(claimLocked(accounts, this.programId));
}
Expand Down
32 changes: 16 additions & 16 deletions packages/distributor/solana/generated/accounts/ClaimStatus.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PublicKey, Connection } from "@solana/web3.js";
import BigNumber from "bignumber.js"; // eslint-disable-line @typescript-eslint/no-unused-vars
import BN from "bn.js"; // eslint-disable-line @typescript-eslint/no-unused-vars
import * as borsh from "@coral-xyz/borsh"; // eslint-disable-line @typescript-eslint/no-unused-vars

import { PROGRAM_ID } from "../programId";
Expand All @@ -8,15 +8,15 @@ export interface ClaimStatusFields {
/** Authority that claimed the tokens. */
claimant: PublicKey;
/** Locked amount */
lockedAmount: BigNumber;
lockedAmount: BN;
/** Locked amount withdrawn */
lockedAmountWithdrawn: BigNumber;
lockedAmountWithdrawn: BN;
/** Unlocked amount */
unlockedAmount: BigNumber;
unlockedAmount: BN;
/** Last claim time */
lastClaimTs: BigNumber;
lastClaimTs: BN;
/** Track amount per unlock, can be useful for non-linear vesting */
lastAmountPerUnlock: BigNumber;
lastAmountPerUnlock: BN;
/** Whether claim is closed */
closed: boolean;
/** Buffer for additional fields */
Expand Down Expand Up @@ -52,19 +52,19 @@ export class ClaimStatus {
readonly claimant: PublicKey;

/** Locked amount */
readonly lockedAmount: BigNumber;
readonly lockedAmount: BN;

/** Locked amount withdrawn */
readonly lockedAmountWithdrawn: BigNumber;
readonly lockedAmountWithdrawn: BN;

/** Unlocked amount */
readonly unlockedAmount: BigNumber;
readonly unlockedAmount: BN;

/** Last claim time */
readonly lastClaimTs: BigNumber;
readonly lastClaimTs: BN;

/** Track amount per unlock, can be useful for non-linear vesting */
readonly lastAmountPerUnlock: BigNumber;
readonly lastAmountPerUnlock: BN;

/** Whether claim is closed */
readonly closed: boolean;
Expand Down Expand Up @@ -174,11 +174,11 @@ export class ClaimStatus {
static fromJSON(obj: ClaimStatusJSON): ClaimStatus {
return new ClaimStatus({
claimant: new PublicKey(obj.claimant),
lockedAmount: BigNumber(obj.lockedAmount),
lockedAmountWithdrawn: BigNumber(obj.lockedAmountWithdrawn),
unlockedAmount: BigNumber(obj.unlockedAmount),
lastClaimTs: BigNumber(obj.lastClaimTs),
lastAmountPerUnlock: BigNumber(obj.lastAmountPerUnlock),
lockedAmount: new BN(obj.lockedAmount),
lockedAmountWithdrawn: new BN(obj.lockedAmountWithdrawn),
unlockedAmount: new BN(obj.unlockedAmount),
lastClaimTs: new BN(obj.lastClaimTs),
lastAmountPerUnlock: new BN(obj.lastAmountPerUnlock),
closed: obj.closed,
buffer1: obj.buffer1,
buffer2: obj.buffer2,
Expand Down
56 changes: 28 additions & 28 deletions packages/distributor/solana/generated/accounts/MerkleDistributor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PublicKey, Connection } from "@solana/web3.js";
import BigNumber from "bignumber.js"; // eslint-disable-line @typescript-eslint/no-unused-vars
import BN from "bn.js"; // eslint-disable-line @typescript-eslint/no-unused-vars
import * as borsh from "@coral-xyz/borsh"; // eslint-disable-line @typescript-eslint/no-unused-vars

import { PROGRAM_ID } from "../programId";
Expand All @@ -8,29 +8,29 @@ export interface MerkleDistributorFields {
/** Bump seed. */
bump: number;
/** Version of the airdrop */
version: BigNumber;
version: BN;
/** The 256-bit merkle root. */
root: Array<number>;
/** [Mint] of the token to be distributed. */
mint: PublicKey;
/** Token Address of the vault */
tokenVault: PublicKey;
/** Maximum number of tokens that can ever be claimed from this [MerkleDistributor]. */
maxTotalClaim: BigNumber;
maxTotalClaim: BN;
/** Maximum number of nodes in [MerkleDistributor]. */
maxNumNodes: BigNumber;
maxNumNodes: BN;
/** Time step (period) in seconds per which the unlock occurs */
unlockPeriod: BigNumber;
unlockPeriod: BN;
/** Total amount of tokens that have been claimed. */
totalAmountClaimed: BigNumber;
totalAmountClaimed: BN;
/** Number of nodes that have been claimed. */
numNodesClaimed: BigNumber;
numNodesClaimed: BN;
/** Lockup time start (Unix Timestamp) */
startTs: BigNumber;
startTs: BN;
/** Lockup time end (Unix Timestamp) */
endTs: BigNumber;
endTs: BN;
/** Clawback start (Unix Timestamp) */
clawbackStartTs: BigNumber;
clawbackStartTs: BN;
/** Clawback receiver */
clawbackReceiver: PublicKey;
/** Admin wallet */
Expand Down Expand Up @@ -96,7 +96,7 @@ export class MerkleDistributor {
readonly bump: number;

/** Version of the airdrop */
readonly version: BigNumber;
readonly version: BN;

/** The 256-bit merkle root. */
readonly root: Array<number>;
Expand All @@ -108,28 +108,28 @@ export class MerkleDistributor {
readonly tokenVault: PublicKey;

/** Maximum number of tokens that can ever be claimed from this [MerkleDistributor]. */
readonly maxTotalClaim: BigNumber;
readonly maxTotalClaim: BN;

/** Maximum number of nodes in [MerkleDistributor]. */
readonly maxNumNodes: BigNumber;
readonly maxNumNodes: BN;

/** Time step (period) in seconds per which the unlock occurs */
readonly unlockPeriod: BigNumber;
readonly unlockPeriod: BN;

/** Total amount of tokens that have been claimed. */
readonly totalAmountClaimed: BigNumber;
readonly totalAmountClaimed: BN;

/** Number of nodes that have been claimed. */
readonly numNodesClaimed: BigNumber;
readonly numNodesClaimed: BN;

/** Lockup time start (Unix Timestamp) */
readonly startTs: BigNumber;
readonly startTs: BN;

/** Lockup time end (Unix Timestamp) */
readonly endTs: BigNumber;
readonly endTs: BN;

/** Clawback start (Unix Timestamp) */
readonly clawbackStartTs: BigNumber;
readonly clawbackStartTs: BN;

/** Clawback receiver */
readonly clawbackReceiver: PublicKey;
Expand Down Expand Up @@ -295,18 +295,18 @@ export class MerkleDistributor {
static fromJSON(obj: MerkleDistributorJSON): MerkleDistributor {
return new MerkleDistributor({
bump: obj.bump,
version: BigNumber(obj.version),
version: new BN(obj.version),
root: obj.root,
mint: new PublicKey(obj.mint),
tokenVault: new PublicKey(obj.tokenVault),
maxTotalClaim: BigNumber(obj.maxTotalClaim),
maxNumNodes: BigNumber(obj.maxNumNodes),
unlockPeriod: BigNumber(obj.unlockPeriod),
totalAmountClaimed: BigNumber(obj.totalAmountClaimed),
numNodesClaimed: BigNumber(obj.numNodesClaimed),
startTs: BigNumber(obj.startTs),
endTs: BigNumber(obj.endTs),
clawbackStartTs: BigNumber(obj.clawbackStartTs),
maxTotalClaim: new BN(obj.maxTotalClaim),
maxNumNodes: new BN(obj.maxNumNodes),
unlockPeriod: new BN(obj.unlockPeriod),
totalAmountClaimed: new BN(obj.totalAmountClaimed),
numNodesClaimed: new BN(obj.numNodesClaimed),
startTs: new BN(obj.startTs),
endTs: new BN(obj.endTs),
clawbackStartTs: new BN(obj.clawbackStartTs),
clawbackReceiver: new PublicKey(obj.clawbackReceiver),
admin: new PublicKey(obj.admin),
clawedBack: obj.clawedBack,
Expand Down
Loading
Loading