@ocap/tx-protocols 1.30.23 → 1.30.25
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 +6 -2
- package/esm/execute.mjs +214 -25
- package/esm/index.d.mts +25 -2
- package/esm/index.mjs +2 -2
- package/esm/pipes/ensure-cost.mjs +2 -2
- package/esm/pipes/write-ledger.mjs +7 -0
- package/esm/protocols/account/delegate.mjs +7 -6
- package/esm/protocols/account/migrate.mjs +2 -2
- package/esm/protocols/account/revoke-delegate.mjs +1 -1
- package/esm/protocols/asset/acquire-v2.mjs +25 -6
- package/esm/protocols/asset/acquire-v3.d.mts +11 -1
- package/esm/protocols/asset/acquire-v3.mjs +78 -9
- package/esm/protocols/asset/consume.mjs +1 -1
- package/esm/protocols/asset/create.mjs +1 -1
- package/esm/protocols/asset/mint.mjs +8 -2
- package/esm/protocols/asset/pipes/verify-itx-address.mjs +1 -1
- package/esm/protocols/asset/update.mjs +1 -1
- package/esm/protocols/factory/create.mjs +4 -3
- package/esm/protocols/governance/claim-stake.mjs +2 -2
- package/esm/protocols/governance/return-stake.mjs +2 -2
- package/esm/protocols/governance/revoke-stake.mjs +2 -2
- package/esm/protocols/governance/slash-stake.mjs +2 -2
- package/esm/protocols/governance/stake.mjs +2 -2
- package/esm/protocols/rollup/claim-reward.mjs +5 -4
- package/esm/protocols/rollup/close.mjs +1 -1
- package/esm/protocols/rollup/create-block.mjs +1 -1
- package/esm/protocols/rollup/create.mjs +1 -1
- package/esm/protocols/rollup/join.mjs +2 -2
- package/esm/protocols/rollup/leave.mjs +1 -1
- package/esm/protocols/rollup/migrate.mjs +1 -1
- package/esm/protocols/rollup/pause.mjs +1 -1
- package/esm/protocols/rollup/pipes/verify-evidence.mjs +5 -2
- package/esm/protocols/rollup/pipes/verify-signers.mjs +5 -3
- package/esm/protocols/rollup/resume.mjs +1 -1
- package/esm/protocols/rollup/update.mjs +4 -4
- package/esm/protocols/token/create.mjs +2 -2
- package/esm/protocols/token/deposit-v2.mjs +2 -2
- package/esm/protocols/token/withdraw-v2.mjs +13 -9
- package/esm/protocols/token-factory/burn.mjs +4 -4
- package/esm/protocols/token-factory/create.mjs +2 -2
- package/esm/protocols/token-factory/mint.mjs +2 -2
- package/esm/protocols/token-factory/update.mjs +7 -5
- package/esm/protocols/trade/exchange-v2.d.mts +3 -1
- package/esm/protocols/trade/exchange-v2.mjs +69 -11
- package/esm/protocols/trade/transfer-v2.mjs +39 -23
- package/esm/protocols/trade/transfer-v3.d.mts +11 -1
- package/esm/protocols/trade/transfer-v3.mjs +102 -11
- package/esm/tx-timer.d.mts +5 -0
- package/esm/tx-timer.mjs +13 -0
- package/esm/util.mjs +2 -1
- package/lib/execute.cjs +211 -20
- package/lib/execute.d.cts +6 -2
- package/lib/index.cjs +3 -1
- package/lib/index.d.cts +25 -2
- package/lib/pipes/ensure-cost.cjs +2 -2
- package/lib/pipes/write-ledger.cjs +7 -0
- package/lib/protocols/account/delegate.cjs +7 -6
- package/lib/protocols/account/migrate.cjs +2 -2
- package/lib/protocols/account/revoke-delegate.cjs +1 -1
- package/lib/protocols/asset/acquire-v2.cjs +24 -5
- package/lib/protocols/asset/acquire-v3.cjs +77 -8
- package/lib/protocols/asset/acquire-v3.d.cts +11 -1
- package/lib/protocols/asset/consume.cjs +1 -1
- package/lib/protocols/asset/create.cjs +1 -1
- package/lib/protocols/asset/mint.cjs +7 -1
- package/lib/protocols/asset/pipes/verify-itx-address.cjs +1 -1
- package/lib/protocols/asset/update.cjs +1 -1
- package/lib/protocols/factory/create.cjs +4 -3
- package/lib/protocols/governance/claim-stake.cjs +2 -2
- package/lib/protocols/governance/return-stake.cjs +2 -2
- package/lib/protocols/governance/revoke-stake.cjs +2 -2
- package/lib/protocols/governance/slash-stake.cjs +2 -2
- package/lib/protocols/governance/stake.cjs +2 -2
- package/lib/protocols/rollup/claim-reward.cjs +5 -4
- package/lib/protocols/rollup/close.cjs +1 -1
- package/lib/protocols/rollup/create-block.cjs +1 -1
- package/lib/protocols/rollup/create.cjs +1 -1
- package/lib/protocols/rollup/join.cjs +2 -2
- package/lib/protocols/rollup/leave.cjs +1 -1
- package/lib/protocols/rollup/migrate.cjs +1 -1
- package/lib/protocols/rollup/pause.cjs +1 -1
- package/lib/protocols/rollup/pipes/verify-evidence.cjs +5 -2
- package/lib/protocols/rollup/pipes/verify-signers.cjs +5 -4
- package/lib/protocols/rollup/resume.cjs +1 -1
- package/lib/protocols/rollup/update.cjs +4 -4
- package/lib/protocols/token/create.cjs +2 -2
- package/lib/protocols/token/deposit-v2.cjs +2 -2
- package/lib/protocols/token/withdraw-v2.cjs +13 -9
- package/lib/protocols/token-factory/burn.cjs +4 -4
- package/lib/protocols/token-factory/create.cjs +2 -2
- package/lib/protocols/token-factory/mint.cjs +2 -2
- package/lib/protocols/token-factory/update.cjs +7 -5
- package/lib/protocols/trade/exchange-v2.cjs +68 -10
- package/lib/protocols/trade/exchange-v2.d.cts +3 -1
- package/lib/protocols/trade/transfer-v2.cjs +39 -23
- package/lib/protocols/trade/transfer-v3.cjs +101 -10
- package/lib/protocols/trade/transfer-v3.d.cts +11 -1
- package/lib/tx-timer.cjs +13 -0
- package/lib/tx-timer.d.cts +5 -0
- package/lib/util.cjs +2 -1
- package/package.json +16 -16
|
@@ -2,11 +2,11 @@ import { applyTokenChange, applyTokenUpdates } from "../../util.mjs";
|
|
|
2
2
|
import ensure_cost_default from "../../pipes/ensure-cost.mjs";
|
|
3
3
|
import ensure_gas_default from "../../pipes/ensure-gas.mjs";
|
|
4
4
|
import calc_reserve_default from "./pipes/calc-reserve.mjs";
|
|
5
|
+
import { account, token, tokenFactory } from "@ocap/state";
|
|
5
6
|
import { Runner, pipes } from "@ocap/tx-pipeline";
|
|
7
|
+
import { BN, fromUnitToToken } from "@ocap/util";
|
|
6
8
|
import { CustomError } from "@ocap/util/lib/error";
|
|
7
9
|
import pick from "lodash/pick.js";
|
|
8
|
-
import { account, token, tokenFactory } from "@ocap/state";
|
|
9
|
-
import { BN, fromUnitToToken } from "@ocap/util";
|
|
10
10
|
import { getListField } from "@ocap/util/lib/get-list-field";
|
|
11
11
|
import { schemas, v, vBN, vBNGreater, vDID, vStripUnknown, vValidate } from "@arcblock/validator";
|
|
12
12
|
|
|
@@ -109,8 +109,8 @@ runner.use(pipes.verifyTokenAccess({
|
|
|
109
109
|
runner.use((context, next) => {
|
|
110
110
|
const { inputs = [], tokenFactoryState } = context;
|
|
111
111
|
let amount = new BN("0");
|
|
112
|
-
for (const
|
|
113
|
-
const delta =
|
|
112
|
+
for (const input of inputs) {
|
|
113
|
+
const delta = getListField(input, "tokens").find((x) => x.address === tokenFactoryState.tokenAddress)?.value;
|
|
114
114
|
if (!delta) return next(new CustomError("INVALID_TX", "Invalid inputs"));
|
|
115
115
|
amount = amount.add(new BN(delta));
|
|
116
116
|
}
|
|
@@ -5,11 +5,11 @@ import { any, array, check, literal, maxLength, maxValue, minLength, minValue, n
|
|
|
5
5
|
import verify_icon_default from "./pipes/verify-icon.mjs";
|
|
6
6
|
import verify_ownership_default from "./pipes/verify-ownership.mjs";
|
|
7
7
|
import verify_url_default from "./pipes/verify-url.mjs";
|
|
8
|
+
import { account, delegation, stake, token, tokenFactory } from "@ocap/state";
|
|
8
9
|
import { Runner, pipes } from "@ocap/tx-pipeline";
|
|
10
|
+
import { BN, fromTokenToUnit, fromUnitToToken } from "@ocap/util";
|
|
9
11
|
import { CustomError } from "@ocap/util/lib/error";
|
|
10
12
|
import { toStakeAddress, toTokenAddress, toTokenFactoryAddress } from "@arcblock/did-util";
|
|
11
|
-
import { account, delegation, stake, token, tokenFactory } from "@ocap/state";
|
|
12
|
-
import { BN, fromTokenToUnit, fromUnitToToken } from "@ocap/util";
|
|
13
13
|
import Debug from "debug";
|
|
14
14
|
import { getListField } from "@ocap/util/lib/get-list-field";
|
|
15
15
|
import cloneDeep from "lodash/cloneDeep.js";
|
|
@@ -3,11 +3,11 @@ import ensure_cost_default from "../../pipes/ensure-cost.mjs";
|
|
|
3
3
|
import ensure_gas_default from "../../pipes/ensure-gas.mjs";
|
|
4
4
|
import { any, nullable, object, optional, pipe } from "../../node_modules/valibot/dist/index.mjs";
|
|
5
5
|
import calc_reserve_default from "./pipes/calc-reserve.mjs";
|
|
6
|
+
import { account, token, tokenFactory } from "@ocap/state";
|
|
6
7
|
import { Runner, pipes } from "@ocap/tx-pipeline";
|
|
8
|
+
import { BN, fromUnitToToken } from "@ocap/util";
|
|
7
9
|
import { CustomError } from "@ocap/util/lib/error";
|
|
8
10
|
import pick from "lodash/pick.js";
|
|
9
|
-
import { account, token, tokenFactory } from "@ocap/state";
|
|
10
|
-
import { BN, fromUnitToToken } from "@ocap/util";
|
|
11
11
|
import { getListField } from "@ocap/util/lib/get-list-field";
|
|
12
12
|
import { schemas, vBN, vBNGreater, vDID, vStripUnknown, vValidate } from "@arcblock/validator";
|
|
13
13
|
|
|
@@ -4,9 +4,9 @@ import ensure_gas_default from "../../pipes/ensure-gas.mjs";
|
|
|
4
4
|
import verify_icon_default from "./pipes/verify-icon.mjs";
|
|
5
5
|
import verify_ownership_default from "./pipes/verify-ownership.mjs";
|
|
6
6
|
import verify_url_default from "./pipes/verify-url.mjs";
|
|
7
|
+
import { account, delegation, token, tokenFactory } from "@ocap/state";
|
|
7
8
|
import { Runner, pipes } from "@ocap/tx-pipeline";
|
|
8
9
|
import { CustomError } from "@ocap/util/lib/error";
|
|
9
|
-
import { account, delegation, token, tokenFactory } from "@ocap/state";
|
|
10
10
|
import Debug from "debug";
|
|
11
11
|
import { v, vDID, vStripUnknown, vValidate } from "@arcblock/validator";
|
|
12
12
|
|
|
@@ -121,11 +121,13 @@ runner.use(async (context, next) => {
|
|
|
121
121
|
const tokenStateValue = tokenState;
|
|
122
122
|
const { tokens: senderTokens = {} } = senderStateValue;
|
|
123
123
|
const senderUpdates = senderChange ? applyTokenChange({ tokens: senderTokens }, senderChange) : { tokens: senderTokens };
|
|
124
|
-
const
|
|
124
|
+
const rawItx = tx$1?.itx || {};
|
|
125
|
+
const hasExplicitFeeRate = typeof rawItx.feeRate === "number" || rawItx.value && typeof rawItx.value === "object" && typeof rawItx.value.feeRate === "number" || !rawItx.typeUrl && !rawItx.type && typeof itx.feeRate === "number";
|
|
126
|
+
const factoryUpdates = Boolean(itx.feeRate) || itx.feeRate === 0 && (hasExplicitFeeRate || !itx.token && !itx.data) ? { feeRate: itx.feeRate } : {};
|
|
125
127
|
const tokenUpdates = itx.token ? {
|
|
126
|
-
icon: itx.token
|
|
127
|
-
website: itx.token
|
|
128
|
-
metadata: itx.token
|
|
128
|
+
icon: itx.token.icon || tokenStateValue.icon,
|
|
129
|
+
website: itx.token.website || tokenStateValue.website,
|
|
130
|
+
metadata: itx.token.metadata || tokenStateValue.metadata
|
|
129
131
|
} : null;
|
|
130
132
|
const [newSenderState, newTokenFactoryState, newTokenState, newDelegationState] = await Promise.all([
|
|
131
133
|
statedb.account.update(senderStateValue.address, account.update(senderStateValue, {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Runner } from "@ocap/tx-pipeline";
|
|
2
|
-
import { IAssetState, IReadyContext, ITokenState, IWithDelegation, IWithDelegator, IWithReceiver, IWithSender, IWithSigners, TExchangeV2Tx } from "@ocap/types";
|
|
2
|
+
import { IAssetState, IDelegateState, IReadyContext, ITokenState, IWithDelegation, IWithDelegator, IWithReceiver, IWithSender, IWithSigners, TExchangeV2Tx } from "@ocap/types";
|
|
3
3
|
|
|
4
4
|
//#region src/protocols/trade/exchange-v2.d.ts
|
|
5
5
|
/** Token input - value can be string or number from decodeBigInt */
|
|
@@ -37,6 +37,8 @@ type Context = IReadyContext<TExchangeV2Tx> & IWithSender & IWithReceiver & Part
|
|
|
37
37
|
tokenStates?: ITokenState[];
|
|
38
38
|
/** Receiver address extracted from tx signatures or itx.to */
|
|
39
39
|
receiver?: string;
|
|
40
|
+
/** Multisig delegation states */
|
|
41
|
+
delegationStates?: IDelegateState[];
|
|
40
42
|
/** Private state for asset states */
|
|
41
43
|
priv?: {
|
|
42
44
|
senderAssets?: IAssetState[];
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { applyTokenChange } from "../../util.mjs";
|
|
2
2
|
import ensure_cost_default from "../../pipes/ensure-cost.mjs";
|
|
3
3
|
import ensure_gas_default from "../../pipes/ensure-gas.mjs";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import { account } from "@ocap/state";
|
|
4
|
+
import { account, delegation } from "@ocap/state";
|
|
5
|
+
import { Runner, pipes, verifyAssetHierarchy } from "@ocap/tx-pipeline";
|
|
7
6
|
import { BN } from "@ocap/util";
|
|
7
|
+
import { CustomError } from "@ocap/util/lib/error";
|
|
8
8
|
import Debug from "debug";
|
|
9
9
|
import { decodeBigInt } from "@ocap/message";
|
|
10
10
|
import { getListField } from "@ocap/util/lib/get-list-field";
|
|
@@ -94,7 +94,23 @@ runner.use(pipes.ExtractState({
|
|
|
94
94
|
runner.use(pipes.VerifyDelegation({
|
|
95
95
|
type: "signature",
|
|
96
96
|
signerKey: "senderState",
|
|
97
|
-
delegatorKey: "delegatorState"
|
|
97
|
+
delegatorKey: "delegatorState",
|
|
98
|
+
getRequirements: (ctx) => {
|
|
99
|
+
const context = ctx;
|
|
100
|
+
const tokens = context.senderTokens || [];
|
|
101
|
+
const assets = context.senderAssets || [];
|
|
102
|
+
const toAddresses = context.receiver ? [context.receiver] : [context.itx.to];
|
|
103
|
+
return [...tokens.map((x) => ({
|
|
104
|
+
type: "token",
|
|
105
|
+
address: x.address,
|
|
106
|
+
value: String(x.value),
|
|
107
|
+
to: toAddresses
|
|
108
|
+
})), ...assets.map((x) => ({
|
|
109
|
+
type: "asset",
|
|
110
|
+
address: x,
|
|
111
|
+
to: toAddresses
|
|
112
|
+
}))];
|
|
113
|
+
}
|
|
98
114
|
}));
|
|
99
115
|
runner.use(pipes.ExtractSigner({
|
|
100
116
|
signerKey: "signerStates",
|
|
@@ -105,7 +121,22 @@ runner.use(pipes.VerifyDelegation({
|
|
|
105
121
|
signerKey: "signerStates",
|
|
106
122
|
delegatorKey: "delegatorStates",
|
|
107
123
|
delegationKey: "delegationStates",
|
|
108
|
-
getRequirements: () =>
|
|
124
|
+
getRequirements: (ctx) => {
|
|
125
|
+
const context = ctx;
|
|
126
|
+
const tokens = context.receiverTokens || [];
|
|
127
|
+
const assets = context.receiverAssets || [];
|
|
128
|
+
const toAddresses = context.senderState ? [context.senderState.address] : [context.tx.from];
|
|
129
|
+
return [...tokens.map((x) => ({
|
|
130
|
+
type: "token",
|
|
131
|
+
address: x.address,
|
|
132
|
+
value: String(x.value),
|
|
133
|
+
to: toAddresses
|
|
134
|
+
})), ...assets.map((x) => ({
|
|
135
|
+
type: "asset",
|
|
136
|
+
address: x,
|
|
137
|
+
to: toAddresses
|
|
138
|
+
}))];
|
|
139
|
+
}
|
|
109
140
|
}));
|
|
110
141
|
runner.use(pipes.ExtractState({
|
|
111
142
|
from: "tokenAddress",
|
|
@@ -176,6 +207,21 @@ runner.use(pipes.VerifyUpdater({
|
|
|
176
207
|
assetKey: "priv.receiverAssets",
|
|
177
208
|
ownerKey: "receiverState"
|
|
178
209
|
}));
|
|
210
|
+
runner.use(async (context, next) => {
|
|
211
|
+
const { senderAssets = [], receiverAssets = [], senderState, receiverState } = context;
|
|
212
|
+
const pendingOwners = {};
|
|
213
|
+
if (receiverState?.address) for (const addr of senderAssets) pendingOwners[addr] = receiverState.address;
|
|
214
|
+
if (senderState?.address) for (const addr of receiverAssets) pendingOwners[addr] = senderState.address;
|
|
215
|
+
if (senderAssets.length && receiverState?.address) {
|
|
216
|
+
const err = await verifyAssetHierarchy(senderAssets, receiverState.address, context, pendingOwners);
|
|
217
|
+
if (err) return next(err);
|
|
218
|
+
}
|
|
219
|
+
if (receiverAssets.length && senderState?.address) {
|
|
220
|
+
const err = await verifyAssetHierarchy(receiverAssets, senderState.address, context, pendingOwners);
|
|
221
|
+
if (err) return next(err);
|
|
222
|
+
}
|
|
223
|
+
return next();
|
|
224
|
+
});
|
|
179
225
|
runner.use(ensure_gas_default((context) => {
|
|
180
226
|
const ops = {
|
|
181
227
|
create: 0,
|
|
@@ -197,7 +243,7 @@ runner.use(pipes.UpdateOwner({
|
|
|
197
243
|
}));
|
|
198
244
|
runner.use(pipes.TakeStateSnapshot());
|
|
199
245
|
runner.use(async (context, next) => {
|
|
200
|
-
const { tx: tx$1, itx, senderAssets = [], receiverAssets = [], senderTokens = [], receiverTokens = [], senderState, receiverState, senderChange, updateVaults, statedb } = context;
|
|
246
|
+
const { tx: tx$1, itx, senderAssets = [], receiverAssets = [], senderTokens = [], receiverTokens = [], senderState, receiverState, senderChange, delegationState, delegationStates = [], updateVaults, statedb } = context;
|
|
201
247
|
const senderStateTokens = senderState?.tokens || {};
|
|
202
248
|
const receiverStateTokens = receiverState?.tokens || {};
|
|
203
249
|
for (const { address, value } of senderTokens) {
|
|
@@ -207,17 +253,29 @@ runner.use(async (context, next) => {
|
|
|
207
253
|
}
|
|
208
254
|
for (const { address, value } of receiverTokens) {
|
|
209
255
|
const delta = new BN(value);
|
|
210
|
-
senderStateTokens[address] = new BN(senderStateTokens[address]).add(delta).toString();
|
|
256
|
+
senderStateTokens[address] = new BN(senderStateTokens[address] || "0").add(delta).toString();
|
|
211
257
|
receiverStateTokens[address] = new BN(receiverStateTokens[address] || "0").sub(delta).toString();
|
|
212
258
|
}
|
|
213
259
|
const senderUpdates = senderChange ? applyTokenChange({ tokens: senderStateTokens }, senderChange) : { tokens: senderStateTokens };
|
|
214
|
-
const [
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
260
|
+
const [existingSenderState, existingReceiverState] = await Promise.all([statedb.account.get(senderState.address, context), statedb.account.get(receiverState.address, context)]);
|
|
261
|
+
const [newSenderState, newReceiverState, newDelegationState, newDelegationStates] = await Promise.all([
|
|
262
|
+
statedb.account.updateOrCreate(existingSenderState ?? null, account.updateOrCreate(existingSenderState ?? null, {
|
|
263
|
+
address: senderState.address,
|
|
264
|
+
nonce: tx$1.nonce,
|
|
265
|
+
...senderUpdates
|
|
266
|
+
}, context), context),
|
|
267
|
+
statedb.account.updateOrCreate(existingReceiverState ?? null, account.updateOrCreate(existingReceiverState ?? null, {
|
|
268
|
+
address: receiverState.address,
|
|
269
|
+
tokens: receiverStateTokens
|
|
270
|
+
}, context), context),
|
|
271
|
+
context.isDelegationChanged && delegationState ? statedb.delegation.update(delegationState.address, delegation.update(delegationState, {}, context), context) : delegationState,
|
|
272
|
+
context.isDelegationChanged && delegationStates.length ? Promise.all(delegationStates.map((d) => statedb.delegation.update(d.address, delegation.update(d, {}, context), context))) : delegationStates
|
|
273
|
+
]);
|
|
218
274
|
if (updateVaults) await updateVaults();
|
|
219
275
|
context.senderState = newSenderState;
|
|
220
276
|
context.receiverState = newReceiverState;
|
|
277
|
+
context.delegationState = newDelegationState;
|
|
278
|
+
context.delegationStates = newDelegationStates;
|
|
221
279
|
debug("exchange", {
|
|
222
280
|
from: tx$1.from,
|
|
223
281
|
to: itx.to,
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { applyTokenChange } from "../../util.mjs";
|
|
2
2
|
import ensure_cost_default from "../../pipes/ensure-cost.mjs";
|
|
3
3
|
import ensure_gas_default from "../../pipes/ensure-gas.mjs";
|
|
4
|
+
import { account, delegation } from "@ocap/state";
|
|
4
5
|
import { Runner, pipes } from "@ocap/tx-pipeline";
|
|
6
|
+
import { BN } from "@ocap/util";
|
|
5
7
|
import { CustomError } from "@ocap/util/lib/error";
|
|
6
8
|
import { toStakeAddress } from "@arcblock/did-util";
|
|
7
|
-
import { account, delegation } from "@ocap/state";
|
|
8
|
-
import { BN } from "@ocap/util";
|
|
9
9
|
import { decodeBigInt } from "@ocap/message";
|
|
10
10
|
import { getListField } from "@ocap/util/lib/get-list-field";
|
|
11
11
|
import { schemas, v, vDID, vStripUnknown, vValidate } from "@arcblock/validator";
|
|
@@ -36,33 +36,47 @@ runner.use((context, next) => {
|
|
|
36
36
|
context.tokenAddress = (context.tokens || []).map((x) => x.address);
|
|
37
37
|
next();
|
|
38
38
|
});
|
|
39
|
-
runner.use(pipes.VerifyInfo([
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
39
|
+
runner.use(pipes.VerifyInfo([
|
|
40
|
+
{
|
|
41
|
+
error: "INSUFFICIENT_DATA",
|
|
42
|
+
message: "Can not transfer without any primary token or assets or secondary tokens",
|
|
43
|
+
fn: (ctx) => {
|
|
44
|
+
const { itx, assets, tokens } = ctx;
|
|
45
|
+
if (itx.to && (itx.value || isEmpty(assets) === false || isEmpty(tokens) === false)) return true;
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
error: "INVALID_TX",
|
|
51
|
+
message: "Can not transfer primary token smaller than 0",
|
|
52
|
+
fn: (ctx) => {
|
|
53
|
+
const { itx, assets, tokens } = ctx;
|
|
54
|
+
if (isEmpty(tokens) && isEmpty(assets)) {
|
|
55
|
+
if (new BN(itx.value ? decodeBigInt(itx.value) : 0).lte(new BN(0))) return false;
|
|
56
|
+
}
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
error: "INVALID_TX",
|
|
62
|
+
message: "Can not transfer duplicate tokens in itx.tokens",
|
|
63
|
+
fn: (ctx) => {
|
|
64
|
+
const addresses = (ctx.tokens || []).map((x) => x.address);
|
|
65
|
+
return new Set(addresses).size === addresses.length;
|
|
54
66
|
}
|
|
55
|
-
return true;
|
|
56
67
|
}
|
|
57
|
-
|
|
68
|
+
]));
|
|
58
69
|
runner.use(pipes.VerifyListSize({ listKey: ["assets", "tokens"] }));
|
|
59
70
|
runner.use((context, next) => {
|
|
60
71
|
if (context.itx.value) {
|
|
61
72
|
const amount = new BN(decodeBigInt(context.itx.value));
|
|
62
73
|
if (amount.gt(new BN(0))) {
|
|
63
74
|
const tokens = context.tokens || [];
|
|
64
|
-
|
|
65
|
-
|
|
75
|
+
const primaryTokenAddress = context.config.token.address;
|
|
76
|
+
const existing = tokens.find((x) => x.address === primaryTokenAddress);
|
|
77
|
+
if (existing) existing.value = new BN(existing.value).add(amount).toString();
|
|
78
|
+
else tokens.push({
|
|
79
|
+
address: primaryTokenAddress,
|
|
66
80
|
value: amount.toString()
|
|
67
81
|
});
|
|
68
82
|
context.tokens = tokens;
|
|
@@ -204,10 +218,12 @@ runner.use(async (context, next) => {
|
|
|
204
218
|
receiverTokens[token$1.address] = new BN(receiverTokens[token$1.address] || "0").add(delta).toString();
|
|
205
219
|
}
|
|
206
220
|
const senderUpdates = senderChange ? applyTokenChange({ tokens: senderTokens }, senderChange) : { tokens: senderTokens };
|
|
221
|
+
const existingSenderState = await statedb.account.get(senderState.address, context);
|
|
207
222
|
const [newSenderState, newReceiverState, newDelegationState] = await Promise.all([
|
|
208
|
-
statedb.account.
|
|
223
|
+
statedb.account.updateOrCreate(existingSenderState ?? null, account.updateOrCreate(existingSenderState ?? null, {
|
|
224
|
+
address: senderState.address,
|
|
209
225
|
nonce: tx$1.nonce,
|
|
210
|
-
pk: tx$1.pk,
|
|
226
|
+
pk: tx$1.delegator ? senderState.pk : tx$1.pk,
|
|
211
227
|
...senderUpdates
|
|
212
228
|
}, context), context),
|
|
213
229
|
statedb.account.updateOrCreate(receiverState ?? null, account.updateOrCreate(receiverState ?? null, {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Runner } from "@ocap/tx-pipeline";
|
|
2
|
-
import { IAssetState, IReadyContext, ITokenState, IWithReceivers, IWithSender, IWithSigners, TTransferV3Tx } from "@ocap/types";
|
|
2
|
+
import { IAccountState, IAssetState, IDelegateState, IReadyContext, ITokenState, IWithReceivers, IWithSender, IWithSigners, TTransferV3Tx } from "@ocap/types";
|
|
3
3
|
|
|
4
4
|
//#region src/protocols/trade/transfer-v3.d.ts
|
|
5
5
|
/** Token input type */
|
|
@@ -41,6 +41,16 @@ type Context = IReadyContext<TTransferV3Tx> & Partial<IWithSender> & Partial<IWi
|
|
|
41
41
|
tokenStates?: ITokenState[];
|
|
42
42
|
/** Asset states for transferred assets */
|
|
43
43
|
assetStates?: IAssetState[];
|
|
44
|
+
/** Delegator account state for tx.delegator */
|
|
45
|
+
delegatorState?: IAccountState;
|
|
46
|
+
/** Explicit delegation state for tx.delegator */
|
|
47
|
+
delegationState?: IDelegateState;
|
|
48
|
+
/** Signer states extracted from tx.signatures */
|
|
49
|
+
msSignerStates?: IAccountState[];
|
|
50
|
+
/** Delegator states extracted from tx.signatures */
|
|
51
|
+
delegatorStates?: (IAccountState | null)[];
|
|
52
|
+
/** Explicit delegation states for tx.signatures */
|
|
53
|
+
delegationStates?: IDelegateState[];
|
|
44
54
|
};
|
|
45
55
|
declare const runner: Runner<Context>;
|
|
46
56
|
//#endregion
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { applyTokenChange, applyTokenUpdates } from "../../util.mjs";
|
|
2
2
|
import ensure_cost_default from "../../pipes/ensure-cost.mjs";
|
|
3
3
|
import ensure_gas_default from "../../pipes/ensure-gas.mjs";
|
|
4
|
-
import {
|
|
4
|
+
import { account, asset, delegation } from "@ocap/state";
|
|
5
|
+
import { Runner, pipes, verifyAssetHierarchy } from "@ocap/tx-pipeline";
|
|
6
|
+
import { BN, isSameDid } from "@ocap/util";
|
|
5
7
|
import { CustomError } from "@ocap/util/lib/error";
|
|
6
|
-
import { account, asset } from "@ocap/state";
|
|
7
|
-
import { BN } from "@ocap/util";
|
|
8
8
|
import Debug from "debug";
|
|
9
9
|
import { getListField } from "@ocap/util/lib/get-list-field";
|
|
10
10
|
import { schemas, v, vStripUnknown, vValidate } from "@arcblock/validator";
|
|
@@ -146,6 +146,69 @@ runner.use(pipes.VerifyAccountMigration({
|
|
|
146
146
|
stateKey: "senderState",
|
|
147
147
|
addressKey: "tx.from"
|
|
148
148
|
}));
|
|
149
|
+
const getTransferV3Requirements = (ctx, index, signature) => {
|
|
150
|
+
const { tx: tx$1, inputs = [], outputs = [], receiverStates = [] } = ctx;
|
|
151
|
+
const signatures = getListField(tx$1, "signatures");
|
|
152
|
+
const targetOwner = signature?.signer || (typeof index === "number" ? signatures?.[index]?.signer : tx$1?.from);
|
|
153
|
+
if (!targetOwner) return [];
|
|
154
|
+
const input = inputs.find((x) => isSameDid(x.owner, targetOwner));
|
|
155
|
+
if (!input) return [];
|
|
156
|
+
const tokensList = getListField(input, "tokens");
|
|
157
|
+
const assetsList = getListField(input, "assets");
|
|
158
|
+
const getReceiverAddresses = (owner) => {
|
|
159
|
+
if (!owner) return [];
|
|
160
|
+
const state = receiverStates.find((s) => getRelatedAddresses(s).some((addr) => isSameDid(addr, owner)));
|
|
161
|
+
return state ? getRelatedAddresses(state) : [owner];
|
|
162
|
+
};
|
|
163
|
+
return [...tokensList.flatMap((t) => {
|
|
164
|
+
const tokenOutputs = outputs.filter((o) => getListField(o, "tokens").some((ot) => isSameDid(ot.address, t.address)));
|
|
165
|
+
if (tokenOutputs.length === 0) return [{
|
|
166
|
+
type: "token",
|
|
167
|
+
address: t.address,
|
|
168
|
+
value: t.value,
|
|
169
|
+
to: []
|
|
170
|
+
}];
|
|
171
|
+
return tokenOutputs.map((o, i) => ({
|
|
172
|
+
type: "token",
|
|
173
|
+
address: t.address,
|
|
174
|
+
value: i === 0 ? t.value : "0",
|
|
175
|
+
to: getReceiverAddresses(o.owner)
|
|
176
|
+
}));
|
|
177
|
+
}), ...assetsList.map((a) => {
|
|
178
|
+
return {
|
|
179
|
+
type: "asset",
|
|
180
|
+
address: a,
|
|
181
|
+
to: getReceiverAddresses(outputs.find((o) => getListField(o, "assets").some((oa) => isSameDid(oa, a)))?.owner)
|
|
182
|
+
};
|
|
183
|
+
})];
|
|
184
|
+
};
|
|
185
|
+
runner.use(pipes.ExtractState({
|
|
186
|
+
from: "tx.delegator",
|
|
187
|
+
to: "delegatorState",
|
|
188
|
+
status: "OK",
|
|
189
|
+
table: "account"
|
|
190
|
+
}));
|
|
191
|
+
runner.use(pipes.VerifyAccountMigration({
|
|
192
|
+
stateKey: "delegatorState",
|
|
193
|
+
addressKey: "tx.delegator"
|
|
194
|
+
}));
|
|
195
|
+
runner.use(pipes.VerifyDelegation({
|
|
196
|
+
type: "signature",
|
|
197
|
+
signerKey: "senderState",
|
|
198
|
+
delegatorKey: "delegatorState",
|
|
199
|
+
getRequirements: (ctx) => getTransferV3Requirements(ctx)
|
|
200
|
+
}));
|
|
201
|
+
runner.use(pipes.ExtractSigner({
|
|
202
|
+
signerKey: "msSignerStates",
|
|
203
|
+
delegatorKey: "delegatorStates"
|
|
204
|
+
}));
|
|
205
|
+
runner.use(pipes.VerifyDelegation({
|
|
206
|
+
type: "multisig",
|
|
207
|
+
signerKey: "msSignerStates",
|
|
208
|
+
delegatorKey: "delegatorStates",
|
|
209
|
+
delegationKey: "delegationStates",
|
|
210
|
+
getRequirements: (ctx, index, sig) => getTransferV3Requirements(ctx, index, sig)
|
|
211
|
+
}));
|
|
149
212
|
runner.use(pipes.VerifyTokenBalance({
|
|
150
213
|
ownerKey: "signerStates",
|
|
151
214
|
conditionKey: "inputs"
|
|
@@ -153,7 +216,7 @@ runner.use(pipes.VerifyTokenBalance({
|
|
|
153
216
|
runner.use(pipes.verifyTokenAccess({
|
|
154
217
|
statesKey: "tokenStates",
|
|
155
218
|
listFieldKey: "spenders",
|
|
156
|
-
accountKeys: ["
|
|
219
|
+
accountKeys: ["signerStates"],
|
|
157
220
|
errorMessage: "Account {address} is not allowed to transfer token {tokenAddress}"
|
|
158
221
|
}));
|
|
159
222
|
runner.use(pipes.ExtractState({
|
|
@@ -164,7 +227,7 @@ runner.use(pipes.ExtractState({
|
|
|
164
227
|
}));
|
|
165
228
|
runner.use(pipes.VerifyTransferrable({ assets: "assetStates" }));
|
|
166
229
|
runner.use(async (context, next) => {
|
|
167
|
-
const { inputs = [], assetStates = [], signerStates = [] } = context;
|
|
230
|
+
const { inputs = [], outputs = [], assetStates = [], signerStates = [], receiverStates = [] } = context;
|
|
168
231
|
for (const input of inputs) {
|
|
169
232
|
const { owner } = input;
|
|
170
233
|
const assetsList = getListField(input, "assets");
|
|
@@ -179,6 +242,23 @@ runner.use(async (context, next) => {
|
|
|
179
242
|
return next(err);
|
|
180
243
|
}
|
|
181
244
|
}
|
|
245
|
+
const pendingOwners = {};
|
|
246
|
+
for (const output of outputs) {
|
|
247
|
+
const { owner } = output;
|
|
248
|
+
const assetsList = getListField(output, "assets");
|
|
249
|
+
if (assetsList.length) {
|
|
250
|
+
const ownerState = receiverStates.find((s) => getRelatedAddresses(s).includes(owner)) || { address: owner };
|
|
251
|
+
for (const assetAddr of assetsList) pendingOwners[assetAddr] = ownerState.address;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
for (const output of outputs) {
|
|
255
|
+
const { owner } = output;
|
|
256
|
+
const assetsList = getListField(output, "assets");
|
|
257
|
+
if (assetsList.length) {
|
|
258
|
+
const err = await verifyAssetHierarchy(assetsList, (receiverStates.find((s) => getRelatedAddresses(s).includes(owner)) || { address: owner }).address, context, pendingOwners);
|
|
259
|
+
if (err) return next(err);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
182
262
|
return next();
|
|
183
263
|
});
|
|
184
264
|
runner.use(ensure_gas_default((context) => {
|
|
@@ -200,7 +280,7 @@ runner.use(ensure_gas_default((context) => {
|
|
|
200
280
|
runner.use(ensure_cost_default({ attachSenderChanges: true }));
|
|
201
281
|
runner.use(pipes.TakeStateSnapshot());
|
|
202
282
|
runner.use(async (context, next) => {
|
|
203
|
-
const { tx: tx$1, inputs = [], outputs = [], receivers = [], senderState, signerStates = [], senderChange, updateVaults, receiverStates = [], assetStates = [], statedb } = context;
|
|
283
|
+
const { tx: tx$1, inputs = [], outputs = [], receivers = [], senderState, signerStates = [], senderChange, updateVaults, receiverStates = [], assetStates = [], delegationState, delegationStates = [], statedb } = context;
|
|
204
284
|
const signerUpdates = {};
|
|
205
285
|
inputs.forEach((x) => {
|
|
206
286
|
const { owner } = x;
|
|
@@ -232,13 +312,20 @@ runner.use(async (context, next) => {
|
|
|
232
312
|
isAlsoSigner,
|
|
233
313
|
isAlsoReceiver
|
|
234
314
|
});
|
|
235
|
-
const
|
|
236
|
-
|
|
315
|
+
const existingSenderState = senderState ? await statedb.account.get(senderState.address, context) : null;
|
|
316
|
+
const [newSenderState, newSignerStates, newReceiverStates, newAssetStates, newDelegationState, newDelegationStates] = await Promise.all([
|
|
317
|
+
statedb.account.updateOrCreate(existingSenderState ?? null, account.updateOrCreate(existingSenderState ?? null, Object.assign({
|
|
237
318
|
address: sender,
|
|
238
319
|
nonce: tx$1.nonce,
|
|
239
|
-
pk: tx$1.pk
|
|
320
|
+
pk: tx$1.delegator ? senderState?.pk : tx$1.pk
|
|
240
321
|
}, signerUpdates[sender] || {}, receiverUpdates[sender] || {}, senderUpdatesLocal[sender] || {}), context), context),
|
|
241
|
-
Promise.all(signerStates.filter((x) => x.address !== sender).map((x) =>
|
|
322
|
+
Promise.all(signerStates.filter((x) => x.address !== sender).map(async (x) => {
|
|
323
|
+
const existing = await statedb.account.get(x.address, context);
|
|
324
|
+
return statedb.account.updateOrCreate(existing ?? null, account.updateOrCreate(existing ?? null, {
|
|
325
|
+
address: x.address,
|
|
326
|
+
...signerUpdates[x.address]
|
|
327
|
+
}, context), context);
|
|
328
|
+
})),
|
|
242
329
|
Promise.all(receivers.filter((x) => x !== sender).map((x) => {
|
|
243
330
|
const receiverState = receiverStates.find((s) => getRelatedAddresses(s).includes(x));
|
|
244
331
|
const owner = receiverState ? receiverState.address : x;
|
|
@@ -248,13 +335,17 @@ runner.use(async (context, next) => {
|
|
|
248
335
|
address: owner
|
|
249
336
|
}, context), context);
|
|
250
337
|
})),
|
|
251
|
-
Promise.all(assetStates.map((x) => statedb.asset.update(x.address, asset.update(x, assetUpdates[x.address], context), context)))
|
|
338
|
+
Promise.all(assetStates.map((x) => statedb.asset.update(x.address, asset.update(x, assetUpdates[x.address], context), context))),
|
|
339
|
+
context.isDelegationChanged && delegationState ? statedb.delegation.update(delegationState.address, delegation.update(delegationState, {}, context), context) : delegationState,
|
|
340
|
+
context.isDelegationChanged && delegationStates.length ? Promise.all(delegationStates.map((x) => statedb.delegation.update(x.address, delegation.update(x, {}, context), context))) : delegationStates
|
|
252
341
|
]);
|
|
253
342
|
if (updateVaults) await updateVaults();
|
|
254
343
|
context.senderState = newSenderState;
|
|
255
344
|
context.signerStates = isAlsoSigner ? newSignerStates.concat(newSenderState) : newSignerStates;
|
|
256
345
|
context.receiverStates = isAlsoReceiver ? newReceiverStates.concat(newSenderState) : newReceiverStates;
|
|
257
346
|
context.assetStates = newAssetStates;
|
|
347
|
+
context.delegationState = newDelegationState ?? void 0;
|
|
348
|
+
context.delegationStates = newDelegationStates;
|
|
258
349
|
debug("transfer-v3", {
|
|
259
350
|
inputs,
|
|
260
351
|
outputs
|
package/esm/tx-timer.d.mts
CHANGED
|
@@ -75,6 +75,11 @@ declare class TxTimer {
|
|
|
75
75
|
private queueState;
|
|
76
76
|
/** Record a named timestamp. */
|
|
77
77
|
mark(label: string): void;
|
|
78
|
+
/**
|
|
79
|
+
* Read-only view of all recorded mark intervals in order: `[label, durationMs][]`.
|
|
80
|
+
* Unlike `summary()`, repeated labels are preserved without overwriting earlier marks.
|
|
81
|
+
*/
|
|
82
|
+
durations(): ReadonlyArray<readonly [label: string, duration: number]>;
|
|
78
83
|
/**
|
|
79
84
|
* Compute per-stage durations (ms) between consecutive marks.
|
|
80
85
|
*
|
package/esm/tx-timer.mjs
CHANGED
|
@@ -64,6 +64,19 @@ var TxTimer = class {
|
|
|
64
64
|
this.marks.push([label, Date.now()]);
|
|
65
65
|
}
|
|
66
66
|
/**
|
|
67
|
+
* Read-only view of all recorded mark intervals in order: `[label, durationMs][]`.
|
|
68
|
+
* Unlike `summary()`, repeated labels are preserved without overwriting earlier marks.
|
|
69
|
+
*/
|
|
70
|
+
durations() {
|
|
71
|
+
const out = [];
|
|
72
|
+
let prev = this.origin;
|
|
73
|
+
for (const [label, time] of this.marks) {
|
|
74
|
+
out.push([label, time - prev]);
|
|
75
|
+
prev = time;
|
|
76
|
+
}
|
|
77
|
+
return out;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
67
80
|
* Compute per-stage durations (ms) between consecutive marks.
|
|
68
81
|
*
|
|
69
82
|
* Legacy schema: flat object `{ [label]: ms, total: ms }`. If the same label
|
package/esm/util.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import { BN, fromTokenToUnit } from "@ocap/util";
|
|
1
2
|
import { CustomError } from "@ocap/util/lib/error";
|
|
2
3
|
import { toStakeAddress } from "@arcblock/did-util";
|
|
3
|
-
import { BN, fromTokenToUnit } from "@ocap/util";
|
|
4
4
|
import { decodeAny } from "@ocap/message";
|
|
5
5
|
import { getListField } from "@ocap/util/lib/get-list-field";
|
|
6
6
|
import cloneDeep from "lodash/cloneDeep.js";
|
|
@@ -183,6 +183,7 @@ const ensureBlockReward = (rollupState, minReward, txStates) => {
|
|
|
183
183
|
if (actualFee.isNeg()) throw new CustomError("NEGATIVE_ACTUAL_FEE", "Got negative actualFee for tx, abort!");
|
|
184
184
|
if (actualFee.lt(defaults.reward)) actualFee = defaults.reward;
|
|
185
185
|
if (actualFee.lt(minTxFee)) actualFee = minTxFee;
|
|
186
|
+
if (actualFee.gt(maxFee)) actualFee = maxFee;
|
|
186
187
|
if (actualFee.lt(maxFee)) {
|
|
187
188
|
const refundFee = maxFee.sub(actualFee).toString(10);
|
|
188
189
|
changes.account.push({
|