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

Switch to using bignumber.js library #186

Closed
wants to merge 7 commits into from
Closed
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
1 change: 1 addition & 0 deletions .github/workflows/gh-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
branches:
- master
- v6
- prerelease/v*

jobs:
tests:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/gh-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ on:
branches:
- master
- v6
- v7
types: [closed]

permissions:
Expand Down
7 changes: 5 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"prettier.requireConfig": true,
"prettier.requireConfig": false,
"eslint.enable": true,
"eslint.debug": false,
"eslint.workingDirectories": [
Expand All @@ -10,5 +10,8 @@
],
"cSpell.words": [
"Streamflow"
]
],
"typescript.tsdk": "node_modules/typescript/lib",
"typescript.preferences.importModuleSpecifierEnding": "js",
"javascript.preferences.importModuleSpecifierEnding": "js",
}
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": "6.3.8",
"version": "7.0.0-alpha.1",
"$schema": "node_modules/lerna/schemas/lerna-schema.json"
}
4 changes: 2 additions & 2 deletions packages/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from "./types";
export * from "./utils";
export * from "./types.js";
export * from "./utils.js";
31 changes: 18 additions & 13 deletions packages/common/package.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
{
"name": "@streamflow/common",
"version": "6.3.8",
"version": "7.0.0-alpha.1",
"description": "Common utilities and types used by streamflow packages.",
"homepage": "https://github.com/streamflow-finance/js-sdk/",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"main": "./dist/esm/index.js",
"types": "./dist/esm/index.d.ts",
"type": "module",
"exports": {
".": "./dist/index.js",
"./solana": "./dist/solana/index.js"
},
"typesVersions": {
"*": {
"solana": [
"dist/solana/index.d.ts"
]
".": {
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js",
"types": "./dist/esm/index.d.ts"
},
"./solana": {
"import": "./dist/esm/solana/index.js",
"require": "./dist/cjs/solana/index.js",
"types": "./dist/esm/solana/index.d.ts"
}
},
"scripts": {
"build": "rm -rf dist; tsc -p tsconfig.json",
"build:cjs": "rm -rf dist/cjs; tsc -p tsconfig.cjs.json",
"build:esm": "rm -rf dist/esm; tsc -p tsconfig.esm.json",
"build": "rm -rf dist; pnpm run build:cjs && pnpm run build:esm",
"pack": "pnpm build && pnpm pack",
"lint": "eslint --fix .",
"lint-config": "eslint --print-config",
Expand All @@ -40,9 +44,10 @@
"@solana/wallet-adapter-base": "0.9.19",
"@solana/web3.js": "1.70.1",
"aptos": "1.4.0",
"bignumber.js": "^9.1.2",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do we want to update our public API?
How do we want to version this?
This is just breaking change. We bump major and update README if needed

"bn.js": "5.2.1",
"borsh": "^2.0.0",
"bs58": "5.0.0",
"p-queue": "^6.6.2"
"p-queue": "^8.0.1"
}
}
6 changes: 3 additions & 3 deletions packages/common/solana/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from "./instructions";
export * from "./types";
export * from "./utils";
export * from "./instructions.js";
export * from "./types.js";
export * from "./utils.js";
7 changes: 4 additions & 3 deletions packages/common/solana/instructions.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import {
getAssociatedTokenAddress,
NATIVE_MINT,
createAssociatedTokenAccountInstruction,
createSyncNativeInstruction,
getAssociatedTokenAddress,
} from "@solana/spl-token";
import { Connection, PublicKey, SystemProgram, TransactionInstruction } from "@solana/web3.js";
import BN from "bn.js";
import BigNumber from "bignumber.js";

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

Expand All @@ -26,6 +26,7 @@ export const prepareWrappedAccount = async (
SystemProgram.transfer({
fromPubkey: senderAddress,
toPubkey: tokenAccount,
// pow 10 by decimals - probably since getNumberFromBN wasn't used
lamports: amount.toNumber(),
}),
createSyncNativeInstruction(tokenAccount),
Expand Down
8 changes: 4 additions & 4 deletions packages/common/solana/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ import {
ITransactionSolanaExt,
ThrottleParams,
TransactionFailedError,
} from "./types";
import { sleep } from "../utils";
} from "./types.js";
import { sleep } from "../utils.js";

const SIMULATE_TRIES = 3;

Expand Down Expand Up @@ -254,7 +254,7 @@ export async function sendAndConfirmTransaction(

let signature: string;
if (isVersioned) {
signature = bs58.encode(tx.signatures[0]);
signature = bs58.encode(tx.signatures[0]!);
} else {
signature = bs58.encode(tx.signature!);
}
Expand Down Expand Up @@ -513,7 +513,7 @@ export async function checkOrCreateAtaBatch(
const response = await connection.getMultipleAccountsInfo(atas);
for (let i = 0; i < response.length; i++) {
if (!response[i]) {
ixs.push(createAssociatedTokenAccountInstruction(invoker.publicKey!, atas[i], owners[i], mint, programId));
ixs.push(createAssociatedTokenAccountInstruction(invoker.publicKey!, atas[i]!, owners[i]!, mint, programId));
}
}
return ixs;
Expand Down
6 changes: 6 additions & 0 deletions packages/common/tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../../tsconfig.cjs.json",
"compilerOptions": {
"outDir": "./dist/cjs"
}
}
6 changes: 6 additions & 0 deletions packages/common/tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../../tsconfig.esm.json",
"compilerOptions": {
"outDir": "./dist/esm"
}
}
6 changes: 0 additions & 6 deletions packages/common/tsconfig.json

This file was deleted.

3 changes: 1 addition & 2 deletions packages/common/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { TransactionInstruction } from "@solana/web3.js";
import { Types } from "aptos";

export interface ITransactionResult {
ixs: (TransactionInstruction | Types.TransactionPayload)[];
ixs: TransactionInstruction[];
txId: string;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/common/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import BN from "bn.js";

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

/**
* Used for conversion of token amounts to their Big Number representation.
Expand Down
31 changes: 18 additions & 13 deletions packages/distributor/package.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
{
"name": "@streamflow/distributor",
"version": "6.3.8",
"version": "7.0.0-alpha.1",
"description": "JavaScript SDK to interact with Streamflow Airdrop protocol.",
"homepage": "https://github.com/streamflow-finance/js-sdk/",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"main": "dist/esm/index.js",
"types": "dist/esm/index.d.ts",
"type": "module",
"exports": {
".": "./dist/index.js",
"./solana": "./dist/solana/index.js"
},
"typesVersions": {
"*": {
"solana": [
"dist/solana/index.d.ts"
]
".": {
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js",
"types": "./dist/esm/index.d.ts"
},
"./solana": {
"import": "./dist/esm/solana/index.js",
"require": "./dist/cjs/solana/index.js",
"types": "./dist/esm/solana/index.d.ts"
}
},
"scripts": {
"build": "rm -rf dist; tsc -p tsconfig.json",
"build:cjs": "rm -rf dist/cjs; tsc -p tsconfig.cjs.json",
"build:esm": "rm -rf dist/esm; tsc -p tsconfig.esm.json",
"build": "rm -rf dist; pnpm run build:cjs && pnpm run build:esm",
"pack": "pnpm build && pnpm pack",
"lint": "eslint --fix .",
"prepublishOnly": "npm run lint && npm run build"
Expand All @@ -39,9 +43,10 @@
"@solana/wallet-adapter-base": "0.9.19",
"@solana/web3.js": "1.70.1",
"@streamflow/common": "workspace:*",
"bignumber.js": "^9.1.2",
"bn.js": "5.2.1",
"borsh": "^2.0.0",
"bs58": "5.0.0",
"p-queue": "^6.6.2"
"p-queue": "^8.0.1"
}
}
24 changes: 12 additions & 12 deletions packages/distributor/solana/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import BN from "bn.js";
import BigNumber from "bignumber.js";
import PQueue from "p-queue";
import {
ASSOCIATED_TOKEN_PROGRAM_ID,
Expand Down Expand Up @@ -136,14 +136,14 @@ export default class SolanaDistributorClient {
const senderTokens = await ata(mint, extParams.invoker.publicKey, tokenProgramId);

const args: NewDistributorArgs = {
version: new BN(data.version, 10),
version: BigNumber(data.version),
root: data.root,
maxTotalClaim: data.maxTotalClaim,
maxNumNodes: data.maxNumNodes,
unlockPeriod: new BN(data.unlockPeriod, 10),
startVestingTs: new BN(data.startVestingTs, 10),
endVestingTs: new BN(data.endVestingTs, 10),
clawbackStartTs: new BN(data.clawbackStartTs, 10),
unlockPeriod: BigNumber(data.unlockPeriod),
startVestingTs: BigNumber(data.startVestingTs),
endVestingTs: BigNumber(data.endVestingTs),
clawbackStartTs: BigNumber(data.clawbackStartTs),
claimsClosable: data.claimsClosable,
};
const accounts: NewDistributorAccounts = {
Expand All @@ -161,10 +161,10 @@ export default class SolanaDistributorClient {
ixs.push(...(await prepareWrappedAccount(this.connection, extParams.invoker.publicKey, data.maxTotalClaim)));
}

const nowTs = new BN(Math.floor(Date.now() / 1000));
const endVestingTs = args.endVestingTs.eqn(0) ? nowTs : args.endVestingTs;
const startVestingTs = args.startVestingTs.eqn(0) ? nowTs : args.startVestingTs;
if (endVestingTs.gt(startVestingTs) && endVestingTs.sub(startVestingTs).lt(args.unlockPeriod)) {
const nowTs = BigNumber(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)) {
throw new Error("The unlock period cannot be longer than the total vesting duration!");
}

Expand Down Expand Up @@ -283,8 +283,8 @@ export default class SolanaDistributorClient {
ixs.push(newClaim(args, accounts, this.programId));
}

const nowTs = new BN(Math.floor(Date.now() / 1000));
if (claimStatus || (data.amountLocked.gtn(0) && nowTs.sub(distributor.startTs).gte(distributor.unlockPeriod))) {
const nowTs = BigNumber(Math.floor(Date.now() / 1000));
if (claimStatus || (data.amountLocked.gt(0) && nowTs.minus(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 BN from "bn.js"; // eslint-disable-line @typescript-eslint/no-unused-vars
import BigNumber from "bignumber.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: BN;
lockedAmount: BigNumber;
/** Locked amount withdrawn */
lockedAmountWithdrawn: BN;
lockedAmountWithdrawn: BigNumber;
/** Unlocked amount */
unlockedAmount: BN;
unlockedAmount: BigNumber;
/** Last claim time */
lastClaimTs: BN;
lastClaimTs: BigNumber;
/** Track amount per unlock, can be useful for non-linear vesting */
lastAmountPerUnlock: BN;
lastAmountPerUnlock: BigNumber;
/** 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: BN;
readonly lockedAmount: BigNumber;

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

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

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

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

/** 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: new BN(obj.lockedAmount),
lockedAmountWithdrawn: new BN(obj.lockedAmountWithdrawn),
unlockedAmount: new BN(obj.unlockedAmount),
lastClaimTs: new BN(obj.lastClaimTs),
lastAmountPerUnlock: new BN(obj.lastAmountPerUnlock),
lockedAmount: BigNumber(obj.lockedAmount),
lockedAmountWithdrawn: BigNumber(obj.lockedAmountWithdrawn),
unlockedAmount: BigNumber(obj.unlockedAmount),
lastClaimTs: BigNumber(obj.lastClaimTs),
lastAmountPerUnlock: BigNumber(obj.lastAmountPerUnlock),
closed: obj.closed,
buffer1: obj.buffer1,
buffer2: obj.buffer2,
Expand Down
Loading
Loading