Skip to content

Commit 6c41fa9

Browse files
committed
fix(tx-sdk): further improvements
1 parent 112dda5 commit 6c41fa9

File tree

3 files changed

+78
-66
lines changed

3 files changed

+78
-66
lines changed

docs/network/flare-tx-sdk/1-getting-started.mdx

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ Add the SDK to your project:
4949
```
5050

5151
</TabItem>
52+
<TabItem value="pnpm" label="pnpm">
53+
54+
```bash
55+
pnpm add @flarenetwork/flare-tx-sdk
56+
```
57+
58+
</TabItem>
5259

5360
</Tabs>
5461

@@ -66,10 +73,10 @@ This ensures the same wallet can be used across multiple networks without code c
6673
The `Network` object provides methods to query balances, transfer tokens, claim rewards, and interact with contracts.
6774
It uses Flare's [public RPCs](/network/overview#configuration) by default.
6875

69-
- **`Wallet`**: Represents a user account. The SDK does not store private keys. Instead, it defines a `Wallet` interface which can be implemented by:
70-
- MetaMask and other browser wallets (`EIP1193WalletController`)
71-
- Ledger (`LedgerWalletController`)
72-
- Trezor (`TrezorWalletController`)
76+
- **`Wallet`**: Represents a user account. The SDK does not store private keys. Instead, it defines a [`Wallet`](https://github.com/flare-foundation/flare-tx-sdk/blob/HEAD/src/wallet/index.ts) interface which can be implemented by:
77+
- MetaMask and other browser wallets - `EIP1193WalletController`
78+
- Ledger - `LedgerWalletController`
79+
- Trezor - `TrezorWalletController`
7380
- Your own custom signer
7481

7582
## Example implementation
@@ -120,8 +127,15 @@ Example output:
120127

121128
:::info[Units]
122129

123-
The SDK uses nats (smallest unit of FLR, equivalent to wei).
124-
Use helpers methods `Amount.nats(x)` for FLR and `Amount.wnats(x)` for WFLR.
130+
The SDK uses wei, the smallest unit of FLR (1 FLR = $10^{18}$ wei).
131+
To convert to wei:
132+
133+
```javascript
134+
import { Amount } from "@flarenetwork/flare-tx-sdk";
135+
136+
const natsAmount = Amount.nats(10); // convert 10 FLR to wei
137+
const wNatsAmount = Amount.wnats(10); // convert 10 WFLR to wei
138+
```
125139

126140
:::
127141

docs/network/flare-tx-sdk/2-cookbook.mdx

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,23 @@ const stakedBalance = await network.getBalanceStakedOnP(publicKey);
4747

4848
:::info
4949

50-
Balances are returned in nats (the smallest FLR unit, equivalent to wei).
51-
Use `Amount.nats()` or `Amount.wnats()` helpers to convert human-readable values.
50+
The balances are returned in wei, the smallest unit of FLR (1 FLR = $10^{18}$ wei).
5251

5352
:::
5453

5554
## Wrap and transfer
5655

5756
Transfer native FLR and its wrapped form (WFLR) on the C-Chain.
58-
Wrapping is required for governance and FTSO delegation, since only WFLR can be delegated.
57+
Wrapping is required for governance and FTSO delegation.
5958

60-
- **FLR** (native token) lives on both chains.
61-
- **WFLR** (wrapped FLR) exists only on the C-Chain for use in smart contracts and delegation.
62-
- The SDK provides simple helpers for wrapping, unwrapping, and transferring.
59+
- **FLR** (native token) lives on both C and P-Chain.
60+
- **WFLR** (wrapped FLR) exists only on the C-Chain for use in smart contracts, governance and delegation.
6361

6462
```javascript
6563
import { Amount } from "@flarenetwork/flare-tx-sdk";
6664

6765
// Transfer 1 FLR
68-
await network.transferNative(wallet, recipientAddress, Amount.nats(1)); // Amount.nats handles the conversion to wei
66+
await network.transferNative(wallet, recipientAddress, Amount.nats(1));
6967

7068
// Wrap 1 FLR -> 1 WFLR
7169
await network.wrapNative(wallet, Amount.nats(1));
@@ -77,20 +75,13 @@ await network.transferWrapped(wallet, recipientAddress, Amount.wnats(1));
7775
await network.unwrapToNative(wallet, Amount.wnats(1));
7876
```
7977

80-
:::info
81-
82-
- All transfers return a transaction receipt once confirmed.
83-
- Wrapping/unwrapping is required to participate in FTSO delegation and governance.
84-
85-
:::
86-
8778
## Claim rewards
8879

8980
Flare supports multiple reward sources: FlareDrops, staking rewards, rNat (e.g. rFLR), and FTSO rewards.
9081

9182
### FlareDrops
9283

93-
Rewards periodically distributed to token holders.
84+
[FlareDrops](https://flare.network/news/flaredrop-guide) are rewards periodically distributed to token holders.
9485

9586
```javascript
9687
// Check and claim FlareDrop rewards
@@ -102,9 +93,11 @@ if (flareDropAmount > 0) {
10293

10394
Optional parameters:
10495

105-
- `rewardOwner` (address) - C-Chain address of the reward owner
106-
- `recipient` (address) - C-Chain address to receive the reward
107-
- `wrap` (bool) - `true` for WFLR, `false` for FLR (default).
96+
| **Field** | **Type** | **Description** |
97+
| ------------- | --------- | ------------------------------------------ |
98+
| `rewardOwner` | `address` | C-Chain address of the reward owner |
99+
| `recipient` | `address` | C-Chain address to receive the reward |
100+
| `wrap` | `bool` | `true` for WFLR, `false` for FLR (default) |
108101

109102
### Staking rewards
110103

@@ -139,9 +132,11 @@ let projects = await network.getRNatProjects();
139132

140133
Returns an array of objects of type [`RNatProject`](https://github.com/flare-foundation/flare-tx-sdk/blob/HEAD/src/network/iotype.ts) with:
141134

142-
- `id` (int) - Project ID
143-
- `name` (string) - Project name
144-
- `claimingDisabled` (bool) - Whether claiming rewards is disabled for this project
135+
| **Field** | **Type** | **Description** |
136+
| ------------------ | -------- | ----------------------------------------------------- |
137+
| `id` | `number` | Project ID |
138+
| `name` | `string` | Project name |
139+
| `claimingDisabled` | `bool` | Whether claiming rewards is disabled for this project |
145140

146141
Claim rewards for one or more projects:
147142

@@ -171,9 +166,11 @@ let balance = await network.getRNatAccountBalance(cAddress);
171166

172167
The returned [`RNatAccountBalance`](https://github.com/flare-foundation/flare-tx-sdk/blob/HEAD/src/network/iotype.ts) object includes:
173168

174-
- `wNatBalance` (int) - Wrapped coin balance (wei)
175-
- `rNatBalance` (int) - rNat token balance (wei)
176-
- `lockedBalance` (int) - Locked wrapped coin balance (wei)
169+
| **Field** | **Type** | **Description** |
170+
| --------------- | -------- | --------------------------------- |
171+
| `wNatBalance` | `bigint` | Wrapped coin balance (wei) |
172+
| `rNatBalance` | `bigint` | rNat token balance (wei) |
173+
| `lockedBalance` | `bigint` | Locked wrapped coin balance (wei) |
177174

178175
:::warning[Withdrawing all funds]
179176

@@ -226,9 +223,11 @@ if (ftsoRewardAmount > 0) {
226223

227224
Optional parameters:
228225

229-
- `rewardOwner` (address) - C-Chain address of the reward owner
230-
- `recipient` (address) - C-Chain address of where the reward should be transferred
231-
- `wrap` (bool) - transfer reward as native (default) or wrapped.
226+
| **Field** | **Type** | **Description** |
227+
| ------------- | --------- | --------------------------------------------------------- |
228+
| `rewardOwner` | `address` | C-Chain address of the reward owner |
229+
| `recipient` | `address` | C-Chain address of where the reward should be transferred |
230+
| `wrap` | `bool` | Transfer reward as native (default) or wrapped |
232231

233232
#### Claim with proofs
234233

@@ -239,10 +238,12 @@ As an example for reward epoch 320, see the proofs in [`reward-distribution-data
239238
await network.claimFtsoReward(wallet, rewardOwner, recipient, wrap, proofs);
240239
```
241240

242-
where `proofs` is an array of objects [`FtsoRewardClaimWithProof`](https://github.com/flare-foundation/flare-tx-sdk/blob/HEAD/src/network/iotype.ts) with:
241+
`proofs` is an array of [`FtsoRewardClaimWithProof`](https://github.com/flare-foundation/flare-tx-sdk/blob/HEAD/src/network/iotype.ts):
243242

244-
- `merkleProof` (array of string) - The Merkle proof in hexadecimal encoding.
245-
- `body` ([`FtsoRewardClaim`](https://github.com/flare-foundation/flare-tx-sdk/blob/HEAD/src/network/iotype.ts) ) - Same structure as `FtsoRewardState` without the `initialised` parameter.
243+
| **Field** | **Type** | **Description** |
244+
| ------------- | ----------------------------------------------------------------------------------------------------- | ----------------------------------------- |
245+
| `merkleProof` | `string[]` | The Merkle proof in hexadecimal encoding. |
246+
| `body` | [`FtsoRewardClaim`](https://github.com/flare-foundation/flare-tx-sdk/blob/HEAD/src/network/iotype.ts) | Specifying the reward claim details |
246247

247248
## Delegate to FTSO providers
248249

@@ -256,25 +257,25 @@ import { Amount } from "@flarenetwork/flare-tx-sdk";
256257
const delegations = await network.getFtsoDelegatesOf(cAddress);
257258

258259
// Delegate 100% to one provider
259-
await network.delegateToFtso(wallet, provider, Amount.percentages(100));
260+
await network.delegateToFtso(wallet, providerAddress, Amount.percentages(100));
260261

261262
// Split 50/50 between two providers
262263
await network.delegateToFtso(
263264
wallet,
264-
provider1,
265+
provider1Address,
265266
Amount.percentages(50),
266-
provider2,
267+
provider2Address,
267268
Amount.percentages(50),
268269
);
269270

270271
// Remove all delegations
271272
await network.undelegateFromFtso(wallet);
272273
```
273274

274-
:::info
275+
:::info[Basis points]
275276

276-
- Delegations are expressed in basis points (1% = 100 bp).
277-
- Rewards can later be claimed via [`getClaimableFtsoReward`](#ftso-delegation-rewards).
277+
Delegations are expressed in basis points (1 % = 100 bp).
278+
`Amount.percentages()` handles conversion of percent to basis points.
278279

279280
:::
280281

@@ -293,8 +294,8 @@ The SDK automates these steps.
293294
2. **Delegate on P-Chain:** Once funds are on the P-Chain, you can delegate them.
294295

295296
```javascript
296-
const amountToDelegate = Amount.nats(50);
297-
const nodeId = "NodeID-P73B..."; // The ID of the validator
297+
const amountToDelegate = Amount.nats(50); // 50 FLR
298+
const nodeId = "NodeID-P73B..."; // The ID of the validator, see https://flare-systems-explorer.flare.network/validators
298299
const startTime = Math.floor(Date.now() / 1000); // Current unix timestamp
299300
const endTime = startTime + 14 * 24 * 60 * 60; // e.g. end in 14 days
300301

@@ -303,7 +304,7 @@ The SDK automates these steps.
303304
amountToDelegate,
304305
nodeId,
305306
startTime,
306-
endTime,
307+
endTime, // optional
307308
);
308309
```
309310

@@ -347,6 +348,7 @@ A proposal can be voted on only when its `state` is ACTIVE.
347348
// Check if a voter has already voted:
348349
await network.hasCastVoteForFoundationProposal(cAddress, proposalId);
349350

351+
// Indicate direction of vote
350352
const support = FoundationProposalSupport.FOR; // or FoundationProposalSupport.AGAINST
351353

352354
// Cast vote
@@ -362,12 +364,9 @@ const votePower = await network.getCurrentGovernanceVotePower(cAddress);
362364

363365
// Get current delegate:
364366
let delegate = await network.getCurrentGovernanceVoteDelegate(cAddress);
365-
```
367+
// If `delegate == 0x0` - No delegate assigned
368+
// If `delegate != 0x0` - Voter's own vote power is zero (all delegated)
366369

367-
- If `delegate == 0x0` - No delegate assigned
368-
- If `delegate != 0x0` - Voter's own vote power is zero (all delegated)
369-
370-
```javascript
371370
// Delegate all vote power to another address:
372371
await network.delegateGovernanceVotePower(wallet, delegateAddress);
373372

@@ -390,7 +389,7 @@ const delegate = await network.getVoteDelegateForFoundationProposal(
390389
);
391390
```
392391

393-
Queries may **fail** for older proposals, since historical governance vote data is periodically removed from C-Chain storage.
392+
Queries **may fail** for older proposals, since historical governance vote data is periodically removed from C-Chain storage.
394393

395394
:::
396395

@@ -413,8 +412,8 @@ await network.invokeContractMethodOnC(wallet, "FtsoRewardManager", abi, "claimRe
413412

414413
:::info
415414

416-
- ABI must match the contract interface.
417-
- For official Flare contracts, names are predefined and easier to use.
415+
The ABI must match the contract interface.
416+
For official Flare contracts, i.e. contracts defined in the [`FlareContractRegistry`](/network/solidity-reference/IFlareContractRegistry), names are predefined and easier to use.
418417

419418
:::
420419

docs/network/flare-tx-sdk/3-advanced-usage.mdx

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,24 @@ tags: [advanced, javascript]
88

99
## Transaction lifecycle tracking
1010

11-
Every transaction goes through a series of stages:
11+
Every transaction goes through a series of stages.
12+
The SDK allows you to register callbacks at each stage to inspect or intercept the process.
13+
This is useful for logging, UI updates, or offline signing workflows.
1214

1315
```plaintext
14-
[Unsigned] → [Before Signature] → [Signed] → [Before Submission] → [Submitted] → [After Submission] → [Confirmed]
16+
Unsigned
17+
↓ sign
18+
Signed (before submission)
19+
↓ submit
20+
Submitted (pending confirmation)
21+
↓ confirm
22+
Confirmed
1523
```
1624

17-
The SDK allows you to register callbacks at each stage to inspect or intercept the process.
18-
This is useful for logging, UI updates, security audits, or offline signing workflows.
19-
2025
### Example
2126

2227
```javascript
23-
// 1. Before Signing
28+
// 1. Before Signature
2429
network.setBeforeTxSignatureCallback(async (data) => {
2530
console.log("Transaction prepared for signing:", data.unsignedTxHex);
2631
console.log("Transaction type:", data.txType);
@@ -63,7 +68,7 @@ If implementing your own wallet, ensure private keys are never exposed; use secu
6368
:::info
6469

6570
C-Chain support is sufficient for most applications.
66-
See [wallet.ts](https://github.com/flare-foundation/flare-tx-sdk/blob/main/src/wallet/wallet.ts) for the full Wallet interface.
71+
See the full [Wallet](https://github.com/flare-foundation/flare-tx-sdk/blob/main/src/wallet/wallet.ts) interface.
6772

6873
:::
6974

@@ -147,12 +152,6 @@ export class EthersWallet implements SdkWallet {
147152
}
148153
```
149154

150-
:::tip
151-
152-
Start with C-Chain only. Add P-Chain support once you need staking or P-Chain operations.
153-
154-
:::
155-
156155
:::tip[Need help or found a bug?]
157156

158157
Open an issue on the [flare-foundation/flare-tx-sdk](https://github.com/flare-foundation/flare-tx-sdk) GitHub repository.

0 commit comments

Comments
 (0)