Created
October 2, 2018 12:35
-
-
Save dgarcia360/c44cc31c272836234040fc73e6498cf3 to your computer and use it in GitHub Desktop.
FormatMosaicName.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
AccountHttp, | |
Mosaic, | |
MosaicHttp, | |
NamespaceHttp, | |
NetworkType, | |
PublicAccount, | |
TransferTransaction | |
} from 'nem2-sdk'; | |
import {filter, map, mergeMap, toArray} from "rxjs/operators"; | |
import {from} from "rxjs"; | |
const nodeUrl = 'http://localhost:3000'; | |
const mosaicHttp = new MosaicHttp(nodeUrl); | |
const namespaceHttp = new NamespaceHttp(nodeUrl); | |
const accountHttp = new AccountHttp(nodeUrl); | |
const publicAccount = PublicAccount.createFromPublicKey(process.env.PUBLIC_KEY, | |
NetworkType.MIJIN_TEST); | |
const formatMosaicName = (mosaic : Mosaic) => { | |
return mosaicHttp | |
.getMosaicsName([mosaic.id]) | |
.pipe( | |
mergeMap( (mosaicName) => { | |
return namespaceHttp | |
.getNamespacesName([mosaicName[0].namespaceId]) | |
.pipe( | |
map(namespaceName => { | |
const formattedName = namespaceName[0].name + ":" + mosaicName[0].name; | |
return {'mosaic': mosaic, 'name': formattedName}; | |
}) | |
) | |
}) | |
); | |
}; | |
accountHttp | |
.transactions(publicAccount) | |
.pipe( | |
mergeMap(transaction => transaction), | |
filter( transaction => transaction instanceof TransferTransaction), | |
map( transaction => <TransferTransaction> transaction), | |
mergeMap(transaction => { | |
return from(transaction.mosaics) | |
.pipe( | |
mergeMap(mosaic => formatMosaicName(mosaic)), | |
toArray(), | |
map( mosaics => { | |
return {'transaction': transaction, 'formattedMosaics': mosaics} | |
}) | |
); | |
}) | |
).subscribe(x => console.log(JSON.stringify(x)), err => console.log(err)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment