I am getting an issue when creating a Lucid transaction and calling payToContract I have created the datum and it is erroring on the line that converts this datum toHex

With the following error: Error: encoding/hex: invalid byte: s This happens every time I run the logic,

Code to build the datum

onst deadline = Date.now() + 6 * 60 * 60 * 1000; // in 6 hours
const datum = new RequestDatum(
  new RequestMetadaDatum(
    ownerAddress.payment_cred()!, // the owner whe can reclaim the request
    bigint.from_str(deadline.toFixed(0)), // deadline in posix time
    assetClassFromUnit('lovelace'), // the LP assets need to be ordered
  new SwapAction(SwapDirection.BTOA, bigint.from_str(minimumAmountTokenOut.toString()))

Transaction code

// Create a transaction that creates a swap request
const unsignedTx = await this._lucid
  .payToContract(this._lucid.utils.credentialToAddress(this._lucid.utils.scriptHashToCredential(REQUEST_SCRIPT_HASH)), toHex(datum.to_bytes()), {
    lovelace: BigInt(lockedCoins.to_str()),
    assetSubject: quantityOfToken,

This is an implementation using the Wingriders Blockfrost Adapter found here: https://github.com/WingRiders/dex-blockfrost-adapter/blob/main/examples/swap-fe-vanilla-js/src/swap.ts

Would appreciate any guidance here, thanks!

1 Answer 1


it's a bit hard to tell from where your error might come from, but your error simply means that you tried to use something that wasn't properly encoded in hex. Therefore, the error message arises that the character s was read, but it's indeed not a valid character of a hex-encoding.

This problem often occurs for me, when I have forgotten to convert the name of a token from UTF-8 to hex. This could also be the issue for you, i.e., you have a token starting with s in its name, and now you want to use the string encoded with utf-8 in, e.g., your datum. Instead, the token name should be converted to hex before using it as part of your datum.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.