@ocap/tx-protocols 1.28.8 → 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
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { CustomError } from "@ocap/util/lib/error";
|
|
2
|
+
import { Blacklist } from "@ocap/state";
|
|
3
|
+
import { IAccountState, IGasContext } from "@ocap/types";
|
|
4
|
+
|
|
5
|
+
//#region src/execute.d.ts
|
|
6
|
+
/** State helpers interface */
|
|
7
|
+
interface IStateHelpers {
|
|
8
|
+
tx: {
|
|
9
|
+
create: (ctx: IExecutionContext, status: string, persist?: boolean) => {
|
|
10
|
+
hash: string;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
account: {
|
|
14
|
+
update: (state: unknown, updates: unknown, ctx: unknown) => unknown;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
/** Extended context with tx-protocols specific fields */
|
|
18
|
+
interface IExecutionContext extends IGasContext {
|
|
19
|
+
events?: Array<{
|
|
20
|
+
table: string;
|
|
21
|
+
name: string;
|
|
22
|
+
data: unknown;
|
|
23
|
+
ctx: Record<string, unknown>;
|
|
24
|
+
}>;
|
|
25
|
+
states?: IStateHelpers;
|
|
26
|
+
stateSnapshot?: Record<string, unknown>;
|
|
27
|
+
txn?: unknown;
|
|
28
|
+
cacheStates?: unknown;
|
|
29
|
+
senderState?: IAccountState;
|
|
30
|
+
}
|
|
31
|
+
/** Protocol runner interface */
|
|
32
|
+
interface IProtocolRunner {
|
|
33
|
+
run: (context: IExecutionContext, callback: (error?: CustomError | null) => void) => void;
|
|
34
|
+
}
|
|
35
|
+
/** Protocols map */
|
|
36
|
+
type ProtocolsMap = Record<string, IProtocolRunner>;
|
|
37
|
+
/** Execute options */
|
|
38
|
+
interface IExecuteOptions {
|
|
39
|
+
filter: InstanceType<typeof Blacklist>;
|
|
40
|
+
runAsLambda?: <T>(fn: (txn: unknown) => Promise<T>, opts: {
|
|
41
|
+
cleanWorkingSet: boolean;
|
|
42
|
+
verifyHash: boolean;
|
|
43
|
+
context: IExecutionContext;
|
|
44
|
+
}) => Promise<T>;
|
|
45
|
+
}
|
|
46
|
+
/** Execute function type */
|
|
47
|
+
type ExecuteFn = (context: IExecutionContext, protocols: ProtocolsMap, isRetrySupported?: boolean) => Promise<IExecutionContext>;
|
|
48
|
+
declare const createExecute: ({
|
|
49
|
+
filter,
|
|
50
|
+
runAsLambda
|
|
51
|
+
}: IExecuteOptions) => ExecuteFn;
|
|
52
|
+
//#endregion
|
|
53
|
+
export { createExecute as default };
|
package/esm/execute.mjs
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import ensure_cost_default from "./pipes/ensure-cost.mjs";
|
|
2
|
+
import ensure_gas_default from "./pipes/ensure-gas.mjs";
|
|
3
|
+
import { Runner, pipes } from "@ocap/tx-pipeline";
|
|
4
|
+
import { CustomError } from "@ocap/util/lib/error";
|
|
5
|
+
import camelCase from "lodash/camelCase.js";
|
|
6
|
+
import get from "lodash/get.js";
|
|
7
|
+
import merge from "lodash/merge.js";
|
|
8
|
+
import pick from "lodash/pick.js";
|
|
9
|
+
|
|
10
|
+
//#region src/execute.ts
|
|
11
|
+
const ensureTxGas = ensure_gas_default(() => ({
|
|
12
|
+
create: 0,
|
|
13
|
+
update: 2
|
|
14
|
+
}));
|
|
15
|
+
const ensureTxCost = ensure_cost_default({
|
|
16
|
+
attachSenderChanges: true,
|
|
17
|
+
gasOnly: true
|
|
18
|
+
});
|
|
19
|
+
const getTxName = (typeUrl) => camelCase(typeUrl.split(":").pop() ?? "");
|
|
20
|
+
/** Get states helper with type guard - throws if states is not initialized */
|
|
21
|
+
const getStates = (context) => {
|
|
22
|
+
if (!context.states) throw new CustomError("INTERNAL", "States helper is not initialized in context");
|
|
23
|
+
return context.states;
|
|
24
|
+
};
|
|
25
|
+
const flushEvents = (context, extra = {}) => {
|
|
26
|
+
(context.events || []).forEach((e) => {
|
|
27
|
+
const table = context.statedb[e.table];
|
|
28
|
+
if (table?.emit) table.emit(e.name, e.data, {
|
|
29
|
+
...e.ctx,
|
|
30
|
+
...extra
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
context.events = [];
|
|
34
|
+
};
|
|
35
|
+
const shouldPersistTx = (err) => !err || Boolean(get(err, "props.persist"));
|
|
36
|
+
const createExecute = ({ filter, runAsLambda }) => {
|
|
37
|
+
const pre = new Runner("pre-execute");
|
|
38
|
+
pre.use(pipes.DecodeTx);
|
|
39
|
+
pre.use(pipes.VerifyBlocked({
|
|
40
|
+
filter,
|
|
41
|
+
stateKeys: ["tx.from"]
|
|
42
|
+
}));
|
|
43
|
+
pre.use(pipes.DecodeItx);
|
|
44
|
+
pre.use(pipes.VerifyTx);
|
|
45
|
+
pre.use(pipes.VerifyServiceFee);
|
|
46
|
+
pre.use(async (context, next) => {
|
|
47
|
+
if (filter.has(context.txHash)) {
|
|
48
|
+
if (await context.statedb.tx.get(context.txHash, context)) return next(new CustomError("DUPLICATE_TX", "Can not execute duplicate transaction"));
|
|
49
|
+
}
|
|
50
|
+
filter.add(context.txHash);
|
|
51
|
+
return next();
|
|
52
|
+
});
|
|
53
|
+
pre.use(pipes.VerifyTxSize);
|
|
54
|
+
pre.use(pipes.VerifySignature);
|
|
55
|
+
const ensureGasFeePaid = (context) => {
|
|
56
|
+
const gasRunner = new Runner("ensure-gas-fee-paid");
|
|
57
|
+
gasRunner.use(ensureTxGas);
|
|
58
|
+
gasRunner.use(ensureTxCost);
|
|
59
|
+
gasRunner.use(async (ctx, next) => {
|
|
60
|
+
const { tx, statedb, senderUpdates, updateVaults } = ctx;
|
|
61
|
+
if (ctx.senderState && ctx.states && tx) {
|
|
62
|
+
const senderState = ctx.stateSnapshot?.[ctx.senderState.address] || ctx.senderState;
|
|
63
|
+
const updates = ctx.states.account.update(senderState, {
|
|
64
|
+
nonce: tx.nonce,
|
|
65
|
+
pk: tx.pk,
|
|
66
|
+
...senderUpdates
|
|
67
|
+
}, ctx);
|
|
68
|
+
await statedb.account.update(senderState.address, updates, ctx);
|
|
69
|
+
if (updateVaults) await updateVaults();
|
|
70
|
+
}
|
|
71
|
+
return next();
|
|
72
|
+
});
|
|
73
|
+
return new Promise((resolve, reject) => {
|
|
74
|
+
gasRunner.run(context, (err) => {
|
|
75
|
+
if (err) {
|
|
76
|
+
context.logger?.error("Failed to charge gas fee for errored tx", {
|
|
77
|
+
error: err,
|
|
78
|
+
tx: context.tx,
|
|
79
|
+
txBase64: context.txBase64,
|
|
80
|
+
txHash: context.txHash
|
|
81
|
+
});
|
|
82
|
+
reject(err);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
resolve();
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
};
|
|
89
|
+
const execute = async (context, protocols, isRetrySupported = false) => new Promise((resolve, reject) => {
|
|
90
|
+
pre.run(context, (err) => {
|
|
91
|
+
if (err) {
|
|
92
|
+
context.logger?.error("Failed to prepare transaction", {
|
|
93
|
+
error: err,
|
|
94
|
+
txHash: context.txHash,
|
|
95
|
+
request: context.extra?.request
|
|
96
|
+
});
|
|
97
|
+
return reject(err);
|
|
98
|
+
}
|
|
99
|
+
const protocol = protocols[getTxName(context.txType)];
|
|
100
|
+
if (!protocol) return reject(new CustomError("UNSUPPORTED_TX", `Unsupported tx type ${context.txType}`));
|
|
101
|
+
protocol.run(context, async (error) => {
|
|
102
|
+
if (isRetrySupported) {
|
|
103
|
+
if (error) return reject(error);
|
|
104
|
+
return resolve(context);
|
|
105
|
+
}
|
|
106
|
+
const txStatus = error ? error.code || "INTERNAL" : "OK";
|
|
107
|
+
const states = getStates(context);
|
|
108
|
+
let txState = states.tx.create(context, txStatus);
|
|
109
|
+
if (shouldPersistTx(error)) try {
|
|
110
|
+
if (error) {
|
|
111
|
+
await ensureGasFeePaid(context);
|
|
112
|
+
txState = states.tx.create(context, txStatus);
|
|
113
|
+
}
|
|
114
|
+
await context.statedb.tx.create(txState.hash, txState, context);
|
|
115
|
+
flushEvents(context, { txState });
|
|
116
|
+
} catch (e) {
|
|
117
|
+
context.logger?.error("Failed to save transaction to statedb", {
|
|
118
|
+
error: e,
|
|
119
|
+
txError: error,
|
|
120
|
+
txHash: context.txHash,
|
|
121
|
+
txState
|
|
122
|
+
});
|
|
123
|
+
return reject(error);
|
|
124
|
+
}
|
|
125
|
+
context.logger?.info("Tx finalized", {
|
|
126
|
+
txHash: context.txHash,
|
|
127
|
+
txStatus,
|
|
128
|
+
txState,
|
|
129
|
+
error
|
|
130
|
+
});
|
|
131
|
+
if (error) return reject(error);
|
|
132
|
+
return resolve(context);
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
if (typeof runAsLambda === "function") return async (context, protocols) => {
|
|
137
|
+
let ctx = context;
|
|
138
|
+
const startTime = Date.now();
|
|
139
|
+
try {
|
|
140
|
+
const txState = await runAsLambda(async (txn) => {
|
|
141
|
+
ctx = pick(context, [
|
|
142
|
+
"txBase64",
|
|
143
|
+
"statedb",
|
|
144
|
+
"indexdb",
|
|
145
|
+
"config",
|
|
146
|
+
"states",
|
|
147
|
+
"filter",
|
|
148
|
+
"extra",
|
|
149
|
+
"logger"
|
|
150
|
+
]);
|
|
151
|
+
ctx.txn = txn;
|
|
152
|
+
await execute(ctx, protocols, true);
|
|
153
|
+
const state = getStates(context).tx.create(ctx, "OK");
|
|
154
|
+
await context.statedb.tx.create(state.hash, state, ctx);
|
|
155
|
+
return state;
|
|
156
|
+
}, {
|
|
157
|
+
cleanWorkingSet: true,
|
|
158
|
+
verifyHash: true,
|
|
159
|
+
context: ctx
|
|
160
|
+
});
|
|
161
|
+
flushEvents(ctx, { txState });
|
|
162
|
+
ctx.logger?.info("Tx finalized", {
|
|
163
|
+
txHash: ctx.txHash,
|
|
164
|
+
txState,
|
|
165
|
+
txStatus: "OK",
|
|
166
|
+
duration: Date.now() - startTime
|
|
167
|
+
});
|
|
168
|
+
} catch (error) {
|
|
169
|
+
const err = error;
|
|
170
|
+
const txStatus = err.code || "INTERNAL";
|
|
171
|
+
let txState = ctx.tx ? getStates(context).tx.create(ctx, txStatus, false) : null;
|
|
172
|
+
ctx.logger?.error("Failed to execute transaction", {
|
|
173
|
+
error,
|
|
174
|
+
txHash: ctx.txHash,
|
|
175
|
+
txState,
|
|
176
|
+
request: context.extra?.request
|
|
177
|
+
});
|
|
178
|
+
if (txState && shouldPersistTx(err)) {
|
|
179
|
+
ctx.events = [];
|
|
180
|
+
try {
|
|
181
|
+
txState = await runAsLambda(async (txn) => {
|
|
182
|
+
ctx = Object.assign({}, ctx, {
|
|
183
|
+
txn,
|
|
184
|
+
cacheStates: null
|
|
185
|
+
});
|
|
186
|
+
await ensureGasFeePaid(ctx);
|
|
187
|
+
const state = getStates(context).tx.create(ctx, txStatus);
|
|
188
|
+
await ctx.statedb.tx.create(state.hash, state, ctx);
|
|
189
|
+
return state;
|
|
190
|
+
}, {
|
|
191
|
+
cleanWorkingSet: true,
|
|
192
|
+
verifyHash: true,
|
|
193
|
+
context: ctx
|
|
194
|
+
});
|
|
195
|
+
flushEvents(ctx, { txState });
|
|
196
|
+
ctx.logger?.info("Tx finalized", {
|
|
197
|
+
txHash: ctx.txHash,
|
|
198
|
+
txState,
|
|
199
|
+
txStatus,
|
|
200
|
+
error,
|
|
201
|
+
duration: Date.now() - startTime
|
|
202
|
+
});
|
|
203
|
+
} catch (innerErr) {
|
|
204
|
+
ctx.logger?.error("Failed to save invalid transaction to statedb", {
|
|
205
|
+
error: innerErr,
|
|
206
|
+
txError: error,
|
|
207
|
+
txHash: ctx.txHash,
|
|
208
|
+
txState,
|
|
209
|
+
request: context.extra?.request
|
|
210
|
+
});
|
|
211
|
+
throw error;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
throw error;
|
|
215
|
+
} finally {
|
|
216
|
+
merge(context, ctx);
|
|
217
|
+
}
|
|
218
|
+
return ctx;
|
|
219
|
+
};
|
|
220
|
+
return execute;
|
|
221
|
+
};
|
|
222
|
+
var execute_default = createExecute;
|
|
223
|
+
|
|
224
|
+
//#endregion
|
|
225
|
+
export { execute_default as default };
|
package/esm/index.d.mts
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import * as states from "@ocap/state";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
interface ICreateExecutorOptions {
|
|
5
|
+
filter: InstanceType<typeof states.Blacklist>;
|
|
6
|
+
runAsLambda?: <T>(fn: (txn: unknown) => Promise<T>, opts: {
|
|
7
|
+
cleanWorkingSet: boolean;
|
|
8
|
+
verifyHash: boolean;
|
|
9
|
+
context: unknown;
|
|
10
|
+
}) => Promise<T>;
|
|
11
|
+
}
|
|
12
|
+
declare const createExecutor: ({
|
|
13
|
+
filter,
|
|
14
|
+
runAsLambda
|
|
15
|
+
}: ICreateExecutorOptions) => {
|
|
16
|
+
execute: (context: unknown, done?: (err: Error | null, res?: unknown) => void) => any;
|
|
17
|
+
transfer: any;
|
|
18
|
+
transferV2: any;
|
|
19
|
+
transferV3: any;
|
|
20
|
+
exchangeV2: any;
|
|
21
|
+
accountMigrate: any;
|
|
22
|
+
delegate: any;
|
|
23
|
+
revokeDelegate: any;
|
|
24
|
+
createAsset: any;
|
|
25
|
+
updateAsset: any;
|
|
26
|
+
consumeAsset: any;
|
|
27
|
+
acquireAssetV2: any;
|
|
28
|
+
acquireAssetV3: any;
|
|
29
|
+
mintAsset: any;
|
|
30
|
+
createFactory: any;
|
|
31
|
+
createToken: any;
|
|
32
|
+
depositTokenV2: any;
|
|
33
|
+
withdrawTokenV2: any;
|
|
34
|
+
createTokenFactory: any;
|
|
35
|
+
updateTokenFactory: any;
|
|
36
|
+
mintToken: any;
|
|
37
|
+
burnToken: any;
|
|
38
|
+
stake: any;
|
|
39
|
+
revokeStake: any;
|
|
40
|
+
claimStake: any;
|
|
41
|
+
slashStake: any;
|
|
42
|
+
returnStake: any;
|
|
43
|
+
createRollup: any;
|
|
44
|
+
updateRollup: any;
|
|
45
|
+
joinRollup: any;
|
|
46
|
+
leaveRollup: any;
|
|
47
|
+
pauseRollup: any;
|
|
48
|
+
closeRollup: any;
|
|
49
|
+
resumeRollup: any;
|
|
50
|
+
createRollupBlock: any;
|
|
51
|
+
claimBlockReward: any;
|
|
52
|
+
migrateRollup: any;
|
|
53
|
+
};
|
|
54
|
+
declare const defaultExecutor: {
|
|
55
|
+
execute: (context: unknown, done?: (err: Error | null, res?: unknown) => void) => any;
|
|
56
|
+
transfer: any;
|
|
57
|
+
transferV2: any;
|
|
58
|
+
transferV3: any;
|
|
59
|
+
exchangeV2: any;
|
|
60
|
+
accountMigrate: any;
|
|
61
|
+
delegate: any;
|
|
62
|
+
revokeDelegate: any;
|
|
63
|
+
createAsset: any;
|
|
64
|
+
updateAsset: any;
|
|
65
|
+
consumeAsset: any;
|
|
66
|
+
acquireAssetV2: any;
|
|
67
|
+
acquireAssetV3: any;
|
|
68
|
+
mintAsset: any;
|
|
69
|
+
createFactory: any;
|
|
70
|
+
createToken: any;
|
|
71
|
+
depositTokenV2: any;
|
|
72
|
+
withdrawTokenV2: any;
|
|
73
|
+
createTokenFactory: any;
|
|
74
|
+
updateTokenFactory: any;
|
|
75
|
+
mintToken: any;
|
|
76
|
+
burnToken: any;
|
|
77
|
+
stake: any;
|
|
78
|
+
revokeStake: any;
|
|
79
|
+
claimStake: any;
|
|
80
|
+
slashStake: any;
|
|
81
|
+
returnStake: any;
|
|
82
|
+
createRollup: any;
|
|
83
|
+
updateRollup: any;
|
|
84
|
+
joinRollup: any;
|
|
85
|
+
leaveRollup: any;
|
|
86
|
+
pauseRollup: any;
|
|
87
|
+
closeRollup: any;
|
|
88
|
+
resumeRollup: any;
|
|
89
|
+
createRollupBlock: any;
|
|
90
|
+
claimBlockReward: any;
|
|
91
|
+
migrateRollup: any;
|
|
92
|
+
};
|
|
93
|
+
declare const execute: (context: unknown, done?: (err: Error | null, res?: unknown) => void) => any;
|
|
94
|
+
//#endregion
|
|
95
|
+
export { createExecutor, defaultExecutor as default, execute };
|
package/esm/index.mjs
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import execute_default from "./execute.mjs";
|
|
2
|
+
import delegate_default from "./protocols/account/delegate.mjs";
|
|
3
|
+
import migrate_default from "./protocols/account/migrate.mjs";
|
|
4
|
+
import revoke_delegate_default from "./protocols/account/revoke-delegate.mjs";
|
|
5
|
+
import acquire_v2_default from "./protocols/asset/acquire-v2.mjs";
|
|
6
|
+
import acquire_v3_default from "./protocols/asset/acquire-v3.mjs";
|
|
7
|
+
import consume_default from "./protocols/asset/consume.mjs";
|
|
8
|
+
import create_default from "./protocols/asset/create.mjs";
|
|
9
|
+
import mint_default from "./protocols/asset/mint.mjs";
|
|
10
|
+
import update_default from "./protocols/asset/update.mjs";
|
|
11
|
+
import create_default$1 from "./protocols/factory/create.mjs";
|
|
12
|
+
import claim_stake_default from "./protocols/governance/claim-stake.mjs";
|
|
13
|
+
import return_stake_default from "./protocols/governance/return-stake.mjs";
|
|
14
|
+
import revoke_stake_default from "./protocols/governance/revoke-stake.mjs";
|
|
15
|
+
import slash_stake_default from "./protocols/governance/slash-stake.mjs";
|
|
16
|
+
import stake_default from "./protocols/governance/stake.mjs";
|
|
17
|
+
import claim_reward_default from "./protocols/rollup/claim-reward.mjs";
|
|
18
|
+
import close_default from "./protocols/rollup/close.mjs";
|
|
19
|
+
import create_default$2 from "./protocols/rollup/create.mjs";
|
|
20
|
+
import create_block_default from "./protocols/rollup/create-block.mjs";
|
|
21
|
+
import join_default from "./protocols/rollup/join.mjs";
|
|
22
|
+
import leave_default from "./protocols/rollup/leave.mjs";
|
|
23
|
+
import migrate_default$1 from "./protocols/rollup/migrate.mjs";
|
|
24
|
+
import pause_default from "./protocols/rollup/pause.mjs";
|
|
25
|
+
import resume_default from "./protocols/rollup/resume.mjs";
|
|
26
|
+
import update_default$1 from "./protocols/rollup/update.mjs";
|
|
27
|
+
import burn_default from "./protocols/token-factory/burn.mjs";
|
|
28
|
+
import create_default$3 from "./protocols/token-factory/create.mjs";
|
|
29
|
+
import mint_default$1 from "./protocols/token-factory/mint.mjs";
|
|
30
|
+
import update_default$2 from "./protocols/token-factory/update.mjs";
|
|
31
|
+
import create_default$4 from "./protocols/token/create.mjs";
|
|
32
|
+
import deposit_v2_default from "./protocols/token/deposit-v2.mjs";
|
|
33
|
+
import withdraw_v2_default from "./protocols/token/withdraw-v2.mjs";
|
|
34
|
+
import exchange_v2_default from "./protocols/trade/exchange-v2.mjs";
|
|
35
|
+
import transfer_v2_default from "./protocols/trade/transfer-v2.mjs";
|
|
36
|
+
import transfer_v3_default from "./protocols/trade/transfer-v3.mjs";
|
|
37
|
+
import * as states from "@ocap/state";
|
|
38
|
+
|
|
39
|
+
//#region src/index.ts
|
|
40
|
+
const createExecutor = ({ filter, runAsLambda }) => {
|
|
41
|
+
const protocols = {
|
|
42
|
+
transfer: transfer_v2_default,
|
|
43
|
+
transferV2: transfer_v2_default,
|
|
44
|
+
transferV3: transfer_v3_default,
|
|
45
|
+
exchangeV2: exchange_v2_default,
|
|
46
|
+
accountMigrate: migrate_default,
|
|
47
|
+
delegate: delegate_default,
|
|
48
|
+
revokeDelegate: revoke_delegate_default,
|
|
49
|
+
createAsset: create_default,
|
|
50
|
+
updateAsset: update_default,
|
|
51
|
+
consumeAsset: consume_default,
|
|
52
|
+
acquireAssetV2: acquire_v2_default,
|
|
53
|
+
acquireAssetV3: acquire_v3_default,
|
|
54
|
+
mintAsset: mint_default,
|
|
55
|
+
createFactory: create_default$1,
|
|
56
|
+
createToken: create_default$4,
|
|
57
|
+
depositTokenV2: deposit_v2_default,
|
|
58
|
+
withdrawTokenV2: withdraw_v2_default,
|
|
59
|
+
createTokenFactory: create_default$3,
|
|
60
|
+
updateTokenFactory: update_default$2,
|
|
61
|
+
mintToken: mint_default$1,
|
|
62
|
+
burnToken: burn_default,
|
|
63
|
+
stake: stake_default,
|
|
64
|
+
revokeStake: revoke_stake_default,
|
|
65
|
+
claimStake: claim_stake_default,
|
|
66
|
+
slashStake: slash_stake_default,
|
|
67
|
+
returnStake: return_stake_default,
|
|
68
|
+
createRollup: create_default$2,
|
|
69
|
+
updateRollup: update_default$1,
|
|
70
|
+
joinRollup: join_default,
|
|
71
|
+
leaveRollup: leave_default,
|
|
72
|
+
pauseRollup: pause_default,
|
|
73
|
+
closeRollup: close_default,
|
|
74
|
+
resumeRollup: resume_default,
|
|
75
|
+
createRollupBlock: create_block_default,
|
|
76
|
+
claimBlockReward: claim_reward_default,
|
|
77
|
+
migrateRollup: migrate_default$1
|
|
78
|
+
};
|
|
79
|
+
const execute$1 = execute_default({
|
|
80
|
+
filter,
|
|
81
|
+
runAsLambda
|
|
82
|
+
});
|
|
83
|
+
return {
|
|
84
|
+
...protocols,
|
|
85
|
+
execute: (context, done) => {
|
|
86
|
+
Object.defineProperty(context, "states", { value: states });
|
|
87
|
+
if (typeof done === "function") return execute$1(context, protocols).then((res) => done(null, res)).catch((err) => done(err));
|
|
88
|
+
return execute$1(context, protocols);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
const defaultExecutor = createExecutor({
|
|
93
|
+
filter: new states.Blacklist(),
|
|
94
|
+
runAsLambda: void 0
|
|
95
|
+
});
|
|
96
|
+
const { execute } = defaultExecutor;
|
|
97
|
+
var src_default = defaultExecutor;
|
|
98
|
+
|
|
99
|
+
//#endregion
|
|
100
|
+
export { createExecutor, src_default as default, execute };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { PipeFunction } from "@ocap/tx-pipeline";
|
|
2
|
+
|
|
3
|
+
//#region src/pipes/ensure-cost.d.ts
|
|
4
|
+
/** Options for CreateEnsureTxCostPipe */
|
|
5
|
+
interface IEnsureTxCostOptions {
|
|
6
|
+
attachSenderChanges?: boolean;
|
|
7
|
+
throwOnInsufficientFund?: boolean;
|
|
8
|
+
gasOnly?: boolean;
|
|
9
|
+
}
|
|
10
|
+
/** Context for ensure cost pipe */
|
|
11
|
+
declare function CreateEnsureTxCostPipe({
|
|
12
|
+
attachSenderChanges,
|
|
13
|
+
throwOnInsufficientFund,
|
|
14
|
+
// In some cases, we can tolerance insufficient fund, such as account migration
|
|
15
|
+
gasOnly
|
|
16
|
+
}?: IEnsureTxCostOptions): PipeFunction<any>;
|
|
17
|
+
//#endregion
|
|
18
|
+
export { CreateEnsureTxCostPipe as default };
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { applyTokenUpdates, isGasStakeValid } from "../util.mjs";
|
|
2
|
+
import { CustomError } from "@ocap/util/lib/error";
|
|
3
|
+
import { toAddress } from "@arcblock/did";
|
|
4
|
+
import { toStakeAddress } from "@arcblock/did-util";
|
|
5
|
+
import * as JWT from "@arcblock/jwt";
|
|
6
|
+
import { account } from "@ocap/state";
|
|
7
|
+
import { BN, fromTokenToUnit, fromUnitToToken, hexToNumber } from "@ocap/util";
|
|
8
|
+
import noop from "lodash/noop.js";
|
|
9
|
+
import Debug from "debug";
|
|
10
|
+
|
|
11
|
+
//#region src/pipes/ensure-cost.ts
|
|
12
|
+
const debug = Debug("@ocap/tx-protocols:pipes:ensure-cost");
|
|
13
|
+
const ZERO = new BN(0);
|
|
14
|
+
/** Context for ensure cost pipe */ function CreateEnsureTxCostPipe({ attachSenderChanges = true, throwOnInsufficientFund = true, gasOnly = false } = {}) {
|
|
15
|
+
return async function EnsureTxCost(context, next) {
|
|
16
|
+
const { statedb, txType, senderState, gasEstimate, totalGas } = context;
|
|
17
|
+
const { config } = context;
|
|
18
|
+
const { tx: tx$1, extra = {}, txHash = "" } = context;
|
|
19
|
+
const { token: token$1, pk } = extra.gasStakeHeaders || {};
|
|
20
|
+
const gasStake = {};
|
|
21
|
+
if (token$1 && pk && await JWT.verify(token$1, pk, {
|
|
22
|
+
enforceTimestamp: true,
|
|
23
|
+
tolerance: 5
|
|
24
|
+
})) {
|
|
25
|
+
const decoded = JWT.decode(token$1);
|
|
26
|
+
if ((decoded.txHash || "").split(",").map((x) => x.trim()).filter(Boolean).includes(txHash)) {
|
|
27
|
+
gasStake.owner = toAddress(decoded.iss);
|
|
28
|
+
gasStake.stakeId = toStakeAddress(gasStake.owner, gasStake.owner);
|
|
29
|
+
} else console.warn(`Invalid gas payer header for: ${txHash}`);
|
|
30
|
+
}
|
|
31
|
+
if (!gasStake.owner && tx$1?.from) {
|
|
32
|
+
const owner = tx$1.from;
|
|
33
|
+
gasStake.owner = owner;
|
|
34
|
+
gasStake.stakeId = toStakeAddress(owner, owner);
|
|
35
|
+
}
|
|
36
|
+
gasStake.state = await statedb.stake.get(gasStake.stakeId, context);
|
|
37
|
+
gasStake.valid = isGasStakeValid(gasStake.state, config);
|
|
38
|
+
context.gasStake = gasStake;
|
|
39
|
+
debug("gasStake", gasStake, context.extra);
|
|
40
|
+
const changes = {};
|
|
41
|
+
let txCost = new BN(0);
|
|
42
|
+
const txFeeMap = config.transaction.txFee;
|
|
43
|
+
const txFee = txType ? txFeeMap[txType] : void 0;
|
|
44
|
+
if (!gasOnly && txFee) {
|
|
45
|
+
const totalFee = fromTokenToUnit(txFee, config.token.decimal);
|
|
46
|
+
txCost = txCost.add(totalFee);
|
|
47
|
+
changes.fee = {
|
|
48
|
+
address: config.token.address,
|
|
49
|
+
value: totalFee.toString(10)
|
|
50
|
+
};
|
|
51
|
+
context.feeVaultChange = changes.fee;
|
|
52
|
+
}
|
|
53
|
+
const totalGasBN = totalGas ?? ZERO;
|
|
54
|
+
if (!gasStake.valid && totalGasBN.gt(ZERO)) {
|
|
55
|
+
txCost = txCost.add(totalGasBN);
|
|
56
|
+
changes.gas = {
|
|
57
|
+
address: config.token.address,
|
|
58
|
+
value: totalGasBN.toString(10)
|
|
59
|
+
};
|
|
60
|
+
context.gasVaultChange = changes.gas;
|
|
61
|
+
}
|
|
62
|
+
if (changes.fee) if (config.vaults.txFees?.length) {
|
|
63
|
+
const feeIndex = hexToNumber(txHash.slice(-8)) % config.vaults.txFees.length;
|
|
64
|
+
context.feeVault = config.vaults.txFees[feeIndex];
|
|
65
|
+
} else context.feeVault = config.vaults.txFee;
|
|
66
|
+
let isCostCharged = false;
|
|
67
|
+
if (txCost.gt(ZERO)) {
|
|
68
|
+
const expected = new BN(gasEstimate?.payment || 0).add(txCost);
|
|
69
|
+
const actual = new BN(senderState?.tokens?.[config.token.address] || 0);
|
|
70
|
+
if (senderState && actual.gte(expected)) {
|
|
71
|
+
isCostCharged = true;
|
|
72
|
+
context.senderUpdates = applyTokenUpdates([{
|
|
73
|
+
address: config.token.address,
|
|
74
|
+
value: txCost.toString(10)
|
|
75
|
+
}], senderState, "sub");
|
|
76
|
+
context.senderChange = {
|
|
77
|
+
address: senderState.address,
|
|
78
|
+
token: config.token.address,
|
|
79
|
+
delta: `-${txCost.toString(10)}`
|
|
80
|
+
};
|
|
81
|
+
debug({
|
|
82
|
+
changes,
|
|
83
|
+
senderUpdates: context.senderUpdates
|
|
84
|
+
});
|
|
85
|
+
context.updateVaults = async function updateVaults() {
|
|
86
|
+
const [feeVaultState, gasVaultState] = await Promise.all([changes.fee && context.feeVault ? statedb.account.get(context.feeVault, context) : null, changes.gas && context.gasVault ? statedb.account.get(context.gasVault, context) : null]);
|
|
87
|
+
const [newFeeVaultState, newGasVaultState] = await Promise.all([changes.fee && feeVaultState ? statedb.account.update(feeVaultState.address, account.update(feeVaultState, applyTokenUpdates([changes.fee], feeVaultState, "add"), context), context) : null, changes.gas && gasVaultState ? statedb.account.update(gasVaultState.address, account.update(gasVaultState, applyTokenUpdates([changes.gas], gasVaultState, "add"), context), context) : null]);
|
|
88
|
+
if (newFeeVaultState) context.feeVaultState = newFeeVaultState;
|
|
89
|
+
if (newGasVaultState) context.gasVaultState = newGasVaultState;
|
|
90
|
+
context.updatedAccounts = context.updatedAccounts || [];
|
|
91
|
+
if (changes.fee && feeVaultState) {
|
|
92
|
+
context.updatedAccounts.push({
|
|
93
|
+
address: feeVaultState.address,
|
|
94
|
+
token: config.token.address,
|
|
95
|
+
delta: changes.fee.value,
|
|
96
|
+
action: "fee"
|
|
97
|
+
});
|
|
98
|
+
if (attachSenderChanges) context.updatedAccounts.push({
|
|
99
|
+
address: senderState.address,
|
|
100
|
+
token: config.token.address,
|
|
101
|
+
delta: `-${changes.fee.value}`,
|
|
102
|
+
action: "fee"
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
if (changes.gas && gasVaultState) {
|
|
106
|
+
context.updatedAccounts.push({
|
|
107
|
+
address: gasVaultState.address,
|
|
108
|
+
token: config.token.address,
|
|
109
|
+
delta: changes.gas.value,
|
|
110
|
+
action: "gas"
|
|
111
|
+
});
|
|
112
|
+
if (attachSenderChanges) context.updatedAccounts.push({
|
|
113
|
+
address: senderState.address,
|
|
114
|
+
token: config.token.address,
|
|
115
|
+
delta: `-${changes.gas.value}`,
|
|
116
|
+
action: "gas"
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
context.gasPaid = true;
|
|
120
|
+
};
|
|
121
|
+
} else if (!senderState) return next(new CustomError("INVALID_GAS_PAYER", `Gas payer ${tx$1.from} does not exist on chain`));
|
|
122
|
+
else if (throwOnInsufficientFund) return next(new CustomError("INSUFFICIENT_FUND", `Insufficient fund to pay for tx cost from ${senderState.address || tx$1.from}, expected ${fromUnitToToken(expected, config.token.decimal)} ${config.token.symbol}, got ${fromUnitToToken(actual, config.token.decimal)} ${config.token.symbol}`));
|
|
123
|
+
}
|
|
124
|
+
if (!isCostCharged) {
|
|
125
|
+
context.senderUpdates = {};
|
|
126
|
+
context.senderChange = null;
|
|
127
|
+
context.updateVaults = noop;
|
|
128
|
+
context.updatedAccounts = [];
|
|
129
|
+
}
|
|
130
|
+
return next();
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
var ensure_cost_default = CreateEnsureTxCostPipe;
|
|
134
|
+
|
|
135
|
+
//#endregion
|
|
136
|
+
export { ensure_cost_default as default };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { PipeFunction } from "@ocap/tx-pipeline";
|
|
2
|
+
|
|
3
|
+
//#region src/pipes/ensure-gas.d.ts
|
|
4
|
+
/** Gas estimate result */
|
|
5
|
+
interface IGasEstimate {
|
|
6
|
+
create: number;
|
|
7
|
+
update: number;
|
|
8
|
+
payment?: number;
|
|
9
|
+
}
|
|
10
|
+
/** Gas estimator function type - uses any for flexible context access */
|
|
11
|
+
type GasEstimatorFn = (context: any) => IGasEstimate;
|
|
12
|
+
declare function CreateGasEnsureFn(estimateTxGas: GasEstimatorFn): PipeFunction<any>;
|
|
13
|
+
//#endregion
|
|
14
|
+
export { CreateGasEnsureFn as default };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { BN, hexToNumber } from "@ocap/util";
|
|
2
|
+
import Debug from "debug";
|
|
3
|
+
|
|
4
|
+
//#region src/pipes/ensure-gas.ts
|
|
5
|
+
const debug = Debug("@ocap/tx-protocols:pipes:ensure-gas");
|
|
6
|
+
function CreateGasEnsureFn(estimateTxGas) {
|
|
7
|
+
return function EnsureTxGas(context, next) {
|
|
8
|
+
const { txHash = "", config } = context;
|
|
9
|
+
const { txGas } = config.transaction;
|
|
10
|
+
const basePrice = new BN(txGas.price);
|
|
11
|
+
const estimate = estimateTxGas(context);
|
|
12
|
+
let totalGas = new BN(0);
|
|
13
|
+
if (context.txBaseGas) {
|
|
14
|
+
totalGas = basePrice.mul(new BN(txGas.dataStorage)).mul(new BN(context.txSize));
|
|
15
|
+
totalGas = totalGas.add(basePrice.mul(new BN(txGas.createState)).mul(new BN(estimate.create + 1)));
|
|
16
|
+
totalGas = totalGas.add(basePrice.mul(new BN(txGas.updateState)).mul(new BN(estimate.update)));
|
|
17
|
+
}
|
|
18
|
+
const { txGas: gasVaults = [] } = config.vaults;
|
|
19
|
+
const gasVault = gasVaults[gasVaults.length > 0 ? hexToNumber(txHash.slice(-8)) % gasVaults.length : 0];
|
|
20
|
+
context.gasVault = gasVault;
|
|
21
|
+
debug({
|
|
22
|
+
txType: context.txType,
|
|
23
|
+
txHash: context.txHash,
|
|
24
|
+
txSize: context.txSize,
|
|
25
|
+
txBaseGas: context.txBaseGas,
|
|
26
|
+
totalGas: totalGas.toString(10),
|
|
27
|
+
gasVault,
|
|
28
|
+
estimate
|
|
29
|
+
});
|
|
30
|
+
context.totalGas = totalGas;
|
|
31
|
+
context.gasEstimate = estimate;
|
|
32
|
+
return next();
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
var ensure_gas_default = CreateGasEnsureFn;
|
|
36
|
+
|
|
37
|
+
//#endregion
|
|
38
|
+
export { ensure_gas_default as default };
|