Skip to content

Instantly share code, notes, and snippets.

@yornaath
Last active September 9, 2022 19:58
Show Gist options
  • Save yornaath/8d59ab114041319effdd32236fad5114 to your computer and use it in GitHub Desktop.
Save yornaath/8d59ab114041319effdd32236fad5114 to your computer and use it in GitHub Desktop.
const transfer = async (
api: ApiPromise,
receiver: string,
ammount: number
): Promise<
| Ok<BalanceTransferEvent>
| Error<RejectedFees | Canceled | DispatchError | ShouldBeUnreachable>
> => {
const transaction = api.tx.balances.transfer(receiver, ammount)
for await (const step of transact(transaction)) {
if (isFeesUpdate(step)) {
if (confirm(`Do you accept ${step.fees} in fees?`)) {
continue
}
return error(RejectedFees)
}
if (isSigning(step)) {
try {
const sender = await ui.state.get("selected-address")
const injector = await web3FromAddress(sender)
step(injector)
continue
}
catch(error) {
return error(Canceled)
}
}
if (isError(step)) {
ui.showError("rejected")
return error(step)
}
if (isInBlock(step)) {
ui.notify(`transaction included in ${step.block}`)
}
if (isFinalized(step)) {
const balanceTransferEvent = getEvent(
step.block,
api.events.balances.Transfer
)
ui.showSuccess(
`transaction finalized, transfered ${balanceTransferEvent.ammount.toHuman()} to ${receiver}`
)
return ok(balanceTransferEvent)
}
}
return error(ShouldBeUnreachable)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment