genlayer-js 0.18.3 → 0.18.5

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 (44) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/CLAUDE.md +66 -0
  3. package/README.md +52 -1
  4. package/dist/chains/index.cjs +2 -2
  5. package/dist/chains/index.d.cts +2 -2
  6. package/dist/chains/index.d.ts +2 -2
  7. package/dist/chains/index.js +1 -1
  8. package/dist/{chains-BYSCF33g.d.cts → chains-B7B7UXdn.d.cts} +7 -2
  9. package/dist/{chains-BYSCF33g.d.ts → chains-B7B7UXdn.d.ts} +7 -2
  10. package/dist/{chunk-GEN4SJ6K.js → chunk-NZI52PRP.js} +535 -4
  11. package/dist/{chunk-ZKBMABRA.cjs → chunk-TBF5WZHL.cjs} +536 -5
  12. package/dist/{index-Bw-kn7CF.d.cts → index-BVDASTaU.d.cts} +1 -1
  13. package/dist/index-DPyenUTZ.d.ts +1380 -0
  14. package/dist/index-Dfaw9WiR.d.cts +1380 -0
  15. package/dist/{index-DCzgdyMa.d.ts → index-ucNO2REF.d.ts} +1 -1
  16. package/dist/index.cjs +541 -39
  17. package/dist/index.d.cts +17 -5
  18. package/dist/index.d.ts +17 -5
  19. package/dist/index.js +518 -16
  20. package/dist/types/index.cjs +2 -2
  21. package/dist/types/index.d.cts +2 -2
  22. package/dist/types/index.d.ts +2 -2
  23. package/dist/types/index.js +1 -1
  24. package/package.json +1 -1
  25. package/src/abi/index.ts +1 -0
  26. package/src/abi/staking.ts +518 -0
  27. package/src/chains/localnet.ts +3 -1
  28. package/src/chains/studionet.ts +2 -0
  29. package/src/chains/testnetAsimov.ts +885 -877
  30. package/src/client/client.ts +17 -15
  31. package/src/index.ts +1 -0
  32. package/src/staking/actions.ts +598 -0
  33. package/src/staking/index.ts +2 -0
  34. package/src/staking/utils.ts +22 -0
  35. package/src/transactions/actions.ts +6 -8
  36. package/src/types/chains.ts +7 -2
  37. package/src/types/clients.ts +6 -6
  38. package/src/types/index.ts +1 -0
  39. package/src/types/staking.ts +228 -0
  40. package/tsconfig.vitest-temp.json +41 -0
  41. package/dist/index-Rls_T310.d.ts +0 -407
  42. package/dist/index-tixobC8m.d.cts +0 -407
  43. /package/dist/{chunk-FPZNF3JH.cjs → chunk-W4V73RPN.cjs} +0 -0
  44. /package/dist/{chunk-47QDX7IX.js → chunk-ZHBOSLFN.js} +0 -0
@@ -0,0 +1,22 @@
1
+ import {parseEther, formatEther} from "viem";
2
+
3
+ /**
4
+ * Parse staking amount. Use "gen" suffix for GEN tokens (e.g. "42gen"),
5
+ * otherwise value is treated as wei (e.g. "42000000000000000000" = 42 GEN).
6
+ */
7
+ export function parseStakingAmount(amount: string | bigint): bigint {
8
+ if (typeof amount === "bigint") return amount;
9
+ const trimmed = amount.trim();
10
+ const lower = trimmed.toLowerCase();
11
+ if (lower.endsWith("gen")) {
12
+ return parseEther(lower.slice(0, -3).trim());
13
+ }
14
+ return BigInt(trimmed);
15
+ }
16
+
17
+ /**
18
+ * Format bigint amount to human-readable GEN string.
19
+ */
20
+ export function formatStakingAmount(amount: bigint): string {
21
+ return `${formatEther(amount)} GEN`;
22
+ }
@@ -14,8 +14,6 @@ import {Abi, PublicClient, Address} from "viem";
14
14
  import {localnet} from "@/chains/localnet";
15
15
  import {decodeLocalnetTransaction, decodeTransaction, simplifyTransactionReceipt} from "./decoders";
16
16
 
17
-
18
-
19
17
  export const receiptActions = (client: GenLayerClient<GenLayerChain>, publicClient: PublicClient) => ({
20
18
  waitForTransactionReceipt: async ({
21
19
  hash,
@@ -70,7 +68,7 @@ export const receiptActions = (client: GenLayerClient<GenLayerChain>, publicClie
70
68
 
71
69
  export const transactionActions = (client: GenLayerClient<GenLayerChain>, publicClient: PublicClient) => ({
72
70
  getTransaction: async ({hash}: {hash: TransactionHash}): Promise<GenLayerTransaction> => {
73
- if (client.chain.id === localnet.id) {
71
+ if (client.chain.isStudio) {
74
72
  const transaction = await client.getTransaction({hash});
75
73
  const localnetStatus =
76
74
  (transaction.status as string) === "ACTIVATED" ? TransactionStatus.PENDING : transaction.status;
@@ -100,16 +98,16 @@ export const transactionActions = (client: GenLayerClient<GenLayerChain>, public
100
98
  from: transactionParams.from || client.account?.address,
101
99
  to: transactionParams.to,
102
100
  data: transactionParams.data || "0x",
103
- value: transactionParams.value ? `0x${transactionParams.value.toString(16)}` as `0x${string}` : "0x0" as `0x${string}`,
101
+ value: transactionParams.value
102
+ ? (`0x${transactionParams.value.toString(16)}` as `0x${string}`)
103
+ : ("0x0" as `0x${string}`),
104
104
  };
105
105
 
106
- const gasHex = await client.request({
106
+ const gasHex = (await client.request({
107
107
  method: "eth_estimateGas",
108
108
  params: [formattedParams],
109
- }) as string;
109
+ })) as string;
110
110
 
111
111
  return BigInt(gasHex);
112
112
  },
113
113
  });
114
-
115
-
@@ -2,16 +2,21 @@ import {Chain} from "viem";
2
2
  import {Address} from "./accounts";
3
3
 
4
4
  export type GenLayerChain = Chain & {
5
+ isStudio: boolean;
5
6
  consensusMainContract: {
6
7
  address: Address;
7
- abi: any[];
8
+ abi: readonly unknown[];
8
9
  bytecode: string;
9
10
  } | null;
10
11
  consensusDataContract: {
11
12
  address: Address;
12
- abi: any[];
13
+ abi: readonly unknown[];
13
14
  bytecode: string;
14
15
  } | null;
16
+ stakingContract: {
17
+ address: Address;
18
+ abi: readonly unknown[];
19
+ } | null;
15
20
  defaultNumberOfInitialValidators: number;
16
21
  defaultConsensusMaxRotations: number;
17
22
  };
@@ -7,6 +7,7 @@ import {ContractSchema} from "./contracts";
7
7
  import {Network} from "./network";
8
8
  import {SnapSource} from "@/types/snapSource";
9
9
  import {MetaMaskClientResult} from "@/types/metamaskClientResult";
10
+ import {StakingActions} from "./staking";
10
11
 
11
12
  export type GenLayerMethod =
12
13
  | {method: "sim_fundAccount"; params: [address: Address, amount: number]}
@@ -22,14 +23,13 @@ export type GenLayerMethod =
22
23
  | {method: "gen_call"; params: [requestParams: any]};
23
24
 
24
25
  /*
25
- Take all the properties from PublicActions<Transport, TGenLayerChain>
26
- Remove the transport, readContract, and getTransaction properties
27
- The resulting type will have everything from PublicActions EXCEPT those
28
- two properties which are added later
26
+ Take all the properties from Client<Transport, TGenLayerChain>
27
+ Remove getTransaction and readContract because they are redefined with custom implementations.
28
+ Keep transport as it's needed for viem contract interactions (e.g., staking).
29
29
  */
30
30
  export type GenLayerClient<TGenLayerChain extends GenLayerChain> = Omit<
31
31
  Client<Transport, TGenLayerChain>,
32
- "transport" | "getTransaction" | "readContract"
32
+ "getTransaction" | "readContract"
33
33
  > &
34
34
  Omit<WalletActions<TGenLayerChain>, "deployContract" | "writeContract"> &
35
35
  Omit<
@@ -103,4 +103,4 @@ export type GenLayerClient<TGenLayerChain extends GenLayerChain> = Omit<
103
103
  account?: Account;
104
104
  txId: `0x${string}`;
105
105
  }) => Promise<any>;
106
- };
106
+ } & StakingActions;
@@ -6,3 +6,4 @@ export * from "./contracts";
6
6
  export * from "./transactions";
7
7
  export * from "./network";
8
8
  export * from "./snapSource";
9
+ export * from "./staking";
@@ -0,0 +1,228 @@
1
+ import {Address} from "./accounts";
2
+ import {GetContractReturnType, PublicClient, Client, Transport, Chain, Account, Address as ViemAddress} from "viem";
3
+ import {STAKING_ABI} from "@/abi/staking";
4
+
5
+ type WalletClientWithAccount = Client<Transport, Chain, Account>;
6
+
7
+ type StakingKeyedClient = {
8
+ public: PublicClient;
9
+ wallet: WalletClientWithAccount;
10
+ };
11
+
12
+ export type StakingContract = GetContractReturnType<
13
+ typeof STAKING_ABI,
14
+ StakingKeyedClient,
15
+ ViemAddress
16
+ >;
17
+
18
+ export interface ValidatorView {
19
+ left: Address;
20
+ right: Address;
21
+ parent: Address;
22
+ eBanned: bigint;
23
+ ePrimed: bigint;
24
+ vStake: bigint;
25
+ vShares: bigint;
26
+ dStake: bigint;
27
+ dShares: bigint;
28
+ vDeposit: bigint;
29
+ vWithdrawal: bigint;
30
+ live: boolean;
31
+ }
32
+
33
+ export interface ValidatorIdentity {
34
+ moniker: string;
35
+ logoUri: string;
36
+ website: string;
37
+ description: string;
38
+ email: string;
39
+ twitter: string;
40
+ telegram: string;
41
+ github: string;
42
+ extraCid: string;
43
+ }
44
+
45
+ export interface ValidatorInfo {
46
+ address: Address;
47
+ owner: Address;
48
+ operator: Address;
49
+ vStake: string;
50
+ vStakeRaw: bigint;
51
+ vShares: bigint;
52
+ dStake: string;
53
+ dStakeRaw: bigint;
54
+ dShares: bigint;
55
+ vDeposit: string;
56
+ vDepositRaw: bigint;
57
+ vWithdrawal: string;
58
+ vWithdrawalRaw: bigint;
59
+ ePrimed: bigint;
60
+ live: boolean;
61
+ banned: boolean;
62
+ bannedEpoch?: bigint;
63
+ needsPriming: boolean;
64
+ identity?: ValidatorIdentity;
65
+ pendingDeposits: PendingDeposit[];
66
+ pendingWithdrawals: PendingWithdrawal[];
67
+ }
68
+
69
+ export interface WithdrawalCommit {
70
+ input: bigint;
71
+ output: bigint;
72
+ epoch: bigint;
73
+ linkToNextCommit: bigint;
74
+ }
75
+
76
+ export interface PendingDeposit {
77
+ epoch: bigint;
78
+ stake: string;
79
+ stakeRaw: bigint;
80
+ shares: bigint;
81
+ }
82
+
83
+ export interface PendingWithdrawal {
84
+ epoch: bigint;
85
+ shares: bigint;
86
+ stake: string;
87
+ stakeRaw: bigint;
88
+ }
89
+
90
+ export interface BannedValidatorInfo {
91
+ validator: Address;
92
+ untilEpoch: bigint;
93
+ permanentlyBanned: boolean;
94
+ }
95
+
96
+ export interface StakeInfo {
97
+ delegator: Address;
98
+ validator: Address;
99
+ shares: bigint;
100
+ stake: string;
101
+ stakeRaw: bigint;
102
+ pendingDeposits: PendingDeposit[];
103
+ pendingWithdrawals: PendingWithdrawal[];
104
+ }
105
+
106
+ export interface EpochData {
107
+ start: bigint;
108
+ end: bigint;
109
+ inflation: bigint;
110
+ weight: bigint;
111
+ weightDeposit: bigint;
112
+ weightWithdrawal: bigint;
113
+ vcount: bigint;
114
+ claimed: bigint;
115
+ stakeDeposit: bigint;
116
+ stakeWithdrawal: bigint;
117
+ }
118
+
119
+ export interface EpochInfo {
120
+ currentEpoch: bigint;
121
+ validatorMinStake: string;
122
+ validatorMinStakeRaw: bigint;
123
+ delegatorMinStake: string;
124
+ delegatorMinStakeRaw: bigint;
125
+ activeValidatorsCount: bigint;
126
+ epochMinDuration: bigint;
127
+ currentEpochStart: Date;
128
+ currentEpochEnd: Date | null;
129
+ nextEpochEstimate: Date | null;
130
+ inflation: string;
131
+ inflationRaw: bigint;
132
+ totalWeight: bigint;
133
+ totalClaimed: string;
134
+ totalClaimedRaw: bigint;
135
+ }
136
+
137
+ export interface StakingTransactionResult {
138
+ transactionHash: `0x${string}`;
139
+ blockNumber: bigint;
140
+ gasUsed: bigint;
141
+ }
142
+
143
+ export interface ValidatorJoinResult extends StakingTransactionResult {
144
+ validatorWallet: Address;
145
+ operator: Address;
146
+ amount: string;
147
+ amountRaw: bigint;
148
+ }
149
+
150
+ export interface DelegatorJoinResult extends StakingTransactionResult {
151
+ validator: Address;
152
+ delegator: Address;
153
+ amount: string;
154
+ amountRaw: bigint;
155
+ }
156
+
157
+ export interface ValidatorJoinOptions {
158
+ amount: bigint | string;
159
+ operator?: Address;
160
+ }
161
+
162
+ export interface ValidatorDepositOptions {
163
+ amount: bigint | string;
164
+ }
165
+
166
+ export interface ValidatorExitOptions {
167
+ shares: bigint | string;
168
+ }
169
+
170
+ export interface ValidatorClaimOptions {
171
+ validator?: Address;
172
+ }
173
+
174
+ export interface ValidatorPrimeOptions {
175
+ validator: Address;
176
+ }
177
+
178
+ export interface SetOperatorOptions {
179
+ validator: Address;
180
+ operator: Address;
181
+ }
182
+
183
+ export interface SetIdentityOptions {
184
+ validator: Address;
185
+ moniker: string;
186
+ logoUri?: string;
187
+ website?: string;
188
+ description?: string;
189
+ email?: string;
190
+ twitter?: string;
191
+ telegram?: string;
192
+ github?: string;
193
+ extraCid?: string;
194
+ }
195
+
196
+ export interface DelegatorJoinOptions {
197
+ validator: Address;
198
+ amount: bigint | string;
199
+ }
200
+
201
+ export interface DelegatorExitOptions {
202
+ validator: Address;
203
+ shares: bigint | string;
204
+ }
205
+
206
+ export interface DelegatorClaimOptions {
207
+ validator: Address;
208
+ delegator?: Address;
209
+ }
210
+
211
+ export interface StakingActions {
212
+ validatorJoin: (options: ValidatorJoinOptions) => Promise<ValidatorJoinResult>;
213
+ validatorDeposit: (options: ValidatorDepositOptions) => Promise<StakingTransactionResult>;
214
+ validatorExit: (options: ValidatorExitOptions) => Promise<StakingTransactionResult>;
215
+ validatorClaim: (options?: ValidatorClaimOptions) => Promise<StakingTransactionResult & {claimedAmount: bigint}>;
216
+ delegatorJoin: (options: DelegatorJoinOptions) => Promise<DelegatorJoinResult>;
217
+ delegatorExit: (options: DelegatorExitOptions) => Promise<StakingTransactionResult>;
218
+ delegatorClaim: (options: DelegatorClaimOptions) => Promise<StakingTransactionResult>;
219
+ isValidator: (address: Address) => Promise<boolean>;
220
+ getValidatorInfo: (validator: Address) => Promise<ValidatorInfo>;
221
+ getStakeInfo: (delegator: Address, validator: Address) => Promise<StakeInfo>;
222
+ getEpochInfo: () => Promise<EpochInfo>;
223
+ getActiveValidators: () => Promise<Address[]>;
224
+ getActiveValidatorsCount: () => Promise<bigint>;
225
+ getStakingContract: () => StakingContract;
226
+ parseStakingAmount: (amount: string | bigint) => bigint;
227
+ formatStakingAmount: (amount: bigint) => string;
228
+ }
@@ -0,0 +1,41 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "module": "ES2022",
5
+ "moduleResolution": "node",
6
+ "paths": {
7
+ "@/*": [
8
+ "./src/*"
9
+ ],
10
+ "@@/tests/*": [
11
+ "./tests/*"
12
+ ]
13
+ },
14
+ "rootDirs": [
15
+ "./src",
16
+ "./tests"
17
+ ],
18
+ "types": [
19
+ "node",
20
+ "jest"
21
+ ],
22
+ "resolveJsonModule": true,
23
+ "sourceMap": true,
24
+ "outDir": "./dist",
25
+ "esModuleInterop": true,
26
+ "forceConsistentCasingInFileNames": true,
27
+ "strict": true,
28
+ "skipLibCheck": true,
29
+ "emitDeclarationOnly": false,
30
+ "incremental": true,
31
+ "tsBuildInfoFile": "./node_modules/.cache/tsconfig.vitest.tsbuildinfo"
32
+ },
33
+ "include": [
34
+ "src/**/*",
35
+ "tests/**/*",
36
+ "src/global.d.ts"
37
+ ],
38
+ "exclude": [
39
+ "./dist"
40
+ ]
41
+ }