/
provider.ts
207 lines (186 loc) · 6 KB
/
provider.ts
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
import {
OperationContentsAndResult,
RPCRunOperationParam,
RPCSimulateOperationParam,
RpcClientInterface,
} from '@taquito/rpc';
import { Context } from './context';
import { ForgedBytes, ParamsWithKind, RPCOperation, isOpRequireReveal } from './operations/types';
import {
InvalidEstimateValueError,
TezosOperationError,
TezosPreapplyFailureError,
flattenErrors,
} from './operations/errors';
import {
createOriginationOperation,
createRegisterGlobalConstantOperation,
createSetDelegateOperation,
createTransferOperation,
createTransferTicketOperation,
createIncreasePaidStorageOperation,
createSmartRollupAddMessagesOperation,
createSmartRollupOriginateOperation,
createSmartRollupExecuteOutboxMessageOperation,
} from './contract/prepare';
import { OpKind } from '@taquito/rpc';
import { InvalidOperationKindError } from '@taquito/utils';
import { PreparedOperation } from './prepare';
import { Estimate } from './estimate';
export abstract class Provider {
get rpc(): RpcClientInterface {
return this.context.rpc;
}
get signer() {
return this.context.signer;
}
constructor(protected context: Context) {}
protected async forge({ opOb: { branch, contents, protocol }, counter }: PreparedOperation) {
const forgedBytes = await this.context.forger.forge({ branch, contents });
return {
opbytes: forgedBytes,
opOb: {
branch,
contents,
protocol,
},
counter,
};
}
protected async estimate<T extends { fee?: number; gasLimit?: number; storageLimit?: number }>(
{ fee, gasLimit, storageLimit, ...rest }: T,
estimator: (param: T) => Promise<Estimate>
) {
let calculatedFee = fee;
let calculatedGas = gasLimit;
let calculatedStorage = storageLimit;
if (calculatedFee && calculatedFee % 1 !== 0) {
throw new InvalidEstimateValueError(`Fee value must not be a decimal: ${calculatedFee}`);
}
if (calculatedGas && calculatedGas % 1 !== 0) {
throw new InvalidEstimateValueError(
`Gas Limit value must not be a decimal: ${calculatedGas}`
);
}
if (calculatedStorage && calculatedStorage % 1 !== 0) {
throw new InvalidEstimateValueError(
`Storage Limit value must not be a decimal: ${calculatedStorage}`
);
}
if (fee === undefined || gasLimit === undefined || storageLimit === undefined) {
const estimation = await estimator({ fee, gasLimit, storageLimit, ...(rest as any) });
calculatedFee ??= estimation.suggestedFeeMutez;
calculatedGas ??= estimation.gasLimit;
calculatedStorage ??= estimation.storageLimit;
}
return {
fee: calculatedFee,
gasLimit: calculatedGas,
storageLimit: calculatedStorage,
};
}
async getRPCOp(param: ParamsWithKind) {
switch (param.kind) {
case OpKind.TRANSACTION:
return createTransferOperation({
...param,
});
case OpKind.ORIGINATION:
return createOriginationOperation(
await this.context.parser.prepareCodeOrigination({
...param,
})
);
case OpKind.DELEGATION:
return createSetDelegateOperation({
...param,
});
case OpKind.REGISTER_GLOBAL_CONSTANT:
return createRegisterGlobalConstantOperation({
...param,
});
case OpKind.INCREASE_PAID_STORAGE:
return createIncreasePaidStorageOperation({
...param,
});
case OpKind.TRANSFER_TICKET:
return createTransferTicketOperation({
...param,
});
case OpKind.SMART_ROLLUP_ADD_MESSAGES:
return createSmartRollupAddMessagesOperation({
...param,
});
case OpKind.SMART_ROLLUP_ORIGINATE:
return createSmartRollupOriginateOperation({
...param,
});
case OpKind.SMART_ROLLUP_EXECUTE_OUTBOX_MESSAGE:
return createSmartRollupExecuteOutboxMessageOperation({
...param,
});
default:
throw new InvalidOperationKindError((param as any).kind);
}
}
protected async runOperation(op: RPCRunOperationParam) {
return {
opResponse: await this.rpc.runOperation(op),
op,
context: this.context.clone(),
};
}
protected async simulate(op: RPCSimulateOperationParam) {
return {
opResponse: await this.rpc.simulateOperation(op),
op,
context: this.context.clone(),
};
}
protected async isRevealOpNeeded(op: RPCOperation[] | ParamsWithKind[], pkh: string) {
return !(await this.isAccountRevealRequired(pkh)) || !this.isRevealRequiredForOpType(op)
? false
: true;
}
protected async isAccountRevealRequired(publicKeyHash: string) {
return !(await this.context.readProvider.isAccountRevealed(publicKeyHash, 'head'));
}
protected isRevealRequiredForOpType(op: RPCOperation[] | ParamsWithKind[]) {
let opRequireReveal = false;
for (const operation of op) {
if (isOpRequireReveal(operation)) {
opRequireReveal = true;
}
}
return opRequireReveal;
}
protected async signAndInject(forgedBytes: ForgedBytes) {
const signed = await this.signer.sign(forgedBytes.opbytes, new Uint8Array([3]));
forgedBytes.opbytes = signed.sbytes;
forgedBytes.opOb.signature = signed.prefixSig;
const opResponse: OperationContentsAndResult[] = [];
const results = await this.rpc.preapplyOperations([forgedBytes.opOb]);
if (!Array.isArray(results)) {
throw new TezosPreapplyFailureError(results);
}
for (let i = 0; i < results.length; i++) {
for (let j = 0; j < results[i].contents.length; j++) {
opResponse.push(results[i].contents[j]);
}
}
const errors = flattenErrors(results);
if (errors.length) {
throw new TezosOperationError(
errors,
'Error occurred during validation simulation of operation',
opResponse
);
}
return {
hash: await this.context.injector.inject(forgedBytes.opbytes),
forgedBytes,
opResponse,
context: this.context.clone(),
};
}
}