Skip to content

Mosaic definition transaction

yaaccount edited this page Jun 5, 2019 · 4 revisions
import { Deadline, Account, NetworkType, TransactionHttp, Listener, UInt64, SignedTransaction, PublicAccount, MosaicDefinitionTransaction, MosaicNonce, MosaicId, MosaicProperties } from 'tsjs-xpx-catapult-sdk';

const transactionHttp = new TransactionHttp('http://localhost:3000');
const listener = new Listener('http://localhost:3000');

const sender = Account.createFromPrivateKey(
    '28FCECEA252231D2C86E1BCF7DD541552BDBBEFBB09324758B3AC199B4AA7B78',
    NetworkType.MIJIN_TEST);

const announceAndWaitForConfirmation = (signedTx: SignedTransaction) => {
    listener.open().then(() => {
        const subscription = listener.confirmed(PublicAccount.createFromPublicKey(signedTx.signer, NetworkType.MIJIN_TEST).address).subscribe(confirmed => {
            if (confirmed && confirmed.transactionInfo && confirmed.transactionInfo.hash === signedTx.hash) {
                console.log('confirmed: ' + JSON.stringify(confirmed));
                subscription.unsubscribe();
                listener.close();
            }
        }, error => {
            console.error(error);
        }, () => {
            console.log('done.');
        });

        transactionHttp.announce(signedTx);
    });
}

const nonce = MosaicNonce.createRandom();
const mosaicId = MosaicId.createFromNonce(nonce, sender.publicAccount);
const mosaicDefinitionTx = MosaicDefinitionTransaction.create(
    Deadline.create(),
    nonce,
    mosaicId,
    MosaicProperties.create({
        "supplyMutable": false,
        "transferable": true,
        "levyMutable": false,
        "divisibility": 3,
        "duration": UInt64.fromUint(100000),
    }),
    NetworkType.MIJIN_TEST
);

const signedMosaicDefinitionTransaction = sender.sign(mosaicDefinitionTx);

const mosaicDefinitionTransactionExample = () => {
    announceAndWaitForConfirmation(signedMosaicDefinitionTransaction);
}

export {
    mosaicDefinitionTransactionExample
}