@d9-network/spec 0.1.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/README.md +89 -0
- package/dist/index.cjs +101 -111
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -7
- package/dist/index.d.mts +3 -7
- package/dist/index.mjs +101 -106
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/README.md
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# @d9-network/spec
|
|
2
|
+
|
|
3
|
+
TypeScript SDK for D9 blockchain interactions, wallet cryptography, and chain specifications.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @d9-network/spec
|
|
9
|
+
# or
|
|
10
|
+
bun add @d9-network/spec
|
|
11
|
+
# or
|
|
12
|
+
pnpm add @d9-network/spec
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Quick Start
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { createD9SdkClient, generateMnemonic, createSR25519Wallet } from "@d9-network/spec";
|
|
19
|
+
|
|
20
|
+
// Create SDK client
|
|
21
|
+
const d9 = createD9SdkClient({
|
|
22
|
+
endpoint: "wss://mainnet.d9network.xyz:40300",
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
// Generate a new wallet
|
|
26
|
+
const mnemonic = generateMnemonic();
|
|
27
|
+
const wallet = createSR25519Wallet(mnemonic);
|
|
28
|
+
console.log("Address:", wallet.address);
|
|
29
|
+
|
|
30
|
+
// Query USDT balance
|
|
31
|
+
const result = await d9.contracts.usdt.query("PSP22::balance_of", {
|
|
32
|
+
origin: wallet.address,
|
|
33
|
+
args: { owner: wallet.address },
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
if (result.success) {
|
|
37
|
+
console.log("Balance:", result.value);
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Features
|
|
42
|
+
|
|
43
|
+
- **D9 Blockchain Client**: Pre-configured Polkadot-API client for D9 network
|
|
44
|
+
- **Wallet Management**: HD wallet derivation, SR25519 signing, SS58 encoding
|
|
45
|
+
- **Contract Descriptors**: Type-safe descriptors for D9 smart contracts
|
|
46
|
+
- **Chain Specifications**: D9 network metadata and type definitions
|
|
47
|
+
- **Utility Functions**: Balance formatting, address validation, gas estimation
|
|
48
|
+
|
|
49
|
+
## API Overview
|
|
50
|
+
|
|
51
|
+
### Client
|
|
52
|
+
|
|
53
|
+
| Export | Description |
|
|
54
|
+
| ------------------- | ------------------------------------------ |
|
|
55
|
+
| `createD9SdkClient` | Create D9 SDK client with contract support |
|
|
56
|
+
|
|
57
|
+
### Wallet
|
|
58
|
+
|
|
59
|
+
| Export | Description |
|
|
60
|
+
| --------------------- | ----------------------------------- |
|
|
61
|
+
| `generateMnemonic` | Generate BIP39 mnemonic |
|
|
62
|
+
| `createSR25519Wallet` | Create SR25519 wallet from mnemonic |
|
|
63
|
+
| `deriveKeyPair` | Derive key pair with HD path |
|
|
64
|
+
|
|
65
|
+
### Utilities
|
|
66
|
+
|
|
67
|
+
| Export | Description |
|
|
68
|
+
| ---------------- | -------------------------------- |
|
|
69
|
+
| `formatBalance` | Format token balance for display |
|
|
70
|
+
| `parseBalance` | Parse balance string to bigint |
|
|
71
|
+
| `isValidAddress` | Validate SS58 address |
|
|
72
|
+
|
|
73
|
+
### Contract Descriptors
|
|
74
|
+
|
|
75
|
+
Pre-built descriptors for D9 smart contracts:
|
|
76
|
+
|
|
77
|
+
- `usdt` - USDT PSP22 token
|
|
78
|
+
- `burnManager` - Token burning mechanism
|
|
79
|
+
- `miningPool` - Mining reward contracts
|
|
80
|
+
|
|
81
|
+
## Documentation
|
|
82
|
+
|
|
83
|
+
For complete documentation, examples, and API reference, visit:
|
|
84
|
+
|
|
85
|
+
- [D9 SDK Documentation](https://docs.d9network.xyz/docs/sdk/spec/overview)
|
|
86
|
+
|
|
87
|
+
## License
|
|
88
|
+
|
|
89
|
+
MIT
|
package/dist/index.cjs
CHANGED
|
@@ -7584,110 +7584,6 @@ function createAccountFromPrivateKey(options) {
|
|
|
7584
7584
|
};
|
|
7585
7585
|
}
|
|
7586
7586
|
|
|
7587
|
-
//#endregion
|
|
7588
|
-
//#region src/utils/balance.ts
|
|
7589
|
-
/**
|
|
7590
|
-
* Balance formatting utilities for D9 blockchain
|
|
7591
|
-
*/
|
|
7592
|
-
const D9_DECIMALS = _d9_network_ink.D9_DECIMALS;
|
|
7593
|
-
const D9_SYMBOL = _d9_network_ink.D9_SYMBOL;
|
|
7594
|
-
const D9_SS58_PREFIX = _d9_network_ink.D9_SS58_PREFIX;
|
|
7595
|
-
const USDT_DECIMALS = _d9_network_ink.USDT_DECIMALS;
|
|
7596
|
-
const USDT_SYMBOL = _d9_network_ink.USDT_SYMBOL;
|
|
7597
|
-
/**
|
|
7598
|
-
* Pre-configured D9 token
|
|
7599
|
-
*/
|
|
7600
|
-
const D9_TOKEN = {
|
|
7601
|
-
decimals: D9_DECIMALS,
|
|
7602
|
-
symbol: D9_SYMBOL
|
|
7603
|
-
};
|
|
7604
|
-
/**
|
|
7605
|
-
* Pre-configured USDT token
|
|
7606
|
-
*/
|
|
7607
|
-
const USDT_TOKEN = {
|
|
7608
|
-
decimals: USDT_DECIMALS,
|
|
7609
|
-
symbol: USDT_SYMBOL
|
|
7610
|
-
};
|
|
7611
|
-
/**
|
|
7612
|
-
* Format a raw planck value to a human-readable balance string
|
|
7613
|
-
*
|
|
7614
|
-
* @param planck - The raw balance in planck units (smallest unit)
|
|
7615
|
-
* @param options - Formatting options
|
|
7616
|
-
* @returns Formatted balance string
|
|
7617
|
-
*
|
|
7618
|
-
* @example
|
|
7619
|
-
* ```typescript
|
|
7620
|
-
* formatBalance(1_500_000_000_000n, { decimals: 12 }); // "1.5"
|
|
7621
|
-
* formatBalance(1_500_000_000_000n, { decimals: 12, maxDecimals: 2 }); // "1.50"
|
|
7622
|
-
* formatBalance(1_000_000_000_000_000n, { decimals: 12, thousandsSeparator: "," }); // "1,000"
|
|
7623
|
-
* ```
|
|
7624
|
-
*/
|
|
7625
|
-
function formatBalance(planck, options = {}) {
|
|
7626
|
-
const { decimals = D9_DECIMALS, maxDecimals = 4, trimTrailingZeros = true, thousandsSeparator } = options;
|
|
7627
|
-
if (decimals < 0 || decimals > 30) throw new Error("Decimals must be between 0 and 30");
|
|
7628
|
-
const divisor = 10n ** BigInt(decimals);
|
|
7629
|
-
const isNegative = planck < 0n;
|
|
7630
|
-
const absolutePlanck = isNegative ? -planck : planck;
|
|
7631
|
-
const wholePart = absolutePlanck / divisor;
|
|
7632
|
-
const fractionalPart = absolutePlanck % divisor;
|
|
7633
|
-
let wholeStr = wholePart.toString();
|
|
7634
|
-
if (thousandsSeparator && wholeStr.length > 3) wholeStr = wholeStr.replace(/\B(?=(\d{3})+(?!\d))/g, thousandsSeparator);
|
|
7635
|
-
if (decimals === 0 || maxDecimals === 0) return isNegative ? `-${wholeStr}` : wholeStr;
|
|
7636
|
-
let fractionalStr = fractionalPart.toString().padStart(decimals, "0");
|
|
7637
|
-
if (fractionalStr.length > maxDecimals) fractionalStr = fractionalStr.slice(0, maxDecimals);
|
|
7638
|
-
if (trimTrailingZeros) fractionalStr = fractionalStr.replace(/0+$/, "");
|
|
7639
|
-
const prefix = isNegative ? "-" : "";
|
|
7640
|
-
if (fractionalStr === "") return `${prefix}${wholeStr}`;
|
|
7641
|
-
return `${prefix}${wholeStr}.${fractionalStr}`;
|
|
7642
|
-
}
|
|
7643
|
-
/**
|
|
7644
|
-
* Parse a human-readable amount string to planck units
|
|
7645
|
-
*
|
|
7646
|
-
* @param amount - The amount as a string or number
|
|
7647
|
-
* @param options - Parse options
|
|
7648
|
-
* @returns The amount in planck units (bigint)
|
|
7649
|
-
*
|
|
7650
|
-
* @example
|
|
7651
|
-
* ```typescript
|
|
7652
|
-
* parseAmount("1.5", { decimals: 12 }); // 1_500_000_000_000n
|
|
7653
|
-
* parseAmount(1.5, { decimals: 12 }); // 1_500_000_000_000n
|
|
7654
|
-
* parseAmount("1,000.50", { decimals: 12 }); // 1_000_500_000_000_000n
|
|
7655
|
-
* ```
|
|
7656
|
-
*/
|
|
7657
|
-
function parseAmount(amount, options = {}) {
|
|
7658
|
-
const { decimals = D9_DECIMALS } = options;
|
|
7659
|
-
if (decimals < 0 || decimals > 30) throw new Error("Decimals must be between 0 and 30");
|
|
7660
|
-
let amountStr = String(amount).replace(/,/g, "").trim();
|
|
7661
|
-
const isNegative = amountStr.startsWith("-");
|
|
7662
|
-
if (isNegative) amountStr = amountStr.slice(1);
|
|
7663
|
-
if (!/^\d*\.?\d*$/.test(amountStr) || amountStr === "" || amountStr === ".") throw new Error(`Invalid amount format: ${amount}`);
|
|
7664
|
-
const [wholePart = "0", fractionalPart = ""] = amountStr.split(".");
|
|
7665
|
-
if (fractionalPart.length > decimals) throw new Error(`Amount has more decimal places (${fractionalPart.length}) than supported (${decimals})`);
|
|
7666
|
-
const planckStr = wholePart + fractionalPart.padEnd(decimals, "0");
|
|
7667
|
-
const planck = BigInt(planckStr);
|
|
7668
|
-
return isNegative ? -planck : planck;
|
|
7669
|
-
}
|
|
7670
|
-
/**
|
|
7671
|
-
* Format a token amount with symbol
|
|
7672
|
-
*
|
|
7673
|
-
* @param planck - The raw balance in planck units
|
|
7674
|
-
* @param token - Token configuration
|
|
7675
|
-
* @param options - Additional formatting options
|
|
7676
|
-
* @returns Formatted string with token symbol
|
|
7677
|
-
*
|
|
7678
|
-
* @example
|
|
7679
|
-
* ```typescript
|
|
7680
|
-
* formatTokenAmount(1_500_000_000_000n, D9_TOKEN); // "1.5 D9"
|
|
7681
|
-
* formatTokenAmount(1000n, USDT_TOKEN); // "10 USDT"
|
|
7682
|
-
* ```
|
|
7683
|
-
*/
|
|
7684
|
-
function formatTokenAmount(planck, token, options = {}) {
|
|
7685
|
-
return `${formatBalance(planck, {
|
|
7686
|
-
...options,
|
|
7687
|
-
decimals: token.decimals
|
|
7688
|
-
})} ${token.symbol}`;
|
|
7689
|
-
}
|
|
7690
|
-
|
|
7691
7587
|
//#endregion
|
|
7692
7588
|
//#region src/wallet/validation.ts
|
|
7693
7589
|
/**
|
|
@@ -7728,7 +7624,7 @@ function isValidAddress(address) {
|
|
|
7728
7624
|
* ```
|
|
7729
7625
|
*/
|
|
7730
7626
|
function isValidD9Address(address) {
|
|
7731
|
-
return isValidAddressForNetwork(address, D9_SS58_PREFIX);
|
|
7627
|
+
return isValidAddressForNetwork(address, _d9_network_ink.D9_SS58_PREFIX);
|
|
7732
7628
|
}
|
|
7733
7629
|
/**
|
|
7734
7630
|
* Check if a value is a valid address for a specific network
|
|
@@ -7827,7 +7723,106 @@ function toNetworkAddress(address, targetPrefix) {
|
|
|
7827
7723
|
* ```
|
|
7828
7724
|
*/
|
|
7829
7725
|
function toD9Address(address) {
|
|
7830
|
-
return toNetworkAddress(address, D9_SS58_PREFIX);
|
|
7726
|
+
return toNetworkAddress(address, _d9_network_ink.D9_SS58_PREFIX);
|
|
7727
|
+
}
|
|
7728
|
+
|
|
7729
|
+
//#endregion
|
|
7730
|
+
//#region src/utils/balance.ts
|
|
7731
|
+
/**
|
|
7732
|
+
* Balance formatting utilities for D9 blockchain
|
|
7733
|
+
*/
|
|
7734
|
+
/**
|
|
7735
|
+
* Pre-configured D9 token
|
|
7736
|
+
*/
|
|
7737
|
+
const D9_TOKEN = {
|
|
7738
|
+
decimals: _d9_network_ink.D9_DECIMALS,
|
|
7739
|
+
symbol: _d9_network_ink.D9_SYMBOL
|
|
7740
|
+
};
|
|
7741
|
+
/**
|
|
7742
|
+
* Pre-configured USDT token
|
|
7743
|
+
*/
|
|
7744
|
+
const USDT_TOKEN = {
|
|
7745
|
+
decimals: _d9_network_ink.USDT_DECIMALS,
|
|
7746
|
+
symbol: _d9_network_ink.USDT_SYMBOL
|
|
7747
|
+
};
|
|
7748
|
+
/**
|
|
7749
|
+
* Format a raw planck value to a human-readable balance string
|
|
7750
|
+
*
|
|
7751
|
+
* @param planck - The raw balance in planck units (smallest unit)
|
|
7752
|
+
* @param options - Formatting options
|
|
7753
|
+
* @returns Formatted balance string
|
|
7754
|
+
*
|
|
7755
|
+
* @example
|
|
7756
|
+
* ```typescript
|
|
7757
|
+
* formatBalance(1_500_000_000_000n, { decimals: 12 }); // "1.5"
|
|
7758
|
+
* formatBalance(1_500_000_000_000n, { decimals: 12, maxDecimals: 2 }); // "1.50"
|
|
7759
|
+
* formatBalance(1_000_000_000_000_000n, { decimals: 12, thousandsSeparator: "," }); // "1,000"
|
|
7760
|
+
* ```
|
|
7761
|
+
*/
|
|
7762
|
+
function formatBalance(planck, options = {}) {
|
|
7763
|
+
const { decimals = _d9_network_ink.D9_DECIMALS, maxDecimals = 4, trimTrailingZeros = true, thousandsSeparator } = options;
|
|
7764
|
+
if (decimals < 0 || decimals > 30) throw new Error("Decimals must be between 0 and 30");
|
|
7765
|
+
const divisor = 10n ** BigInt(decimals);
|
|
7766
|
+
const isNegative = planck < 0n;
|
|
7767
|
+
const absolutePlanck = isNegative ? -planck : planck;
|
|
7768
|
+
const wholePart = absolutePlanck / divisor;
|
|
7769
|
+
const fractionalPart = absolutePlanck % divisor;
|
|
7770
|
+
let wholeStr = wholePart.toString();
|
|
7771
|
+
if (thousandsSeparator && wholeStr.length > 3) wholeStr = wholeStr.replace(/\B(?=(\d{3})+(?!\d))/g, thousandsSeparator);
|
|
7772
|
+
if (decimals === 0 || maxDecimals === 0) return isNegative ? `-${wholeStr}` : wholeStr;
|
|
7773
|
+
let fractionalStr = fractionalPart.toString().padStart(decimals, "0");
|
|
7774
|
+
if (fractionalStr.length > maxDecimals) fractionalStr = fractionalStr.slice(0, maxDecimals);
|
|
7775
|
+
if (trimTrailingZeros) fractionalStr = fractionalStr.replace(/0+$/, "");
|
|
7776
|
+
const prefix = isNegative ? "-" : "";
|
|
7777
|
+
if (fractionalStr === "") return `${prefix}${wholeStr}`;
|
|
7778
|
+
return `${prefix}${wholeStr}.${fractionalStr}`;
|
|
7779
|
+
}
|
|
7780
|
+
/**
|
|
7781
|
+
* Parse a human-readable amount string to planck units
|
|
7782
|
+
*
|
|
7783
|
+
* @param amount - The amount as a string or number
|
|
7784
|
+
* @param options - Parse options
|
|
7785
|
+
* @returns The amount in planck units (bigint)
|
|
7786
|
+
*
|
|
7787
|
+
* @example
|
|
7788
|
+
* ```typescript
|
|
7789
|
+
* parseAmount("1.5", { decimals: 12 }); // 1_500_000_000_000n
|
|
7790
|
+
* parseAmount(1.5, { decimals: 12 }); // 1_500_000_000_000n
|
|
7791
|
+
* parseAmount("1,000.50", { decimals: 12 }); // 1_000_500_000_000_000n
|
|
7792
|
+
* ```
|
|
7793
|
+
*/
|
|
7794
|
+
function parseAmount(amount, options = {}) {
|
|
7795
|
+
const { decimals = _d9_network_ink.D9_DECIMALS } = options;
|
|
7796
|
+
if (decimals < 0 || decimals > 30) throw new Error("Decimals must be between 0 and 30");
|
|
7797
|
+
let amountStr = String(amount).replace(/,/g, "").trim();
|
|
7798
|
+
const isNegative = amountStr.startsWith("-");
|
|
7799
|
+
if (isNegative) amountStr = amountStr.slice(1);
|
|
7800
|
+
if (!/^\d*\.?\d*$/.test(amountStr) || amountStr === "" || amountStr === ".") throw new Error(`Invalid amount format: ${amount}`);
|
|
7801
|
+
const [wholePart = "0", fractionalPart = ""] = amountStr.split(".");
|
|
7802
|
+
if (fractionalPart.length > decimals) throw new Error(`Amount has more decimal places (${fractionalPart.length}) than supported (${decimals})`);
|
|
7803
|
+
const planckStr = wholePart + fractionalPart.padEnd(decimals, "0");
|
|
7804
|
+
const planck = BigInt(planckStr);
|
|
7805
|
+
return isNegative ? -planck : planck;
|
|
7806
|
+
}
|
|
7807
|
+
/**
|
|
7808
|
+
* Format a token amount with symbol
|
|
7809
|
+
*
|
|
7810
|
+
* @param planck - The raw balance in planck units
|
|
7811
|
+
* @param token - Token configuration
|
|
7812
|
+
* @param options - Additional formatting options
|
|
7813
|
+
* @returns Formatted string with token symbol
|
|
7814
|
+
*
|
|
7815
|
+
* @example
|
|
7816
|
+
* ```typescript
|
|
7817
|
+
* formatTokenAmount(1_500_000_000_000n, D9_TOKEN); // "1.5 D9"
|
|
7818
|
+
* formatTokenAmount(1000n, USDT_TOKEN); // "10 USDT"
|
|
7819
|
+
* ```
|
|
7820
|
+
*/
|
|
7821
|
+
function formatTokenAmount(planck, token, options = {}) {
|
|
7822
|
+
return `${formatBalance(planck, {
|
|
7823
|
+
...options,
|
|
7824
|
+
decimals: token.decimals
|
|
7825
|
+
})} ${token.symbol}`;
|
|
7831
7826
|
}
|
|
7832
7827
|
|
|
7833
7828
|
//#endregion
|
|
@@ -7979,9 +7974,6 @@ Object.defineProperty(exports, 'ContractEventParser', {
|
|
|
7979
7974
|
}
|
|
7980
7975
|
});
|
|
7981
7976
|
exports.D9_BLOCK_TIME_MS = D9_BLOCK_TIME_MS;
|
|
7982
|
-
exports.D9_DECIMALS = D9_DECIMALS;
|
|
7983
|
-
exports.D9_SS58_PREFIX = D9_SS58_PREFIX;
|
|
7984
|
-
exports.D9_SYMBOL = D9_SYMBOL;
|
|
7985
7977
|
exports.D9_TOKEN = D9_TOKEN;
|
|
7986
7978
|
exports.DigestItem = DigestItem;
|
|
7987
7979
|
exports.DispatchClass = DispatchClass;
|
|
@@ -7994,8 +7986,6 @@ exports.SessionEvent = SessionEvent;
|
|
|
7994
7986
|
exports.TransactionPaymentEvent = TransactionPaymentEvent;
|
|
7995
7987
|
exports.TransactionPaymentReleases = TransactionPaymentReleases;
|
|
7996
7988
|
exports.TransactionalError = TransactionalError;
|
|
7997
|
-
exports.USDT_DECIMALS = USDT_DECIMALS;
|
|
7998
|
-
exports.USDT_SYMBOL = USDT_SYMBOL;
|
|
7999
7989
|
exports.USDT_TOKEN = USDT_TOKEN;
|
|
8000
7990
|
exports.addressesEqual = addressesEqual;
|
|
8001
7991
|
exports.blocksToMs = blocksToMs;
|