@streamflow/common 7.0.0-alpha.2 → 7.0.0-alpha.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.js +2 -1
- package/dist/cjs/lib/assertions.js +13 -0
- package/dist/cjs/lib/utils.js +62 -0
- package/dist/cjs/solana/account-filters.js +19 -0
- package/dist/cjs/solana/index.js +2 -0
- package/dist/cjs/solana/instructions.js +18 -88
- package/dist/cjs/solana/public-key.js +8 -0
- package/dist/cjs/solana/types.js +6 -24
- package/dist/cjs/solana/utils.js +233 -513
- package/dist/cjs/types.js +9 -27
- package/dist/esm/index.d.ts +2 -1
- package/dist/esm/index.js +2 -1
- package/dist/esm/lib/assertions.d.ts +1 -0
- package/dist/esm/lib/assertions.js +9 -0
- package/dist/esm/{utils.d.ts → lib/utils.d.ts} +10 -9
- package/dist/esm/{utils.js → lib/utils.js} +17 -13
- package/dist/esm/solana/account-filters.d.ts +2 -0
- package/dist/esm/solana/account-filters.js +15 -0
- package/dist/esm/solana/index.d.ts +2 -0
- package/dist/esm/solana/index.js +2 -0
- package/dist/esm/solana/instructions.d.ts +2 -2
- package/dist/esm/solana/public-key.d.ts +2 -0
- package/dist/esm/solana/public-key.js +4 -0
- package/dist/esm/solana/types.d.ts +4 -0
- package/dist/esm/solana/utils.js +6 -5
- package/package.json +8 -7
- package/dist/cjs/utils.js +0 -102
package/dist/cjs/types.js
CHANGED
|
@@ -1,19 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
if (typeof b !== "function" && b !== null)
|
|
11
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
-
extendStatics(d, b);
|
|
13
|
-
function __() { this.constructor = d; }
|
|
14
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
-
};
|
|
16
|
-
})();
|
|
17
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
3
|
exports.ContractError = exports.IChain = exports.ICluster = void 0;
|
|
19
4
|
// Utility types
|
|
@@ -36,23 +21,20 @@ var IChain;
|
|
|
36
21
|
/**
|
|
37
22
|
* Error wrapper for calls made to the contract on chain
|
|
38
23
|
*/
|
|
39
|
-
|
|
40
|
-
__extends(ContractError, _super);
|
|
24
|
+
class ContractError extends Error {
|
|
41
25
|
/**
|
|
42
26
|
* Constructs the Error Wrapper
|
|
43
27
|
* @param error Original error raised probably by the chain SDK
|
|
44
28
|
* @param code extracted code from the error if managed to parse it
|
|
45
29
|
*/
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
30
|
+
constructor(error, code, description) {
|
|
31
|
+
super(error.message); // Call the base class constructor with the error message
|
|
32
|
+
this.contractErrorCode = code ?? null;
|
|
33
|
+
this.description = description ?? null;
|
|
50
34
|
// Copy properties from the original error
|
|
51
|
-
Object.setPrototypeOf(
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
return _this;
|
|
35
|
+
Object.setPrototypeOf(this, ContractError.prototype);
|
|
36
|
+
this.name = "ContractError"; // Set the name property
|
|
37
|
+
this.stack = error.stack;
|
|
55
38
|
}
|
|
56
|
-
|
|
57
|
-
}(Error));
|
|
39
|
+
}
|
|
58
40
|
exports.ContractError = ContractError;
|
package/dist/esm/index.d.ts
CHANGED
package/dist/esm/index.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const invariant: (condition: any, message?: string | (() => string)) => asserts condition;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
const prefix = "Assertion failed";
|
|
2
|
+
export const invariant = (condition, message) => {
|
|
3
|
+
if (condition) {
|
|
4
|
+
return;
|
|
5
|
+
}
|
|
6
|
+
const provided = typeof message === "function" ? message() : message;
|
|
7
|
+
const value = provided ? `${prefix}: ${provided}` : prefix;
|
|
8
|
+
throw new Error(value);
|
|
9
|
+
};
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import
|
|
1
|
+
import BN from "bn.js";
|
|
2
2
|
/**
|
|
3
|
-
* Used for token amounts
|
|
4
|
-
* Get
|
|
5
|
-
* @param {
|
|
3
|
+
* Used for conversion of token amounts to their Big Number representation.
|
|
4
|
+
* Get Big Number representation in the smallest units from the same value in the highest units.
|
|
5
|
+
* @param {number} value - Number of tokens you want to convert to its BN representation.
|
|
6
6
|
* @param {number} decimals - Number of decimals the token has.
|
|
7
7
|
*/
|
|
8
|
-
export declare const
|
|
8
|
+
export declare const getBN: (value: number, decimals: number) => BN;
|
|
9
9
|
/**
|
|
10
|
-
* Used for
|
|
11
|
-
* Get
|
|
12
|
-
* @param {
|
|
10
|
+
* Used for token amounts conversion from their Big Number representation to number.
|
|
11
|
+
* Get value in the highest units from BN representation of the same value in the smallest units.
|
|
12
|
+
* @param {BN} value - Big Number representation of value in the smallest units.
|
|
13
13
|
* @param {number} decimals - Number of decimals the token has.
|
|
14
14
|
*/
|
|
15
|
-
export declare const
|
|
15
|
+
export declare const getNumberFromBN: (value: BN, decimals: number) => number;
|
|
16
16
|
/**
|
|
17
17
|
* Used to make on chain calls to the contract and wrap raised errors if any
|
|
18
18
|
* @param func function that interacts with the contract
|
|
@@ -25,3 +25,4 @@ export declare function handleContractError<T>(func: () => Promise<T>, callback?
|
|
|
25
25
|
* @param ms millisecond to sleep for
|
|
26
26
|
*/
|
|
27
27
|
export declare function sleep(ms: number): Promise<void>;
|
|
28
|
+
export declare const divCeilN: (n: bigint, d: bigint) => bigint;
|
|
@@ -1,23 +1,26 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { ContractError } from "
|
|
1
|
+
import BN from "bn.js";
|
|
2
|
+
import { ContractError } from "../types.js";
|
|
3
3
|
/**
|
|
4
|
-
* Used for token amounts
|
|
5
|
-
* Get
|
|
6
|
-
* @param {
|
|
4
|
+
* Used for conversion of token amounts to their Big Number representation.
|
|
5
|
+
* Get Big Number representation in the smallest units from the same value in the highest units.
|
|
6
|
+
* @param {number} value - Number of tokens you want to convert to its BN representation.
|
|
7
7
|
* @param {number} decimals - Number of decimals the token has.
|
|
8
8
|
*/
|
|
9
|
-
export const
|
|
10
|
-
|
|
9
|
+
export const getBN = (value, decimals) => {
|
|
10
|
+
const decimalPart = value - Math.trunc(value);
|
|
11
|
+
const integerPart = new BN(Math.trunc(value));
|
|
12
|
+
const decimalE = new BN(decimalPart * 1e9);
|
|
13
|
+
const sum = integerPart.mul(new BN(1e9)).add(decimalE);
|
|
14
|
+
const resultE = sum.mul(new BN(10).pow(new BN(decimals)));
|
|
15
|
+
return resultE.div(new BN(1e9));
|
|
11
16
|
};
|
|
12
17
|
/**
|
|
13
|
-
* Used for
|
|
14
|
-
* Get
|
|
15
|
-
* @param {
|
|
18
|
+
* Used for token amounts conversion from their Big Number representation to number.
|
|
19
|
+
* Get value in the highest units from BN representation of the same value in the smallest units.
|
|
20
|
+
* @param {BN} value - Big Number representation of value in the smallest units.
|
|
16
21
|
* @param {number} decimals - Number of decimals the token has.
|
|
17
22
|
*/
|
|
18
|
-
export const
|
|
19
|
-
return new BigNumber(value).times(new BigNumber(10).pow(decimals));
|
|
20
|
-
};
|
|
23
|
+
export const getNumberFromBN = (value, decimals) => value.gt(new BN(2 ** 53 - 1)) ? value.div(new BN(10 ** decimals)).toNumber() : value.toNumber() / 10 ** decimals;
|
|
21
24
|
/**
|
|
22
25
|
* Used to make on chain calls to the contract and wrap raised errors if any
|
|
23
26
|
* @param func function that interacts with the contract
|
|
@@ -45,3 +48,4 @@ export async function handleContractError(func, callback) {
|
|
|
45
48
|
export function sleep(ms) {
|
|
46
49
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
47
50
|
}
|
|
51
|
+
export const divCeilN = (n, d) => n / d + (n % d ? BigInt(1) : BigInt(0));
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export const getFilters = (criteria, byteOffsets) => {
|
|
2
|
+
return Object.entries(criteria).reduce((acc, [key, value]) => {
|
|
3
|
+
const criteriaKey = key;
|
|
4
|
+
const effectiveByteOffset = byteOffsets[criteriaKey];
|
|
5
|
+
if (criteria[criteriaKey] && effectiveByteOffset) {
|
|
6
|
+
acc.push({
|
|
7
|
+
memcmp: {
|
|
8
|
+
offset: effectiveByteOffset,
|
|
9
|
+
bytes: value.toString(),
|
|
10
|
+
},
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
return acc;
|
|
14
|
+
}, []);
|
|
15
|
+
};
|
package/dist/esm/solana/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { Connection, PublicKey, TransactionInstruction } from "@solana/web3.js";
|
|
2
|
-
import
|
|
3
|
-
export declare const prepareWrappedAccount: (connection: Connection, senderAddress: PublicKey, amount:
|
|
2
|
+
import BN from "bn.js";
|
|
3
|
+
export declare const prepareWrappedAccount: (connection: Connection, senderAddress: PublicKey, amount: BN) => Promise<TransactionInstruction[]>;
|
|
@@ -30,6 +30,10 @@ export interface ThrottleParams {
|
|
|
30
30
|
sendRate?: number;
|
|
31
31
|
sendThrottler?: PQueue;
|
|
32
32
|
}
|
|
33
|
+
export interface IProgramAccount<T> {
|
|
34
|
+
publicKey: PublicKey;
|
|
35
|
+
account: T;
|
|
36
|
+
}
|
|
33
37
|
export declare class TransactionFailedError extends Error {
|
|
34
38
|
constructor(m: string);
|
|
35
39
|
}
|
package/dist/esm/solana/utils.js
CHANGED
|
@@ -3,7 +3,7 @@ import { ComputeBudgetProgram, Keypair, TransactionMessage, VersionedTransaction
|
|
|
3
3
|
import bs58 from "bs58";
|
|
4
4
|
import PQueue from "p-queue";
|
|
5
5
|
import { TransactionFailedError, } from "./types.js";
|
|
6
|
-
import { sleep } from "../utils.js";
|
|
6
|
+
import { sleep } from "../lib/utils.js";
|
|
7
7
|
const SIMULATE_TRIES = 3;
|
|
8
8
|
export const buildSendThrottler = (sendRate) => {
|
|
9
9
|
return new PQueue({ concurrency: sendRate, intervalCap: 1, interval: 1000 });
|
|
@@ -17,7 +17,7 @@ export const buildSendThrottler = (sendRate) => {
|
|
|
17
17
|
* @return {Promise<Account[]>} - Array of resulting accounts.
|
|
18
18
|
*/
|
|
19
19
|
export async function getProgramAccounts(connection, wallet, offset, programId) {
|
|
20
|
-
|
|
20
|
+
const programAccounts = await connection?.getProgramAccounts(programId, {
|
|
21
21
|
filters: [
|
|
22
22
|
{
|
|
23
23
|
memcmp: {
|
|
@@ -27,6 +27,7 @@ export async function getProgramAccounts(connection, wallet, offset, programId)
|
|
|
27
27
|
},
|
|
28
28
|
],
|
|
29
29
|
});
|
|
30
|
+
return [...programAccounts];
|
|
30
31
|
}
|
|
31
32
|
/**
|
|
32
33
|
* Utility function to check if the transaction initiator is a Wallet object
|
|
@@ -243,11 +244,11 @@ export async function simulateTransaction(connection, tx) {
|
|
|
243
244
|
* @returns Transaction Status
|
|
244
245
|
*/
|
|
245
246
|
export async function confirmAndEnsureTransaction(connection, signature, ignoreError) {
|
|
246
|
-
const response = await connection.
|
|
247
|
-
if (!response) {
|
|
247
|
+
const response = await connection.getSignatureStatuses([signature]);
|
|
248
|
+
if (!response || response.value.length === 0 || response.value[0] === null) {
|
|
248
249
|
return null;
|
|
249
250
|
}
|
|
250
|
-
const
|
|
251
|
+
const value = response.value[0];
|
|
251
252
|
if (!value) {
|
|
252
253
|
return null;
|
|
253
254
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@streamflow/common",
|
|
3
|
-
"version": "7.0.0-alpha.
|
|
3
|
+
"version": "7.0.0-alpha.20",
|
|
4
4
|
"description": "Common utilities and types used by streamflow packages.",
|
|
5
5
|
"homepage": "https://github.com/streamflow-finance/js-sdk/",
|
|
6
6
|
"main": "./dist/esm/index.js",
|
|
@@ -27,20 +27,21 @@
|
|
|
27
27
|
"lint-config": "eslint --print-config",
|
|
28
28
|
"prepublishOnly": "npm run lint && npm run build"
|
|
29
29
|
},
|
|
30
|
-
"gitHead": "
|
|
30
|
+
"gitHead": "415bf87d86dbf18d3ac40db20e378cd1df90531e",
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"@streamflow/eslint-config": "7.0.0-alpha.
|
|
32
|
+
"@streamflow/eslint-config": "7.0.0-alpha.20",
|
|
33
|
+
"@types/bn.js": "5.1.1",
|
|
33
34
|
"date-fns": "2.28.0",
|
|
34
35
|
"typescript": "^4.9.5"
|
|
35
36
|
},
|
|
36
37
|
"dependencies": {
|
|
37
|
-
"@coral-xyz/borsh": "
|
|
38
|
+
"@coral-xyz/borsh": "0.30.1",
|
|
38
39
|
"@solana/buffer-layout": "4.0.1 ",
|
|
39
40
|
"@solana/spl-token": "0.3.6",
|
|
40
41
|
"@solana/wallet-adapter-base": "0.9.19",
|
|
41
|
-
"@solana/web3.js": "1.
|
|
42
|
-
"aptos": "1.
|
|
43
|
-
"
|
|
42
|
+
"@solana/web3.js": "1.90.2",
|
|
43
|
+
"aptos": "1.21.0",
|
|
44
|
+
"bn.js": "5.2.1",
|
|
44
45
|
"borsh": "^2.0.0",
|
|
45
46
|
"bs58": "5.0.0",
|
|
46
47
|
"p-queue": "^8.0.1"
|
package/dist/cjs/utils.js
DELETED
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
-
};
|
|
41
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
-
exports.sleep = exports.handleContractError = exports.getScaledBigNumber = exports.getNumberFromBigNumber = void 0;
|
|
43
|
-
var bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
44
|
-
var types_js_1 = require("./types.js");
|
|
45
|
-
/**
|
|
46
|
-
* Used for token amounts conversion from their Big Number representation to number.
|
|
47
|
-
* Get value in the highest units from BigNumber representation of the same value in the smallest units.
|
|
48
|
-
* @param {BigNumber} value - Big Number representation of value in the smallest units.
|
|
49
|
-
* @param {number} decimals - Number of decimals the token has.
|
|
50
|
-
*/
|
|
51
|
-
var getNumberFromBigNumber = function (bigNum, decimals) {
|
|
52
|
-
return bigNum.div((0, bignumber_js_1.default)(10).pow(decimals)).toNumber();
|
|
53
|
-
};
|
|
54
|
-
exports.getNumberFromBigNumber = getNumberFromBigNumber;
|
|
55
|
-
/**
|
|
56
|
-
* Used for conversion of token amounts to their Big Number representation.
|
|
57
|
-
* Get Big Number representation in the smallest units from the same value in the highest units.
|
|
58
|
-
* @param {number} value - Number of tokens you want to convert to its BigNumber representation.
|
|
59
|
-
* @param {number} decimals - Number of decimals the token has.
|
|
60
|
-
*/
|
|
61
|
-
var getScaledBigNumber = function (value, decimals) {
|
|
62
|
-
return new bignumber_js_1.default(value).times(new bignumber_js_1.default(10).pow(decimals));
|
|
63
|
-
};
|
|
64
|
-
exports.getScaledBigNumber = getScaledBigNumber;
|
|
65
|
-
/**
|
|
66
|
-
* Used to make on chain calls to the contract and wrap raised errors if any
|
|
67
|
-
* @param func function that interacts with the contract
|
|
68
|
-
* @param callback callback that may be used to extract error code
|
|
69
|
-
* @returns {T}
|
|
70
|
-
*/
|
|
71
|
-
function handleContractError(func, callback) {
|
|
72
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
73
|
-
var err_1;
|
|
74
|
-
return __generator(this, function (_a) {
|
|
75
|
-
switch (_a.label) {
|
|
76
|
-
case 0:
|
|
77
|
-
_a.trys.push([0, 2, , 3]);
|
|
78
|
-
return [4 /*yield*/, func()];
|
|
79
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
80
|
-
case 2:
|
|
81
|
-
err_1 = _a.sent();
|
|
82
|
-
if (err_1 instanceof Error) {
|
|
83
|
-
if (callback) {
|
|
84
|
-
throw new types_js_1.ContractError(err_1, callback(err_1));
|
|
85
|
-
}
|
|
86
|
-
throw new types_js_1.ContractError(err_1);
|
|
87
|
-
}
|
|
88
|
-
throw err_1;
|
|
89
|
-
case 3: return [2 /*return*/];
|
|
90
|
-
}
|
|
91
|
-
});
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
exports.handleContractError = handleContractError;
|
|
95
|
-
/**
|
|
96
|
-
* Pause async function execution for given amount of milliseconds
|
|
97
|
-
* @param ms millisecond to sleep for
|
|
98
|
-
*/
|
|
99
|
-
function sleep(ms) {
|
|
100
|
-
return new Promise(function (resolve) { return setTimeout(resolve, ms); });
|
|
101
|
-
}
|
|
102
|
-
exports.sleep = sleep;
|