-
Notifications
You must be signed in to change notification settings - Fork 613
Expand file tree
/
Copy pathfees.ts
More file actions
208 lines (186 loc) · 7.07 KB
/
Copy pathfees.ts
File metadata and controls
208 lines (186 loc) · 7.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import {
type AccountWallet,
FeeJuicePaymentMethod,
FeeJuicePaymentMethodWithClaim,
type FeePaymentMethod,
NoFeePaymentMethod,
PrivateFeePaymentMethod,
PublicFeePaymentMethod,
type SendMethodOptions,
} from '@aztec/aztec.js';
import { AztecAddress, Fr, Gas, GasFees, GasSettings } from '@aztec/circuits.js';
import { parseBigint } from '@aztec/cli/utils';
import { type LogFn } from '@aztec/foundation/log';
import { Option } from 'commander';
import { type WalletDB } from '../../storage/wallet_db.js';
import { aliasedAddressParser } from './index.js';
export type CliFeeArgs = {
estimateGasOnly: boolean;
inclusionFee?: bigint;
gasLimits?: string;
payment?: string;
estimateGas?: boolean;
};
export interface IFeeOpts {
estimateOnly: boolean;
gasSettings: GasSettings;
toSendOpts(sender: AccountWallet): Promise<SendMethodOptions>;
}
export function printGasEstimates(
feeOpts: IFeeOpts,
gasEstimates: Pick<GasSettings, 'gasLimits' | 'teardownGasLimits'>,
log: LogFn,
) {
const inclusionFee = feeOpts.gasSettings.inclusionFee;
log(`Maximum total tx fee: ${getEstimatedCost(gasEstimates, inclusionFee, GasSettings.default().maxFeesPerGas)}`);
log(`Estimated total tx fee: ${getEstimatedCost(gasEstimates, inclusionFee, GasFees.default())}`);
log(`Estimated gas usage: ${formatGasEstimate(gasEstimates)}`);
}
function formatGasEstimate(estimate: Pick<GasSettings, 'gasLimits' | 'teardownGasLimits'>) {
return `da=${estimate.gasLimits.daGas},l2=${estimate.gasLimits.l2Gas},teardownDA=${estimate.teardownGasLimits.daGas},teardownL2=${estimate.teardownGasLimits.l2Gas}`;
}
function getEstimatedCost(
estimate: Pick<GasSettings, 'gasLimits' | 'teardownGasLimits'>,
inclusionFee: Fr,
fees: GasFees,
) {
return GasSettings.from({ ...GasSettings.default(), ...estimate, inclusionFee, maxFeesPerGas: fees })
.getFeeLimit()
.toBigInt();
}
export class FeeOpts implements IFeeOpts {
constructor(
public estimateOnly: boolean,
public gasSettings: GasSettings,
private paymentMethodFactory: (sender: AccountWallet) => Promise<FeePaymentMethod>,
private estimateGas: boolean,
) {}
async toSendOpts(sender: AccountWallet): Promise<SendMethodOptions> {
return {
estimateGas: this.estimateGas,
fee: {
gasSettings: this.gasSettings ?? GasSettings.default(),
paymentMethod: await this.paymentMethodFactory(sender),
},
};
}
static getOptions() {
return [
new Option('--inclusion-fee <value>', 'Inclusion fee to pay for the tx.').argParser(parseBigint),
new Option('--gas-limits <da=100,l2=100,teardownDA=10,teardownL2=10>', 'Gas limits for the tx.'),
new Option(
'--payment <method=name,asset=address,fpc=address,claimSecret=string,claimAmount=string,rebateSecret=string>',
'Fee payment method and arguments. Valid methods are: none, fee_juice, fpc-public, fpc-private.',
),
new Option('--no-estimate-gas', 'Whether to automatically estimate gas limits for the tx.'),
new Option('--estimate-gas-only', 'Only report gas estimation for the tx, do not send it.'),
];
}
static fromCli(args: CliFeeArgs, log: LogFn, db?: WalletDB) {
const estimateOnly = args.estimateGasOnly;
if (!args.inclusionFee && !args.gasLimits && !args.payment) {
return new NoFeeOpts(estimateOnly);
}
const gasSettings = GasSettings.from({
...GasSettings.default(),
...(args.gasLimits ? parseGasLimits(args.gasLimits) : {}),
...(args.inclusionFee ? { inclusionFee: new Fr(args.inclusionFee) } : {}),
maxFeesPerGas: GasFees.default(),
});
return new FeeOpts(
estimateOnly,
gasSettings,
args.payment ? parsePaymentMethod(args.payment, log, db) : () => Promise.resolve(new NoFeePaymentMethod()),
!!args.estimateGas,
);
}
}
class NoFeeOpts implements IFeeOpts {
constructor(public estimateOnly: boolean) {}
get gasSettings(): GasSettings {
return GasSettings.default();
}
toSendOpts(): Promise<SendMethodOptions> {
return Promise.resolve({});
}
}
function parsePaymentMethod(
payment: string,
log: LogFn,
db?: WalletDB,
): (sender: AccountWallet) => Promise<FeePaymentMethod> {
const parsed = payment.split(',').reduce((acc, item) => {
const [dimension, value] = item.split('=');
acc[dimension] = value ?? 1;
return acc;
}, {} as Record<string, string>);
const getFpcOpts = (parsed: Record<string, string>, db?: WalletDB) => {
if (!parsed.fpc) {
throw new Error('Missing "fpc" in payment option');
}
if (!parsed.asset) {
throw new Error('Missing "asset" in payment option');
}
const fpc = aliasedAddressParser('contracts', parsed.fpc, db);
return [AztecAddress.fromString(parsed.asset), fpc];
};
return async (sender: AccountWallet) => {
switch (parsed.method) {
case 'none':
log('Using no fee payment');
return new NoFeePaymentMethod();
case 'native':
if (parsed.claim || (parsed.claimSecret && parsed.claimAmount)) {
let claimAmount, claimSecret;
if (parsed.claim && db) {
({ amount: claimAmount, secret: claimSecret } = await db.popBridgedFeeJuice(sender.getAddress(), log));
} else {
({ claimAmount, claimSecret } = parsed);
}
log(`Using Fee Juice for fee payments with claim for ${claimAmount} tokens`);
return new FeeJuicePaymentMethodWithClaim(
sender.getAddress(),
BigInt(claimAmount),
Fr.fromString(claimSecret),
);
} else {
log(`Using Fee Juice for fee payment`);
return new FeeJuicePaymentMethod(sender.getAddress());
}
case 'fpc-public': {
const [asset, fpc] = getFpcOpts(parsed, db);
log(`Using public fee payment with asset ${asset} via paymaster ${fpc}`);
return new PublicFeePaymentMethod(asset, fpc, sender);
}
case 'fpc-private': {
const [asset, fpc] = getFpcOpts(parsed, db);
const rebateSecret = parsed.rebateSecret ? Fr.fromString(parsed.rebateSecret) : Fr.random();
log(
`Using private fee payment with asset ${asset} via paymaster ${fpc} with rebate secret ${rebateSecret.toString()}`,
);
return new PrivateFeePaymentMethod(asset, fpc, sender, rebateSecret);
}
case undefined:
throw new Error('Missing "method" in payment option');
default:
throw new Error(`Invalid fee payment method: ${payment}`);
}
};
}
function parseGasLimits(gasLimits: string): { gasLimits: Gas; teardownGasLimits: Gas } {
const parsed = gasLimits.split(',').reduce((acc, limit) => {
const [dimension, value] = limit.split('=');
acc[dimension] = parseInt(value, 10);
return acc;
}, {} as Record<string, number>);
const expected = ['da', 'l2', 'teardownDA', 'teardownL2'];
for (const dimension of expected) {
if (!(dimension in parsed)) {
throw new Error(`Missing gas limit for ${dimension}`);
}
}
return {
gasLimits: new Gas(parsed.da, parsed.l2),
teardownGasLimits: new Gas(parsed.teardownDA, parsed.teardownL2),
};
}