Nem-sdk .js mosaic transferTransaction doesn't compute fee

I’m working with nem-sdk for .js , and I am attempting to follow the mosaicTransfer.js example (https://github.com/QuantumMechanics/NEM-sdk/blob/master/examples/nodejs/mosaicTransfer.js) on the mainnet. I’ve got 99% of the code sorted out, loaded my wallet. However, I notice when I send a transferTransaction, the fee is set to NaN. If I send, I get ‘FAILURE_INSUFFICIENT_FEE’.

Can someone illuminate why the fee defaults to NaN? I have a valid NS, a valid mosaic, valid attachments in the mosaicDefinitionMetaDataPair. I can set the fee to a value manually before sending. However, is there some other step that’s not in the example used to generate the fee? I tried using fees.calculateMosaics(), but that also returns NaN. Puzzled at this. The example code for mosaicTransfer.js mentions fees, but seems to indicate that this will be computed in the definition.

Also, if I set the fee manually to 100000, it sends just fine. But to improve my understanding of API usage, I’d like to use the algorithm provided in the library to compute network fees.
Thanks in advance.

Without seeing your code it’ll be very difficult to help you. Could you post it (formated plz)?

Check my example. Maybe it will hp you. Probably you have no set supply for mosaic definition.
https://pastebin.com/HqcDeaEC

My code is literally the example (https://github.com/QuantumMechanics/NEM-sdk/blob/master/examples/nodejs/mosaicTransfer.js) linked in the original question, instead of testnet I use mainnet. I also use my own wallet and a different recipient for receiving the mosaic as the examples are using some testnet address.

Thank you for posting this sample pawelm. I have a conceptual question. Why, in the examples I’ve seen thus far, do we provide two mosaicAttachments, one “nem:xem” with some value, and one “mymosaicgoeshere” with the amountwe want to send? I’m not understanding why we need the first attachment, “nem:xem”. Is that to carry the fee?

Hello,
No. It’s only example of sending two mosaics in one transaction. If you want send only one mosaic then remove nem:xem and all will work.
Fee is separate field as you noticed already.

Your first comment was absolutely correct: mosaicDefinitionMetaDataPair[fullMosaicName].supply = neededDefinition[fullMosaicName].properties[1].value;

Is the line that is missing from the example code. Took me a few reads to catch what I was missing (also not reading your initial comment completely). Thank you for the nudge.

Also different in your example vs. the example provided in the SDK, you added a line to set the chain.time. What is the consequence of not setting this? Does it mean we might not (1) have the right network time on the local system making the transaction, and that would cause (2) longer time to settle the transaction? (3) or a possible failure?

@truedataio nem-sdk use by default your local time which should be considered as bug. Network time is not guaranted to by synchronized with local server time (it has own synchronization mechanisms). That’s why for example testnet network time is delayed around 10-15 sec. And when you put timestamp in the future transaction will be rejected by node with error “timestamp to far in the future”. That’s why I’m setting time from node.