Created
December 20, 2018 19:13
-
-
Save dgarcia360/3e35241b5f2847eede6c84897de02030 to your computer and use it in GitHub Desktop.
Monitor transaction status
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 { | |
Account, | |
Address, | |
Deadline, | |
Listener, | |
NetworkType, | |
PlainMessage, | |
TransactionHttp, | |
TransferTransaction | |
} from "nem2-sdk"; | |
import {filter} from "rxjs/operators"; | |
// 01 - Create Transaction | |
const recipientAddress = Address.createFromRawAddress("SD5DT3-CH4BLA-BL5HIM-EKP2TA-PUKF4N-Y3L5HR-IR54"); | |
const transferTransaction = TransferTransaction.create( | |
Deadline.create(), | |
recipientAddress, | |
[], | |
PlainMessage.create('Test'), | |
NetworkType.MIJIN_TEST); | |
// 02 - Sign Transaction | |
const signer = Account.createFromPrivateKey(process.env.PRIVATE_KEY, NetworkType.MIJIN_TEST); | |
const signedTransaction = signer.sign(transferTransaction); | |
// 03 - Announce and monitor the transaction status | |
const url = 'http://localhost:3000'; | |
const listener = new Listener(url); | |
const transactionHttp = new TransactionHttp(url); | |
listener.open().then(() => { | |
listener | |
.status(signer.address) | |
.pipe(filter(error => error.hash === signedTransaction.hash)) | |
.subscribe(error => console.log("❌:" + error.status), | |
err => console.error(err)); | |
listener | |
.unconfirmedAdded(signer.address) | |
.pipe(filter(transaction => (transaction.transactionInfo !== undefined | |
&& transaction.transactionInfo.hash === signedTransaction.hash))) | |
.subscribe(ignored => console.log("⏳: Transaction status changed to unconfirmed"), | |
err => console.error(err)); | |
listener | |
.confirmed(signer.address) | |
.pipe(filter(transaction =>(transaction.transactionInfo !== undefined | |
&& transaction.transactionInfo.hash === signedTransaction.hash))) | |
.subscribe(ignored => console.log("✅: Transaction confirmed"), | |
err => console.error(err)); | |
transactionHttp | |
.announce(signedTransaction) | |
.subscribe(x => console.log(x), | |
err => console.error(err)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment