using:
express.js
nem-sdk
I can make mosaic transactions
they are listed in nem nano wallet
BUT if I click on explorer in nem nano wallet balance is still the same
how can I check if mosaics where properly transmitted?
plz check my code here
// NEM
import NEM from “nem-sdk”
const nem = NEM.default
import accounts from “…/accounts/accounts.mjs”
// start SERVER
// node --experimental-modules transferMosaic.mjs
import express from “express”
const app = express()
app.use(express.json())
app.post("/mosaic/transfer", async (req, res) => {
try {
// parse body to get transaction parameters
const { mosaicName,
mosaicTransferAmount,
message,
recieverAdresss,
sender } = req.body
// CONSTANTS
const { alice, peterlustig } = accounts
const namespaceName = "cryptopay"
//const mosaicName = "crystal"
//const mosaicTransferAmount = 5
// const recieverAdresss = alice.address
const xemTransferAmount = 0
let privateKey;
if (sender === "peterlustig") {
privateKey = peterlustig.privateKey
} else {
throw Error("must implement sender.privateKey")
}
// signer
var common = nem.model.objects.create("common")("", privateKey);
console.log("common")
console.log(common)
// {
// password: '',
// privateKey: 'b580d.....0a9c',
// isHW: false
// }
// ?BUG: when running local node mosaic transfers will not be properly reflected in nanowallet
// remote node is working
// const url = "http://127.0.0.1"
// const port = "7890"
// var endpoint = nem.model.objects.create("endpoint")(url, port);
const endpoint = nem.model.objects.create("endpoint")(nem.model.nodes.defaultTestnet, nem.model.nodes.defaultPort);
console.log("endpoint ---------------------------------------------")
console.log(endpoint)
// <>
// mosaic -> transferTransaction
// const namespaceName = "cryptopay"
// const mosaicName = "crystal"
//const mosaicTransferAmount = 5
const mosaicAttachment = nem.model.objects.create("mosaicAttachment")(namespaceName, mosaicName, mosaicTransferAmount);
console.log("mosaicAttachment ---------------------------------------------")
console.log(mosaicAttachment)
// {
// mosaicId: { namespaceId: 'cryptopay', name: 'crystal' },
// quantity: 5
// }
// transferTransaction
//const recieverAdresss = alice.address
//const xemTransferAmount = 0
//const message = "feel the crystal clear water"
const transferTransaction = nem.model.objects.create("transferTransaction")(recieverAdresss, xemTransferAmount, message);
console.log("transferTransaction ---------------------------------------------")
console.log(transferTransaction)
// { amount: 0,
// recipient: 'TBKRXIJC4YNIOWQLZFVQNP2FFL5GN4Q2OTHDG6B7',
// recipientPublicKey: '',
// isMultisig: false,
// multisigAccount: '',
// message: 'feel the crystal clear water',
// messageType: 1,
// mosaics: []
// }
//console.log <>
transferTransaction.mosaics.push(mosaicAttachment);
const mosaicDefinitions = await nem.com.requests.namespace.mosaicDefinitions(endpoint, mosaicAttachment.mosaicId.namespaceId)
console.log("mosaicDefinitions ---------------------------------------------")
console.log(mosaicDefinitions)
// {
// data:
// [{ meta: [Object], mosaic: [Object] },
// { meta: [Object], mosaic: [Object] }]
// }
const definitions = nem.utils.helpers.searchMosaicDefinitionArray(mosaicDefinitions.data, [mosaicName]);
console.log("definitions ---------------------------------------------")
console.log(definitions)
// {
// 'cryptopay:crystal':
// {
// creator: 'f1fcecfebb32e4230e401ca9d10c3f804d92079847272acaa0b04b017d060796',
// description: 'image the sole beauty of the pure function',
// id: { namespaceId: 'cryptopay', name: 'crystal' },
// properties: [[Object], [Object], [Object], [Object]],
// levy: { }
// }
// }
const fullMosaicName = nem.utils.format.mosaicIdToName(mosaicAttachment.mosaicId);
console.log("fullMosaicName ---------------------------------------------")
console.log(fullMosaicName)
// cryptopay:crystal
if (undefined === definitions[fullMosaicName]) return console.error("ERROR: Mosaic not found !");
const mosaicDefinitionMetaDataPair = nem.model.objects.get("mosaicDefinitionMetaDataPair");
console.log("mosaicDefinitionMetaDataPair ---------------------------------------------")
console.log(mosaicDefinitionMetaDataPair)
// {
// 'nem:xem':
// {
// mosaicDefinition:
// {
// creator: '3e82e1c1e4a75adaa3cba8c101c3cd31d9817a2eb966eb3b511fb2ed45b8e262',
// description: 'reserved xem mosaic',
// id: [Object],
// properties: [Array],
// levy: { }
// }
// },
// 'cryptopay:crystal':
// {
// mosaicDefinition:
// {
// creator: 'f1fcecfebb32e4230e401ca9d10c3f804d92079847272acaa0b04b017d060796',
// description: 'image the sole beauty of the pure function',
// id: [Object],
// properties: [Array],
// levy: { }
// }
// }
// }
const mosaicDefinition_cryptopay_crystal = {
"creator": "f1fcecfebb32e4230e401ca9d10c3f804d92079847272acaa0b04b017d060796",
"description": "image the sole beauty of the pure function",
"id": { "namespaceId": "cryptopay", "name": "crystal" },
"properties": [
{
"name": "divisibility",
"value": "0"
},
{
"name": "initialSupply",
"value": "10000"
},
{
"name": "supplyMutable",
"value": "true"
},
{
"name": "transferable",
"value": "true"
}
],
"levy": {}
}
mosaicDefinitionMetaDataPair[fullMosaicName] = {}; // fullMosaicName == cryptopay:crystal
mosaicDefinitionMetaDataPair[fullMosaicName].mosaicDefinition = mosaicDefinition_cryptopay_crystal;
var transactionEntity = await nem.model.transactions.prepare("mosaicTransferTransaction")(common, transferTransaction, mosaicDefinitionMetaDataPair, nem.model.network.data.testnet.id);
console.log("transactionEntity ---------------------------------------------")
console.log(transactionEntity)
// { type: 257,
// version: -1744830462,
// signer: 'f1fcecfebb32e4230e401ca9d10c3f804d92079847272acaa0b04b017d060796',
// timeStamp: 106053913,
// deadline: 106057513,
// recipient: 'TBKRXIJC4YNIOWQLZFVQNP2FFL5GN4Q2OTHDG6B7',
// amount: 0,
// fee: NaN,
// message: {
// type: 1,
// payload: '6665656c20746865206372797374616c20636c656172207761746572'
// },
// mosaics: [{ mosaicId: [Object], quantity: 5 }]
// }
const oneXEM = 1000000.0 // 1 xem == 1000000.0 [small xem units] sth. like [wei] for ether
const totalTransactionFeeXEM = (0.05 + 0.05 * Math.ceil(message.length / 21)) * oneXEM
// due to a bug in nem-sdk you must set .fee manualy else sdk will through Error: FAILURE_INSUFFICIENT_FEE
transactionEntity.fee = Math.max(0.05 * oneXEM, totalTransactionFeeXEM) // must be of type double! else nem/result will throw error
const result = await nem.model.transactions.send(common, transactionEntity, endpoint)
console.log("result ---------------------------------------------")
console.log(result)
// { innerTransactionHash: { },
// code: 1,
// type: 1,
// message: 'SUCCESS',
// transactionHash: { data: '1a1af57b8b416cabe0d203868f381ebcd3ebddbd268a1886b744d1bde214f3b2' }
// }
res.send({
mosaicAttachment,
// mosaicAttachment2,
transferTransaction,
transactionEntity,
result,
})
// {
// "mosaicAttachment": {
// "mosaicId": {
// "namespaceId": "cryptopay",
// "name": "crystal"
// },
// "quantity": 5
// },
// "transferTransaction": {
// "amount": 1,
// "recipient": "TBKRXIJC4YNIOWQLZFVQNP2FFL5GN4Q2OTHDG6B7",
// "recipientPublicKey": "",
// "isMultisig": false,
// "multisigAccount": "",
// "message": "feel the crystal clear water",
// "messageType": 1,
// "mosaics": [
// {
// "mosaicId": {
// "namespaceId": "cryptopay",
// "name": "crystal"
// },
// "quantity": 5
// }
// ]
// },
// "transactionEntity": {
// "type": 257,
// "version": -1744830462,
// "signer": "f1fcecfebb32e4230e401ca9d10c3f804d92079847272acaa0b04b017d060796",
// "timeStamp": 106060260,
// "deadline": 106063860,
// "recipient": "TBKRXIJC4YNIOWQLZFVQNP2FFL5GN4Q2OTHDG6B7",
// "amount": 1000000,
// "fee": 150000.00000000003,
// "message": {
// "type": 1,
// "payload": "6665656c20746865206372797374616c20636c656172207761746572"
// },
// "mosaics": [
// {
// "mosaicId": {
// "namespaceId": "cryptopay",
// "name": "crystal"
// },
// "quantity": 5
// }
// ]
// },
// "result": {
// "innerTransactionHash": {},
// "code": 1,
// "type": 1,
// "message": "SUCCESS",
// "transactionHash": {
// "data": "1be923f1483cf424d49541cb20f5eb3f99ed5a1f8060a12f2a28f70198ca38d2"
// }
// }
// }
} catch (err) {
console.log(err)
res.send({
success: false,
error: err.toString()
})
}
})
app.listen(3000, () => console.log(‘winnetou can no send mosaics on port 3000!’))