Skip to content

Commit be92f29

Browse files
committed
Fix divide by zero bug
1 parent 5527594 commit be92f29

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@magiceden-oss/runestone-lib",
3-
"version": "0.9.9-alpha",
3+
"version": "0.9.10-alpha",
44
"description": "",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",

src/indexer/updater.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,23 @@ export class RuneUpdater implements RuneBlockIndex {
160160
.filter(([_, vout]) => !isScriptPubKeyHexOpReturn(vout.scriptPubKey.hex))
161161
.map(([index]) => index);
162162

163-
if (amount === 0n) {
164-
// if amount is zero, divide balance between eligible outputs
165-
const amount = u128(u128(maybeBalance.amount) / u128(destinations.length));
166-
const remainder = u128(maybeBalance.amount) % u128(destinations.length);
167-
168-
for (const [i, output] of destinations.entries()) {
169-
allocate(i < remainder ? u128.checkedAddThrow(amount, u128(1)) : amount, output);
170-
}
171-
} else {
172-
// if amount is non-zero, distribute amount to eligible outputs
173-
for (const output of destinations) {
174-
allocate(amount < maybeBalance.amount ? amount : u128(maybeBalance.amount), output);
163+
if (destinations.length !== 0) {
164+
if (amount === 0n) {
165+
// if amount is zero, divide balance between eligible outputs
166+
const amount = u128(u128(maybeBalance.amount) / u128(destinations.length));
167+
const remainder = u128(maybeBalance.amount) % u128(destinations.length);
168+
169+
for (const [i, output] of destinations.entries()) {
170+
allocate(i < remainder ? u128.checkedAddThrow(amount, u128(1)) : amount, output);
171+
}
172+
} else {
173+
// if amount is non-zero, distribute amount to eligible outputs
174+
for (const output of destinations) {
175+
allocate(
176+
amount < maybeBalance.amount ? amount : u128(maybeBalance.amount),
177+
output
178+
);
179+
}
175180
}
176181
}
177182
} else {

0 commit comments

Comments
 (0)