tusdt-sdk 0.1.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.
Files changed (42) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +167 -0
  3. package/dist/chunk-5HS7VSOA.js +1844 -0
  4. package/dist/chunk-5HS7VSOA.js.map +1 -0
  5. package/dist/client.d.ts +2 -0
  6. package/dist/client.d.ts.map +1 -0
  7. package/dist/client.js +3 -0
  8. package/dist/client.js.map +1 -0
  9. package/dist/contract/events.d.ts +39 -0
  10. package/dist/contract/events.d.ts.map +1 -0
  11. package/dist/contract/finalized-events.d.ts +32 -0
  12. package/dist/contract/finalized-events.d.ts.map +1 -0
  13. package/dist/contract/helpers.d.ts +19 -0
  14. package/dist/contract/helpers.d.ts.map +1 -0
  15. package/dist/contract/payment-listener.d.ts +88 -0
  16. package/dist/contract/payment-listener.d.ts.map +1 -0
  17. package/dist/contract/queries.d.ts +23 -0
  18. package/dist/contract/queries.d.ts.map +1 -0
  19. package/dist/contract/transactions.d.ts +36 -0
  20. package/dist/contract/transactions.d.ts.map +1 -0
  21. package/dist/contract/tusdt-contract.d.ts +186 -0
  22. package/dist/contract/tusdt-contract.d.ts.map +1 -0
  23. package/dist/core/constants.d.ts +10 -0
  24. package/dist/core/constants.d.ts.map +1 -0
  25. package/dist/core/errors.d.ts +85 -0
  26. package/dist/core/errors.d.ts.map +1 -0
  27. package/dist/core/types.d.ts +97 -0
  28. package/dist/core/types.d.ts.map +1 -0
  29. package/dist/core/validation.d.ts +30 -0
  30. package/dist/core/validation.d.ts.map +1 -0
  31. package/dist/index.d.ts +13 -0
  32. package/dist/index.d.ts.map +1 -0
  33. package/dist/index.js +3 -0
  34. package/dist/index.js.map +1 -0
  35. package/dist/server.d.ts +19 -0
  36. package/dist/server.d.ts.map +1 -0
  37. package/dist/server.js +204 -0
  38. package/dist/server.js.map +1 -0
  39. package/dist/signer/types.d.ts +2 -0
  40. package/dist/signer/types.d.ts.map +1 -0
  41. package/metadata/tusdt_erc20.json +1133 -0
  42. package/package.json +83 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 tensorusd
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,167 @@
1
+ # tusdt-sdk
2
+
3
+ TypeScript SDK for the TUSDT ERC20 ink! smart contract on Substrate, built with [Dedot](https://docs.dedot.dev/).
4
+
5
+ Three entry points for different environments:
6
+
7
+ | Entry point | Import path | Use case |
8
+ |---|---|---|
9
+ | Root | `tusdt-sdk` | Shared types, queries, transactions, events |
10
+ | Client | `tusdt-sdk/client` | Browser-safe (same as root, no Node.js APIs) |
11
+ | Server | `tusdt-sdk/server` | Node.js backend: `createSigner`, `TusdtPaymentListener` |
12
+
13
+ ## Install
14
+
15
+ ```bash
16
+ npm install tusdt-sdk
17
+ ```
18
+
19
+ For server-side signing, also install the optional peer dependencies:
20
+
21
+ ```bash
22
+ npm install @polkadot/keyring @polkadot/util-crypto
23
+ ```
24
+
25
+ ## Quick start
26
+
27
+ ### Client (browser / frontend)
28
+
29
+ ```ts
30
+ import { DedotClient, WsProvider } from 'dedot';
31
+ import { TusdtContract } from 'tusdt-sdk/client';
32
+
33
+ const client = await DedotClient.legacy(new WsProvider('wss://test.finney.opentensor.ai:443'));
34
+ const tusdt = new TusdtContract(client, '5Cp7QWW...');
35
+
36
+ // Read-only queries
37
+ const balance = await tusdt.balanceOf('5HhJ...');
38
+ const supply = await tusdt.totalSupply();
39
+ const allowance = await tusdt.allowance(owner, spender);
40
+
41
+ // Transactions (with browser wallet signer)
42
+ const result = await tusdt.transfer(signer, '5HhJ...', 1000n);
43
+ await tusdt.approve(signer, spender, 500n);
44
+
45
+ // Event subscriptions (best-chain, real-time)
46
+ const unsub = await tusdt.onTransfer((event) => {
47
+ console.log(`${event.from} -> ${event.to}: ${event.value}`);
48
+ });
49
+ ```
50
+
51
+ ### Server (Node.js backend)
52
+
53
+ ```ts
54
+ import { DedotClient, WsProvider } from 'dedot';
55
+ import { TusdtContract, createSigner, TusdtPaymentListener } from 'tusdt-sdk/server';
56
+
57
+ const client = await DedotClient.legacy(new WsProvider('wss://test.finney.opentensor.ai:443'));
58
+ const tusdt = new TusdtContract(client, '5Cp7QWW...');
59
+
60
+ // Create a signer from a secret URI
61
+ const alice = await createSigner('//Alice');
62
+ await tusdt.transfer(alice, '5HhJ...', 1000n);
63
+
64
+ // Production payment listener (finalized blocks, reconnection, deduplication)
65
+ const listener = new TusdtPaymentListener(client, '5Cp7QWW...', {
66
+ receiverWallet: '5HhJ...',
67
+ onPayment: (event) => {
68
+ console.log(`Payment: ${event.value} from ${event.from} at block ${event.blockNumber}`);
69
+ // Trigger webhook, update DB, etc.
70
+ },
71
+ onError: (err) => console.error('Listener error:', err),
72
+ });
73
+
74
+ await listener.start();
75
+ // listener.isRunning === true
76
+ // listener.lastBlockNumber === <latest finalized block>
77
+
78
+ // Later: graceful shutdown
79
+ await listener.stop();
80
+ ```
81
+
82
+ ## API
83
+
84
+ ### `TusdtContract`
85
+
86
+ Main SDK class. Wraps a deployed TUSDT ERC20 ink! contract.
87
+
88
+ **Constructor:** `new TusdtContract(client, contractAddress, options?)`
89
+
90
+ | Method | Returns | Description |
91
+ |---|---|---|
92
+ | `controller()` | `Promise<string>` | Controller account address |
93
+ | `totalSupply()` | `Promise<bigint>` | Total token supply |
94
+ | `balanceOf(owner)` | `Promise<bigint>` | Token balance of an account |
95
+ | `allowance(owner, spender)` | `Promise<bigint>` | Remaining allowance |
96
+ | `transfer(signer, to, value, options?)` | `Promise<TxResult>` | Transfer tokens |
97
+ | `approve(signer, spender, value, options?)` | `Promise<TxResult>` | Approve spender |
98
+ | `transferFrom(signer, from, to, value, options?)` | `Promise<TxResult>` | Transfer using allowance |
99
+ | `safeApprove(signer, spender, value, options?)` | `Promise<TxResult>` | Reset-and-set approve |
100
+ | `onTransfer(callback, options?)` | `Promise<Unsubscribe>` | Subscribe to Transfer events |
101
+ | `onApproval(callback, options?)` | `Promise<Unsubscribe>` | Subscribe to Approval events |
102
+ | `onTransferFrom(account, callback, options?)` | `Promise<Unsubscribe>` | Transfers from account |
103
+ | `onTransferTo(account, callback, options?)` | `Promise<Unsubscribe>` | Transfers to account |
104
+ | `onFinalizedTransfer(callback, options?)` | `Promise<Unsubscribe>` | Finalized Transfer events |
105
+ | `onFinalizedTransferTo(account, callback, options?)` | `Promise<Unsubscribe>` | Finalized transfers to account |
106
+
107
+ ### `TusdtPaymentListener`
108
+
109
+ Production-grade payment listener for server-side use.
110
+
111
+ - Listens on **finalized blocks** only (no reorg risk)
112
+ - Automatic **reconnection** with exponential backoff
113
+ - **Event deduplication** (monotonic block tracking + bounded event ID set)
114
+ - **Error isolation** (callback errors don't kill the subscription)
115
+
116
+ ```ts
117
+ new TusdtPaymentListener(client, contractAddress, {
118
+ receiverWallet: '5HhJ...',
119
+ onPayment: (event) => { /* ... */ },
120
+ onError: (err) => { /* ... */ },
121
+ reconnect: { // optional, defaults shown
122
+ maxRetries: Infinity,
123
+ baseDelayMs: 1000,
124
+ maxDelayMs: 30000,
125
+ },
126
+ });
127
+ ```
128
+
129
+ | Method/Property | Description |
130
+ |---|---|
131
+ | `start()` | Begin listening |
132
+ | `stop()` | Unsubscribe and clean up |
133
+ | `isRunning` | Whether the listener is active |
134
+ | `lastBlockNumber` | Last processed finalized block number |
135
+
136
+ ### Standalone functions
137
+
138
+ All tree-shakeable -- import only what you need:
139
+
140
+ ```ts
141
+ import { queryBalanceOf, executeTransfer } from 'tusdt-sdk';
142
+ import { watchFinalizedTransfers } from 'tusdt-sdk';
143
+ ```
144
+
145
+ ### Error types
146
+
147
+ | Error | Thrown when |
148
+ |---|---|
149
+ | `InsufficientBalanceError` | Sender has insufficient balance |
150
+ | `InsufficientAllowanceError` | Spender has insufficient allowance |
151
+ | `ValidationError` | Invalid address, negative value, or u64 overflow |
152
+ | `DryRunError` | Dry-run query fails |
153
+ | `DispatchError` | Transaction dispatch fails on chain |
154
+
155
+ ## Finality levels
156
+
157
+ | Option | Use case |
158
+ |---|---|
159
+ | `waitFor: 'bestChainBlockIncluded'` | Default. Fast confirmation, may reorg. |
160
+ | `waitFor: 'finalized'` | Slower but immutable. Use for payments. |
161
+
162
+ Event subscriptions via `onTransfer`/`onApproval` use best-chain blocks.
163
+ Event subscriptions via `onFinalizedTransfer`/`onFinalizedTransferTo` and `TusdtPaymentListener` use finalized blocks.
164
+
165
+ ## License
166
+
167
+ MIT