Skip to content

Commit d669d71

Browse files
committed
Cost
1 parent 5156465 commit d669d71

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

explorer/src/lib/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export const MAX_BLOCK_COST = 11_000_000_000;
2+
export const COST_PER_BYTE = 12_000n;

explorer/src/lib/parser.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import {
44
Coin,
55
CoinSpend,
66
Constants,
7-
Output,
87
Program,
98
Puzzle,
109
sha256,
1110
SpendBundle,
1211
toHex,
1312
} from 'chia-wallet-sdk-wasm';
13+
import { COST_PER_BYTE } from './constants';
1414

1515
export interface ParsedSpendBundle {
1616
coinSpends: ParsedCoinSpend[];
@@ -22,7 +22,7 @@ export interface ParsedCoinSpend {
2222
coin: ParsedCoin;
2323
puzzleReveal: string;
2424
solution: string;
25-
runtimeCost: string;
25+
cost: string;
2626
conditions: ParsedCondition[];
2727
}
2828

@@ -79,8 +79,8 @@ interface DeserializedCoinSpend {
7979
coinSpend: CoinSpend;
8080
puzzle: Puzzle;
8181
solution: Program;
82-
output: Output;
8382
conditions: Program[];
83+
cost: bigint;
8484
}
8585

8686
export function parseSpendBundle(
@@ -116,6 +116,11 @@ export function parseSpendBundle(
116116
announcements.spentCoinIds.add(toHex(coinId));
117117
announcements.spentPuzzleHashes.add(toHex(puzzleHash));
118118

119+
let cost =
120+
output.cost +
121+
COST_PER_BYTE *
122+
BigInt(coinSpend.puzzleReveal.length + coinSpend.solution.length);
123+
119124
for (const condition of conditions) {
120125
const createCoinAnnouncement = condition.parseCreateCoinAnnouncement();
121126
if (createCoinAnnouncement) {
@@ -159,20 +164,35 @@ export function parseSpendBundle(
159164
new Coin(coinId, createCoin.puzzleHash, createCoin.amount).coinId(),
160165
),
161166
);
167+
cost += 1_800_000n;
162168
}
163169

164170
const assertConcurrentSpend = condition.parseAssertConcurrentSpend();
165171
if (assertConcurrentSpend) {
166172
announcements.assertedCoinIds.add(toHex(assertConcurrentSpend.coinId));
167173
}
174+
175+
const aggSig =
176+
condition.parseAggSigParent() ??
177+
condition.parseAggSigPuzzle() ??
178+
condition.parseAggSigAmount() ??
179+
condition.parseAggSigPuzzleAmount() ??
180+
condition.parseAggSigParentAmount() ??
181+
condition.parseAggSigParentPuzzle() ??
182+
condition.parseAggSigUnsafe() ??
183+
condition.parseAggSigMe();
184+
185+
if (aggSig) {
186+
cost += 1_200_000n;
187+
}
168188
}
169189

170190
deserializedCoinSpends.push({
171191
coinSpend,
172192
puzzle,
173193
solution,
174-
output,
175194
conditions,
195+
cost,
176196
});
177197
}
178198

@@ -186,7 +206,7 @@ export function parseSpendBundle(
186206
}
187207

188208
function parseCoinSpend(
189-
{ coinSpend, output, puzzle, conditions }: DeserializedCoinSpend,
209+
{ coinSpend, cost, puzzle, conditions }: DeserializedCoinSpend,
190210
ctx: BundleContext,
191211
): ParsedCoinSpend {
192212
const coinId = coinSpend.coin.coinId();
@@ -262,7 +282,7 @@ function parseCoinSpend(
262282
coin: parseCoin(coinSpend.coin),
263283
puzzleReveal: toHex(coinSpend.puzzleReveal),
264284
solution: toHex(coinSpend.solution),
265-
runtimeCost: output.cost.toString(),
285+
cost: cost.toLocaleString(),
266286
conditions: conditions.map((condition) =>
267287
parseCondition(coinSpend.coin, condition, ctx, isFastForwardable),
268288
),

explorer/src/pages/Tools.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ function SpendViewer({ spend }: SpendViewerProps) {
127127
<Truncated value={spend.solution} />
128128
</div>
129129
<div className='flex flex-col'>
130-
<div className='text-muted-foreground'>Runtime Cost</div>
131-
<div>{spend.runtimeCost}</div>
130+
<div className='text-muted-foreground'>Total Cost</div>
131+
<div>{spend.cost}</div>
132132
</div>
133133
</div>
134134
</div>

0 commit comments

Comments
 (0)