@mistcash/sdk 0.1.0-beta.1 → 0.2.0
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/index.d.mts +4 -2
- package/dist/index.d.ts +4 -2
- package/dist/index.js +33 -5
- package/dist/index.mjs +31 -5
- package/package.json +11 -11
- package/readme.md +15 -0
- package/src/utils.ts +44 -5
package/dist/index.d.mts
CHANGED
|
@@ -24,7 +24,7 @@ interface Asset {
|
|
|
24
24
|
* @param provider Starknet provider
|
|
25
25
|
* @returns Typed chamber contract
|
|
26
26
|
*/
|
|
27
|
-
declare function getChamber(
|
|
27
|
+
declare function getChamber(providerOrAccount?: ProviderInterface | AccountInterface): ChamberTypedContract;
|
|
28
28
|
/**
|
|
29
29
|
* Fetch transaction assets from the chamber contract.
|
|
30
30
|
* ⚠️ Will show assets even if transaction is spent
|
|
@@ -58,5 +58,7 @@ declare function checkTxExists(contract: ChamberTypedContract, valKey: string, v
|
|
|
58
58
|
* @returns index of the transaction
|
|
59
59
|
*/
|
|
60
60
|
declare function getTxIndexInTree(leaves: bigint[], valKey: string, valTo: string, tokenAddr: string, amount: string): Promise<number>;
|
|
61
|
+
declare function fmtAmount(amount: bigint, decimals: number): string;
|
|
62
|
+
declare function fmtAmtToBigInt(amountStr: string, decimals: number): bigint;
|
|
61
63
|
|
|
62
|
-
export { type Asset, checkTxExists, devStr, devVal, fetchTxAssets, getChamber, getTxIndexInTree };
|
|
64
|
+
export { type Asset, checkTxExists, devStr, devVal, fetchTxAssets, fmtAmount, fmtAmtToBigInt, getChamber, getTxIndexInTree };
|
package/dist/index.d.ts
CHANGED
|
@@ -24,7 +24,7 @@ interface Asset {
|
|
|
24
24
|
* @param provider Starknet provider
|
|
25
25
|
* @returns Typed chamber contract
|
|
26
26
|
*/
|
|
27
|
-
declare function getChamber(
|
|
27
|
+
declare function getChamber(providerOrAccount?: ProviderInterface | AccountInterface): ChamberTypedContract;
|
|
28
28
|
/**
|
|
29
29
|
* Fetch transaction assets from the chamber contract.
|
|
30
30
|
* ⚠️ Will show assets even if transaction is spent
|
|
@@ -58,5 +58,7 @@ declare function checkTxExists(contract: ChamberTypedContract, valKey: string, v
|
|
|
58
58
|
* @returns index of the transaction
|
|
59
59
|
*/
|
|
60
60
|
declare function getTxIndexInTree(leaves: bigint[], valKey: string, valTo: string, tokenAddr: string, amount: string): Promise<number>;
|
|
61
|
+
declare function fmtAmount(amount: bigint, decimals: number): string;
|
|
62
|
+
declare function fmtAmtToBigInt(amountStr: string, decimals: number): bigint;
|
|
61
63
|
|
|
62
|
-
export { type Asset, checkTxExists, devStr, devVal, fetchTxAssets, getChamber, getTxIndexInTree };
|
|
64
|
+
export { type Asset, checkTxExists, devStr, devVal, fetchTxAssets, fmtAmount, fmtAmtToBigInt, getChamber, getTxIndexInTree };
|
package/dist/index.js
CHANGED
|
@@ -24,6 +24,8 @@ __export(index_exports, {
|
|
|
24
24
|
devStr: () => devStr,
|
|
25
25
|
devVal: () => devVal,
|
|
26
26
|
fetchTxAssets: () => fetchTxAssets,
|
|
27
|
+
fmtAmount: () => fmtAmount,
|
|
28
|
+
fmtAmtToBigInt: () => fmtAmtToBigInt,
|
|
27
29
|
getChamber: () => getChamber,
|
|
28
30
|
getTxIndexInTree: () => getTxIndexInTree
|
|
29
31
|
});
|
|
@@ -39,11 +41,13 @@ var devVal = (val, deflt = void 0) => {
|
|
|
39
41
|
var import_starknet = require("starknet");
|
|
40
42
|
var import_config = require("@mistcash/config");
|
|
41
43
|
var import_crypto = require("@mistcash/crypto");
|
|
42
|
-
function getChamber(
|
|
44
|
+
function getChamber(providerOrAccount) {
|
|
43
45
|
return new import_starknet.Contract(
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
46
|
+
{
|
|
47
|
+
abi: import_config.CHAMBER_ABI,
|
|
48
|
+
address: import_config.CHAMBER_ADDR_MAINNET,
|
|
49
|
+
providerOrAccount
|
|
50
|
+
}
|
|
47
51
|
).typedv2(import_config.CHAMBER_ABI);
|
|
48
52
|
}
|
|
49
53
|
async function fetchTxAssets(contract, valKey, valTo) {
|
|
@@ -54,21 +58,45 @@ async function fetchTxAssets(contract, valKey, valTo) {
|
|
|
54
58
|
} else if (typeof amount != "bigint") {
|
|
55
59
|
amount = BigInt(`${amount.low}`);
|
|
56
60
|
}
|
|
57
|
-
|
|
61
|
+
let addr = asset.addr;
|
|
62
|
+
if (typeof addr === "bigint") {
|
|
63
|
+
addr = "0x" + addr.toString(16);
|
|
64
|
+
}
|
|
65
|
+
return { amount, addr };
|
|
58
66
|
}
|
|
59
67
|
async function checkTxExists(contract, valKey, valTo, tokenAddr, amount) {
|
|
68
|
+
const allTransactions = await contract.tx_array();
|
|
69
|
+
const tx = await (0, import_crypto.txHash)(valKey, valTo, tokenAddr, amount);
|
|
70
|
+
allTransactions.indexOf(tx);
|
|
60
71
|
return await getTxIndexInTree(await contract.tx_array(), valKey, valTo, tokenAddr, amount) !== -1;
|
|
61
72
|
}
|
|
62
73
|
async function getTxIndexInTree(leaves, valKey, valTo, tokenAddr, amount) {
|
|
63
74
|
const tx_hash = await (0, import_crypto.txHash)(valKey, valTo, tokenAddr, amount);
|
|
64
75
|
return leaves.indexOf(tx_hash);
|
|
65
76
|
}
|
|
77
|
+
function fmtAmount(amount, decimals) {
|
|
78
|
+
const factor = BigInt(10 ** decimals);
|
|
79
|
+
const integerPart = amount / factor;
|
|
80
|
+
const fractionalPart = amount % factor;
|
|
81
|
+
const fractionalStr = fractionalPart.toString().padStart(decimals, "0");
|
|
82
|
+
const trimmedFractional = fractionalStr.replace(/0+$/, "") || "0";
|
|
83
|
+
return `${integerPart.toString()}.${trimmedFractional}`;
|
|
84
|
+
}
|
|
85
|
+
function fmtAmtToBigInt(amountStr, decimals) {
|
|
86
|
+
let amt = BigInt(Math.floor(1e6 * +amountStr));
|
|
87
|
+
if (decimals > 6) {
|
|
88
|
+
amt = BigInt(10 ** (decimals - 6)) * amt;
|
|
89
|
+
}
|
|
90
|
+
return amt;
|
|
91
|
+
}
|
|
66
92
|
// Annotate the CommonJS export names for ESM import in node:
|
|
67
93
|
0 && (module.exports = {
|
|
68
94
|
checkTxExists,
|
|
69
95
|
devStr,
|
|
70
96
|
devVal,
|
|
71
97
|
fetchTxAssets,
|
|
98
|
+
fmtAmount,
|
|
99
|
+
fmtAmtToBigInt,
|
|
72
100
|
getChamber,
|
|
73
101
|
getTxIndexInTree
|
|
74
102
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -8,11 +8,13 @@ var devVal = (val, deflt = void 0) => {
|
|
|
8
8
|
import { Contract } from "starknet";
|
|
9
9
|
import { CHAMBER_ABI, CHAMBER_ADDR_MAINNET } from "@mistcash/config";
|
|
10
10
|
import { txSecret, txHash } from "@mistcash/crypto";
|
|
11
|
-
function getChamber(
|
|
11
|
+
function getChamber(providerOrAccount) {
|
|
12
12
|
return new Contract(
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
{
|
|
14
|
+
abi: CHAMBER_ABI,
|
|
15
|
+
address: CHAMBER_ADDR_MAINNET,
|
|
16
|
+
providerOrAccount
|
|
17
|
+
}
|
|
16
18
|
).typedv2(CHAMBER_ABI);
|
|
17
19
|
}
|
|
18
20
|
async function fetchTxAssets(contract, valKey, valTo) {
|
|
@@ -23,20 +25,44 @@ async function fetchTxAssets(contract, valKey, valTo) {
|
|
|
23
25
|
} else if (typeof amount != "bigint") {
|
|
24
26
|
amount = BigInt(`${amount.low}`);
|
|
25
27
|
}
|
|
26
|
-
|
|
28
|
+
let addr = asset.addr;
|
|
29
|
+
if (typeof addr === "bigint") {
|
|
30
|
+
addr = "0x" + addr.toString(16);
|
|
31
|
+
}
|
|
32
|
+
return { amount, addr };
|
|
27
33
|
}
|
|
28
34
|
async function checkTxExists(contract, valKey, valTo, tokenAddr, amount) {
|
|
35
|
+
const allTransactions = await contract.tx_array();
|
|
36
|
+
const tx = await txHash(valKey, valTo, tokenAddr, amount);
|
|
37
|
+
allTransactions.indexOf(tx);
|
|
29
38
|
return await getTxIndexInTree(await contract.tx_array(), valKey, valTo, tokenAddr, amount) !== -1;
|
|
30
39
|
}
|
|
31
40
|
async function getTxIndexInTree(leaves, valKey, valTo, tokenAddr, amount) {
|
|
32
41
|
const tx_hash = await txHash(valKey, valTo, tokenAddr, amount);
|
|
33
42
|
return leaves.indexOf(tx_hash);
|
|
34
43
|
}
|
|
44
|
+
function fmtAmount(amount, decimals) {
|
|
45
|
+
const factor = BigInt(10 ** decimals);
|
|
46
|
+
const integerPart = amount / factor;
|
|
47
|
+
const fractionalPart = amount % factor;
|
|
48
|
+
const fractionalStr = fractionalPart.toString().padStart(decimals, "0");
|
|
49
|
+
const trimmedFractional = fractionalStr.replace(/0+$/, "") || "0";
|
|
50
|
+
return `${integerPart.toString()}.${trimmedFractional}`;
|
|
51
|
+
}
|
|
52
|
+
function fmtAmtToBigInt(amountStr, decimals) {
|
|
53
|
+
let amt = BigInt(Math.floor(1e6 * +amountStr));
|
|
54
|
+
if (decimals > 6) {
|
|
55
|
+
amt = BigInt(10 ** (decimals - 6)) * amt;
|
|
56
|
+
}
|
|
57
|
+
return amt;
|
|
58
|
+
}
|
|
35
59
|
export {
|
|
36
60
|
checkTxExists,
|
|
37
61
|
devStr,
|
|
38
62
|
devVal,
|
|
39
63
|
fetchTxAssets,
|
|
64
|
+
fmtAmount,
|
|
65
|
+
fmtAmtToBigInt,
|
|
40
66
|
getChamber,
|
|
41
67
|
getTxIndexInTree
|
|
42
68
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mistcash/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -17,22 +17,22 @@
|
|
|
17
17
|
"main": "dist/index.js",
|
|
18
18
|
"module": "dist/index.mjs",
|
|
19
19
|
"types": "dist/index.d.ts",
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
22
|
+
"dev": "tsup src/index.ts --format cjs,esm --dts --watch",
|
|
23
|
+
"test": "jest",
|
|
24
|
+
"test:watch": "jest --watch",
|
|
25
|
+
"clean": "rm -rf dist"
|
|
26
|
+
},
|
|
20
27
|
"dependencies": {
|
|
21
|
-
"
|
|
22
|
-
"@mistcash/
|
|
23
|
-
"
|
|
28
|
+
"@mistcash/config": "workspace:*",
|
|
29
|
+
"@mistcash/crypto": "workspace:*",
|
|
30
|
+
"starknet": "^8.0.0"
|
|
24
31
|
},
|
|
25
32
|
"devDependencies": {
|
|
26
33
|
"@types/jest": "^29.5.0",
|
|
27
34
|
"jest": "^29.5.0",
|
|
28
35
|
"tsup": "^8.0.0",
|
|
29
36
|
"typescript": "^5.0.0"
|
|
30
|
-
},
|
|
31
|
-
"scripts": {
|
|
32
|
-
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
33
|
-
"dev": "tsup src/index.ts --format cjs,esm --dts --watch",
|
|
34
|
-
"test": "jest",
|
|
35
|
-
"test:watch": "jest --watch",
|
|
36
|
-
"clean": "rm -rf dist"
|
|
37
37
|
}
|
|
38
38
|
}
|
package/readme.md
CHANGED
|
@@ -22,6 +22,21 @@ const contract = getChamber(provider);
|
|
|
22
22
|
// ⚠️ Will show assets even if transaction is spent
|
|
23
23
|
// ⚠️ Contract has no way of knowing which transaction is spent
|
|
24
24
|
const asset = await fetchTxAssets(contract, valKey, valTo);
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
// Manually checking if tx exists
|
|
28
|
+
|
|
29
|
+
// fetch existing transactions
|
|
30
|
+
const allTransactions = await contract.tx_array();
|
|
31
|
+
|
|
32
|
+
// generate full transaction hash
|
|
33
|
+
const tx = await txHash(claimingKey, recipient, tokenAddr, amount)
|
|
34
|
+
|
|
35
|
+
// check if your transaction is in the list
|
|
36
|
+
allTransactions.indexOf(tx);
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
25
40
|
```
|
|
26
41
|
|
|
27
42
|
## Contributing
|
package/src/utils.ts
CHANGED
|
@@ -8,11 +8,13 @@ import { Asset } from './types';
|
|
|
8
8
|
* @param provider Starknet provider
|
|
9
9
|
* @returns Typed chamber contract
|
|
10
10
|
*/
|
|
11
|
-
export function getChamber(
|
|
11
|
+
export function getChamber(providerOrAccount?: ProviderInterface | AccountInterface): ChamberTypedContract {
|
|
12
12
|
return new Contract(
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
{
|
|
14
|
+
abi: CHAMBER_ABI,
|
|
15
|
+
address: CHAMBER_ADDR_MAINNET,
|
|
16
|
+
providerOrAccount
|
|
17
|
+
}
|
|
16
18
|
).typedv2(CHAMBER_ABI)
|
|
17
19
|
}
|
|
18
20
|
|
|
@@ -34,7 +36,11 @@ export async function fetchTxAssets(contract: ChamberTypedContract, valKey: stri
|
|
|
34
36
|
} else if (typeof amount != 'bigint') {
|
|
35
37
|
amount = BigInt(`${amount.low}`);
|
|
36
38
|
}
|
|
37
|
-
|
|
39
|
+
let addr = asset.addr as string | bigint;
|
|
40
|
+
if (typeof addr === 'bigint') {
|
|
41
|
+
addr = '0x' + addr.toString(16);
|
|
42
|
+
}
|
|
43
|
+
return { amount, addr }
|
|
38
44
|
}
|
|
39
45
|
|
|
40
46
|
/**
|
|
@@ -50,6 +56,16 @@ export async function fetchTxAssets(contract: ChamberTypedContract, valKey: stri
|
|
|
50
56
|
* @returns True if the transaction exists, false otherwise.
|
|
51
57
|
*/
|
|
52
58
|
export async function checkTxExists(contract: ChamberTypedContract, valKey: string, valTo: string, tokenAddr: string, amount: string): Promise<boolean> {
|
|
59
|
+
|
|
60
|
+
// fetch existing transactions
|
|
61
|
+
const allTransactions = await contract.tx_array();
|
|
62
|
+
|
|
63
|
+
// generate full transaction hash
|
|
64
|
+
const tx = await txHash(valKey, valTo, tokenAddr, amount)
|
|
65
|
+
|
|
66
|
+
// check if your transaction is in the list
|
|
67
|
+
allTransactions.indexOf(tx);
|
|
68
|
+
|
|
53
69
|
return await getTxIndexInTree(await contract.tx_array() as bigint[], valKey, valTo, tokenAddr, amount) !== -1;
|
|
54
70
|
}
|
|
55
71
|
|
|
@@ -66,3 +82,26 @@ export async function getTxIndexInTree(leaves: bigint[], valKey: string, valTo:
|
|
|
66
82
|
const tx_hash = await txHash(valKey, valTo, tokenAddr, amount)
|
|
67
83
|
return leaves.indexOf(tx_hash);
|
|
68
84
|
}
|
|
85
|
+
|
|
86
|
+
export function fmtAmount(amount: bigint, decimals: number): string {
|
|
87
|
+
// Convert bigint amount to string with specified decimal places
|
|
88
|
+
const factor = BigInt(10 ** decimals);
|
|
89
|
+
const integerPart = amount / factor;
|
|
90
|
+
const fractionalPart = amount % factor;
|
|
91
|
+
|
|
92
|
+
// Pad fractional part with leading zeros to match decimal places
|
|
93
|
+
const fractionalStr = fractionalPart.toString().padStart(decimals, '0');
|
|
94
|
+
// Remove trailing zeros
|
|
95
|
+
const trimmedFractional = fractionalStr.replace(/0+$/, '') || '0';
|
|
96
|
+
|
|
97
|
+
return `${integerPart.toString()}.${trimmedFractional}`;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function fmtAmtToBigInt(amountStr: string, decimals: number): bigint {
|
|
101
|
+
let amt = BigInt(Math.floor(1_000_000 * +amountStr));
|
|
102
|
+
if (decimals > 6) {
|
|
103
|
+
// Mul by 10**12 for decimal points == 18 for all token but usdc
|
|
104
|
+
amt = BigInt(10 ** (decimals - 6)) * amt;
|
|
105
|
+
}
|
|
106
|
+
return amt;
|
|
107
|
+
}
|