antelopeql 2.0.0-rc.2 → 2.0.0-rc.4

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/antelopeql.mjs CHANGED
@@ -12,8 +12,8 @@ import blockchain_query_field from "./blockchain_query_field.mjs";
12
12
  import build_graphql_fields_from_abis from "./build_graphql_fields_from_abis.mjs";
13
13
  import get_abis from "./get_abis.mjs";
14
14
  import actions from "./graphql_input_types/actions.mjs";
15
- import push_serialized_transaction from "./push_serialized_transaction.mjs";
16
- import push_transaction from "./push_transaction.mjs";
15
+ import send_serialized_transaction from "./send_serialized_transaction.mjs";
16
+ import send_transaction from "./send_transaction.mjs";
17
17
  import serialize_transaction from "./serialize_transaction.mjs";
18
18
 
19
19
  /**
@@ -22,7 +22,6 @@ import serialize_transaction from "./serialize_transaction.mjs";
22
22
  * @property {String} rpc_url Chain remote proceedure call (RPC) Uniform Resource Locator (URL).
23
23
  * @property {*} [variableValues] GraphQL variables.
24
24
  * @property {String} [operationName] GraphQL operation name (query resolution).
25
- * @property {Function} [fetch] Custom fetch implementation.
26
25
  * @property {Array<String>} [contracts] List of Antelope smart contracts.
27
26
  * @property {async Function} [signTransaction] A function to sign the Antelope transaction.
28
27
  * @property {Headers} [headers] Headers to pass to the network request.
@@ -39,30 +38,12 @@ import serialize_transaction from "./serialize_transaction.mjs";
39
38
  * AntelopeQL for interacting with Antelope based blockchains.
40
39
  * @param {AntelopeQLArgument} Argument
41
40
  * @returns {AntelopeQLResult}
42
- * @example
43
- * ```js
44
- * import AntelopeQL from 'antelopeql/antelopeql.mjs'
45
- * import fetch from 'node-fetch'
46
- *
47
- * const query = `{ eosio_token { accounts(arg: { scope: "relockeblock" }){ balance } } }`
48
- *
49
- * AntelopeQL({
50
- * query,
51
- * contracts: ["eosio.token"],
52
- * fetch,
53
- * rpc_url: "https://eos.relocke.io",
54
- * headers: { "content-type": "application/json" }
55
- * }).then(console.log);
56
- * ```
57
- * > Logged output was "data": {"eosio_token": {"accounts": [{"balance": "100.0211 EOS"}]}}}
58
41
  */
59
-
60
42
  export default async function AntelopeQL({
61
43
  query,
62
44
  variableValues,
63
45
  operationName,
64
46
  signTransaction,
65
- fetch,
66
47
  contracts = [],
67
48
  ABIs = [],
68
49
  rpc_url,
@@ -70,9 +51,7 @@ export default async function AntelopeQL({
70
51
  signal
71
52
  }) {
72
53
  try {
73
- if (!fetch && !(typeof window == "undefined")) fetch = window.fetch;
74
- if (!fetch && typeof window == "undefined")
75
- throw new GraphQLError("No fetch implementation provided");
54
+ if (!fetch) throw new GraphQLError("No fetch implementation provided");
76
55
 
77
56
  const fetchOptions = {};
78
57
  if (headers) fetchOptions.headers = headers;
@@ -96,19 +75,17 @@ export default async function AntelopeQL({
96
75
  const action_fields = actions(mutation_fields);
97
76
  mutations = new GraphQLObjectType({
98
77
  name: "Mutation",
99
- description: "Push transactions to the blockchain.",
100
78
  fields: {
101
- push_transaction: push_transaction(action_fields, ast_list),
79
+ send_transaction: send_transaction(action_fields, ast_list),
102
80
  serialize_transaction: serialize_transaction(action_fields, ast_list),
103
- push_serialized_transaction
81
+ send_serialized_transaction
104
82
  }
105
83
  });
106
84
  } else
107
85
  mutations = new GraphQLObjectType({
108
86
  name: "Mutation",
109
- description: "Push transactions to the blockchain.",
110
87
  fields: {
111
- push_serialized_transaction
88
+ send_serialized_transaction
112
89
  }
113
90
  });
114
91
 
@@ -1,11 +1,75 @@
1
- // const { required_keys, ...errors } = await fetch(
2
- // `${rpc_url}/v1/chain/get_required_keys`,
3
- // {
4
- // method: "POST",
5
- // ...fetchOptions,
6
- // body: JSON.stringify({
7
- // available_keys,
8
- // transaction
9
- // })
10
- // }
11
- // ).then((res) => res.json());
1
+ import { GraphQLInputObjectType, GraphQLList, GraphQLString } from "graphql";
2
+
3
+ import bytes_type from "../eosio_types/bytes_type.mjs";
4
+ import name_type from "../eosio_types/name_type.mjs";
5
+ import public_key_type from "../eosio_types/public_key_type.mjs";
6
+ import authorization_type from "../graphql_input_types/authorization.mjs";
7
+
8
+ const action_type = new GraphQLInputObjectType({
9
+ name: "required_keys_action",
10
+ fields: {
11
+ account: {
12
+ type: name_type,
13
+ description: "The smart contract name"
14
+ },
15
+ name: { type: name_type },
16
+ authorization: { type: new GraphQLList(authorization_type) },
17
+ data: { type: GraphQLString },
18
+ hex_data: { type: bytes_type }
19
+ }
20
+ });
21
+
22
+ const transaction_type = new GraphQLInputObjectType({
23
+ name: "required_keys_transaction",
24
+ fields: {
25
+ expiration: { type: GraphQLString },
26
+ ref_block_num: { type: GraphQLString },
27
+ ref_block_prefix: { type: GraphQLString },
28
+ max_net_usage_words: { type: GraphQLString },
29
+ max_cpu_usage_ms: { type: GraphQLString },
30
+ actions: { type: new GraphQLList(action_type) },
31
+ context_free_actions: { type: GraphQLString }
32
+ }
33
+ });
34
+
35
+ const get_required_keys = {
36
+ description: "Retrieve a table from the blockchain.",
37
+ type: new GraphQLList(public_key_type),
38
+ args: {
39
+ transaction: {
40
+ type: transaction_type
41
+ },
42
+ available_keys: {
43
+ type: new GraphQLList(public_key_type)
44
+ }
45
+ },
46
+ async resolve(
47
+ _,
48
+ { available_keys, transaction },
49
+ { network: { fetch, rpc_url } }
50
+ ) {
51
+ const uri = `${rpc_url}/v1/chain/get_required_keys`;
52
+
53
+ const { actions, ...txn } = transaction;
54
+
55
+ const data = await fetch(uri, {
56
+ method: "POST",
57
+ body: JSON.stringify({
58
+ available_keys: await Promise.all(available_keys),
59
+ transaction: {
60
+ ...txn,
61
+ actions: actions.map(({ data, ...object }) => ({
62
+ ...object,
63
+ ...(data ? { data: JSON.parse(data) } : {})
64
+ }))
65
+ }
66
+ })
67
+ });
68
+
69
+ const { required_keys } = await data.json();
70
+
71
+ return required_keys;
72
+ }
73
+ };
74
+
75
+ export default get_required_keys;
@@ -1,6 +1,5 @@
1
1
  import { GraphQLError, GraphQLObjectType } from "graphql";
2
2
 
3
- import eos_bandwidth_price from "./blockchain/bandwidth_quote.mjs";
4
3
  import deserialize_action_data from "./blockchain/deserialize_action_data.mjs";
5
4
  import get_abi from "./blockchain/get_abi.mjs";
6
5
  import get_account from "./blockchain/get_account.mjs";
@@ -11,9 +10,9 @@ import get_currency_stats from "./blockchain/get_currency_stats.mjs";
11
10
  import get_info from "./blockchain/get_info.mjs";
12
11
  import get_producers from "./blockchain/get_producers.mjs";
13
12
  import get_ram_price from "./blockchain/get_ram_price.mjs";
13
+ import get_required_keys from "./blockchain/get_required_keys.mjs";
14
14
  import get_smart_contract from "./blockchain/get_smart_contract.mjs";
15
15
  import get_table from "./blockchain/get_table_by_scope.mjs";
16
- import serialize_abi from "./blockchain/serialize_abi.mjs";
17
16
 
18
17
  const blockchain_query_field = {
19
18
  type: new GraphQLObjectType({
@@ -24,25 +23,21 @@ const blockchain_query_field = {
24
23
  get_abi,
25
24
  get_accounts_by_authorizers,
26
25
  get_block,
27
- get_smart_contract,
28
26
  get_currency_balance,
29
27
  get_currency_stats,
28
+ get_required_keys,
29
+ get_smart_contract,
30
30
  get_info,
31
31
  get_producers,
32
- deserialize_action_data,
33
32
  get_table,
34
33
  get_ram_price,
35
- eos_bandwidth_price,
36
- serialize_abi
34
+ deserialize_action_data
37
35
  }
38
36
  }),
39
- resolve(_, __, { network: { fetch, rpc_url } }) {
40
- if (!fetch)
41
- throw new GraphQLError(
42
- "Fetch was not supplied to the `antelopeql_context`."
43
- );
37
+ resolve(_, __, { network: { rpc_url } }) {
44
38
  if (!rpc_url)
45
39
  throw new GraphQLError("No RPC url supplied to `antelopeql_context`");
40
+
46
41
  return {};
47
42
  }
48
43
  };
@@ -26,7 +26,7 @@ import {
26
26
  * ```js
27
27
  * import actions_type from 'antelopeql/graphql_input_types/actions.js'
28
28
  * import serialize_transaction from 'antelopeql/graphql_input_types/actions.js'
29
- * import push_transaction from 'antelopeql/push_transaction.js'
29
+ * import send_transaction from 'antelopeql/send_transaction.js'
30
30
  *
31
31
  * const network = { fetch, rpc_url: 'https://eos.relocke.io', headers: {}, signal }
32
32
  * const ABI_list = [{ account_name: 'eosio.token', abi: … }]
@@ -45,9 +45,9 @@ import {
45
45
  * const mutations = new GraphQLObjectType({
46
46
  * name: 'Mutation',
47
47
  * fields: {
48
- * push_transaction: push_transaction(action_fields, ast_list),
48
+ * send_transaction: send_transaction(action_fields, ast_list),
49
49
  * serialize_transaction: serialize_transaction(action_fields, ast_list),
50
- * push_serialized_transaction
50
+ * send_serialized_transaction
51
51
  * }
52
52
  * })
53
53
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "antelopeql",
3
- "version": "2.0.0-rc.2",
3
+ "version": "2.0.0-rc.4",
4
4
  "description": "A GraphQL implementation for interacting with Antelope based blockchains.",
5
5
  "repository": "github:pur3miish/antelopeql",
6
6
  "bugs": "https://github.com/pur3miish/antelopeql/issues",
@@ -11,6 +11,7 @@
11
11
  "eslint": "eslint .",
12
12
  "prettier": "prettier -c .",
13
13
  "prettier:readme": "prettier --write readme.md",
14
+ "server": "nodemon test/server.mjs",
14
15
  "types": "tsc -p jsconfig.json",
15
16
  "snapshot": "SAVE_SNAPSHOTS=1 coverage-node test/index.test.mjs",
16
17
  "coverage": "coverage-node test/index.test.mjs",
@@ -19,26 +20,26 @@
19
20
  "prepublishOnly": "npm test"
20
21
  },
21
22
  "engines": {
22
- "node": "^16.0.0 || >= 17.0.0 || >= 18.0.0"
23
+ "node": ">= 18.0.0"
23
24
  },
24
25
  "files": [
25
26
  "blockchain/*.mjs",
26
27
  "eosio_types/*.mjs",
27
28
  "graphql_input_types/*.mjs",
28
29
  "graphql_object_types/*.mjs",
30
+ "antelopeql.mjs",
29
31
  "blockchain_query_field.mjs",
30
32
  "build_graphql_fields_from_abis.mjs",
31
33
  "eosio_abi_to_graphql_ast.mjs",
32
34
  "eosio_types.mjs",
35
+ "get_abis.mjs",
33
36
  "mutation_resolver.mjs",
34
- "push_serialized_transaction.mjs",
35
- "push_transaction_rpc.mjs",
36
- "push_transaction.mjs",
37
37
  "query_resolver.mjs",
38
+ "send_serialized_transaction.mjs",
39
+ "send_transaction_rpc.mjs",
40
+ "send_transaction.mjs",
38
41
  "serialize_transaction.mjs",
39
- "antelopeql.mjs",
40
- "types.mjs",
41
- "get_abis.mjs"
42
+ "types.mjs"
42
43
  ],
43
44
  "sideEffects": false,
44
45
  "exports": {
@@ -46,19 +47,19 @@
46
47
  "./eosio_types/*.mjs": "./eosio_types/*.mjs",
47
48
  "./graphql_input_types/*.mjs": "./graphql_input_types/*.mjs",
48
49
  "./graphql_object_types/*.mjs": "./graphql_object_types/*.mjs",
50
+ "./antelopeql.mjs": "./antelopeql.mjs",
49
51
  "./blockchain_query_field.mjs": "./blockchain_query_field.mjs",
50
52
  "./build_graphql_fields_from_abis.mjs": "./build_graphql_fields_from_abis.mjs",
51
53
  "./eosio_abi_to_graphql_ast.mjs": "./eosio_abi_to_graphql_ast.mjs",
52
54
  "./eosio_types.mjs": "./eosio_types.mjs",
55
+ "./get_abis.mjs": "./get_abis.mjs",
53
56
  "./mutation_resolver.mjs": "./mutation_resolver.mjs",
54
- "./push_serialized_transaction.mjs": "./push_serialized_transaction.mjs",
55
- "./push_transaction_rpc.mjs": "./push_transaction_rpc.mjs",
56
- "./push_transaction.mjs": "./push_transaction.mjs",
57
57
  "./query_resolver.mjs": "./query_resolver.mjs",
58
+ "./send_serialized_transaction.mjs": "./send_serialized_transaction.mjs",
59
+ "./send_transaction_rpc.mjs": "./send_transaction_rpc.mjs",
60
+ "./send_transaction.mjs": "./send_transaction.mjs",
58
61
  "./serialize_transaction.mjs": "./serialize_transaction.mjs",
59
- "./antelopeql.mjs": "./antelopeql.mjs",
60
- "./types.mjs": "./types.mjs",
61
- "./get_abis.mjs": "./get_abis.mjs"
62
+ "./types.mjs": "./types.mjs"
62
63
  },
63
64
  "browserslist": [
64
65
  "> 0.5%, not OperaMini all, not IE > 0, not dead"
@@ -79,17 +80,13 @@
79
80
  "coverage-node": "^8.0.0",
80
81
  "eslint": "^8.34.0",
81
82
  "eslint-plugin-simple-import-sort": "^10.0.0",
82
- "graphql": "^16.6.0",
83
- "node-fetch": "^3.3.0",
84
- "nodemon": "^2.0.20",
83
+ "graphql": "^16.8.0",
84
+ "nodemon": "^3.0.1",
85
85
  "prettier": "^2.8.4",
86
86
  "snapshot-assertion": "^5.0.0",
87
87
  "test-director": "^10.0.0",
88
88
  "typescript": "^4.9.5"
89
89
  },
90
- "peerDependencies": {
91
- "graphql": "^16.6.0"
92
- },
93
90
  "dependencies": {
94
91
  "base58-js": "^2.0.0",
95
92
  "eosio-wasm-js": "^4.1.1",
package/readme.md CHANGED
@@ -31,42 +31,35 @@ See the examples folder on how to run AntelopeQL as a [Node.js](https://nodejs.o
31
31
  ### Query a blockchain account
32
32
 
33
33
  ```js
34
- import fetch from "node-fetch";
35
34
  import AntelopeQL from "antelopeql/antelopeql.mjs";
36
35
 
37
- const { data } = await AntelopeQL(
38
- {
39
- query: /*GraphQL*/ `{
40
- blockchain{
41
- get_account(account_name:"relockeblock") {
42
- core_liquid_balance
43
- ram_quota
44
- net_weight
45
- cpu_weight
46
- ram_usage
47
- permissions {
48
- linked_actions {
49
- account
50
- action
51
- }
52
- required_auth {
53
- keys {
54
- key
55
- weight
36
+ const { data } = await AntelopeQL({
37
+ query: /*GraphQL*/ `{
38
+ blockchain{
39
+ get_account(account_name:"relockeblock") {
40
+ core_liquid_balance
41
+ ram_quota
42
+ net_weight
43
+ cpu_weight
44
+ ram_usage
45
+ permissions {
46
+ linked_actions {
47
+ account
48
+ action
49
+ }
50
+ required_auth {
51
+ keys {
52
+ key
53
+ weight
54
+ }
55
+ threshold
56
56
  }
57
- threshold
58
57
  }
59
58
  }
60
59
  }
61
- }
62
- }`
63
- fetch,
64
- rpc_url: "https://jungle.relocke.io",
65
- headers: {
66
- "content-type": "application/json"
67
- }
68
- },
69
- );
60
+ }`,
61
+ rpc_url: "https://jungle.relocke.io"
62
+ });
70
63
 
71
64
  console.log(data);
72
65
  ```
@@ -76,13 +69,13 @@ console.log(data);
76
69
  ### Transfer EOS cryptocurrency
77
70
 
78
71
  ```js
79
- import fetch from "node-fetch";
80
72
  import AntelopeQL from "antelopeql/antelopeql.mjs";
73
+ import sign_txn from "antelopeql-ecc/sign_txn.mjs";
81
74
 
82
75
  const { data } = await AntelopeQL({
83
76
  query: /*GraphQL*/ `
84
- mutation{
85
- push_transaction(actions: [{
77
+ mutation {
78
+ send_transaction(actions: [{
86
79
  eosio_token:{
87
80
  transfer: {
88
81
  authorization:{
@@ -98,18 +91,14 @@ const { data } = await AntelopeQL({
98
91
  transaction_id
99
92
  block_num
100
93
  }
101
- }`,
102
- contracts: ["eosio.token"],
103
- signTransaction: async () => {
104
- const private_keys = [...] // your private keys
105
- const signatures = generate_signature_function(...privatekeys) // your antelope digital signature provider
106
- return signatures // signaures must return array
107
- }
108
- fetch,
109
- rpc_url: "https://eos.relocke.io", // eos blockchain.
110
- headers: {
111
- "content-type": "application/json"
112
- }
94
+ }`,
95
+ contracts: ["eosio.token", "eosio"], // List of smart contracts
96
+ signTransaction: async (hash) => {
97
+ const wif_private_key = "PVT_K1_…"; // your private key
98
+ const signature = await sign_txn({ hash, wif_private_key });
99
+ return [signature]; // signaures must return array
100
+ },
101
+ rpc_url: "https://eos.relocke.io" // eos blockchain url.
113
102
  });
114
103
 
115
104
  console.log(data);
@@ -134,24 +123,9 @@ console.log(data);
134
123
 
135
124
  Supported runtime environments:
136
125
 
137
- - [Node.js](https://nodejs.org) versions `>=16.0.0`.
126
+ - [Node.js](https://nodejs.org) versions `>=18.0.0`.
138
127
  - Browsers matching the [Browserslist](https://browsersl.ist) query [`> 0.5%, not OperaMini all, not dead`](https://browsersl.ist/?q=%3E+0.5%25%2C+not+OperaMini+all%2C+not+dead).
139
- - [Deno](https://deno.land) version `>=1.30.0`.
140
128
 
141
129
  ## Exports
142
130
 
143
131
  The [npm](https://npmjs.com) package [`AntelopeQL`](https://npm.im/antelopeql) features [optimal JavaScript module design](https://jaydenseric.com/blog/optimal-javascript-module-design). It doesn’t have a main index module, so use deep imports from the ECMAScript modules that are exported via the [`package.json`](./package.json) field [`exports`](https://nodejs.org/api/packages.html#exports):
144
-
145
- - [`antelopeql.mjs`](./antelopeql.mjs)
146
- - [`blockchain_query_field.mjs`](blockchain_query_field.mjs)
147
- - [`build_graphql_fields_from_abis.mjs`](build_graphql_fields_from_abis.mjs)
148
- - [`eosio_abi_to_graphql_ast.mjs`](eosio_abi_to_graphql_ast.mjs)
149
- - [`eosio_types.mjs`](eosio_types.mjs)
150
- - [`mutation_resolver.mjs`](mutation_resolver.mjs)
151
- - [`push_serialized_transaction.mjs`](push_serialized_transaction.mjs)
152
- - [`push_transaction_rpc.mjs`](push_transaction_rpc.mjs)
153
- - [`push_transaction.mjs`](push_transaction.mjs)
154
- - [`query_resolver.mjs`](query_resolver.mjs)
155
- - [`serialize_transaction.mjs`](serialize_transaction.mjs)
156
- - [`antelopeql.mjs`](antelopeql.mjs)
157
- - [`types.mjs`](types.mjs)
@@ -3,13 +3,13 @@ import { GraphQLList, GraphQLNonNull } from "graphql";
3
3
  import bytes_type from "./eosio_types/bytes_type.mjs";
4
4
  import signature_type from "./eosio_types/signature_type.mjs";
5
5
  import transaction_receipt from "./graphql_object_types/transaction_receipt.mjs";
6
- import push_transaction_rpc from "./push_transaction_rpc.mjs";
6
+ import send_transaction_rpc from "./send_transaction_rpc.mjs";
7
7
 
8
8
  /**
9
9
  * Push transaction type.
10
10
  */
11
- const push_serialized_transaction = {
12
- description: "Pushes a serialized transaction to the blockchain.",
11
+ const send_serialized_transaction = {
12
+ description: "Sends a serialized transaction to the blockchain.",
13
13
  type: new GraphQLNonNull(transaction_receipt),
14
14
  args: {
15
15
  transaction_header: { type: new GraphQLNonNull(bytes_type) },
@@ -17,8 +17,8 @@ const push_serialized_transaction = {
17
17
  signaures: { type: new GraphQLNonNull(new GraphQLList(signature_type)) }
18
18
  },
19
19
  resolve(_, args, ctx) {
20
- return push_transaction_rpc(args, ctx.network);
20
+ return send_transaction_rpc(args, ctx.network);
21
21
  }
22
22
  };
23
23
 
24
- export default push_serialized_transaction;
24
+ export default send_serialized_transaction;
@@ -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),
@@ -41,11 +41,11 @@ const push_transaction = (actions, ast_list) => ({
41
41
  transaction
42
42
  });
43
43
 
44
- return push_transaction_rpc(
44
+ return send_transaction_rpc(
45
45
  { transaction_body, transaction_header, signatures },
46
46
  network
47
47
  );
48
48
  }
49
49
  });
50
50
 
51
- export default push_transaction;
51
+ 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,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;
@@ -1,284 +0,0 @@
1
- import serialize from "eosio-wasm-js/serialize.mjs";
2
-
3
- import { eosio_abi_to_graphql_ast } from "../eosio_abi_to_graphql_ast.mjs";
4
- import Bytes from "../eosio_types/bytes_type.mjs";
5
-
6
- const ABI = {
7
- version: "eosio::abi/1.1",
8
- structs: [
9
- {
10
- name: "extensions_entry",
11
- base: "",
12
- fields: [
13
- {
14
- name: "tag",
15
- type: "uint16"
16
- },
17
- {
18
- name: "value",
19
- type: "bytes"
20
- }
21
- ]
22
- },
23
- {
24
- name: "type_def",
25
- base: "",
26
- fields: [
27
- {
28
- name: "new_type_name",
29
- type: "string"
30
- },
31
- {
32
- name: "type",
33
- type: "string"
34
- }
35
- ]
36
- },
37
- {
38
- name: "field_def",
39
- base: "",
40
- fields: [
41
- {
42
- name: "name",
43
- type: "string"
44
- },
45
- {
46
- name: "type",
47
- type: "string"
48
- }
49
- ]
50
- },
51
- {
52
- name: "struct_def",
53
- base: "",
54
- fields: [
55
- {
56
- name: "name",
57
- type: "string"
58
- },
59
- {
60
- name: "base",
61
- type: "string"
62
- },
63
- {
64
- name: "fields",
65
- type: "field_def[]"
66
- }
67
- ]
68
- },
69
- {
70
- name: "action_def",
71
- base: "",
72
- fields: [
73
- {
74
- name: "name",
75
- type: "name"
76
- },
77
- {
78
- name: "type",
79
- type: "string"
80
- },
81
- {
82
- name: "ricardian_contract",
83
- type: "string"
84
- }
85
- ]
86
- },
87
- {
88
- name: "table_def",
89
- base: "",
90
- fields: [
91
- {
92
- name: "name",
93
- type: "name"
94
- },
95
- {
96
- name: "index_type",
97
- type: "string"
98
- },
99
- {
100
- name: "key_names",
101
- type: "string[]"
102
- },
103
- {
104
- name: "key_types",
105
- type: "string[]"
106
- },
107
- {
108
- name: "type",
109
- type: "string"
110
- }
111
- ]
112
- },
113
- {
114
- name: "clause_pair",
115
- base: "",
116
- fields: [
117
- {
118
- name: "id",
119
- type: "string"
120
- },
121
- {
122
- name: "body",
123
- type: "string"
124
- }
125
- ]
126
- },
127
- {
128
- name: "error_message",
129
- base: "",
130
- fields: [
131
- {
132
- name: "error_code",
133
- type: "uint64"
134
- },
135
- {
136
- name: "error_msg",
137
- type: "string"
138
- }
139
- ]
140
- },
141
- {
142
- name: "variant_def",
143
- base: "",
144
- fields: [
145
- {
146
- name: "name",
147
- type: "string"
148
- },
149
- {
150
- name: "types",
151
- type: "string[]"
152
- }
153
- ]
154
- },
155
- {
156
- name: "abi_def",
157
- base: "",
158
- fields: [
159
- {
160
- name: "version",
161
- type: "string"
162
- },
163
- {
164
- name: "types",
165
- type: "type_def[]"
166
- },
167
- {
168
- name: "structs",
169
- type: "struct_def[]"
170
- },
171
- {
172
- name: "actions",
173
- type: "action_def[]"
174
- },
175
- {
176
- name: "tables",
177
- type: "table_def[]"
178
- },
179
- {
180
- name: "ricardian_clauses",
181
- type: "clause_pair[]"
182
- },
183
- {
184
- name: "error_messages",
185
- type: "error_message[]"
186
- },
187
- {
188
- name: "abi_extensions",
189
- type: "extensions_entry[]"
190
- },
191
- {
192
- name: "variants",
193
- type: "variant_def[]$"
194
- }
195
- ]
196
- }
197
- ]
198
- };
199
- const AST = eosio_abi_to_graphql_ast(ABI);
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
210
- }
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
- );
219
-
220
- const build_serialize_list = async (data, instructions) => {
221
- const serialize_list = [];
222
-
223
- for (const instruction of instructions) {
224
- const { $info, name, type } = instruction;
225
- const datum = data[name];
226
-
227
- const next_instruction = AST[type];
228
-
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
- }
239
-
240
- if ($info.binary_ex) $info.optional = false; // Binary extentions are optional (GraphQL types) but should not serialize an optional type.
241
-
242
- if ($info.optional)
243
- serialize_list.push({ type: "bool", value: datum != undefined }); // Add an optional item to serialize list.
244
-
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.
248
-
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
- };
272
-
273
- const ser_list = await build_serialize_list(JSON_ABI, AST.abi_def);
274
-
275
- let hex_string = "";
276
- ser_list.forEach(
277
- ({ type, value }) => (hex_string += serialize[type](value))
278
- );
279
-
280
- return hex_string;
281
- }
282
- };
283
-
284
- export default serialize_abi;