Skip to content

Websocket

Chun Lam edited this page Feb 21, 2024 · 4 revisions
import { Listener, Address } from "tsjs-xpx-chain-sdk";

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

const subscribeNewBlock = () => {
    listener.open().then(() => {
        const subscription = listener.newBlock().subscribe({
            next: blockInfo => {
                console.log(JSON.stringify(blockInfo));
            }, 
            error: error => {
                console.error(error);
            }, 
            complete: () => {
                console.log('done.');
            }
        })
    });
}

const subscribeStatus = (address: Address) => {
    listener.open().then(() => {
        const subscription = listener.status(address).subscribe({
            next: transactionStatusError => {
                console.log(JSON.stringify(transactionStatusError));
            }, 
            error: error => {
                console.error(error);
            }, 
            complete: () => {
                console.log('done.');
            }
        });
    });
}

const subscribeConfirmed = (address: Address) => {
    listener.open().then(() => {
        const subscription = listener.confirmed(address).subscribe({
            next: transaction => {
                console.log(JSON.stringify(transaction));
            }, 
            error: error => {
                console.error(error);
            }, 
            complete: () => {
                console.log('done.');
            }
        })
    });
}

const subscribeUnconfirmedAdded = (address: Address) => {
    listener.open().then(() => {
        const subscription = listener.unconfirmedAdded(address).subscribe({
            next: transaction => {
                console.log(JSON.stringify(transaction));
            }, 
            error: error => {
                console.error(error);
            }, 
            complete: () => {
                console.log('done.');
            }
        });
    });
}

const subscribeUnconfirmedRemoved = (address: Address) => {
    listener.open().then(() => {
        const subscription = listener.unconfirmedRemoved(address).subscribe({
            next: hash => {
                console.log(hash);
            }, 
            error: error => {
                console.error(error);
            }, 
            complete: () => {
                console.log('done.');
            }
        })
    });
}

const subscribePartialAdded = (address: Address) => {
    listener.open().then(() => {
        const subscription = listener.aggregateBondedAdded(address).subscribe({
            next: aggregateTransaction => {
                console.log(JSON.stringify(aggregateTransaction));
            }, 
            error: error => {
                console.error(error);
            }, 
            complete: () => {
                console.log('done.');
            }
        });
    });
}

const subscribePartialRemoved = (address: Address) => {
    listener.open().then(() => {
        const subscription = listener.aggregateBondedRemoved(address).subscribe({
            next: hash => {
                console.log(hash);
            }, 
            error: error => {
                console.error(error);
            }, 
            complete: () => {
                console.log('done.');
            }
        });
    });
}

const subscribeCosignatureAdded = (address: Address) => {
    listener.open().then(() => {
        const subscription = listener.cosignatureAdded(address).subscribe({
            next: cosignatureSignedTransaction => {
                console.log(JSON.stringify(cosignatureSignedTransaction));
            }, 
            error: error => {
                console.error(error);
            }, 
            complete: () => {
                console.log('done.');
            }
        });
    });
}

export {
    subscribeStatus,
    subscribeNewBlock,
    subscribeConfirmed,
    subscribeUnconfirmedAdded,
    subscribeUnconfirmedRemoved,
    subscribePartialAdded,
    subscribePartialRemoved,
    subscribeCosignatureAdded
}