antelopeql 2.0.0-rc.2 → 2.0.0-rc.21

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 (34) hide show
  1. package/antelopeql.mjs +9 -32
  2. package/blockchain/deserialize_action_data.mjs +5 -1
  3. package/blockchain/get_abi.mjs +5 -5
  4. package/blockchain/get_account.mjs +5 -18
  5. package/blockchain/get_accounts_by_authorizers.mjs +17 -6
  6. package/blockchain/get_block.mjs +5 -5
  7. package/blockchain/get_currency_balance.mjs +6 -3
  8. package/blockchain/get_currency_stats.mjs +5 -5
  9. package/blockchain/get_info.mjs +5 -1
  10. package/blockchain/get_producers.mjs +6 -2
  11. package/blockchain/get_ram_price.mjs +6 -6
  12. package/blockchain/get_required_keys.mjs +84 -11
  13. package/blockchain/get_smart_contract.mjs +5 -5
  14. package/blockchain/get_table_by_scope.mjs +11 -2
  15. package/blockchain_query_field.mjs +10 -11
  16. package/build_graphql_fields_from_abis.mjs +20 -9
  17. package/eosio_abi_to_graphql_ast.mjs +31 -29
  18. package/eosio_types/name_type.mjs +1 -1
  19. package/eosio_types/public_key_type.mjs +19 -5
  20. package/eosio_types/signature_type.mjs +1 -3
  21. package/eosio_types/symbol_type.mjs +1 -0
  22. package/graphql_input_types/actions.mjs +2 -2
  23. package/graphql_input_types/configuration.mjs +0 -6
  24. package/graphql_object_types/packed_transaction.mjs +82 -16
  25. package/mutation_resolver.mjs +6 -8
  26. package/package.json +20 -20
  27. package/query_resolver.mjs +3 -1
  28. package/readme.md +35 -61
  29. package/{push_serialized_transaction.mjs → send_serialized_transaction.mjs} +7 -6
  30. package/{push_transaction.mjs → send_transaction.mjs} +12 -6
  31. package/{push_transaction_rpc.mjs → send_transaction_rpc.mjs} +2 -2
  32. package/{blockchain/serialize_abi.mjs → serialize_abi.mjs} +62 -73
  33. package/serialize_transaction.mjs +15 -3
  34. package/blockchain/bandwidth_quote.mjs +0 -244
@@ -4,9 +4,9 @@ import sha256 from "universal-sha256-js/sha256.mjs";
4
4
  import configuration_type from "./graphql_input_types/configuration.mjs";
5
5
  import transaction_receipt from "./graphql_object_types/transaction_receipt.mjs";
6
6
  import mutation_resolver from "./mutation_resolver.mjs";
7
- import push_transaction_rpc from "./push_transaction_rpc.mjs";
7
+ import send_transaction_rpc from "./send_transaction_rpc.mjs";
8
8
 
9
- const push_transaction = (actions, ast_list) => ({
9
+ const send_transaction = (actions, ast_list) => ({
10
10
  description:
11
11
  "Serialize a list of actions and push them to the blockchain in one step, requires private keys to be supplied to AntelopeQL.",
12
12
  type: new GraphQLNonNull(transaction_receipt),
@@ -18,11 +18,17 @@ const push_transaction = (actions, ast_list) => ({
18
18
  type: configuration_type
19
19
  }
20
20
  },
21
- async resolve(_, args, { network, signTransaction }) {
21
+ async resolve(root, args, getContext, info) {
22
+ const { network, signTransaction } = getContext(root, args, info);
23
+
22
24
  const { chain_id, transaction_header, transaction_body, transaction } =
23
25
  await mutation_resolver(args, network, ast_list);
24
26
 
25
- const transaction_bytes = chain_id + transaction_header + transaction_body;
27
+ const transaction_bytes =
28
+ chain_id +
29
+ transaction_header +
30
+ transaction_body +
31
+ "0000000000000000000000000000000000000000000000000000000000000000";
26
32
 
27
33
  const hash_to_sign = await sha256(
28
34
  Uint8Array.from(
@@ -41,11 +47,11 @@ const push_transaction = (actions, ast_list) => ({
41
47
  transaction
42
48
  });
43
49
 
44
- return push_transaction_rpc(
50
+ return send_transaction_rpc(
45
51
  { transaction_body, transaction_header, signatures },
46
52
  network
47
53
  );
48
54
  }
49
55
  });
50
56
 
51
- export default push_transaction;
57
+ export default send_transaction;
@@ -1,7 +1,7 @@
1
1
  import { GraphQLError } from "graphql";
2
2
 
3
3
  /**
4
- * Pushes a serialized transaction to the blockchain.
4
+ * Send a serialized transaction to the blockchain.
5
5
  * @param {Object} root Argument
6
6
  * @param {String} root.transaction_header Serialized transaction header.
7
7
  * @param {String} root.transaction_body Serialized transaction body.
@@ -12,7 +12,7 @@ import { GraphQLError } from "graphql";
12
12
  * @param {Object} network.headers transaction headers.
13
13
  *
14
14
  */
15
- export default async function push_transaction_rpc(
15
+ export default async function send_transaction_rpc(
16
16
  { transaction_header, transaction_body, signatures },
17
17
  network
18
18
  ) {
@@ -1,7 +1,6 @@
1
1
  import serialize from "eosio-wasm-js/serialize.mjs";
2
2
 
3
- import { eosio_abi_to_graphql_ast } from "../eosio_abi_to_graphql_ast.mjs";
4
- import Bytes from "../eosio_types/bytes_type.mjs";
3
+ import { eosio_abi_to_graphql_ast } from "./eosio_abi_to_graphql_ast.mjs";
5
4
 
6
5
  const ABI = {
7
6
  version: "eosio::abi/1.1",
@@ -196,89 +195,79 @@ const ABI = {
196
195
  }
197
196
  ]
198
197
  };
198
+
199
199
  const AST = eosio_abi_to_graphql_ast(ABI);
200
200
 
201
- const serialize_abi = {
202
- description:
203
- "Serializes ABI to WASM hex code, this function is useful when you are using set ABI",
204
- type: Bytes,
205
- args: {
206
- abi: {
207
- type: Bytes,
208
- description: "ABI to serialize as buffer",
209
- defaultValue: 0
201
+ /**
202
+ * Given a Smart contract convert the ABI to serialized byte code representation.
203
+ * @param {Object} abi JS Object of the Antelope ABI for a given smart contract.
204
+ * @returns {String} serialized Byte code representation of the ABI.
205
+ */
206
+ export default async function serialize_abi(abi) {
207
+ let JSON_ABI = abi;
208
+
209
+ ABI.structs[9].fields.forEach(({ name }) => {
210
+ if (JSON_ABI[name] == undefined) {
211
+ JSON_ABI[name] = [];
210
212
  }
211
- },
212
- async resolve(_, { abi }) {
213
- const JSON_ABI = JSON.parse(
214
- abi
215
- .match(/[a-z0-9]{2}/gmu)
216
- .map((i) => String.fromCharCode(`0x${i}`))
217
- .join("")
218
- );
213
+ });
219
214
 
220
- const build_serialize_list = async (data, instructions) => {
221
- const serialize_list = [];
215
+ const build_serialize_list = async (data, instructions) => {
216
+ const serialize_list = [];
222
217
 
223
- for (const instruction of instructions) {
224
- const { $info, name, type } = instruction;
225
- const datum = data[name];
218
+ for (const instruction of instructions) {
219
+ const { $info, name, type } = instruction;
220
+ const datum = data[name];
226
221
 
227
- const next_instruction = AST[type];
222
+ const next_instruction = AST[type];
228
223
 
229
- if ($info.variant) {
230
- if (Object.keys(data).length > 1)
231
- throw new Error(`Must only include one type for variant.`);
232
- if (!datum) continue;
233
- else
234
- serialize_list.push({
235
- type: "varuint32",
236
- value: instructions.findIndex((i) => i.type == type)
237
- });
238
- }
224
+ if ($info.variant) {
225
+ if (Object.keys(data).length > 1)
226
+ throw new Error(`Must only include one type for variant.`);
227
+ if (!datum) continue;
228
+ else
229
+ serialize_list.push({
230
+ type: "varuint32",
231
+ value: instructions.findIndex((i) => i.type == type)
232
+ });
233
+ }
239
234
 
240
- if ($info.binary_ex) $info.optional = false; // Binary extentions are optional (GraphQL types) but should not serialize an optional type.
235
+ if ($info.binary_ex) $info.optional = false; // Binary extentions are optional (GraphQL types) but should not serialize an optional type.
241
236
 
242
- if ($info.optional)
243
- serialize_list.push({ type: "bool", value: datum != undefined }); // Add an optional item to serialize list.
237
+ if ($info.optional)
238
+ serialize_list.push({ type: "bool", value: datum != undefined }); // Add an optional item to serialize list.
244
239
 
245
- if ($info.list)
246
- if (datum !== undefined)
247
- serialize_list.push({ type: "varuint32", value: datum.length }); // Add an length of list to serialize list.
240
+ if ($info.list)
241
+ if (datum !== undefined)
242
+ serialize_list.push({ type: "varuint32", value: datum.length }); // Add an length of list to serialize list.
248
243
 
249
- // Indicates that we need to recursion loop through each data item.
250
- if (next_instruction)
251
- if ($info.list) {
252
- if (datum != undefined && !$info.binary_ex)
253
- for await (const d of datum)
254
- serialize_list.push(
255
- ...(await build_serialize_list(await d, next_instruction))
256
- );
257
- }
258
- // None list recursion
259
- else
260
- serialize_list.push(
261
- ...(await build_serialize_list(datum, next_instruction))
262
- );
263
- // Indicates that the list of data can be serilaized and so is pushed into serialize_list.
264
- else if ($info.list && datum !== undefined)
265
- for await (const d of datum) serialize_list.push({ type, value: d });
266
- else if (datum !== undefined)
267
- serialize_list.push({ type, value: datum }); // Native eoio types than can be serialized.
268
- }
269
-
270
- return serialize_list;
271
- };
244
+ // Indicates that we need to recursion loop through each data item.
245
+ if (next_instruction)
246
+ if ($info.list) {
247
+ if (datum != undefined && !$info.binary_ex)
248
+ for await (const d of datum)
249
+ serialize_list.push(
250
+ ...(await build_serialize_list(await d, next_instruction))
251
+ );
252
+ }
253
+ // None list recursion
254
+ else
255
+ serialize_list.push(
256
+ ...(await build_serialize_list(datum, next_instruction))
257
+ );
258
+ // Indicates that the list of data can be serilaized and so is pushed into serialize_list.
259
+ else if ($info.list && datum !== undefined)
260
+ for await (const d of datum) serialize_list.push({ type, value: d });
261
+ else if (datum !== undefined) serialize_list.push({ type, value: datum }); // Native eoio types than can be serialized.
262
+ }
272
263
 
273
- const ser_list = await build_serialize_list(JSON_ABI, AST.abi_def);
264
+ return serialize_list;
265
+ };
274
266
 
275
- let hex_string = "";
276
- ser_list.forEach(
277
- ({ type, value }) => (hex_string += serialize[type](value))
278
- );
267
+ const ser_list = await build_serialize_list(JSON_ABI, AST.abi_def);
279
268
 
280
- return hex_string;
281
- }
282
- };
269
+ let hex_string = "";
270
+ ser_list.forEach(({ type, value }) => (hex_string += serialize[type](value)));
283
271
 
284
- export default serialize_abi;
272
+ return hex_string;
273
+ }
@@ -1,5 +1,6 @@
1
- import { GraphQLNonNull } from "graphql";
1
+ import { GraphQLList, GraphQLNonNull } from "graphql";
2
2
 
3
+ import public_key_type from "./eosio_types/public_key_type.mjs";
3
4
  import configuration_type from "./graphql_input_types/configuration.mjs";
4
5
  import packed_transaction_type from "./graphql_object_types/packed_transaction.mjs";
5
6
  import mutation_resolver from "./mutation_resolver.mjs";
@@ -13,10 +14,21 @@ const serialize_transaction = (actions, ast_list) => ({
13
14
  },
14
15
  configuration: {
15
16
  type: configuration_type
17
+ },
18
+ available_keys: {
19
+ type: new GraphQLList(public_key_type)
16
20
  }
17
21
  },
18
- resolve(_, args, { network }) {
19
- return mutation_resolver(args, network, ast_list);
22
+ async resolve(root, { available_keys, ...args }, getContext, info) {
23
+ const { network } = getContext(root, { available_keys, ...args }, info);
24
+
25
+ const { transaction, ...serialized_txn } = await mutation_resolver(
26
+ args,
27
+ network,
28
+ ast_list
29
+ );
30
+
31
+ return { ...serialized_txn, available_keys, transaction };
20
32
  }
21
33
  });
22
34
 
@@ -1,244 +0,0 @@
1
- import { GraphQLObjectType, GraphQLScalarType, GraphQLString } from "graphql";
2
-
3
- const string_or_int = new GraphQLScalarType({
4
- name: "string_or_int",
5
- description: "`String` or `Integer` type."
6
- });
7
-
8
- /**
9
- * Calculates the fraction of bw needed for power up action for the EOSIO blockchain.
10
- * @param {number | string} us Microseconds needed to supply to account.
11
- * @param {number | string} us Kibibytes needed to supply to account.
12
- * @param {Object} power_up_state Power up state.
13
- * @param {Object} account_info Account to get an approximate cost of BW.
14
- * @returns {number} Fraction of CPU and Netwotk bandwidth to supply to powerup action.
15
- */
16
- function calculate_fraction(us, bytes, power_up_state, account_info) {
17
- function get_sample_usage({ cpu_limit, cpu_weight, net_limit, net_weight }) {
18
- const c = BigInt(1000000);
19
-
20
- const cpu = parseInt(
21
- (BigInt(cpu_limit.max) * c) / BigInt(cpu_weight) + BigInt(1)
22
- );
23
-
24
- const net = parseInt(
25
- (BigInt(net_limit.max) * c) / BigInt(net_weight) + BigInt(1)
26
- );
27
-
28
- return { cpu, net };
29
- }
30
-
31
- function frac_by_us(cpu) {
32
- const { weight } = power_up_state.cpu;
33
- const n = Math.floor((us / cpu) * 1e6) / parseInt(weight);
34
- return Math.floor(n * 1e15);
35
- }
36
-
37
- function frac_by_bytes(net) {
38
- const { weight } = power_up_state.net;
39
- return Math.floor((Math.floor((bytes / net) * 1e6) / weight) * 1e15);
40
- }
41
-
42
- const { cpu, net } = get_sample_usage(account_info);
43
-
44
- return {
45
- net_frac: frac_by_bytes(net),
46
- cpu_frac: frac_by_us(cpu)
47
- };
48
- }
49
-
50
- /**
51
- * Return an approximate EOS amount for the fraction of bandwidth.
52
- * @param {number} frac The bandwidth fraction required.
53
- * @param {Object} bw Powerup state bandwidth.
54
- * @returns {{Asset}} The EOS value for CPU and Network bandwidth fraction
55
- */
56
- function calculate_price(frac, bw) {
57
- let { utilization, weight, exponent, max_price, min_price } = bw;
58
- min_price = parseFloat(min_price.replace(" EOS").split(".")[0]);
59
- max_price = parseFloat(max_price.replace(" EOS").split(".")[0]);
60
- exponent = parseFloat(exponent);
61
- utilization = parseInt(utilization);
62
- weight = parseInt(weight);
63
-
64
- const determine_adjusted_utilization = () => {
65
- const { decay_secs, utilization, utilization_timestamp } = bw;
66
- let { adjusted_utilization } = bw;
67
-
68
- if (parseInt(utilization) < parseInt(adjusted_utilization)) {
69
- const now = Math.floor(new Date().getTime() / 1e3);
70
- const diff = parseInt(adjusted_utilization) - parseInt(utilization);
71
- let delta =
72
- diff *
73
- Math.exp(
74
- -(now - parseInt(utilization_timestamp)) / parseInt(decay_secs)
75
- );
76
-
77
- delta = Math.min(Math.max(delta, 0), diff);
78
- adjusted_utilization = parseInt(utilization) + delta;
79
- }
80
- return adjusted_utilization;
81
- };
82
-
83
- const utilization_increase = Math.ceil((frac * weight) / 1e15);
84
- const adjusted_utilization = determine_adjusted_utilization(bw);
85
-
86
- const price_function = (utilization) => {
87
- let price = min_price;
88
- const new_exponent = exponent - 1.0;
89
- if (new_exponent <= 0.0) return max_price;
90
- else
91
- price +=
92
- (max_price - min_price) * Math.pow(utilization / weight, new_exponent);
93
-
94
- return price;
95
- };
96
-
97
- const price_integral_delta = (start_utilization, end_utilization) => {
98
- const coefficient = (max_price - min_price) / exponent;
99
- const start_u = start_utilization / weight;
100
- const end_u = end_utilization / weight;
101
- const delta =
102
- min_price * end_u -
103
- min_price * start_u +
104
- coefficient * Math.pow(end_u, exponent) -
105
- coefficient * Math.pow(start_u, exponent);
106
- return delta;
107
- };
108
-
109
- let start_utilization = utilization;
110
- const end_utilization = start_utilization + utilization_increase;
111
- let fee = 0;
112
-
113
- if (start_utilization < adjusted_utilization) {
114
- fee +=
115
- (price_function(adjusted_utilization) *
116
- Math.min(
117
- utilization_increase,
118
- adjusted_utilization - start_utilization
119
- )) /
120
- weight;
121
- start_utilization = adjusted_utilization;
122
- }
123
- if (start_utilization < end_utilization)
124
- fee += price_integral_delta(start_utilization, end_utilization);
125
-
126
- return fee;
127
- // return Math.ceil(fee * 1e4) / 1e4
128
- }
129
-
130
- const eos_bandwidth_price = {
131
- description: "`EOS` bandwidth powerup quote.",
132
- type: new GraphQLObjectType({
133
- name: "eos_bandwidth_price",
134
- fields: {
135
- cpu_frac: {
136
- type: GraphQLString
137
- },
138
- net_frac: {
139
- type: GraphQLString
140
- },
141
- cpu_estimate: {
142
- type: GraphQLString
143
- },
144
- net_estimate: {
145
- type: GraphQLString
146
- }
147
- }
148
- }),
149
- args: {
150
- us: {
151
- type: string_or_int,
152
- description:
153
- "Number of micro seconds of CPU bandwidth you want a quote for.",
154
- defaultValue: 0
155
- },
156
- bytes: {
157
- type: string_or_int,
158
- description: "Number of bytes of network bandwidth you want a quote for.",
159
- defaultValue: 0
160
- }
161
- },
162
- async resolve(_, { us, bytes }, { network: { fetch, rpc_url } }) {
163
- rpc_url = "https://jungle.relocke.io";
164
-
165
- const get_sample_account = async () => {
166
- const account_names = await fetch(rpc_url + "/v1/chain/get_table_rows", {
167
- method: "post",
168
- body: JSON.stringify({
169
- code: "eosio",
170
- table: "powup.order",
171
- scope: "",
172
- json: true
173
- })
174
- });
175
-
176
- const { rows } = await account_names.json();
177
-
178
- const request = await fetch(rpc_url + "/v1/chain/get_account", {
179
- method: "post",
180
- body: JSON.stringify({ account_name: rows[2].owner })
181
- });
182
-
183
- return request.json();
184
- };
185
-
186
- const get_powerup_state = fetch(rpc_url + "/v1/chain/get_table_rows", {
187
- method: "post",
188
- headers: {
189
- "Content-Type": "application/json"
190
- },
191
- body: JSON.stringify({
192
- code: "eosio",
193
- table: "powup.state",
194
- scope: "",
195
- json: true
196
- })
197
- });
198
-
199
- const [sample_account, get_powerup_state_req] = await Promise.all([
200
- await get_sample_account(),
201
- get_powerup_state
202
- ]);
203
-
204
- const power_up_state = await get_powerup_state_req.json();
205
-
206
- let { cpu_frac, net_frac } = calculate_fraction(
207
- us,
208
- bytes,
209
- power_up_state.rows[0],
210
- sample_account
211
- );
212
-
213
- let cpu_estimate = Number(
214
- calculate_price(cpu_frac, power_up_state.rows[0].cpu)
215
- );
216
- let net_estimate = Number(
217
- calculate_price(net_frac, power_up_state.rows[0].net)
218
- );
219
-
220
- // handle the net and cpu estimates so that they are > minimum system token.
221
- const MIN_TOKEN = Number(
222
- power_up_state.rows[0].min_powerup_fee.replace(/[A-Z\s]+/gmu, "")
223
- );
224
- console.log(MIN_TOKEN);
225
- // if (cpu_estimate < MIN_TOKEN) {
226
- // cpu_frac *= Math.floor(MIN_TOKEN / cpu_estimate);
227
- // cpu_estimate = Number(MIN_TOKEN) + cpu_estimate;
228
- // }
229
-
230
- // if (net_estimate < MIN_TOKEN) {
231
- // net_frac *= Math.floor(MIN_TOKEN / net_estimate);
232
- // net_estimate = MIN_TOKEN + net_estimate;
233
- // }
234
-
235
- return {
236
- cpu_frac,
237
- net_frac,
238
- cpu_estimate,
239
- net_estimate
240
- };
241
- }
242
- };
243
-
244
- export default eos_bandwidth_price;