Useful Patterns

Helper functions to abstract away some complexity

Transactions

The steps to validate, prepare, and execute a contract are used quite frequently. In the code examples throughout this documentation we explicitly call each function to. However, in the real world, we've found it helpful to abstract those steps into a single function, as such:

function executeTransaction(transaction, options) {
return new Promise(async (resolve, reject) => {
try {
await transaction.validate();
const opts = await transaction.prepare(options);
const tx = transaction.send(opts);
tx.once('transactionHash', hash => console.log(`Pending: ${hash}`));
tx.once('receipt', receipt => resolve(receipt));
tx.once('error', error => reject(error));
} catch (e) {
reject(e);
}
});
}