jay-network 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.
package/dist/index.js ADDED
@@ -0,0 +1,371 @@
1
+ // src/chain.ts
2
+ var JAY_CHAIN_ID = "thejaynetwork";
3
+ var JAY_CHAIN_NAME = "Jay Network";
4
+ var JAY_DENOM = "ujay";
5
+ var JAY_SYMBOL = "JAY";
6
+ var JAY_DECIMALS = 6;
7
+ var JAY_PREFIX = "yjay";
8
+ var DEFAULT_RPC = "https://pixture.thejaynetwork.com/rpc";
9
+ var DEFAULT_REST = "https://pixture.thejaynetwork.com/rest";
10
+ var JAY_CHAIN_INFO = {
11
+ chainId: JAY_CHAIN_ID,
12
+ chainName: JAY_CHAIN_NAME,
13
+ rpc: DEFAULT_RPC,
14
+ rest: DEFAULT_REST,
15
+ bip44: { coinType: 118 },
16
+ bech32Config: {
17
+ bech32PrefixAccAddr: JAY_PREFIX,
18
+ bech32PrefixAccPub: `${JAY_PREFIX}pub`,
19
+ bech32PrefixValAddr: `${JAY_PREFIX}valoper`,
20
+ bech32PrefixValPub: `${JAY_PREFIX}valoperpub`,
21
+ bech32PrefixConsAddr: `${JAY_PREFIX}valcons`,
22
+ bech32PrefixConsPub: `${JAY_PREFIX}valconspub`
23
+ },
24
+ currencies: [
25
+ { coinDenom: JAY_SYMBOL, coinMinimalDenom: JAY_DENOM, coinDecimals: JAY_DECIMALS }
26
+ ],
27
+ feeCurrencies: [
28
+ {
29
+ coinDenom: JAY_SYMBOL,
30
+ coinMinimalDenom: JAY_DENOM,
31
+ coinDecimals: JAY_DECIMALS,
32
+ gasPriceStep: { low: 0.01, average: 0.025, high: 0.04 }
33
+ }
34
+ ],
35
+ stakeCurrency: {
36
+ coinDenom: JAY_SYMBOL,
37
+ coinMinimalDenom: JAY_DENOM,
38
+ coinDecimals: JAY_DECIMALS
39
+ }
40
+ };
41
+
42
+ // src/utils.ts
43
+ import { fromBech32, toBech32 } from "@cosmjs/encoding";
44
+ function toMicro(amount, decimals = JAY_DECIMALS) {
45
+ const n = typeof amount === "string" ? parseFloat(amount) : amount;
46
+ if (!isFinite(n)) throw new Error(`Invalid amount: ${amount}`);
47
+ return Math.floor(n * Math.pow(10, decimals)).toString();
48
+ }
49
+ function fromMicro(amount, decimals = JAY_DECIMALS) {
50
+ const n = typeof amount === "string" ? parseInt(amount, 10) : amount;
51
+ return (n / Math.pow(10, decimals)).toFixed(decimals);
52
+ }
53
+ function jayCoin(amount, denom = JAY_DENOM) {
54
+ return { denom, amount: toMicro(amount) };
55
+ }
56
+ function reEncodeAddress(address, prefix) {
57
+ const { data } = fromBech32(address);
58
+ return toBech32(prefix, data);
59
+ }
60
+ function toValoper(accountAddress) {
61
+ const { prefix, data } = fromBech32(accountAddress);
62
+ return toBech32(`${prefix}valoper`, data);
63
+ }
64
+ function isValidatorOperator(accountAddress, valoperAddress) {
65
+ try {
66
+ return toValoper(accountAddress) === valoperAddress;
67
+ } catch {
68
+ return false;
69
+ }
70
+ }
71
+ function isValidJayAddress(address) {
72
+ try {
73
+ return fromBech32(address).prefix === JAY_PREFIX;
74
+ } catch {
75
+ return false;
76
+ }
77
+ }
78
+ function shortenAddress(address, head = 10, tail = 6) {
79
+ if (!address || address.length <= head + tail) return address;
80
+ return `${address.slice(0, head)}\u2026${address.slice(-tail)}`;
81
+ }
82
+
83
+ // src/client.ts
84
+ var JayClient = class {
85
+ constructor(options = {}) {
86
+ this.rpc = options.rpc ?? DEFAULT_RPC;
87
+ this.rest = options.rest ?? DEFAULT_REST;
88
+ }
89
+ async getJson(base, path) {
90
+ const res = await fetch(`${base}${path}`);
91
+ if (!res.ok) throw new Error(`Request failed (${res.status}): ${path}`);
92
+ return res.json();
93
+ }
94
+ /** Latest block height and time from the RPC status endpoint. */
95
+ async getLatestBlock() {
96
+ const data = await this.getJson(this.rpc, "/status");
97
+ const info = data.result.sync_info;
98
+ return { height: info.latest_block_height, time: info.latest_block_time };
99
+ }
100
+ /** Node sync / network identity info. */
101
+ async getNodeInfo() {
102
+ return this.getJson(this.rest, "/cosmos/base/tendermint/v1beta1/node_info");
103
+ }
104
+ /** All balances for an address. Amounts are in base units (ujay). */
105
+ async getBalances(address) {
106
+ const data = await this.getJson(
107
+ this.rest,
108
+ `/cosmos/bank/v1beta1/balances/${address}?pagination.limit=200`
109
+ );
110
+ return data.balances ?? [];
111
+ }
112
+ /** Native JAY balance as a human-readable string. */
113
+ async getJayBalance(address) {
114
+ const balances = await this.getBalances(address);
115
+ const jay = balances.find((c) => c.denom === JAY_DENOM);
116
+ return fromMicro(jay?.amount ?? "0");
117
+ }
118
+ /** Bonded validators (optionally filter by status). */
119
+ async getValidators(status = "BOND_STATUS_BONDED") {
120
+ const data = await this.getJson(
121
+ this.rest,
122
+ `/cosmos/staking/v1beta1/validators?status=${status}&pagination.limit=500`
123
+ );
124
+ return data.validators ?? [];
125
+ }
126
+ /** A single validator by operator (valoper) address. */
127
+ async getValidator(valoperAddress) {
128
+ const data = await this.getJson(
129
+ this.rest,
130
+ `/cosmos/staking/v1beta1/validators/${valoperAddress}`
131
+ );
132
+ return data.validator;
133
+ }
134
+ /** All delegations for a delegator. */
135
+ async getDelegations(delegatorAddress) {
136
+ const data = await this.getJson(
137
+ this.rest,
138
+ `/cosmos/staking/v1beta1/delegations/${delegatorAddress}`
139
+ );
140
+ return data.delegation_responses ?? [];
141
+ }
142
+ /** Pending staking rewards for a delegator across all validators. */
143
+ async getRewards(delegatorAddress) {
144
+ const data = await this.getJson(
145
+ this.rest,
146
+ `/cosmos/distribution/v1beta1/delegators/${delegatorAddress}/rewards`
147
+ );
148
+ return data;
149
+ }
150
+ /** Validator commission accrued by an operator. */
151
+ async getValidatorCommission(valoperAddress) {
152
+ const data = await this.getJson(
153
+ this.rest,
154
+ `/cosmos/distribution/v1beta1/validators/${valoperAddress}/commission`
155
+ );
156
+ const jay = data.commission?.commission?.find(
157
+ (c) => c.denom === JAY_DENOM
158
+ );
159
+ return fromMicro(jay?.amount ?? "0");
160
+ }
161
+ /** Governance proposals (optionally filter by status). */
162
+ async getProposals(status) {
163
+ const q = status ? `?proposal_status=${status}` : "";
164
+ const data = await this.getJson(this.rest, `/cosmos/gov/v1/proposals${q}`);
165
+ return data.proposals ?? [];
166
+ }
167
+ /** A single governance proposal by id. */
168
+ async getProposal(proposalId) {
169
+ const data = await this.getJson(this.rest, `/cosmos/gov/v1/proposals/${proposalId}`);
170
+ return data.proposal;
171
+ }
172
+ /** Total token supply. */
173
+ async getSupply() {
174
+ const data = await this.getJson(this.rest, "/cosmos/bank/v1beta1/supply?pagination.limit=50");
175
+ return data.supply ?? [];
176
+ }
177
+ /** Open IBC transfer channels. */
178
+ async getIbcChannels() {
179
+ const data = await this.getJson(
180
+ this.rest,
181
+ "/ibc/core/channel/v1/channels?pagination.limit=300"
182
+ );
183
+ return (data.channels ?? []).filter(
184
+ (c) => c.state === "STATE_OPEN" && c.port_id === "transfer"
185
+ );
186
+ }
187
+ /** Resolve an `ibc/<hash>` denom to its base denom + path. */
188
+ async resolveIbcDenom(denom) {
189
+ if (!denom.startsWith("ibc/")) return { baseDenom: denom, path: "" };
190
+ const hash = denom.slice(4);
191
+ const data = await this.getJson(
192
+ this.rest,
193
+ `/ibc/apps/transfer/v1/denom_traces/${hash}`
194
+ );
195
+ const trace = data.denom_trace;
196
+ return { baseDenom: trace?.base_denom ?? denom, path: trace?.path ?? "" };
197
+ }
198
+ };
199
+
200
+ // src/signer.ts
201
+ import { SigningStargateClient } from "@cosmjs/stargate";
202
+ import Long from "long";
203
+ var VOTE_ENUM = {
204
+ VOTE_OPTION_YES: 1,
205
+ VOTE_OPTION_ABSTAIN: 2,
206
+ VOTE_OPTION_NO: 3,
207
+ VOTE_OPTION_NO_WITH_VETO: 4
208
+ };
209
+ function defaultFee(gas = "250000", amount = "7500") {
210
+ return { amount: [{ denom: JAY_DENOM, amount }], gas };
211
+ }
212
+ var JaySigningClient = class _JaySigningClient {
213
+ constructor(client, address) {
214
+ this.client = client;
215
+ this.address = address;
216
+ }
217
+ /** Connect using an offline signer and the sender address. */
218
+ static async connect(signer, address, options = {}) {
219
+ const rpc = options.rpc ?? DEFAULT_RPC;
220
+ const client = await SigningStargateClient.connectWithSigner(rpc, signer);
221
+ return new _JaySigningClient(client, address);
222
+ }
223
+ /** Underlying CosmJS client for advanced use. */
224
+ get raw() {
225
+ return this.client;
226
+ }
227
+ async broadcast(messages, memo = "", fee) {
228
+ const result = await this.client.signAndBroadcast(
229
+ this.address,
230
+ messages,
231
+ fee ?? defaultFee(),
232
+ memo
233
+ );
234
+ if (result.code !== void 0 && result.code !== 0) {
235
+ throw new Error(`Transaction failed: ${result.rawLog}`);
236
+ }
237
+ return { txHash: result.transactionHash, height: result.height, raw: result };
238
+ }
239
+ /** Send JAY to another address. */
240
+ async send(to, amount, memo = "", fee) {
241
+ const msg = {
242
+ typeUrl: "/cosmos.bank.v1beta1.MsgSend",
243
+ value: {
244
+ fromAddress: this.address,
245
+ toAddress: to,
246
+ amount: [{ denom: JAY_DENOM, amount: toMicro(amount) }]
247
+ }
248
+ };
249
+ return this.broadcast([msg], memo, fee);
250
+ }
251
+ /** Delegate (stake) JAY to a validator. */
252
+ async delegate(valoperAddress, amount, memo = "", fee) {
253
+ const msg = {
254
+ typeUrl: "/cosmos.staking.v1beta1.MsgDelegate",
255
+ value: {
256
+ delegatorAddress: this.address,
257
+ validatorAddress: valoperAddress,
258
+ amount: { denom: JAY_DENOM, amount: toMicro(amount) }
259
+ }
260
+ };
261
+ return this.broadcast([msg], memo, fee);
262
+ }
263
+ /** Undelegate (unstake) JAY from a validator. */
264
+ async undelegate(valoperAddress, amount, memo = "", fee) {
265
+ const msg = {
266
+ typeUrl: "/cosmos.staking.v1beta1.MsgUndelegate",
267
+ value: {
268
+ delegatorAddress: this.address,
269
+ validatorAddress: valoperAddress,
270
+ amount: { denom: JAY_DENOM, amount: toMicro(amount) }
271
+ }
272
+ };
273
+ return this.broadcast([msg], memo, fee);
274
+ }
275
+ /** Move stake from one validator to another. */
276
+ async redelegate(srcValoper, dstValoper, amount, memo = "", fee) {
277
+ const msg = {
278
+ typeUrl: "/cosmos.staking.v1beta1.MsgBeginRedelegate",
279
+ value: {
280
+ delegatorAddress: this.address,
281
+ validatorSrcAddress: srcValoper,
282
+ validatorDstAddress: dstValoper,
283
+ amount: { denom: JAY_DENOM, amount: toMicro(amount) }
284
+ }
285
+ };
286
+ return this.broadcast([msg], memo, fee);
287
+ }
288
+ /** Claim staking rewards from one validator. */
289
+ async claimRewards(valoperAddress, memo = "", fee) {
290
+ const msg = {
291
+ typeUrl: "/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward",
292
+ value: { delegatorAddress: this.address, validatorAddress: valoperAddress }
293
+ };
294
+ return this.broadcast([msg], memo, fee);
295
+ }
296
+ /** Claim staking rewards from many validators in one tx. */
297
+ async claimAllRewards(valoperAddresses, memo = "", fee) {
298
+ const msgs = valoperAddresses.map((validatorAddress) => ({
299
+ typeUrl: "/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward",
300
+ value: { delegatorAddress: this.address, validatorAddress }
301
+ }));
302
+ return this.broadcast(msgs, memo, fee ?? defaultFee("400000"));
303
+ }
304
+ /** Withdraw a validator operator's accrued commission. */
305
+ async withdrawCommission(valoperAddress, memo = "", fee) {
306
+ const msg = {
307
+ typeUrl: "/cosmos.distribution.v1beta1.MsgWithdrawValidatorCommission",
308
+ value: { validatorAddress: valoperAddress }
309
+ };
310
+ return this.broadcast([msg], memo, fee);
311
+ }
312
+ /** Vote on a governance proposal (gov v1). */
313
+ async vote(proposalId, option, memo = "", fee) {
314
+ const value = VOTE_ENUM[option];
315
+ if (!value) throw new Error(`Invalid vote option: ${option}`);
316
+ const msg = {
317
+ typeUrl: "/cosmos.gov.v1.MsgVote",
318
+ value: {
319
+ proposalId: Long.fromNumber(Number(proposalId)),
320
+ voter: this.address,
321
+ option: value,
322
+ metadata: ""
323
+ }
324
+ };
325
+ return this.broadcast([msg], memo, fee);
326
+ }
327
+ /** IBC transfer of JAY to another chain. */
328
+ async ibcTransfer(params, memo = "", fee) {
329
+ const { sourceChannel, receiver, amount, denom = JAY_DENOM, timeoutMinutes = 10 } = params;
330
+ const timeoutTimestamp = (Date.now() + timeoutMinutes * 60 * 1e3) * 1e6;
331
+ const msg = {
332
+ typeUrl: "/ibc.applications.transfer.v1.MsgTransfer",
333
+ value: {
334
+ sourcePort: "transfer",
335
+ sourceChannel,
336
+ token: { denom, amount: toMicro(amount) },
337
+ sender: this.address,
338
+ receiver,
339
+ timeoutHeight: { revisionNumber: "0", revisionHeight: "0" },
340
+ timeoutTimestamp: timeoutTimestamp.toString(),
341
+ memo
342
+ }
343
+ };
344
+ return this.broadcast([msg], memo, fee);
345
+ }
346
+ /** Disconnect the underlying client. */
347
+ disconnect() {
348
+ this.client.disconnect();
349
+ }
350
+ };
351
+ export {
352
+ DEFAULT_REST,
353
+ DEFAULT_RPC,
354
+ JAY_CHAIN_ID,
355
+ JAY_CHAIN_INFO,
356
+ JAY_CHAIN_NAME,
357
+ JAY_DECIMALS,
358
+ JAY_DENOM,
359
+ JAY_PREFIX,
360
+ JAY_SYMBOL,
361
+ JayClient,
362
+ JaySigningClient,
363
+ fromMicro,
364
+ isValidJayAddress,
365
+ isValidatorOperator,
366
+ jayCoin,
367
+ reEncodeAddress,
368
+ shortenAddress,
369
+ toMicro,
370
+ toValoper
371
+ };
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "jay-network",
3
+ "version": "0.1.0",
4
+ "description": "JavaScript/TypeScript SDK for The Jay Network — queries, staking, governance, and transfers built on CosmJS.",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js",
13
+ "require": "./dist/index.cjs"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist",
18
+ "README.md",
19
+ "LICENSE"
20
+ ],
21
+ "scripts": {
22
+ "build": "tsup src/index.ts --format esm,cjs --dts --clean",
23
+ "dev": "tsup src/index.ts --format esm,cjs --dts --watch",
24
+ "prepublishOnly": "npm run build"
25
+ },
26
+ "keywords": [
27
+ "jay-network",
28
+ "thejaynetwork",
29
+ "cosmos",
30
+ "cosmjs",
31
+ "cosmwasm",
32
+ "blockchain",
33
+ "sdk",
34
+ "staking",
35
+ "ibc"
36
+ ],
37
+ "author": "Winnode <maladarsih@gmail.com>",
38
+ "license": "MIT",
39
+ "repository": {
40
+ "type": "git",
41
+ "url": "https://github.com/Winnode/hub-jay.git",
42
+ "directory": "jaynetwork-sdk"
43
+ },
44
+ "homepage": "https://thejaynetwork.com",
45
+ "dependencies": {
46
+ "@cosmjs/encoding": "^0.32.0",
47
+ "@cosmjs/proto-signing": "^0.32.0",
48
+ "@cosmjs/stargate": "^0.32.0",
49
+ "long": "^5.2.3"
50
+ },
51
+ "devDependencies": {
52
+ "tsup": "^8.0.0",
53
+ "typescript": "^5.4.0"
54
+ },
55
+ "engines": {
56
+ "node": ">=18"
57
+ }
58
+ }