fix: @meshsdk/contract -> MeshMarketplaceContract -> purchaseAsset-> TypeError: Cannot mix BigInt and other types, use explicit conversions #714
Conversation
Code Review — PR #714The fix correctly replaces the 🔴 CriticalShared
🟡 Medium — Introduced by this PR
// line 182 — ownerToReceiveLovelace
(Number(inputDatum.fields[1].int) * this.feePercentageBasisPoint) / 10000
// line 199 — sellerToReceiveLovelace
Number(inputDatum.fields[1].int) + Number(inputLovelace)
Additionally, at the MAX_SAFE_INTEGER boundary the float addition is off-by-1: Suggested fix — keep arithmetic in BigInt throughout: const priceBig = BigInt(inputDatum.fields[1].int);
const feeBig = BigInt(Math.round(this.feePercentageBasisPoint));
// ceiling division in BigInt (equivalent to Math.ceil)
let ownerToReceiveLovelace = (priceBig * feeBig + 9999n) / 10000n;
if (this.feePercentageBasisPoint > 0 && ownerToReceiveLovelace < 1000000n) {
ownerToReceiveLovelace = 1000000n;
}
// ...
const sellerToReceiveLovelace = priceBig + BigInt(inputLovelace);
// Only convert to string at the txOut quantity boundary
quantity: ownerToReceiveLovelace.toString()
quantity: sellerToReceiveLovelace.toString()🟡 Medium — Pre-existing in same functionFree listing (price = 0) forces buyer to pay spurious 1-ADA fee (line 184) When 1-ADA minimum floor has no on-chain enforcement — any buyer who builds the transaction directly (bypassing this SDK) can pay only the on-chain required amount (e.g., 5 lovelace for a 5,000-lovelace listing at 100bps) and the validator will accept it. The marketplace owner cannot rely on the 1-ADA minimum as a guaranteed invariant. Non-null assertion crash on missing lovelace entry (line 157) const inputLovelace = marketplaceUtxo.output.amount.find(
(a) => a.unit === "lovelace",
)!.quantity; // throws TypeError if no lovelace entryIf a UTxO (from a test fixture, custom No upper-bound validation on A value > 10000 (> 100%) is accepted silently. With Summary
The PR should replace |
Summary
@meshsdk/contract -> MeshMarketplaceContract -> purchaseAsset
I got this error when calling the purchaseAsset function. fixed when converting to number
TypeError: Cannot mix BigInt and other types, use explicit conversionsin compiled code https://unpkg.com/@meshsdk/contract@1.9.0-beta.73/dist/index.js,
let ownerToReceiveLovelace = inputDatum.fields[1].int * this.feePercentageBasisPoint / 1e4;Changed to
let ownerToReceiveLovelace = Number(inputDatum.fields[1].int) * this.feePercentageBasisPoint / 1e4;Affect components
@meshsdk/common@meshsdk/contract@meshsdk/core@meshsdk/core-csl@meshsdk/core-cst@meshsdk/hydra@meshsdk/provider@meshsdk/react@meshsdk/svelte@meshsdk/transaction@meshsdk/walletType of Change
Related Issues
Checklist
npm run test)npm run build)Additional Information