Skip to content

Instantly share code, notes, and snippets.

@acastellana
Created January 27, 2017 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acastellana/17844d86d0f71b0ae18cc48029b66b0f to your computer and use it in GitHub Desktop.
Save acastellana/17844d86d0f71b0ae18cc48029b66b0f to your computer and use it in GitHub Desktop.
sendMosaic(recipient, namespaceId, mosaics, amount, common, options)
/*
* REQUIRES:
* - https://gist.github.com/acastellana/995326e3a41b5f690db50a17153af312
* - https://gist.github.com/acastellana/607a47838e842417d80622568543eb74
*/
/**
* sendMosaic(recipient, namespaceId, mosaics, amount, common, options) Sends a minimal transaction containing a mosaic and optionally a message and some xem
*
* @param {object} recipient - Transaction receiver's account
* @param {string} namespaceId - Mosaic's namespace name
* @param {string} mosaic - Mosaic's name
* @param {integer} amount - Amount of mosaics to transfer
* @param {object} common - password/privateKey object
* @param {object} options - An object that can contain: options.xem and options.message
*
* @return {promise} - An announce transaction promise of the NetworkRequests service
*/
sendMosaic(recipient, namespaceId, mosaics, amount, common, options) {
var xem = ""
var message = ""
if(options.xem) xem = options.xem;
if(options.message) message = options.message;
var transferData = {}
// Check that the recipient is a valid account and process it's public key
transferData.recipient = recipient;
this.processTxData(transferData);
// transferData.recipientPubKey is set now
// In case of mosaic transfer amount is used as multiplier, set to 1 as default
transferData.amount = 1;
// Other necessary
transferData.message = message;
transferData.encryptMessage = false;
// Setup mosaics information
transferData.mosaics = [{
'mosaicId': {
'namespaceId': namespaceId,
'name': mosaics
},
'quantity': amount,
}];
if(xem > 0){
transferData.mosaics[1] = {
'mosaicId': {
'namespaceId': "nem",
'name': 'xem'
},
'quantity': xem,
}
}
// Build the entity to send
let entity = this._Transactions.prepareTransfer(common, transferData, this.mosaicsMetaData);
return this.send(entity, common);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment