@msafe/sui3-sdk 0.0.75 → 0.0.76
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/dist/index.cjs +22 -55
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +14 -10
- package/dist/index.d.ts +14 -10
- package/dist/index.js +22 -55
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/backend/BackendImpl.ts +3 -2
- package/src/backend/interface.ts +1 -1
- package/src/simulate/simulator.ts +19 -61
- package/src/types/msafe.ts +12 -4
|
@@ -34,17 +34,6 @@ export const DEF_GAS_PRICE_OPTION: GasPriceOption = {
|
|
|
34
34
|
},
|
|
35
35
|
};
|
|
36
36
|
|
|
37
|
-
// Default to 120% of the reference gas budget
|
|
38
|
-
export const DEF_GAS_BUDGET_NUM = 120n;
|
|
39
|
-
export const DEF_GAS_BUDGET_DEN = 100n;
|
|
40
|
-
|
|
41
|
-
export const DEF_GAS_BUDGET_OPTION: GasBudgetOption = {
|
|
42
|
-
multiplier: {
|
|
43
|
-
numerator: DEF_GAS_BUDGET_NUM,
|
|
44
|
-
denominator: DEF_GAS_BUDGET_DEN,
|
|
45
|
-
},
|
|
46
|
-
};
|
|
47
|
-
|
|
48
37
|
export class Simulator {
|
|
49
38
|
constructor(private globals: MSafeGlobals) {}
|
|
50
39
|
|
|
@@ -54,7 +43,6 @@ export class Simulator {
|
|
|
54
43
|
sender: string;
|
|
55
44
|
},
|
|
56
45
|
gasOption: GasPriceOption = DEF_GAS_PRICE_OPTION,
|
|
57
|
-
budgetOption: GasBudgetOption = DEF_GAS_BUDGET_OPTION,
|
|
58
46
|
): Promise<SimulationResult> {
|
|
59
47
|
const tx = this.copyTransactionBlock(input.txb);
|
|
60
48
|
|
|
@@ -62,49 +50,31 @@ export class Simulator {
|
|
|
62
50
|
|
|
63
51
|
const gasPrice = await this.getGasPrice(gasOption);
|
|
64
52
|
|
|
65
|
-
|
|
66
|
-
// tx.setGasPrice(gasPrice);
|
|
67
|
-
|
|
68
|
-
try {
|
|
69
|
-
const inspectResult = await suiClient.dryRunTransactionBlock({
|
|
70
|
-
transactionBlock: await tx.build({ client: suiClient }),
|
|
71
|
-
});
|
|
72
|
-
const { gasUsed } = inspectResult.effects;
|
|
73
|
-
const gasBudget = this.toGasBudget(gasUsed, gasPrice);
|
|
74
|
-
const success = inspectResult.effects.status.status === 'success';
|
|
75
|
-
let error: string | undefined;
|
|
53
|
+
tx.setGasPrice(gasPrice);
|
|
76
54
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
55
|
+
const inspectResult = await suiClient.dryRunTransactionBlock({
|
|
56
|
+
transactionBlock: await tx.build({ client: suiClient }),
|
|
57
|
+
});
|
|
58
|
+
const success = inspectResult.effects.status.status === 'success';
|
|
80
59
|
|
|
60
|
+
if (!success) {
|
|
81
61
|
return {
|
|
82
62
|
success,
|
|
83
63
|
gasPrice,
|
|
84
|
-
gasObject: inspectResult.effects.gasObject.reference,
|
|
85
|
-
gasUsed,
|
|
86
|
-
gasBudget: this.applyBudgetOption(gasBudget, budgetOption),
|
|
87
|
-
simulationError: error,
|
|
88
64
|
response: inspectResult,
|
|
65
|
+
simulationError: inspectResult.effects.status.error!,
|
|
89
66
|
};
|
|
90
|
-
} catch (err) {
|
|
91
|
-
if (err instanceof Error) {
|
|
92
|
-
return {
|
|
93
|
-
success: false,
|
|
94
|
-
gasObject: { digest: '', objectId: '', version: '' },
|
|
95
|
-
gasPrice: 0n,
|
|
96
|
-
gasBudget: 0n,
|
|
97
|
-
gasUsed: {
|
|
98
|
-
computationCost: '',
|
|
99
|
-
nonRefundableStorageFee: '',
|
|
100
|
-
storageCost: '',
|
|
101
|
-
storageRebate: '',
|
|
102
|
-
},
|
|
103
|
-
simulationError: err.message,
|
|
104
|
-
};
|
|
105
|
-
}
|
|
106
|
-
throw err;
|
|
107
67
|
}
|
|
68
|
+
const gasBudget = this.toGasBudget(inspectResult.effects.gasUsed, gasPrice);
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
success,
|
|
72
|
+
gasPrice,
|
|
73
|
+
gasObject: inspectResult.effects.gasObject.reference,
|
|
74
|
+
gasUsed: inspectResult.effects.gasUsed,
|
|
75
|
+
gasBudget,
|
|
76
|
+
response: inspectResult,
|
|
77
|
+
};
|
|
108
78
|
}
|
|
109
79
|
|
|
110
80
|
private async getGasPrice(option: GasPriceOption) {
|
|
@@ -118,14 +88,13 @@ export class Simulator {
|
|
|
118
88
|
}
|
|
119
89
|
|
|
120
90
|
private toGasBudget(gasUsed: GasCostSummary, gasPrice: bigint) {
|
|
121
|
-
const { computationCost, storageCost } = gasUsed;
|
|
91
|
+
const { computationCost, storageCost, storageRebate } = gasUsed;
|
|
122
92
|
const safeOverhead = GAS_SAFE_OVERHEAD * gasPrice;
|
|
123
93
|
|
|
124
94
|
const baseComputationCostWithOverhead = BigInt(computationCost) + safeOverhead;
|
|
125
95
|
|
|
126
96
|
// do not subtract rebate for now, because it may cause normally tx gas to be lower than empty tx gas
|
|
127
|
-
|
|
128
|
-
const gasBudget = baseComputationCostWithOverhead + BigInt(storageCost);
|
|
97
|
+
const gasBudget = baseComputationCostWithOverhead + BigInt(storageCost) - BigInt(storageRebate);
|
|
129
98
|
|
|
130
99
|
// Set the budget to max(computation, computation + storage - rebate)
|
|
131
100
|
return gasBudget > baseComputationCostWithOverhead ? gasBudget : baseComputationCostWithOverhead;
|
|
@@ -150,17 +119,6 @@ export class Simulator {
|
|
|
150
119
|
throw new Error('Sanity: Unknown gas price option');
|
|
151
120
|
}
|
|
152
121
|
|
|
153
|
-
private applyBudgetOption(val: bigint, option: GasBudgetOption) {
|
|
154
|
-
if (this.isFixedValue(option)) {
|
|
155
|
-
return option.fixedVal;
|
|
156
|
-
}
|
|
157
|
-
if (this.isMultiplier(option)) {
|
|
158
|
-
const { multiplier } = option;
|
|
159
|
-
return (val * multiplier.numerator) / multiplier.denominator;
|
|
160
|
-
}
|
|
161
|
-
throw new Error('Sanity: Unknown gas budget option');
|
|
162
|
-
}
|
|
163
|
-
|
|
164
122
|
private copyTransactionBlock(tx: TransactionBlock): TransactionBlock {
|
|
165
123
|
return TransactionBlock.from(tx.serialize());
|
|
166
124
|
}
|
package/src/types/msafe.ts
CHANGED
|
@@ -80,14 +80,22 @@ export interface ProposeIntention {
|
|
|
80
80
|
signature: SerializedSignature;
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
-
export
|
|
84
|
-
|
|
83
|
+
export type SimulationResult = SimulationSuccessResult | SimulationFailedResult;
|
|
84
|
+
|
|
85
|
+
export interface SimulationSuccessResult {
|
|
86
|
+
success: true;
|
|
85
87
|
gasPrice: bigint;
|
|
86
88
|
gasUsed: GasCostSummary;
|
|
87
89
|
gasBudget: bigint;
|
|
88
90
|
gasObject: SuiObjectRef;
|
|
89
|
-
|
|
90
|
-
|
|
91
|
+
response: DryRunTransactionBlockResponse;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export interface SimulationFailedResult {
|
|
95
|
+
success: false;
|
|
96
|
+
gasPrice: bigint;
|
|
97
|
+
response: DryRunTransactionBlockResponse;
|
|
98
|
+
simulationError: string;
|
|
91
99
|
}
|
|
92
100
|
|
|
93
101
|
export interface SimulationOption extends IGasOption {
|