stxer 0.3.1 → 0.3.3

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/src/batch-api.ts CHANGED
@@ -1,157 +1,188 @@
1
+ /**
2
+ * WARNING:
3
+ *
4
+ * this file will be used in cross-runtime environments (browser, cloudflare workers, XLinkSDK, etc.),
5
+ * so please be careful when adding `import`s to it.
6
+ */
7
+
1
8
  import {
2
- type ClarityValue,
3
- type ContractPrincipalCV,
4
- deserializeCV,
5
- type OptionalCV,
6
- serializeCV,
7
- } from '@stacks/transactions';
8
-
9
- export interface BatchReads {
10
- variables?: {
11
- contract: ContractPrincipalCV;
12
- variableName: string;
13
- }[];
14
- maps?: {
15
- contract: ContractPrincipalCV;
16
- mapName: string;
17
- mapKey: ClarityValue;
18
- }[];
19
- index_block_hash?: string;
20
- }
21
-
22
- export interface BatchReadsResult {
23
- variables: (ClarityValue | Error)[];
24
- maps: (OptionalCV | Error)[];
25
- }
26
-
27
- export interface BatchApiOptions {
28
- stxerApi?: string;
29
- }
30
-
31
- const DEFAULT_STXER_API = 'https://api.stxer.xyz';
32
-
33
- export async function batchRead(
34
- reads: BatchReads,
35
- options: BatchApiOptions = {}
36
- ): Promise<BatchReadsResult> {
37
- const payload: string[][] = [];
38
- if (reads.variables != null) {
39
- for (const variable of reads.variables) {
40
- payload.push([serializeCV(variable.contract), variable.variableName]);
41
- }
9
+ type ClarityValue,
10
+ type ContractPrincipalCV,
11
+ deserializeCV,
12
+ type OptionalCV,
13
+ serializeCV,
14
+ } from '@stacks/transactions';
15
+
16
+ export interface BatchReads {
17
+ variables?: {
18
+ contract: ContractPrincipalCV;
19
+ variableName: string;
20
+ }[];
21
+ maps?: {
22
+ contract: ContractPrincipalCV;
23
+ mapName: string;
24
+ mapKey: ClarityValue;
25
+ }[];
26
+ readonly?: {
27
+ contract: ContractPrincipalCV;
28
+ functionName: string;
29
+ functionArgs: ClarityValue[];
30
+ }[];
31
+ index_block_hash?: string;
32
+ }
33
+
34
+ export interface BatchReadsResult {
35
+ tip: string;
36
+ vars: (ClarityValue | Error)[];
37
+ maps: (ClarityValue | Error)[];
38
+ readonly: (ClarityValue | Error)[];
39
+ }
40
+
41
+ export interface BatchApiOptions {
42
+ stxerApi?: string;
43
+ }
44
+
45
+ const DEFAULT_STXER_API = 'https://api.stxer.xyz';
46
+
47
+ function convertResults(
48
+ rs: ({ Ok: string } | { Err: string })[],
49
+ ): (ClarityValue | Error)[] {
50
+ const results: (ClarityValue | Error)[] = [];
51
+ for (const v of rs) {
52
+ if ('Ok' in v) {
53
+ results.push(deserializeCV(v.Ok));
54
+ } else {
55
+ results.push(new Error(v.Err));
42
56
  }
43
- if (reads.maps != null) {
44
- for (const map of reads.maps) {
45
- payload.push([
46
- serializeCV(map.contract),
47
- map.mapName,
48
- serializeCV(map.mapKey),
49
- ]);
50
- }
51
- }
52
-
53
- const ibh =
54
- reads.index_block_hash == null
55
- ? null
56
- : reads.index_block_hash.startsWith('0x')
57
+ }
58
+ return results;
59
+ }
60
+
61
+ export async function batchRead(
62
+ reads: BatchReads,
63
+ options: BatchApiOptions = {}
64
+ ): Promise<BatchReadsResult> {
65
+ const ibh =
66
+ reads.index_block_hash == null
67
+ ? undefined
68
+ : reads.index_block_hash.startsWith('0x')
57
69
  ? reads.index_block_hash.substring(2)
58
70
  : reads.index_block_hash;
59
-
60
- const url = `${options.stxerApi ?? DEFAULT_STXER_API}/sidecar/v2/batch-reads${
61
- ibh == null ? '' : `?tip=${ibh}`
62
- }`;
63
- const data = await fetch(url, {
64
- method: 'POST',
65
- body: JSON.stringify(payload),
66
- });
67
- const text = await data.text();
68
- if (!text.includes('Ok') && !text.includes('Err')) {
69
- throw new Error(
70
- `Requesting batch reads failed: ${text}, url: ${url}, payload: ${JSON.stringify(
71
- payload
72
- )}`
73
- );
71
+
72
+ const payload: {
73
+ tip?: string;
74
+ vars: string[][];
75
+ maps: string[][];
76
+ readonly: string[][];
77
+ } = { vars: [], maps: [], readonly: [], tip: ibh };
78
+
79
+ if (reads.variables != null) {
80
+ for (const variable of reads.variables) {
81
+ payload.vars.push([serializeCV(variable.contract), variable.variableName]);
74
82
  }
75
- const results = JSON.parse(text) as [{ Ok: string } | { Err: string }];
76
- const rs: BatchReadsResult = {
77
- variables: [],
78
- maps: [],
79
- };
80
- let variablesLength = 0;
81
- if (reads.variables != null) {
82
- variablesLength = reads.variables.length;
83
- for (let i = 0; i < variablesLength; i++) {
84
- const result = results[i];
85
- if ('Ok' in result) {
86
- rs.variables.push(deserializeCV(result.Ok));
87
- } else {
88
- rs.variables.push(new Error(result.Err));
89
- }
90
- }
83
+ }
84
+
85
+ if (reads.maps != null) {
86
+ for (const map of reads.maps) {
87
+ payload.maps.push([
88
+ serializeCV(map.contract),
89
+ map.mapName,
90
+ serializeCV(map.mapKey),
91
+ ]);
91
92
  }
92
- if (reads.maps != null) {
93
- for (let i = 0; i < reads.maps.length; i++) {
94
- const result = results[i + variablesLength];
95
- if ('Ok' in result) {
96
- rs.maps.push(deserializeCV(result.Ok) as OptionalCV);
97
- } else {
98
- rs.maps.push(new Error(result.Err));
99
- }
100
- }
93
+ }
94
+
95
+ if (reads.readonly != null) {
96
+ for (const ro of reads.readonly) {
97
+ payload.readonly.push([
98
+ serializeCV(ro.contract),
99
+ ro.functionName,
100
+ ...ro.functionArgs.map(v => serializeCV(v)),
101
+ ]);
101
102
  }
102
- return rs;
103
103
  }
104
-
105
- export interface BatchReadonlyRequest {
106
- readonly: {
107
- contract: ContractPrincipalCV;
108
- functionName: string;
109
- functionArgs: ClarityValue[];
110
- }[];
111
- index_block_hash?: string;
104
+
105
+ const url = `${options.stxerApi ?? DEFAULT_STXER_API}/sidecar/v2/batch`;
106
+ const data = await fetch(url, {
107
+ method: 'POST',
108
+ body: JSON.stringify(payload),
109
+ headers: {
110
+ 'Content-Type': 'application/json',
111
+ },
112
+ });
113
+
114
+ const text = await data.text();
115
+ if (!text.includes('Ok') && !text.includes('Err')) {
116
+ throw new Error(
117
+ `Requesting batch reads failed: ${text}, url: ${url}, payload: ${JSON.stringify(
118
+ payload,
119
+ )}`,
120
+ );
112
121
  }
113
-
114
- export async function batchReadonly(
115
- req: BatchReadonlyRequest,
116
- options: BatchApiOptions = {}
117
- ): Promise<(ClarityValue | Error)[]> {
118
- const payload = req.readonly.map((r) => [
119
- serializeCV(r.contract),
120
- r.functionName,
121
- ...r.functionArgs.map((arg) => serializeCV(arg)),
122
- ]);
123
-
124
- const ibh =
125
- req.index_block_hash == null
126
- ? null
127
- : req.index_block_hash.startsWith('0x')
122
+
123
+ const rs = JSON.parse(text) as {
124
+ tip: string;
125
+ vars: ({ Ok: string } | { Err: string })[];
126
+ maps: ({ Ok: string } | { Err: string })[];
127
+ readonly: ({ Ok: string } | { Err: string })[];
128
+ };
129
+
130
+ return {
131
+ tip: rs.tip,
132
+ vars: convertResults(rs.vars),
133
+ maps: convertResults(rs.maps),
134
+ readonly: convertResults(rs.readonly),
135
+ };
136
+ }
137
+
138
+ export interface BatchReadonlyRequest {
139
+ readonly: {
140
+ contract: ContractPrincipalCV;
141
+ functionName: string;
142
+ functionArgs: ClarityValue[];
143
+ }[];
144
+ index_block_hash?: string;
145
+ }
146
+
147
+ export async function batchReadonly(
148
+ req: BatchReadonlyRequest,
149
+ options: BatchApiOptions = {}
150
+ ): Promise<(ClarityValue | Error)[]> {
151
+ const payload = req.readonly.map((r) => [
152
+ serializeCV(r.contract),
153
+ r.functionName,
154
+ ...r.functionArgs.map((arg) => serializeCV(arg)),
155
+ ]);
156
+
157
+ const ibh =
158
+ req.index_block_hash == null
159
+ ? null
160
+ : req.index_block_hash.startsWith('0x')
128
161
  ? req.index_block_hash.substring(2)
129
162
  : req.index_block_hash;
130
-
131
- const url = `${options.stxerApi ?? DEFAULT_STXER_API}/sidecar/v2/batch-readonly${
132
- ibh == null ? '' : `?tip=${ibh}`
163
+
164
+ const url = `${options.stxerApi ?? DEFAULT_STXER_API}/sidecar/v2/batch-readonly${ibh == null ? '' : `?tip=${ibh}`
133
165
  }`;
134
- const data = await fetch(url, {
135
- method: 'POST',
136
- body: JSON.stringify(payload),
137
- });
138
- const text = await data.text();
139
- if (!text.includes('Ok') && !text.includes('Err')) {
140
- throw new Error(
141
- `Requesting batch readonly failed: ${text}, url: ${url}, payload: ${JSON.stringify(
142
- payload
143
- )}`
144
- );
145
- }
146
- const results = JSON.parse(text) as [{ Ok: string } | { Err: string }];
147
- const rs: (ClarityValue | Error)[] = [];
148
- for (const result of results) {
149
- if ('Ok' in result) {
150
- rs.push(deserializeCV(result.Ok));
151
- } else {
152
- rs.push(new Error(result.Err));
153
- }
166
+ const data = await fetch(url, {
167
+ method: 'POST',
168
+ body: JSON.stringify(payload),
169
+ });
170
+ const text = await data.text();
171
+ if (!text.includes('Ok') && !text.includes('Err')) {
172
+ throw new Error(
173
+ `Requesting batch readonly failed: ${text}, url: ${url}, payload: ${JSON.stringify(
174
+ payload
175
+ )}`
176
+ );
177
+ }
178
+ const results = JSON.parse(text) as [{ Ok: string } | { Err: string }];
179
+ const rs: (ClarityValue | Error)[] = [];
180
+ for (const result of results) {
181
+ if ('Ok' in result) {
182
+ rs.push(deserializeCV(result.Ok));
183
+ } else {
184
+ rs.push(new Error(result.Err));
154
185
  }
155
- return rs;
156
186
  }
157
-
187
+ return rs;
188
+ }
@@ -118,7 +118,7 @@ import {
118
118
 
119
119
  async function main() {
120
120
  await batchReadsExample();
121
- await batchReadonlyExample();
121
+ // await batchReadonlyExample();
122
122
  }
123
123
 
124
124
  if (require.main === module) {