Skip to content

Bug: TypeError: Cannot mix BigInt and other types in MeshMarketplaceContract.purchaseAsset() (owner/seller lovelace math) #712

@rayan25nov

Description

@rayan25nov

Summary

When calling MeshMarketplaceContract.purchaseAsset() with on-chain datum values that are BigInt, the contract code mixes BigInt with Number which throws TypeError: Cannot mix BigInt and other types, use explicit conversions in Node.js. I hit this in @meshsdk/contract@1.9.0-beta.72 and resolved it locally by converting the arithmetic to use BigInt. Please consider an upstream fix that consistently uses BigInt arithmetic (or explicitly converts types) and add tests for datum arithmetic.

Steps to reproduce the bug

Create a listing where the price is stored in the datum (the datum integer fields end up parsed as BigInt by the SDK).

Call contract.getUtxoByTxHash(txHash) to obtain the UTXO/datum.

Pass the returned UTXO into contract.purchaseAsset(utxo) (from Node.js environment).

Observe the runtime error.

(If this relates to a particular transaction please include the TX JSON / tx hash.)

Actual Result

What is the reproducible outcome?

A runtime TypeError is thrown. Example stack trace from my run:

TypeError: Cannot mix BigInt and other types, use explicit conversions
at MeshMarketplaceContract.purchaseAsset (.../node_modules/@meshsdk/contract/dist/index.cjs:52912:59)
...
/home/.../node_modules/@meshsdk/contract/dist/index.cjs:52926
const sellerToReceiveLovelace = inputDatum.fields[1].int + Number(inputLovelace);
^
TypeError: Cannot mix BigInt and other types, use explicit conversions

Problematic code patterns found in the compiled file:

// problematic lines
let ownerToReceiveLovelace = inputDatum.fields[1].int * this.feePercentageBasisPoint / 1e4;
const sellerToReceiveLovelace = inputDatum.fields[1].int + Number(inputLovelace);

Expected Result

A clear and concise description of what you expected to happen.

The purchase flow should complete without type errors. Numeric arithmetic with on-chain amounts (lovelace / datum ints) should be handled consistently (no mixing BigInt and Number), with correct fee/seller splitting and no crash.

SDK version

1.9.0-beta.72

Environment type

  • Node.js
  • Browser
  • Browser Extension
  • Other

Environment details

@meshsdk/contract: 1.9.0-beta.72 (from the stack trace)

Node.js: v22.17.0

Context: preview/testnet usage of MeshMarketplaceContract.purchaseAsset() (called with UTXO/datum obtained from the chain)

Fix I applied locally (for reference)

I applied a minimal BigInt-based fix in the compiled file so arithmetic is all BigInt:

  • let ownerToReceiveLovelace = inputDatum.fields[1].int * this.feePercentageBasisPoint / 1e4;
  • let ownerToReceiveLovelace = (BigInt(inputDatum.fields[1].int) * BigInt(this.feePercentageBasisPoint)) / 10000n;
  • const sellerToReceiveLovelace = inputDatum.fields[1].int + Number(inputLovelace);
  • const sellerToReceiveLovelace = inputDatum.fields[1].int + (inputLovelace);

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions