@ocap/tx-protocols 1.28.9 → 1.29.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/esm/execute.d.mts +53 -0
- package/esm/execute.mjs +225 -0
- package/esm/index.d.mts +95 -0
- package/esm/index.mjs +100 -0
- package/esm/pipes/ensure-cost.d.mts +18 -0
- package/esm/pipes/ensure-cost.mjs +136 -0
- package/esm/pipes/ensure-gas.d.mts +14 -0
- package/esm/pipes/ensure-gas.mjs +38 -0
- package/esm/protocols/account/delegate.d.mts +4 -0
- package/esm/protocols/account/delegate.mjs +195 -0
- package/esm/protocols/account/migrate.d.mts +4 -0
- package/esm/protocols/account/migrate.mjs +130 -0
- package/esm/protocols/account/revoke-delegate.d.mts +4 -0
- package/esm/protocols/account/revoke-delegate.mjs +102 -0
- package/esm/protocols/asset/acquire-v2.d.mts +9 -0
- package/esm/protocols/asset/acquire-v2.mjs +213 -0
- package/esm/protocols/asset/acquire-v3.d.mts +17 -0
- package/esm/protocols/asset/acquire-v3.mjs +265 -0
- package/esm/protocols/asset/calls/transfer-token.d.mts +4 -0
- package/esm/protocols/asset/calls/transfer-token.mjs +36 -0
- package/esm/protocols/asset/calls/transfer.d.mts +4 -0
- package/esm/protocols/asset/calls/transfer.mjs +32 -0
- package/esm/protocols/asset/consume.d.mts +4 -0
- package/esm/protocols/asset/consume.mjs +92 -0
- package/esm/protocols/asset/create.d.mts +4 -0
- package/esm/protocols/asset/create.mjs +136 -0
- package/esm/protocols/asset/mint.d.mts +4 -0
- package/esm/protocols/asset/mint.mjs +156 -0
- package/esm/protocols/asset/pipes/exec-mint-hook.d.mts +24 -0
- package/esm/protocols/asset/pipes/exec-mint-hook.mjs +54 -0
- package/esm/protocols/asset/pipes/extract-factory-tokens.d.mts +21 -0
- package/esm/protocols/asset/pipes/extract-factory-tokens.mjs +22 -0
- package/esm/protocols/asset/pipes/verify-itx-address.d.mts +32 -0
- package/esm/protocols/asset/pipes/verify-itx-address.mjs +56 -0
- package/esm/protocols/asset/pipes/verify-itx-assets.d.mts +18 -0
- package/esm/protocols/asset/pipes/verify-itx-assets.mjs +27 -0
- package/esm/protocols/asset/pipes/verify-itx-variables.d.mts +17 -0
- package/esm/protocols/asset/pipes/verify-itx-variables.mjs +20 -0
- package/esm/protocols/asset/pipes/verify-mint-limit.d.mts +15 -0
- package/esm/protocols/asset/pipes/verify-mint-limit.mjs +14 -0
- package/esm/protocols/asset/update.d.mts +4 -0
- package/esm/protocols/asset/update.mjs +111 -0
- package/esm/protocols/factory/create.d.mts +29 -0
- package/esm/protocols/factory/create.mjs +155 -0
- package/esm/protocols/governance/claim-stake.d.mts +27 -0
- package/esm/protocols/governance/claim-stake.mjs +220 -0
- package/esm/protocols/governance/return-stake.d.mts +27 -0
- package/esm/protocols/governance/return-stake.mjs +211 -0
- package/esm/protocols/governance/revoke-stake.d.mts +27 -0
- package/esm/protocols/governance/revoke-stake.mjs +178 -0
- package/esm/protocols/governance/slash-stake.d.mts +17 -0
- package/esm/protocols/governance/slash-stake.mjs +213 -0
- package/esm/protocols/governance/stake.d.mts +15 -0
- package/esm/protocols/governance/stake.mjs +270 -0
- package/esm/protocols/rollup/claim-reward.d.mts +11 -0
- package/esm/protocols/rollup/claim-reward.mjs +322 -0
- package/esm/protocols/rollup/close.d.mts +4 -0
- package/esm/protocols/rollup/close.mjs +105 -0
- package/esm/protocols/rollup/create-block.d.mts +11 -0
- package/esm/protocols/rollup/create-block.mjs +303 -0
- package/esm/protocols/rollup/create.d.mts +4 -0
- package/esm/protocols/rollup/create.mjs +164 -0
- package/esm/protocols/rollup/join.d.mts +4 -0
- package/esm/protocols/rollup/join.mjs +152 -0
- package/esm/protocols/rollup/leave.d.mts +4 -0
- package/esm/protocols/rollup/leave.mjs +137 -0
- package/esm/protocols/rollup/migrate.d.mts +4 -0
- package/esm/protocols/rollup/migrate.mjs +85 -0
- package/esm/protocols/rollup/pause.d.mts +4 -0
- package/esm/protocols/rollup/pause.mjs +76 -0
- package/esm/protocols/rollup/pipes/ensure-validator.d.mts +6 -0
- package/esm/protocols/rollup/pipes/ensure-validator.mjs +12 -0
- package/esm/protocols/rollup/pipes/verify-evidence.d.mts +15 -0
- package/esm/protocols/rollup/pipes/verify-evidence.mjs +29 -0
- package/esm/protocols/rollup/pipes/verify-signers.d.mts +15 -0
- package/esm/protocols/rollup/pipes/verify-signers.mjs +36 -0
- package/esm/protocols/rollup/pipes/verify-status.d.mts +13 -0
- package/esm/protocols/rollup/pipes/verify-status.mjs +26 -0
- package/esm/protocols/rollup/resume.d.mts +4 -0
- package/esm/protocols/rollup/resume.mjs +79 -0
- package/esm/protocols/rollup/update.d.mts +4 -0
- package/esm/protocols/rollup/update.mjs +111 -0
- package/esm/protocols/token/create.d.mts +4 -0
- package/esm/protocols/token/create.mjs +150 -0
- package/esm/protocols/token/deposit-v2.d.mts +11 -0
- package/esm/protocols/token/deposit-v2.mjs +216 -0
- package/esm/protocols/token/withdraw-v2.d.mts +9 -0
- package/esm/protocols/token/withdraw-v2.mjs +222 -0
- package/esm/protocols/token-factory/burn.d.mts +15 -0
- package/esm/protocols/token-factory/burn.mjs +233 -0
- package/esm/protocols/token-factory/create.d.mts +4 -0
- package/esm/protocols/token-factory/create.mjs +254 -0
- package/esm/protocols/token-factory/mint.d.mts +15 -0
- package/esm/protocols/token-factory/mint.mjs +234 -0
- package/esm/protocols/token-factory/pipes/calc-reserve.d.mts +21 -0
- package/esm/protocols/token-factory/pipes/calc-reserve.mjs +34 -0
- package/esm/protocols/token-factory/pipes/verify-icon.d.mts +14 -0
- package/esm/protocols/token-factory/pipes/verify-icon.mjs +18 -0
- package/esm/protocols/token-factory/pipes/verify-ownership.d.mts +12 -0
- package/esm/protocols/token-factory/pipes/verify-ownership.mjs +63 -0
- package/esm/protocols/token-factory/pipes/verify-url.d.mts +12 -0
- package/esm/protocols/token-factory/pipes/verify-url.mjs +26 -0
- package/esm/protocols/token-factory/update.d.mts +10 -0
- package/esm/protocols/token-factory/update.mjs +152 -0
- package/esm/protocols/trade/exchange-v2.d.mts +9 -0
- package/esm/protocols/trade/exchange-v2.mjs +239 -0
- package/esm/protocols/trade/transfer-v2.d.mts +9 -0
- package/esm/protocols/trade/transfer-v2.mjs +226 -0
- package/esm/protocols/trade/transfer-v3.d.mts +17 -0
- package/esm/protocols/trade/transfer-v3.mjs +270 -0
- package/esm/util.d.mts +141 -0
- package/esm/util.mjs +278 -0
- package/lib/_virtual/rolldown_runtime.cjs +29 -0
- package/lib/execute.cjs +231 -0
- package/lib/execute.d.cts +53 -0
- package/lib/index.cjs +105 -0
- package/lib/index.d.cts +95 -0
- package/lib/pipes/ensure-cost.cjs +141 -0
- package/lib/pipes/ensure-cost.d.cts +18 -0
- package/lib/pipes/ensure-gas.cjs +41 -0
- package/lib/pipes/ensure-gas.d.cts +14 -0
- package/lib/protocols/account/delegate.cjs +201 -0
- package/lib/protocols/account/delegate.d.cts +4 -0
- package/lib/protocols/account/migrate.cjs +135 -0
- package/lib/protocols/account/migrate.d.cts +4 -0
- package/lib/protocols/account/revoke-delegate.cjs +107 -0
- package/lib/protocols/account/revoke-delegate.d.cts +4 -0
- package/lib/protocols/asset/acquire-v2.cjs +216 -0
- package/lib/protocols/asset/acquire-v2.d.cts +9 -0
- package/lib/protocols/asset/acquire-v3.cjs +269 -0
- package/lib/protocols/asset/acquire-v3.d.cts +17 -0
- package/lib/protocols/asset/calls/transfer-token.cjs +40 -0
- package/lib/protocols/asset/calls/transfer-token.d.cts +4 -0
- package/lib/protocols/asset/calls/transfer.cjs +35 -0
- package/lib/protocols/asset/calls/transfer.d.cts +4 -0
- package/lib/protocols/asset/consume.cjs +95 -0
- package/lib/protocols/asset/consume.d.cts +4 -0
- package/lib/protocols/asset/create.cjs +140 -0
- package/lib/protocols/asset/create.d.cts +4 -0
- package/lib/protocols/asset/mint.cjs +159 -0
- package/lib/protocols/asset/mint.d.cts +4 -0
- package/lib/protocols/asset/pipes/exec-mint-hook.cjs +57 -0
- package/lib/protocols/asset/pipes/exec-mint-hook.d.cts +24 -0
- package/lib/protocols/asset/pipes/extract-factory-tokens.cjs +25 -0
- package/lib/protocols/asset/pipes/extract-factory-tokens.d.cts +21 -0
- package/lib/protocols/asset/pipes/verify-itx-address.cjs +59 -0
- package/lib/protocols/asset/pipes/verify-itx-address.d.cts +32 -0
- package/lib/protocols/asset/pipes/verify-itx-assets.cjs +29 -0
- package/lib/protocols/asset/pipes/verify-itx-assets.d.cts +18 -0
- package/lib/protocols/asset/pipes/verify-itx-variables.cjs +22 -0
- package/lib/protocols/asset/pipes/verify-itx-variables.d.cts +17 -0
- package/lib/protocols/asset/pipes/verify-mint-limit.cjs +16 -0
- package/lib/protocols/asset/pipes/verify-mint-limit.d.cts +15 -0
- package/lib/protocols/asset/update.cjs +114 -0
- package/lib/protocols/asset/update.d.cts +4 -0
- package/lib/protocols/factory/create.cjs +161 -0
- package/lib/protocols/factory/create.d.cts +29 -0
- package/lib/protocols/governance/claim-stake.cjs +223 -0
- package/lib/protocols/governance/claim-stake.d.cts +27 -0
- package/lib/protocols/governance/return-stake.cjs +215 -0
- package/lib/protocols/governance/return-stake.d.cts +27 -0
- package/lib/protocols/governance/revoke-stake.cjs +182 -0
- package/lib/protocols/governance/revoke-stake.d.cts +27 -0
- package/lib/protocols/governance/slash-stake.cjs +217 -0
- package/lib/protocols/governance/slash-stake.d.cts +17 -0
- package/lib/protocols/governance/stake.cjs +275 -0
- package/lib/protocols/governance/stake.d.cts +15 -0
- package/lib/protocols/rollup/claim-reward.cjs +328 -0
- package/lib/protocols/rollup/claim-reward.d.cts +11 -0
- package/lib/protocols/rollup/close.cjs +107 -0
- package/lib/protocols/rollup/close.d.cts +4 -0
- package/lib/protocols/rollup/create-block.cjs +309 -0
- package/lib/protocols/rollup/create-block.d.cts +11 -0
- package/lib/protocols/rollup/create.cjs +169 -0
- package/lib/protocols/rollup/create.d.cts +4 -0
- package/lib/protocols/rollup/join.cjs +157 -0
- package/lib/protocols/rollup/join.d.cts +4 -0
- package/lib/protocols/rollup/leave.cjs +141 -0
- package/lib/protocols/rollup/leave.d.cts +4 -0
- package/lib/protocols/rollup/migrate.cjs +87 -0
- package/lib/protocols/rollup/migrate.d.cts +4 -0
- package/lib/protocols/rollup/pause.cjs +78 -0
- package/lib/protocols/rollup/pause.d.cts +4 -0
- package/lib/protocols/rollup/pipes/ensure-validator.cjs +14 -0
- package/lib/protocols/rollup/pipes/ensure-validator.d.cts +6 -0
- package/lib/protocols/rollup/pipes/verify-evidence.cjs +32 -0
- package/lib/protocols/rollup/pipes/verify-evidence.d.cts +15 -0
- package/lib/protocols/rollup/pipes/verify-signers.cjs +39 -0
- package/lib/protocols/rollup/pipes/verify-signers.d.cts +15 -0
- package/lib/protocols/rollup/pipes/verify-status.cjs +28 -0
- package/lib/protocols/rollup/pipes/verify-status.d.cts +13 -0
- package/lib/protocols/rollup/resume.cjs +81 -0
- package/lib/protocols/rollup/resume.d.cts +4 -0
- package/lib/protocols/rollup/update.cjs +114 -0
- package/lib/protocols/rollup/update.d.cts +4 -0
- package/lib/protocols/token/create.cjs +156 -0
- package/lib/protocols/token/create.d.cts +4 -0
- package/lib/protocols/token/deposit-v2.cjs +219 -0
- package/lib/protocols/token/deposit-v2.d.cts +11 -0
- package/lib/protocols/token/withdraw-v2.cjs +225 -0
- package/lib/protocols/token/withdraw-v2.d.cts +9 -0
- package/lib/protocols/token-factory/burn.cjs +236 -0
- package/lib/protocols/token-factory/burn.d.cts +15 -0
- package/lib/protocols/token-factory/create.cjs +260 -0
- package/lib/protocols/token-factory/create.d.cts +4 -0
- package/lib/protocols/token-factory/mint.cjs +237 -0
- package/lib/protocols/token-factory/mint.d.cts +15 -0
- package/lib/protocols/token-factory/pipes/calc-reserve.cjs +38 -0
- package/lib/protocols/token-factory/pipes/calc-reserve.d.cts +21 -0
- package/lib/protocols/token-factory/pipes/verify-icon.cjs +22 -0
- package/lib/protocols/token-factory/pipes/verify-icon.d.cts +14 -0
- package/lib/protocols/token-factory/pipes/verify-ownership.cjs +66 -0
- package/lib/protocols/token-factory/pipes/verify-ownership.d.cts +12 -0
- package/lib/protocols/token-factory/pipes/verify-url.cjs +29 -0
- package/lib/protocols/token-factory/pipes/verify-url.d.cts +12 -0
- package/lib/protocols/token-factory/update.cjs +155 -0
- package/lib/protocols/token-factory/update.d.cts +10 -0
- package/lib/protocols/trade/exchange-v2.cjs +243 -0
- package/lib/protocols/trade/exchange-v2.d.cts +9 -0
- package/lib/protocols/trade/transfer-v2.cjs +229 -0
- package/lib/protocols/trade/transfer-v2.d.cts +9 -0
- package/lib/protocols/trade/transfer-v3.cjs +274 -0
- package/lib/protocols/trade/transfer-v3.d.cts +17 -0
- package/lib/util.cjs +296 -0
- package/lib/util.d.cts +141 -0
- package/package.json +49 -22
- package/tools/fixtures.ts +564 -0
- package/lib/execute.js +0 -254
- package/lib/index.js +0 -117
- package/lib/pipes/ensure-cost.js +0 -193
- package/lib/pipes/ensure-gas.js +0 -48
- package/lib/protocols/account/delegate.js +0 -223
- package/lib/protocols/account/migrate.js +0 -153
- package/lib/protocols/account/revoke-delegate.js +0 -110
- package/lib/protocols/asset/acquire-v2.js +0 -262
- package/lib/protocols/asset/acquire-v3.js +0 -330
- package/lib/protocols/asset/calls/README.md +0 -5
- package/lib/protocols/asset/calls/transfer-token.js +0 -36
- package/lib/protocols/asset/calls/transfer.js +0 -28
- package/lib/protocols/asset/consume.js +0 -105
- package/lib/protocols/asset/create.js +0 -151
- package/lib/protocols/asset/mint.js +0 -199
- package/lib/protocols/asset/pipes/exec-mint-hook.js +0 -62
- package/lib/protocols/asset/pipes/extract-factory-tokens.js +0 -18
- package/lib/protocols/asset/pipes/verify-itx-address.js +0 -54
- package/lib/protocols/asset/pipes/verify-itx-assets.js +0 -51
- package/lib/protocols/asset/pipes/verify-itx-variables.js +0 -26
- package/lib/protocols/asset/pipes/verify-mint-limit.js +0 -13
- package/lib/protocols/asset/update.js +0 -131
- package/lib/protocols/factory/create.js +0 -191
- package/lib/protocols/governance/claim-stake.js +0 -266
- package/lib/protocols/governance/return-stake.js +0 -248
- package/lib/protocols/governance/revoke-stake.js +0 -172
- package/lib/protocols/governance/slash-stake.js +0 -271
- package/lib/protocols/governance/stake.js +0 -303
- package/lib/protocols/rollup/claim-reward.js +0 -342
- package/lib/protocols/rollup/close.js +0 -104
- package/lib/protocols/rollup/create-block.js +0 -413
- package/lib/protocols/rollup/create.js +0 -197
- package/lib/protocols/rollup/join.js +0 -182
- package/lib/protocols/rollup/leave.js +0 -145
- package/lib/protocols/rollup/migrate.js +0 -85
- package/lib/protocols/rollup/pause.js +0 -75
- package/lib/protocols/rollup/pipes/ensure-validator.js +0 -12
- package/lib/protocols/rollup/pipes/verify-evidence.js +0 -37
- package/lib/protocols/rollup/pipes/verify-signers.js +0 -87
- package/lib/protocols/rollup/pipes/verify-status.js +0 -30
- package/lib/protocols/rollup/resume.js +0 -75
- package/lib/protocols/rollup/update.js +0 -122
- package/lib/protocols/token/create.js +0 -199
- package/lib/protocols/token/deposit-v2.js +0 -290
- package/lib/protocols/token/withdraw-v2.js +0 -305
- package/lib/protocols/token-factory/burn.js +0 -371
- package/lib/protocols/token-factory/create.js +0 -342
- package/lib/protocols/token-factory/mint.js +0 -385
- package/lib/protocols/token-factory/pipes/calc-reserve.js +0 -37
- package/lib/protocols/token-factory/pipes/verify-icon.js +0 -27
- package/lib/protocols/token-factory/pipes/verify-ownership.js +0 -93
- package/lib/protocols/token-factory/pipes/verify-url.js +0 -32
- package/lib/protocols/token-factory/update.js +0 -208
- package/lib/protocols/trade/exchange-v2.js +0 -239
- package/lib/protocols/trade/transfer-v2.js +0 -233
- package/lib/protocols/trade/transfer-v3.js +0 -333
- package/lib/util.js +0 -442
package/lib/execute.js
DELETED
|
@@ -1,254 +0,0 @@
|
|
|
1
|
-
const get = require('lodash/get');
|
|
2
|
-
const pick = require('lodash/pick');
|
|
3
|
-
const merge = require('lodash/merge');
|
|
4
|
-
const camelCase = require('lodash/camelCase');
|
|
5
|
-
const { CustomError: Error } = require('@ocap/util/lib/error');
|
|
6
|
-
const { Runner, pipes } = require('@ocap/tx-pipeline');
|
|
7
|
-
|
|
8
|
-
const createEnsureGasFn = require('./pipes/ensure-gas');
|
|
9
|
-
const createEnsureCostFn = require('./pipes/ensure-cost');
|
|
10
|
-
|
|
11
|
-
const ensureTxGas = createEnsureGasFn(() => ({ create: 0, update: 2 }));
|
|
12
|
-
const ensureTxCost = createEnsureCostFn({ attachSenderChanges: true, gasOnly: true });
|
|
13
|
-
|
|
14
|
-
const getTxName = (typeUrl) => camelCase(typeUrl.split(':').pop());
|
|
15
|
-
|
|
16
|
-
const flushEvents = (context, extra = {}) => {
|
|
17
|
-
(context.events || []).forEach((e) => {
|
|
18
|
-
context.statedb[e.table].emit(e.name, e.data, { ...e.ctx, ...extra });
|
|
19
|
-
});
|
|
20
|
-
context.events = [];
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
const shouldPersistTx = (err) => !err || get(err, 'props.persist');
|
|
24
|
-
|
|
25
|
-
module.exports = ({ filter, runAsLambda }) => {
|
|
26
|
-
// pipeline before executing the transaction
|
|
27
|
-
const pre = new Runner();
|
|
28
|
-
pre.use(pipes.DecodeTx);
|
|
29
|
-
pre.use(pipes.VerifyBlocked({ filter, stateKeys: ['tx.from'] }));
|
|
30
|
-
pre.use(pipes.DecodeItx);
|
|
31
|
-
pre.use(pipes.VerifyTx);
|
|
32
|
-
pre.use(pipes.VerifyServiceFee);
|
|
33
|
-
|
|
34
|
-
pre.use(async (context, next) => {
|
|
35
|
-
if (filter.has(context.txHash)) {
|
|
36
|
-
// Since bloom-filters may give false positive, we need to check for statedb if replay detected
|
|
37
|
-
const exist = await context.statedb.tx.get(context.txHash, context);
|
|
38
|
-
if (exist) {
|
|
39
|
-
return next(new Error('DUPLICATE_TX', 'Can not execute duplicate transaction'));
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
filter.add(context.txHash);
|
|
44
|
-
return next();
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
pre.use(pipes.VerifyTxSize);
|
|
48
|
-
pre.use(pipes.VerifySignature);
|
|
49
|
-
|
|
50
|
-
// charge gas fee for errored tx
|
|
51
|
-
const ensureGasFeePaid = (context) => {
|
|
52
|
-
const gasRunner = new Runner();
|
|
53
|
-
|
|
54
|
-
gasRunner.use(ensureTxGas);
|
|
55
|
-
gasRunner.use(ensureTxCost);
|
|
56
|
-
gasRunner.use(async (ctx, next) => {
|
|
57
|
-
const { tx, statedb, senderUpdates, updateVaults } = ctx;
|
|
58
|
-
|
|
59
|
-
// If senderState does not exist and there is no stakeId for gas, an error will be thrown in ensureTxCost before here.
|
|
60
|
-
// So if there is no senderState here, it means there has a stake for gas, so allowed to continue.
|
|
61
|
-
if (ctx.senderState) {
|
|
62
|
-
const senderState = ctx.stateSnapshot?.[ctx.senderState.address] || ctx.senderState;
|
|
63
|
-
await statedb.account.update(
|
|
64
|
-
senderState.address,
|
|
65
|
-
ctx.states.account.update(senderState, { nonce: tx.nonce, pk: tx.pk, ...senderUpdates }, ctx),
|
|
66
|
-
ctx
|
|
67
|
-
);
|
|
68
|
-
await updateVaults();
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
return next();
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
return new Promise((resolve, reject) => {
|
|
75
|
-
gasRunner.run(context, (err) => {
|
|
76
|
-
if (err) {
|
|
77
|
-
context.logger?.error('Failed to charge gas fee for errored tx', {
|
|
78
|
-
error: err,
|
|
79
|
-
tx: context.tx,
|
|
80
|
-
txBase64: context.txBase64,
|
|
81
|
-
txHash: context.txHash,
|
|
82
|
-
});
|
|
83
|
-
reject(err);
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
resolve();
|
|
88
|
-
});
|
|
89
|
-
});
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
const execute = async (context, protocols, isRetrySupported = false) =>
|
|
93
|
-
new Promise((resolve, reject) => {
|
|
94
|
-
pre.run(context, (err) => {
|
|
95
|
-
if (err) {
|
|
96
|
-
context.logger?.error('Failed to prepare transaction', {
|
|
97
|
-
error: err,
|
|
98
|
-
txHash: context.txHash,
|
|
99
|
-
request: context.extra?.request,
|
|
100
|
-
});
|
|
101
|
-
return reject(err);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
const name = getTxName(context.txType);
|
|
105
|
-
const protocol = protocols[name];
|
|
106
|
-
if (!protocol) {
|
|
107
|
-
return reject(new Error('UNSUPPORTED_TX', `Unsupported tx type ${context.txType}`));
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
protocol.run(context, async (error) => {
|
|
111
|
-
if (isRetrySupported) {
|
|
112
|
-
if (error) {
|
|
113
|
-
return reject(error);
|
|
114
|
-
}
|
|
115
|
-
return resolve(context);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const txStatus = error ? error.code || 'INTERNAL' : 'OK';
|
|
119
|
-
let txState = context.states.tx.create(context, txStatus);
|
|
120
|
-
|
|
121
|
-
if (shouldPersistTx(error)) {
|
|
122
|
-
try {
|
|
123
|
-
if (error) {
|
|
124
|
-
await ensureGasFeePaid(context);
|
|
125
|
-
// Recreate tx to pick up gas related fields
|
|
126
|
-
txState = context.states.tx.create(context, txStatus);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
await context.statedb.tx.create(txState.hash, txState, context);
|
|
130
|
-
flushEvents(context, { txState });
|
|
131
|
-
} catch (e) {
|
|
132
|
-
context.logger?.error('Failed to save transaction to statedb', {
|
|
133
|
-
error: e,
|
|
134
|
-
txError: error,
|
|
135
|
-
txHash: context.txHash,
|
|
136
|
-
txState,
|
|
137
|
-
});
|
|
138
|
-
// If we get an internal error here, should return the original tx error to client
|
|
139
|
-
return reject(error);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
context.logger?.info('Tx finalized', {
|
|
144
|
-
txHash: context.txHash,
|
|
145
|
-
txStatus,
|
|
146
|
-
txState,
|
|
147
|
-
error,
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
if (error) {
|
|
151
|
-
return reject(error);
|
|
152
|
-
}
|
|
153
|
-
return resolve(context);
|
|
154
|
-
});
|
|
155
|
-
});
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
if (typeof runAsLambda === 'function') {
|
|
159
|
-
return async (context, protocols) => {
|
|
160
|
-
let ctx = context;
|
|
161
|
-
const startTime = Date.now();
|
|
162
|
-
|
|
163
|
-
try {
|
|
164
|
-
const txState = await runAsLambda(
|
|
165
|
-
async (txn) => {
|
|
166
|
-
// create a new context each time in case we are retrying
|
|
167
|
-
ctx = pick(context, ['txBase64', 'statedb', 'indexdb', 'config', 'states', 'filter', 'extra', 'logger']);
|
|
168
|
-
ctx.txn = txn;
|
|
169
|
-
|
|
170
|
-
await execute(ctx, protocols, true);
|
|
171
|
-
|
|
172
|
-
const state = context.states.tx.create(ctx, 'OK');
|
|
173
|
-
await context.statedb.tx.create(state.hash, state, ctx);
|
|
174
|
-
|
|
175
|
-
return state;
|
|
176
|
-
},
|
|
177
|
-
{ cleanWorkingSet: true, verifyHash: true, context: ctx }
|
|
178
|
-
);
|
|
179
|
-
|
|
180
|
-
// update indexdb after statedb commit
|
|
181
|
-
flushEvents(ctx, { txState });
|
|
182
|
-
|
|
183
|
-
ctx.logger?.info('Tx finalized', {
|
|
184
|
-
txHash: ctx.txHash,
|
|
185
|
-
txState,
|
|
186
|
-
txStatus: 'OK',
|
|
187
|
-
duration: Date.now() - startTime,
|
|
188
|
-
});
|
|
189
|
-
} catch (error) {
|
|
190
|
-
const txStatus = error.code || 'INTERNAL';
|
|
191
|
-
let txState = ctx.tx ? context.states.tx.create(ctx, txStatus, false) : null;
|
|
192
|
-
|
|
193
|
-
ctx.logger?.error('Failed to execute transaction', {
|
|
194
|
-
error,
|
|
195
|
-
txHash: ctx.txHash,
|
|
196
|
-
txState,
|
|
197
|
-
request: context.extra?.request,
|
|
198
|
-
});
|
|
199
|
-
|
|
200
|
-
if (txState && shouldPersistTx(error)) {
|
|
201
|
-
// Avoid sync data in failed transactions to indexdb
|
|
202
|
-
ctx.events = [];
|
|
203
|
-
|
|
204
|
-
try {
|
|
205
|
-
txState = await runAsLambda(
|
|
206
|
-
async (txn) => {
|
|
207
|
-
ctx = Object.assign({}, ctx, { txn, cacheStates: null });
|
|
208
|
-
|
|
209
|
-
await ensureGasFeePaid(ctx);
|
|
210
|
-
|
|
211
|
-
// Recreate tx to pick up gas related fields
|
|
212
|
-
const state = context.states.tx.create(ctx, txStatus);
|
|
213
|
-
await ctx.statedb.tx.create(state.hash, state, ctx);
|
|
214
|
-
|
|
215
|
-
return state;
|
|
216
|
-
},
|
|
217
|
-
{ cleanWorkingSet: true, verifyHash: true, context: ctx }
|
|
218
|
-
);
|
|
219
|
-
|
|
220
|
-
// update indexdb after statedb commit
|
|
221
|
-
flushEvents(ctx, { txState });
|
|
222
|
-
|
|
223
|
-
ctx.logger?.info('Tx finalized', {
|
|
224
|
-
txHash: ctx.txHash,
|
|
225
|
-
txState,
|
|
226
|
-
txStatus,
|
|
227
|
-
error,
|
|
228
|
-
duration: Date.now() - startTime,
|
|
229
|
-
});
|
|
230
|
-
} catch (err) {
|
|
231
|
-
ctx.logger?.error('Failed to save invalid transaction to statedb', {
|
|
232
|
-
error: err,
|
|
233
|
-
txError: error,
|
|
234
|
-
txHash: ctx.txHash,
|
|
235
|
-
txState,
|
|
236
|
-
request: context.extra?.request,
|
|
237
|
-
});
|
|
238
|
-
// If we get an error here, should return the original tx error to client
|
|
239
|
-
throw error;
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
throw error;
|
|
244
|
-
} finally {
|
|
245
|
-
// Merge new ctx to original context
|
|
246
|
-
merge(context, ctx);
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
return ctx;
|
|
250
|
-
};
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
return execute;
|
|
254
|
-
};
|
package/lib/index.js
DELETED
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
const states = require('@ocap/state');
|
|
2
|
-
|
|
3
|
-
const transferV2 = require('./protocols/trade/transfer-v2');
|
|
4
|
-
const transferV3 = require('./protocols/trade/transfer-v3');
|
|
5
|
-
const exchangeV2 = require('./protocols/trade/exchange-v2');
|
|
6
|
-
const migrate = require('./protocols/account/migrate');
|
|
7
|
-
const delegate = require('./protocols/account/delegate');
|
|
8
|
-
const revokeDelegate = require('./protocols/account/revoke-delegate');
|
|
9
|
-
const createAsset = require('./protocols/asset/create');
|
|
10
|
-
const updateAsset = require('./protocols/asset/update');
|
|
11
|
-
const consumeAsset = require('./protocols/asset/consume');
|
|
12
|
-
const createFactory = require('./protocols/factory/create');
|
|
13
|
-
const acquireAssetV2 = require('./protocols/asset/acquire-v2');
|
|
14
|
-
const acquireAssetV3 = require('./protocols/asset/acquire-v3');
|
|
15
|
-
const mintAsset = require('./protocols/asset/mint');
|
|
16
|
-
const createToken = require('./protocols/token/create');
|
|
17
|
-
const stake = require('./protocols/governance/stake');
|
|
18
|
-
const revokeStake = require('./protocols/governance/revoke-stake');
|
|
19
|
-
const returnStake = require('./protocols/governance/return-stake');
|
|
20
|
-
const claimStake = require('./protocols/governance/claim-stake');
|
|
21
|
-
const slashStake = require('./protocols/governance/slash-stake');
|
|
22
|
-
const depositTokenV2 = require('./protocols/token/deposit-v2');
|
|
23
|
-
const withdrawTokenV2 = require('./protocols/token/withdraw-v2');
|
|
24
|
-
const createRollup = require('./protocols/rollup/create');
|
|
25
|
-
const updateRollup = require('./protocols/rollup/update');
|
|
26
|
-
const joinRollup = require('./protocols/rollup/join');
|
|
27
|
-
const leaveRollup = require('./protocols/rollup/leave');
|
|
28
|
-
const pauseRollup = require('./protocols/rollup/pause');
|
|
29
|
-
const closeRollup = require('./protocols/rollup/close');
|
|
30
|
-
const resumeRollup = require('./protocols/rollup/resume');
|
|
31
|
-
const createRollupBlock = require('./protocols/rollup/create-block');
|
|
32
|
-
const claimBlockReward = require('./protocols/rollup/claim-reward');
|
|
33
|
-
const migrateRollup = require('./protocols/rollup/migrate');
|
|
34
|
-
const createTokenFactory = require('./protocols/token-factory/create');
|
|
35
|
-
const updateTokenFactory = require('./protocols/token-factory/update');
|
|
36
|
-
const mintToken = require('./protocols/token-factory/mint');
|
|
37
|
-
const burnToken = require('./protocols/token-factory/burn');
|
|
38
|
-
|
|
39
|
-
const executor = require('./execute');
|
|
40
|
-
|
|
41
|
-
const createExecutor = ({ filter, runAsLambda }) => {
|
|
42
|
-
const protocols = {
|
|
43
|
-
// trade
|
|
44
|
-
transfer: transferV2,
|
|
45
|
-
transferV2,
|
|
46
|
-
transferV3,
|
|
47
|
-
exchangeV2,
|
|
48
|
-
|
|
49
|
-
// account
|
|
50
|
-
accountMigrate: migrate,
|
|
51
|
-
delegate,
|
|
52
|
-
revokeDelegate,
|
|
53
|
-
|
|
54
|
-
// asset
|
|
55
|
-
createAsset,
|
|
56
|
-
updateAsset,
|
|
57
|
-
consumeAsset,
|
|
58
|
-
acquireAssetV2,
|
|
59
|
-
acquireAssetV3,
|
|
60
|
-
mintAsset,
|
|
61
|
-
|
|
62
|
-
// factory
|
|
63
|
-
createFactory,
|
|
64
|
-
|
|
65
|
-
// token
|
|
66
|
-
createToken,
|
|
67
|
-
depositTokenV2,
|
|
68
|
-
withdrawTokenV2,
|
|
69
|
-
createTokenFactory,
|
|
70
|
-
updateTokenFactory,
|
|
71
|
-
mintToken,
|
|
72
|
-
burnToken,
|
|
73
|
-
|
|
74
|
-
// governance
|
|
75
|
-
stake,
|
|
76
|
-
revokeStake,
|
|
77
|
-
claimStake,
|
|
78
|
-
slashStake,
|
|
79
|
-
returnStake,
|
|
80
|
-
|
|
81
|
-
// rollup
|
|
82
|
-
createRollup,
|
|
83
|
-
updateRollup,
|
|
84
|
-
joinRollup,
|
|
85
|
-
leaveRollup,
|
|
86
|
-
pauseRollup,
|
|
87
|
-
closeRollup,
|
|
88
|
-
resumeRollup,
|
|
89
|
-
createRollupBlock,
|
|
90
|
-
claimBlockReward,
|
|
91
|
-
migrateRollup,
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
const execute = executor({ filter, runAsLambda });
|
|
95
|
-
|
|
96
|
-
return {
|
|
97
|
-
...protocols,
|
|
98
|
-
|
|
99
|
-
execute: (context, done) => {
|
|
100
|
-
Object.defineProperty(context, 'states', { value: states });
|
|
101
|
-
|
|
102
|
-
if (typeof done === 'function') {
|
|
103
|
-
return execute(context, protocols)
|
|
104
|
-
.then((res) => done(null, res))
|
|
105
|
-
.catch((err) => done(err));
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
return execute(context, protocols);
|
|
109
|
-
},
|
|
110
|
-
};
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
// default in-memory tx-hash-filter
|
|
114
|
-
const filter = new states.Blacklist();
|
|
115
|
-
|
|
116
|
-
module.exports = createExecutor({ filter });
|
|
117
|
-
module.exports.createExecutor = createExecutor;
|
package/lib/pipes/ensure-cost.js
DELETED
|
@@ -1,193 +0,0 @@
|
|
|
1
|
-
const noop = require('lodash/noop');
|
|
2
|
-
const { CustomError: Error } = require('@ocap/util/lib/error');
|
|
3
|
-
const { fromTokenToUnit, BN, fromUnitToToken, hexToNumber } = require('@ocap/util');
|
|
4
|
-
const JWT = require('@arcblock/jwt');
|
|
5
|
-
const { account } = require('@ocap/state');
|
|
6
|
-
const { toAddress } = require('@arcblock/did');
|
|
7
|
-
const { toStakeAddress } = require('@arcblock/did-util');
|
|
8
|
-
|
|
9
|
-
const debug = require('debug')(`${require('../../package.json').name}:pipes:ensure-cost`);
|
|
10
|
-
|
|
11
|
-
const { applyTokenUpdates, isGasStakeValid } = require('../util');
|
|
12
|
-
|
|
13
|
-
const ZERO = new BN(0);
|
|
14
|
-
|
|
15
|
-
// We have a layered transaction cost design
|
|
16
|
-
// - gas: charged for every tx for creating/updating states on the ledger
|
|
17
|
-
// - service fee: charged for creating tokens/assets/factories/rollups
|
|
18
|
-
// - protocol fee: charged for moving tokens across the bridge
|
|
19
|
-
module.exports = function CreateEnsureTxCostPipe({
|
|
20
|
-
attachSenderChanges = true,
|
|
21
|
-
throwOnInsufficientFund = true, // In some cases, we can tolerance insufficient fund, such as account migration
|
|
22
|
-
gasOnly = false,
|
|
23
|
-
} = {}) {
|
|
24
|
-
return async function EnsureTxCost(context, next) {
|
|
25
|
-
// TODO: we are using the sender as gas payer, this may change in future
|
|
26
|
-
const { config, statedb, txType, senderState, gasEstimate, totalGas } = context;
|
|
27
|
-
|
|
28
|
-
// verify gas staking headers
|
|
29
|
-
const { tx, extra = {}, txHash } = context;
|
|
30
|
-
const { token, pk } = extra.gasStakeHeaders || {};
|
|
31
|
-
const gasStake = {};
|
|
32
|
-
if (token && pk && (await JWT.verify(token, pk, { enforceTimestamp: true, tolerance: 5 }))) {
|
|
33
|
-
const decoded = JWT.decode(token);
|
|
34
|
-
const txHashes = (decoded.txHash || '')
|
|
35
|
-
.split(',')
|
|
36
|
-
.map((x) => x.trim())
|
|
37
|
-
.filter(Boolean);
|
|
38
|
-
if (txHashes.includes(txHash)) {
|
|
39
|
-
gasStake.owner = toAddress(decoded.iss);
|
|
40
|
-
gasStake.stakeId = toStakeAddress(gasStake.owner, gasStake.owner);
|
|
41
|
-
} else {
|
|
42
|
-
console.warn(`Invalid gas payer header for: ${txHash}`);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
// fallback to tx sender
|
|
46
|
-
if (!gasStake.owner) {
|
|
47
|
-
gasStake.owner = tx.from;
|
|
48
|
-
gasStake.stakeId = toStakeAddress(gasStake.owner, gasStake.owner);
|
|
49
|
-
}
|
|
50
|
-
gasStake.state = await statedb.stake.get(gasStake.stakeId, context);
|
|
51
|
-
gasStake.valid = isGasStakeValid(gasStake.state, config);
|
|
52
|
-
context.gasStake = gasStake;
|
|
53
|
-
debug('gasStake', gasStake, context.extra);
|
|
54
|
-
|
|
55
|
-
// Get service fee and gas fee
|
|
56
|
-
const changes = {};
|
|
57
|
-
let txCost = new BN(0);
|
|
58
|
-
const txFee = config.transaction.txFee[txType];
|
|
59
|
-
// always charge tx fee
|
|
60
|
-
if (!gasOnly && txFee) {
|
|
61
|
-
const totalFee = fromTokenToUnit(txFee, config.token.decimal);
|
|
62
|
-
txCost = txCost.add(totalFee);
|
|
63
|
-
changes.fee = { address: config.token.address, value: totalFee.toString(10) };
|
|
64
|
-
context.feeVaultChange = changes.fee;
|
|
65
|
-
}
|
|
66
|
-
// only charge gas when gas stake is not valid
|
|
67
|
-
if (!gasStake.valid && totalGas.gt(ZERO)) {
|
|
68
|
-
txCost = txCost.add(totalGas);
|
|
69
|
-
changes.gas = { address: config.token.address, value: totalGas.toString(10) };
|
|
70
|
-
context.gasVaultChange = changes.gas;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// pick fee vault
|
|
74
|
-
if (changes.fee) {
|
|
75
|
-
if (config.vaults.txFees?.length) {
|
|
76
|
-
const feeIndex = hexToNumber(txHash.slice(-8)) % config.vaults.txFees.length;
|
|
77
|
-
const feeVault = config.vaults.txFees[feeIndex];
|
|
78
|
-
context.feeVault = feeVault;
|
|
79
|
-
} else {
|
|
80
|
-
context.feeVault = config.vaults.txFee;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
let isCostCharged = false;
|
|
85
|
-
if (txCost.gt(ZERO)) {
|
|
86
|
-
const expected = new BN(gasEstimate.payment || 0).add(txCost);
|
|
87
|
-
const actual = new BN(senderState?.tokens?.[config.token.address] || 0);
|
|
88
|
-
// If we have someone with enough balance to pay for this tx
|
|
89
|
-
if (senderState && actual.gte(expected)) {
|
|
90
|
-
isCostCharged = true;
|
|
91
|
-
|
|
92
|
-
// to be merged into later pipe
|
|
93
|
-
context.senderUpdates = applyTokenUpdates(
|
|
94
|
-
[{ address: config.token.address, value: txCost.toString(10) }],
|
|
95
|
-
senderState,
|
|
96
|
-
'sub'
|
|
97
|
-
);
|
|
98
|
-
context.senderChange = {
|
|
99
|
-
address: senderState.address,
|
|
100
|
-
token: config.token.address,
|
|
101
|
-
delta: `-${txCost.toString(10)}`,
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
debug({ changes, senderUpdates: context.senderUpdates });
|
|
105
|
-
|
|
106
|
-
// to be called in later pipes
|
|
107
|
-
context.updateVaults = async function updateVaults() {
|
|
108
|
-
const [feeVaultState, gasVaultState] = await Promise.all([
|
|
109
|
-
changes.fee ? statedb.account.get(context.feeVault, context) : null,
|
|
110
|
-
changes.gas ? statedb.account.get(context.gasVault, context) : null,
|
|
111
|
-
]);
|
|
112
|
-
|
|
113
|
-
const [newFeeVaultState, newGasVaultState] = await Promise.all([
|
|
114
|
-
changes.fee
|
|
115
|
-
? statedb.account.update(
|
|
116
|
-
feeVaultState.address,
|
|
117
|
-
account.update(feeVaultState, applyTokenUpdates([changes.fee], feeVaultState, 'add'), context),
|
|
118
|
-
context
|
|
119
|
-
)
|
|
120
|
-
: null,
|
|
121
|
-
changes.gas
|
|
122
|
-
? statedb.account.update(
|
|
123
|
-
gasVaultState.address,
|
|
124
|
-
account.update(gasVaultState, applyTokenUpdates([changes.gas], gasVaultState, 'add'), context),
|
|
125
|
-
context
|
|
126
|
-
)
|
|
127
|
-
: null,
|
|
128
|
-
]);
|
|
129
|
-
|
|
130
|
-
if (newFeeVaultState) context.feeVaultState = newFeeVaultState;
|
|
131
|
-
if (newGasVaultState) context.gasVaultState = newGasVaultState;
|
|
132
|
-
|
|
133
|
-
context.updatedAccounts = context.updatedAccounts || [];
|
|
134
|
-
if (changes.fee) {
|
|
135
|
-
context.updatedAccounts.push({
|
|
136
|
-
address: feeVaultState.address,
|
|
137
|
-
token: config.token.address,
|
|
138
|
-
delta: changes.fee.value,
|
|
139
|
-
action: 'fee',
|
|
140
|
-
});
|
|
141
|
-
if (attachSenderChanges) {
|
|
142
|
-
context.updatedAccounts.push({
|
|
143
|
-
address: senderState.address,
|
|
144
|
-
token: config.token.address,
|
|
145
|
-
delta: `-${changes.fee.value}`,
|
|
146
|
-
action: 'fee',
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
if (changes.gas) {
|
|
151
|
-
context.updatedAccounts.push({
|
|
152
|
-
address: gasVaultState.address,
|
|
153
|
-
token: config.token.address,
|
|
154
|
-
delta: changes.gas.value,
|
|
155
|
-
action: 'gas',
|
|
156
|
-
});
|
|
157
|
-
if (attachSenderChanges) {
|
|
158
|
-
context.updatedAccounts.push({
|
|
159
|
-
address: senderState.address,
|
|
160
|
-
token: config.token.address,
|
|
161
|
-
delta: `-${changes.gas.value}`,
|
|
162
|
-
action: 'gas',
|
|
163
|
-
});
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
context.gasPaid = true;
|
|
168
|
-
};
|
|
169
|
-
} else if (!senderState) {
|
|
170
|
-
return next(new Error('INVALID_GAS_PAYER', `Gas payer ${tx.from} does not exist on chain`));
|
|
171
|
-
} else if (throwOnInsufficientFund) {
|
|
172
|
-
return next(
|
|
173
|
-
new Error(
|
|
174
|
-
'INSUFFICIENT_FUND',
|
|
175
|
-
`Insufficient fund to pay for tx cost from ${senderState?.address || tx.from}, expected ${fromUnitToToken(
|
|
176
|
-
expected,
|
|
177
|
-
config.token.decimal
|
|
178
|
-
)} ${config.token.symbol}, got ${fromUnitToToken(actual, config.token.decimal)} ${config.token.symbol}`
|
|
179
|
-
)
|
|
180
|
-
);
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
if (!isCostCharged) {
|
|
185
|
-
context.senderUpdates = {};
|
|
186
|
-
context.senderChange = null;
|
|
187
|
-
context.updateVaults = noop;
|
|
188
|
-
context.updatedAccounts = [];
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
return next();
|
|
192
|
-
};
|
|
193
|
-
};
|
package/lib/pipes/ensure-gas.js
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
const { BN, hexToNumber } = require('@ocap/util');
|
|
2
|
-
|
|
3
|
-
const debug = require('debug')(`${require('../../package.json').name}:pipes:ensure-gas`);
|
|
4
|
-
|
|
5
|
-
// Must be called after all state ready
|
|
6
|
-
// Must be called before creating/updating any state
|
|
7
|
-
// estimateTxGas should be a function that returns the following data:
|
|
8
|
-
// - create: state creating ops
|
|
9
|
-
// - update: state updating ops
|
|
10
|
-
// - payment: sender payment amount of the gas token
|
|
11
|
-
module.exports = function CreateGasEnsureFn(estimateTxGas) {
|
|
12
|
-
return function EnsureTxGas(context, next) {
|
|
13
|
-
const { txHash, config } = context;
|
|
14
|
-
|
|
15
|
-
// total gas in native token
|
|
16
|
-
const { txGas } = config.transaction;
|
|
17
|
-
const basePrice = new BN(txGas.price);
|
|
18
|
-
|
|
19
|
-
const estimate = estimateTxGas(context);
|
|
20
|
-
let totalGas = new BN(0);
|
|
21
|
-
if (context.txBaseGas) {
|
|
22
|
-
totalGas = basePrice.mul(new BN(txGas.dataStorage)).mul(new BN(context.txSize));
|
|
23
|
-
totalGas = totalGas.add(basePrice.mul(new BN(txGas.createState)).mul(new BN(estimate.create + 1))); // 1 = tx insert
|
|
24
|
-
totalGas = totalGas.add(basePrice.mul(new BN(txGas.updateState)).mul(new BN(estimate.update)));
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// gas receiver address
|
|
28
|
-
const { txGas: gasVaults } = config.vaults;
|
|
29
|
-
const gasIndex = hexToNumber(txHash.slice(-8)) % gasVaults.length;
|
|
30
|
-
const gasVault = gasVaults[gasIndex];
|
|
31
|
-
context.gasVault = gasVault;
|
|
32
|
-
|
|
33
|
-
debug({
|
|
34
|
-
txType: context.txType,
|
|
35
|
-
txHash: context.txHash,
|
|
36
|
-
txSize: context.txSize,
|
|
37
|
-
txBaseGas: context.txBaseGas,
|
|
38
|
-
totalGas: totalGas.toString(10),
|
|
39
|
-
gasVault,
|
|
40
|
-
estimate,
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
context.totalGas = totalGas;
|
|
44
|
-
context.gasEstimate = estimate;
|
|
45
|
-
|
|
46
|
-
return next();
|
|
47
|
-
};
|
|
48
|
-
};
|