MeshJS / mesh

An open-source library to advance Web3 development on Cardano
https://meshjs.dev
Apache License 2.0
207 stars 54 forks source link

MeshWallet + Transaction -> fails to build a TX #289

Closed BenElferink closed 2 weeks ago

BenElferink commented 2 weeks ago

My backend wallet is initiated like this:

const _provider = new BlockfrostProvider(API_KEYS['BLOCKFROST_API_KEY'])
const _wallet = new MeshWallet({
  networkId: 1,
  fetcher: _provider,
  submitter: _provider,
  key: {
    type: 'mnemonic',
    words: BLOODLINE_MINT_WALLET_MNEMONIC,
  },
})

And I'm trying to mint an NFT like this:

const _address = _wallet.getUsedAddresses()[0]
const _script = ForgeScript.withOneSignature(_address)
const _tx = new Transaction({ initiator: _wallet, verbose: true })

_tx.mintAsset(_script, mintPayload)

const _unsigTx = await _tx.build()
const _sigTx = await _wallet.signTx(_unsigTx)
const _txHash = await _wallet.submitTx(_sigTx)

However, building my TX fails with the following error:

Insufficient funds for lovelace
Remaining quantity 5000000

Error: txBuildResult error: 
    at CSLSerializer.serializeTxBody (file:///Users/ben/_Code/Ape-Nation/node_modules/@meshsdk/core-csl/dist/index.js:982:13)
    at MeshTxBuilder.complete (file:///Users/ben/_Code/Ape-Nation/node_modules/@meshsdk/transaction/dist/index.js:1352:33)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

When using verbose=true I can see that the txBodyJson has outputs, but no inputs. I then tried the wallet functions to get it's content, they all returned empty:

console.log(await _wallet.getUtxos())
// ---> []
console.log(await _wallet.getUsedUTxOs())
// ---> []
console.log(await _wallet.getAssets())
// ---> []
console.log(await _wallet.getLovelace())
// ---> 0
console.log(await _wallet.getCollateral())
// ---> []

I know for sure my wallet has lovelaces in it, at least 20+ ADA. Please help me resolve this issue, it happens even for simple TXs with the MeshWallet, like sendAssets or sendLovelace.

P.S. SDK versions:

{
    "@meshsdk/core": "^1.7.1",
    "@meshsdk/react": "^1.7.1",
}
jinglescode commented 2 weeks ago

looks like this wallet is not funded. to mint tokens, need lovelace to pay for fees. so lets check if the wallet has utxos.

const utxos = await blockchainProvider.fetchAddressUTxOs(_address);
console.log(utxos);

If you say that, there are lovelace in this wallet. For MeshWallet, we want to use the first address. This means you wallet is multi-address. i reckon you send lovelace (internal transfer) to the first address (_address).

BenElferink commented 2 weeks ago

looks like this wallet is not funded. to mint tokens, need lovelace to pay for fees. so lets check if the wallet has utxos.

const utxos = await blockchainProvider.fetchAddressUTxOs(_address);
console.log(utxos);

If you say that, there are lovelace in this wallet. For MeshWallet, we want to use the first address. This means you wallet is multi-address. i reckon you send lovelace (internal transfer) to the first address (_address).

Aha! I see. It seems the issue was in older versions the AppWallet was using the enterpriseAddressBech32 address to build TXs. And the new MeshWallet was using the baseAddressBech32 address to build TXs.

Transferring funds from enterpriseAddressBech32 to baseAddressBech32 did work, and I can build TXs now. Thank you.