@ocap/tx-protocols 1.18.71 → 1.18.73
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/lib/pipes/ensure-cost.js +77 -68
- package/lib/protocols/account/migrate.js +1 -1
- package/package.json +14 -14
package/lib/pipes/ensure-cost.js
CHANGED
|
@@ -15,7 +15,11 @@ const { applyTokenUpdates, isGasStakeValid } = require('../util');
|
|
|
15
15
|
// - gas: charged for every tx for creating/updating states on the ledger
|
|
16
16
|
// - service fee: charged for creating tokens/assets/factories/rollups
|
|
17
17
|
// - protocol fee: charged for moving tokens across the bridge
|
|
18
|
-
module.exports = function CreateEnsureTxCostPipe({
|
|
18
|
+
module.exports = function CreateEnsureTxCostPipe({
|
|
19
|
+
attachSenderChanges = true,
|
|
20
|
+
throwOnInsufficientFund = true, // In some cases, we can tolerance insufficient fund, such as account migration
|
|
21
|
+
gasOnly = false,
|
|
22
|
+
} = {}) {
|
|
19
23
|
return async function EnsureTxCost(context, next) {
|
|
20
24
|
// TODO: we are using the sender as gas payer, this may change in future
|
|
21
25
|
const { config, statedb, txType, senderState, gasEstimate, gasVaultState, totalGas } = context;
|
|
@@ -63,92 +67,97 @@ module.exports = function CreateEnsureTxCostPipe({ attachSenderChanges = true, g
|
|
|
63
67
|
context.gasStake = gasStake;
|
|
64
68
|
debug('gasStake', gasStake, context.extra);
|
|
65
69
|
|
|
66
|
-
|
|
70
|
+
let isGasCharged = false;
|
|
67
71
|
if (senderState && !gasStake.valid) {
|
|
68
72
|
const expected = new BN(gasEstimate.payment || 0).add(txCost);
|
|
69
73
|
const actual = new BN(senderState.tokens[config.token.address] || 0);
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
'INSUFFICIENT_FUND',
|
|
74
|
-
`Insufficient fund to pay for tx cost from ${senderState.address}, expected ${expected}, got ${actual}`
|
|
75
|
-
)
|
|
76
|
-
);
|
|
77
|
-
}
|
|
74
|
+
// If we have someone with enough balance to pay for this tx
|
|
75
|
+
if (actual.gte(expected)) {
|
|
76
|
+
isGasCharged = true;
|
|
78
77
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
78
|
+
// to be merged into later pipe
|
|
79
|
+
context.senderUpdates = applyTokenUpdates(
|
|
80
|
+
[{ address: config.token.address, value: txCost.toString(10) }],
|
|
81
|
+
senderState,
|
|
82
|
+
'sub'
|
|
83
|
+
);
|
|
84
|
+
context.senderChange = {
|
|
85
|
+
address: senderState.address,
|
|
86
|
+
token: config.token.address,
|
|
87
|
+
delta: `-${txCost.toString(10)}`,
|
|
88
|
+
};
|
|
90
89
|
|
|
91
|
-
|
|
90
|
+
debug({ changes, senderUpdates: context.senderUpdates });
|
|
92
91
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
92
|
+
// to be called in later pipes
|
|
93
|
+
context.updateVaults = async function updateVaults() {
|
|
94
|
+
const [newFeeVaultState, newGasVaultState] = await Promise.all([
|
|
95
|
+
changes.fee
|
|
96
|
+
? statedb.account.update(
|
|
97
|
+
feeVaultState.address,
|
|
98
|
+
account.update(feeVaultState, applyTokenUpdates([changes.fee], feeVaultState, 'add'), context),
|
|
99
|
+
context
|
|
100
|
+
)
|
|
101
|
+
: Promise.resolve(feeVaultState),
|
|
102
|
+
changes.gas
|
|
103
|
+
? statedb.account.update(
|
|
104
|
+
gasVaultState.address,
|
|
105
|
+
account.update(gasVaultState, applyTokenUpdates([changes.gas], gasVaultState, 'add'), context),
|
|
106
|
+
context
|
|
107
|
+
)
|
|
108
|
+
: Promise.resolve(gasVaultState),
|
|
109
|
+
]);
|
|
111
110
|
|
|
112
|
-
|
|
113
|
-
|
|
111
|
+
context.feeVaultState = newFeeVaultState;
|
|
112
|
+
context.gasVaultState = newGasVaultState;
|
|
114
113
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
context.updatedAccounts.push({
|
|
118
|
-
address: feeVaultState.address,
|
|
119
|
-
token: config.token.address,
|
|
120
|
-
delta: changes.fee.value,
|
|
121
|
-
action: 'fee',
|
|
122
|
-
});
|
|
123
|
-
if (attachSenderChanges) {
|
|
114
|
+
context.updatedAccounts = context.updatedAccounts || [];
|
|
115
|
+
if (changes.fee) {
|
|
124
116
|
context.updatedAccounts.push({
|
|
125
|
-
address:
|
|
117
|
+
address: feeVaultState.address,
|
|
126
118
|
token: config.token.address,
|
|
127
|
-
delta:
|
|
119
|
+
delta: changes.fee.value,
|
|
128
120
|
action: 'fee',
|
|
129
121
|
});
|
|
122
|
+
if (attachSenderChanges) {
|
|
123
|
+
context.updatedAccounts.push({
|
|
124
|
+
address: senderState.address,
|
|
125
|
+
token: config.token.address,
|
|
126
|
+
delta: `-${changes.fee.value}`,
|
|
127
|
+
action: 'fee',
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
130
|
}
|
|
131
|
-
|
|
132
|
-
if (changes.gas) {
|
|
133
|
-
context.updatedAccounts.push({
|
|
134
|
-
address: gasVaultState.address,
|
|
135
|
-
token: config.token.address,
|
|
136
|
-
delta: changes.gas.value,
|
|
137
|
-
action: 'gas',
|
|
138
|
-
});
|
|
139
|
-
if (attachSenderChanges) {
|
|
131
|
+
if (changes.gas) {
|
|
140
132
|
context.updatedAccounts.push({
|
|
141
|
-
address:
|
|
133
|
+
address: gasVaultState.address,
|
|
142
134
|
token: config.token.address,
|
|
143
|
-
delta:
|
|
135
|
+
delta: changes.gas.value,
|
|
144
136
|
action: 'gas',
|
|
145
137
|
});
|
|
138
|
+
if (attachSenderChanges) {
|
|
139
|
+
context.updatedAccounts.push({
|
|
140
|
+
address: senderState.address,
|
|
141
|
+
token: config.token.address,
|
|
142
|
+
delta: `-${changes.gas.value}`,
|
|
143
|
+
action: 'gas',
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
146
|
}
|
|
147
|
-
}
|
|
148
147
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
148
|
+
context.gasPaid = true;
|
|
149
|
+
};
|
|
150
|
+
} else if (throwOnInsufficientFund) {
|
|
151
|
+
return next(
|
|
152
|
+
new Error(
|
|
153
|
+
'INSUFFICIENT_FUND',
|
|
154
|
+
`Insufficient fund to pay for tx cost from ${senderState.address}, expected ${expected}, got ${actual}`
|
|
155
|
+
)
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (!isGasCharged) {
|
|
152
161
|
context.senderUpdates = {};
|
|
153
162
|
context.senderChange = null;
|
|
154
163
|
context.updateVaults = noop;
|
|
@@ -48,7 +48,7 @@ runner.use(
|
|
|
48
48
|
return { create: 1, update: getRelatedAddresses(context.senderState).length, payment: 0 };
|
|
49
49
|
})
|
|
50
50
|
);
|
|
51
|
-
runner.use(EnsureTxCost({ attachSenderChanges: true }));
|
|
51
|
+
runner.use(EnsureTxCost({ attachSenderChanges: true, throwOnInsufficientFund: false }));
|
|
52
52
|
|
|
53
53
|
// Create account state, and update old accounts
|
|
54
54
|
runner.use(
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "1.18.
|
|
6
|
+
"version": "1.18.73",
|
|
7
7
|
"description": "Predefined tx pipeline sets to execute certain type of transactions",
|
|
8
8
|
"main": "lib/index.js",
|
|
9
9
|
"files": [
|
|
@@ -21,18 +21,18 @@
|
|
|
21
21
|
"author": "wangshijun <wangshijun2010@gmail.com> (http://github.com/wangshijun)",
|
|
22
22
|
"license": "MIT",
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@arcblock/did": "1.18.
|
|
25
|
-
"@arcblock/did-util": "1.18.
|
|
26
|
-
"@arcblock/jwt": "1.18.
|
|
27
|
-
"@arcblock/validator": "1.18.
|
|
28
|
-
"@ocap/asset": "1.18.
|
|
29
|
-
"@ocap/mcrypto": "1.18.
|
|
30
|
-
"@ocap/merkle-tree": "1.18.
|
|
31
|
-
"@ocap/message": "1.18.
|
|
32
|
-
"@ocap/state": "1.18.
|
|
33
|
-
"@ocap/tx-pipeline": "1.18.
|
|
34
|
-
"@ocap/util": "1.18.
|
|
35
|
-
"@ocap/wallet": "1.18.
|
|
24
|
+
"@arcblock/did": "1.18.73",
|
|
25
|
+
"@arcblock/did-util": "1.18.73",
|
|
26
|
+
"@arcblock/jwt": "1.18.73",
|
|
27
|
+
"@arcblock/validator": "1.18.73",
|
|
28
|
+
"@ocap/asset": "1.18.73",
|
|
29
|
+
"@ocap/mcrypto": "1.18.73",
|
|
30
|
+
"@ocap/merkle-tree": "1.18.73",
|
|
31
|
+
"@ocap/message": "1.18.73",
|
|
32
|
+
"@ocap/state": "1.18.73",
|
|
33
|
+
"@ocap/tx-pipeline": "1.18.73",
|
|
34
|
+
"@ocap/util": "1.18.73",
|
|
35
|
+
"@ocap/wallet": "1.18.73",
|
|
36
36
|
"debug": "^4.3.4",
|
|
37
37
|
"deep-diff": "^1.0.2",
|
|
38
38
|
"empty-value": "^1.0.1",
|
|
@@ -47,5 +47,5 @@
|
|
|
47
47
|
"jest": "^27.5.1",
|
|
48
48
|
"start-server-and-test": "^1.14.0"
|
|
49
49
|
},
|
|
50
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "d33cc33687903ca7d473547c04fe6c6363c60609"
|
|
51
51
|
}
|