Skip to main content

TransactionObserver

It is a class that allows easy handling of the states of a transaction using the original observable issued by the sdk and making use of pipes to link functions to the different states of the transaction.

Here is an example of its use:

const erc20Api = sdk.transactions.erc20Api
const transaction = erc20Api.transfer(payload, caller); // We don't use the await

const observer = sdk.transactions.adapterTx(transaction);

observe
.on(TransactionStatus.Pending, (tx) => {
console.log(`Transaction ${tx.id} is pending`);
})
.on(TransactionStatus.Success, (tx) => {
console.log(`Transaction ${tx.id} is successful`);
})
.on(TransactionStatus.Fail, (tx) => {
console.log(`Transaction ${tx.id} is failed`);
})
.listen();
info

Always remember to run listen at the end to activate the subscription.

On

The on function allows you to bind a function to a certain transaction state.

observer.on(TransactionStatus.Pending, (tx) => {
console.log(`Transaction ${tx.id} is pending`);
})

Map

The map function allows you to send a function to transform the output data.

const observer = sdk.transactions.adapterTx(transaction); // Here emits ITransaction

const mappedId = observer.map(tx => ({ mappedId: tx.id })); // Here we just cast an object with the id.

mappedId.on(TransactionStatus.Success, ({ mappedId }) => {
console.log(`Transaction ${mappedId} is successful`);
})

Observable

The observable property allows you to access the original observable issued by the sdk and make use of its functions.

const observer = sdk.transactions.adapterTx(transaction);

const originalObservable = observer.observable; // Observable<ITransaction>

originalObservable.subscribe((tx) => {
console.log(`Transaction ${tx.id} is ${tx.status}`);
})