tronlink-signer 0.1.3 → 0.1.4
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 +40 -17
- package/dist/index.d.ts +22 -10
- package/dist/index.js +1028 -102
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -17,8 +17,8 @@ const signer = new TronSigner();
|
|
|
17
17
|
await signer.start();
|
|
18
18
|
|
|
19
19
|
const { address, network } = await signer.connectWallet();
|
|
20
|
-
const { txId } = await signer.sendTrx("TXxx...", 1);
|
|
21
|
-
const { txId: txId2, status } = await signer.signTransaction(tx, "nile", true); // broadcast + auto-confirm
|
|
20
|
+
const { txId, status } = await signer.sendTrx("TXxx...", 1); // status: "success" | "pending" | "failed"
|
|
21
|
+
const { txId: txId2, status: s2 } = await signer.signTransaction(tx, "nile", true); // broadcast + auto-confirm
|
|
22
22
|
const { balance } = await signer.getBalance("TXxx...");
|
|
23
23
|
|
|
24
24
|
await signer.stop();
|
|
@@ -46,9 +46,9 @@ Returns the current configuration.
|
|
|
46
46
|
|
|
47
47
|
Opens the browser to connect TronLink and retrieve the wallet address and current network. If the wallet is already connected (same browser tab still open), auto-completes without user interaction.
|
|
48
48
|
|
|
49
|
-
### `signer.sendTrx(to, amount, network?, options?): Promise<
|
|
49
|
+
### `signer.sendTrx(to, amount, network?, options?): Promise<BroadcastResult>`
|
|
50
50
|
|
|
51
|
-
Sends TRX to a recipient address. Opens a browser approval page for the user to confirm.
|
|
51
|
+
Sends TRX to a recipient address. Opens a browser approval page for the user to confirm. Returns `{ txId, status, error? }` where `status` is `"success"`, `"pending"`, or `"failed"` (see [Broadcast Result](#broadcast-result)).
|
|
52
52
|
|
|
53
53
|
| Parameter | Type | Description |
|
|
54
54
|
| --------- | ---- | ----------- |
|
|
@@ -57,16 +57,16 @@ Sends TRX to a recipient address. Opens a browser approval page for the user to
|
|
|
57
57
|
| `network` | `TronNetwork` | Optional network override |
|
|
58
58
|
| `options` | `SignerOptions` | Optional — pass `{ signal }` to enable cancellation |
|
|
59
59
|
|
|
60
|
-
### `signer.sendTrc20(contractAddress, to, amount, decimals?, network?, options?): Promise<
|
|
60
|
+
### `signer.sendTrc20(contractAddress, to, amount, decimals?, network?, options?): Promise<BroadcastResult>`
|
|
61
61
|
|
|
62
|
-
Sends TRC20 tokens. Opens a browser approval page.
|
|
62
|
+
Sends TRC20 tokens. Opens a browser approval page. Returns `{ txId, status, error? }` — see [Broadcast Result](#broadcast-result).
|
|
63
63
|
|
|
64
64
|
| Parameter | Type | Description |
|
|
65
65
|
| --------- | ---- | ----------- |
|
|
66
66
|
| `contractAddress` | `string` | TRC20 token contract address |
|
|
67
67
|
| `to` | `string` | Recipient Tron address (base58) |
|
|
68
68
|
| `amount` | `string` | Amount in human-readable units (e.g., `"1.5"` for 1.5 USDT). Decimals conversion is automatic. |
|
|
69
|
-
| `decimals` | `number` | Token decimals (
|
|
69
|
+
| `decimals` | `number` | Optional. Token decimals. Omit to auto-detect via the contract's `decimals()` view (recommended — avoids 10^N magnitude errors on non-6dp tokens like USDD/SUN/JST). |
|
|
70
70
|
| `network` | `TronNetwork` | Optional network override |
|
|
71
71
|
| `options` | `SignerOptions` | Optional — pass `{ signal }` to enable cancellation |
|
|
72
72
|
|
|
@@ -89,9 +89,9 @@ const { signature } = await signer.signTypedData({
|
|
|
89
89
|
});
|
|
90
90
|
```
|
|
91
91
|
|
|
92
|
-
### `signer.signTransaction(transaction, network?, broadcast?, options?): Promise<{ signedTransaction; txId?; status? }>`
|
|
92
|
+
### `signer.signTransaction(transaction, network?, broadcast?, options?): Promise<{ signedTransaction; txId?; status?; error? }>`
|
|
93
93
|
|
|
94
|
-
Signs a raw transaction. When `broadcast` is `true`, the signed transaction is broadcast on-chain via TronLink and the SDK automatically polls for on-chain confirmation (
|
|
94
|
+
Signs a raw transaction. When `broadcast` is `true`, the signed transaction is broadcast on-chain via TronLink and the SDK automatically polls for on-chain confirmation — see [Broadcast Result](#broadcast-result) for the possible `status` values.
|
|
95
95
|
|
|
96
96
|
| Parameter | Type | Description |
|
|
97
97
|
| --------- | ---- | ----------- |
|
|
@@ -105,9 +105,10 @@ Signs a raw transaction. When `broadcast` is `true`, the signed transaction is b
|
|
|
105
105
|
const { signedTransaction } = await signer.signTransaction(tx);
|
|
106
106
|
|
|
107
107
|
// Sign, broadcast, and wait for confirmation (default)
|
|
108
|
-
const { signedTransaction, txId, status } = await signer.signTransaction(tx, "nile", true);
|
|
108
|
+
const { signedTransaction, txId, status, error } = await signer.signTransaction(tx, "nile", true);
|
|
109
109
|
// status === "success" — confirmed on-chain
|
|
110
110
|
// status === "pending" — broadcast succeeded but not confirmed within timeout
|
|
111
|
+
// status === "failed" — on-chain execution failed; `error` carries the reason
|
|
111
112
|
|
|
112
113
|
// Broadcast without waiting for confirmation
|
|
113
114
|
const result = await signer.signTransaction(tx, "nile", true, { confirm: false });
|
|
@@ -118,9 +119,23 @@ const result = await signer.signTransaction(tx, "nile", true, {
|
|
|
118
119
|
});
|
|
119
120
|
```
|
|
120
121
|
|
|
121
|
-
###
|
|
122
|
+
### Broadcast Result
|
|
122
123
|
|
|
123
|
-
|
|
124
|
+
Broadcasting methods (`sendTrx`, `sendTrc20`, and `signTransaction` with `broadcast: true`) return a `BroadcastResult`:
|
|
125
|
+
|
|
126
|
+
```ts
|
|
127
|
+
interface BroadcastResult {
|
|
128
|
+
txId: string;
|
|
129
|
+
status: "success" | "pending" | "failed";
|
|
130
|
+
error?: string; // set when status === "failed"
|
|
131
|
+
}
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
`"failed"` means the transaction reached the chain but execution failed (e.g. `OUT_OF_ENERGY`, Solidity revert). **It is not thrown** — inspect `status` instead. The SDK only throws when broadcast itself never happened (signature error, user rejection, network unreachable, etc.).
|
|
135
|
+
|
|
136
|
+
### `signer.waitForTransaction(txId, network?, options?): Promise<{ status; error? }>`
|
|
137
|
+
|
|
138
|
+
Polls the network for transaction confirmation. Returns `{ status: "success" }` when confirmed, `{ status: "pending" }` on timeout, or `{ status: "failed", error }` when the transaction reverted / ran out of energy.
|
|
124
139
|
|
|
125
140
|
| Parameter | Type | Description |
|
|
126
141
|
| --------- | ---- | ----------- |
|
|
@@ -129,10 +144,9 @@ Polls the network for transaction confirmation. Returns `"success"` when the tra
|
|
|
129
144
|
| `options` | `WaitForTransactionOptions` | Optional — `timeoutMs` (default: 30000), `signal` |
|
|
130
145
|
|
|
131
146
|
```ts
|
|
132
|
-
const status = await signer.waitForTransaction(txId, "nile");
|
|
133
|
-
if (status === "success")
|
|
134
|
-
|
|
135
|
-
}
|
|
147
|
+
const { status, error } = await signer.waitForTransaction(txId, "nile");
|
|
148
|
+
if (status === "success") console.log("Confirmed on-chain");
|
|
149
|
+
else if (status === "failed") console.warn("Execution failed:", error);
|
|
136
150
|
```
|
|
137
151
|
|
|
138
152
|
> **Note:** When using `signTransaction` with `broadcast: true`, confirmation polling is automatic — you don't need to call `waitForTransaction` separately unless you set `confirm: false`.
|
|
@@ -237,6 +251,14 @@ interface WaitForTransactionOptions {
|
|
|
237
251
|
timeoutMs?: number;
|
|
238
252
|
signal?: AbortSignal;
|
|
239
253
|
}
|
|
254
|
+
|
|
255
|
+
type BroadcastStatus = "success" | "pending" | "failed";
|
|
256
|
+
|
|
257
|
+
interface BroadcastResult {
|
|
258
|
+
txId: string;
|
|
259
|
+
status: BroadcastStatus;
|
|
260
|
+
error?: string;
|
|
261
|
+
}
|
|
240
262
|
```
|
|
241
263
|
|
|
242
264
|
## Exports
|
|
@@ -252,9 +274,10 @@ export { NETWORKS, DEFAULT_HTTP_PORT, REQUEST_TIMEOUT_MS, loadConfig } from "./c
|
|
|
252
274
|
export type {
|
|
253
275
|
TronNetwork, NetworkConfig, AppConfig,
|
|
254
276
|
PendingRequestType, PendingRequest,
|
|
255
|
-
|
|
277
|
+
SendTrxData, SendTrc20Data,
|
|
256
278
|
SignMessageData, SignTypedDataData, SignTransactionData,
|
|
257
279
|
SignerOptions, WaitForTransactionOptions,
|
|
280
|
+
BroadcastStatus, BroadcastResult,
|
|
258
281
|
} from "./types.js";
|
|
259
282
|
```
|
|
260
283
|
|
package/dist/index.d.ts
CHANGED
|
@@ -20,7 +20,7 @@ interface SendTrc20Data {
|
|
|
20
20
|
contractAddress: string;
|
|
21
21
|
to: string;
|
|
22
22
|
amount: string;
|
|
23
|
-
decimals
|
|
23
|
+
decimals?: number;
|
|
24
24
|
}
|
|
25
25
|
interface SignMessageData {
|
|
26
26
|
message: string;
|
|
@@ -32,6 +32,13 @@ interface SignTransactionData {
|
|
|
32
32
|
transaction: Record<string, unknown>;
|
|
33
33
|
broadcast?: boolean;
|
|
34
34
|
}
|
|
35
|
+
type BroadcastStatus = "success" | "pending" | "failed";
|
|
36
|
+
interface BroadcastResult {
|
|
37
|
+
txId: string;
|
|
38
|
+
status: BroadcastStatus;
|
|
39
|
+
/** Failure reason when status === "failed". */
|
|
40
|
+
error?: string;
|
|
41
|
+
}
|
|
35
42
|
interface AppConfig {
|
|
36
43
|
network: TronNetwork;
|
|
37
44
|
httpPort: number;
|
|
@@ -67,6 +74,7 @@ declare class TronSigner {
|
|
|
67
74
|
private _onBrowserDisconnect;
|
|
68
75
|
private _onWalletChanged;
|
|
69
76
|
private connectedWallet;
|
|
77
|
+
private broadcastListeners;
|
|
70
78
|
set onBrowserDisconnect(cb: (() => void) | null);
|
|
71
79
|
/**
|
|
72
80
|
* Fires when the user switches account/network or disconnects inside TronLink.
|
|
@@ -76,11 +84,15 @@ declare class TronSigner {
|
|
|
76
84
|
*/
|
|
77
85
|
set onWalletChanged(cb: ((reason: string) => void) | null);
|
|
78
86
|
constructor();
|
|
87
|
+
/** Register a listener that fires the moment the browser reports a successful broadcast
|
|
88
|
+
* (before on-chain confirmation). Auto-removed when `promise` settles. */
|
|
89
|
+
private registerBroadcastListener;
|
|
79
90
|
start(): Promise<void>;
|
|
80
91
|
private getPort;
|
|
81
92
|
stop(): Promise<void>;
|
|
82
93
|
getConfig(): AppConfig;
|
|
83
94
|
private resolveNetwork;
|
|
95
|
+
private confirmIfNeeded;
|
|
84
96
|
/** @returns true if the signal was already aborted */
|
|
85
97
|
private attachAbortSignal;
|
|
86
98
|
connectWallet(network?: TronNetwork, options?: SignerOptions): Promise<{
|
|
@@ -98,12 +110,8 @@ declare class TronSigner {
|
|
|
98
110
|
address: string;
|
|
99
111
|
network: TronNetwork;
|
|
100
112
|
} | null;
|
|
101
|
-
sendTrx(to: string, amount: string | number, network?: TronNetwork, options?: SignerOptions): Promise<
|
|
102
|
-
|
|
103
|
-
}>;
|
|
104
|
-
sendTrc20(contractAddress: string, to: string, amount: string, decimals?: number, network?: TronNetwork, options?: SignerOptions): Promise<{
|
|
105
|
-
txId: string;
|
|
106
|
-
}>;
|
|
113
|
+
sendTrx(to: string, amount: string | number, network?: TronNetwork, options?: SignerOptions): Promise<BroadcastResult>;
|
|
114
|
+
sendTrc20(contractAddress: string, to: string, amount: string, decimals?: number, network?: TronNetwork, options?: SignerOptions): Promise<BroadcastResult>;
|
|
107
115
|
signMessage(message: string, network?: TronNetwork, options?: SignerOptions): Promise<{
|
|
108
116
|
signature: string;
|
|
109
117
|
}>;
|
|
@@ -113,14 +121,18 @@ declare class TronSigner {
|
|
|
113
121
|
signTransaction(transaction: Record<string, unknown>, network?: TronNetwork, broadcast?: boolean, options?: SignerOptions): Promise<{
|
|
114
122
|
signedTransaction: Record<string, unknown>;
|
|
115
123
|
txId?: string;
|
|
116
|
-
status?:
|
|
124
|
+
status?: BroadcastStatus;
|
|
125
|
+
error?: string;
|
|
117
126
|
}>;
|
|
118
127
|
getBalance(address: string, network?: TronNetwork): Promise<{
|
|
119
128
|
balance: string;
|
|
120
129
|
balanceSun: number;
|
|
121
130
|
}>;
|
|
122
131
|
private getTronWebFor;
|
|
123
|
-
waitForTransaction(txId: string, network?: TronNetwork, options?: WaitForTransactionOptions): Promise<
|
|
132
|
+
waitForTransaction(txId: string, network?: TronNetwork, options?: WaitForTransactionOptions): Promise<{
|
|
133
|
+
status: BroadcastStatus;
|
|
134
|
+
error?: string;
|
|
135
|
+
}>;
|
|
124
136
|
}
|
|
125
137
|
|
|
126
138
|
declare const NETWORKS: Record<TronNetwork, NetworkConfig>;
|
|
@@ -128,4 +140,4 @@ declare const DEFAULT_HTTP_PORT = 3386;
|
|
|
128
140
|
declare const REQUEST_TIMEOUT_MS: number;
|
|
129
141
|
declare function loadConfig(): AppConfig;
|
|
130
142
|
|
|
131
|
-
export { type AppConfig, DEFAULT_HTTP_PORT, NETWORKS, type NetworkConfig, type PendingRequest, type PendingRequestType, REQUEST_TIMEOUT_MS, type SendTrc20Data, type SendTrxData, type SignMessageData, type SignTransactionData, type SignTypedDataData, type SignerOptions, type TronNetwork, TronSigner, type WaitForTransactionOptions, loadConfig };
|
|
143
|
+
export { type AppConfig, type BroadcastResult, type BroadcastStatus, DEFAULT_HTTP_PORT, NETWORKS, type NetworkConfig, type PendingRequest, type PendingRequestType, REQUEST_TIMEOUT_MS, type SendTrc20Data, type SendTrxData, type SignMessageData, type SignTransactionData, type SignTypedDataData, type SignerOptions, type TronNetwork, TronSigner, type WaitForTransactionOptions, loadConfig };
|