Skip to content

Commit

Permalink
Existing structs are now simpler
Browse files Browse the repository at this point in the history
  • Loading branch information
markopoloparadox committed Oct 16, 2024
1 parent 0536cd3 commit 4cdf3a9
Show file tree
Hide file tree
Showing 9 changed files with 460 additions and 782 deletions.
8 changes: 4 additions & 4 deletions avail-js/docs/advanced_examples/multisig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SDK, WaitFor, Keyring, BN, KeyringPair, Weight, ParsedTxResult, MultisigTimepoint } from "./../../src/index"
import { SDK, WaitFor, Keyring, BN, KeyringPair, Weight, TxResultDetails, MultisigTimepoint } from "./../../src/index"

const main = async () => {
const providerEndpoint = "ws://127.0.0.1:9944"
Expand Down Expand Up @@ -73,7 +73,7 @@ async function firstApproval(
threshold: number,
otherSignatures: string[],
maxWeight: Weight,
): Promise<ParsedTxResult> {
): Promise<TxResultDetails> {
console.log("Alice is creating a Multisig Transaction...")

const maybeTxResult = await sdk.util.firstMultisigApproval(
Expand All @@ -98,7 +98,7 @@ async function nextApproval(
threshold: number,
otherSignatures: string[],
timepoint: MultisigTimepoint,
): Promise<ParsedTxResult> {
): Promise<TxResultDetails> {
console.log("Bob is approving the existing Multisig Transaction...")

const maybeTxResult = await sdk.util.nextMultisigApproval(
Expand All @@ -124,7 +124,7 @@ async function lastApproval(
timepoint: MultisigTimepoint,
callData: string,
maxWeight: Weight,
): Promise<ParsedTxResult> {
): Promise<TxResultDetails> {
console.log("Charlie is approving and executing the existing Multisig Transaction...")

const maybeTxResult = await sdk.util.lastMultisigApproval(
Expand Down
2 changes: 1 addition & 1 deletion avail-js/src/sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export { Keyring } from "@polkadot/api"
export { KeyringPair } from "@polkadot/keyring/types"
export { Bytes } from "@polkadot/types-codec"
export { H256, Weight } from "@polkadot/types/interfaces"
export { ParsedTxResult, MultisigTimepoint } from "./utils"
export { TxResultDetails, MultisigTimepoint } from "./utils"

export {
WaitFor,
Expand Down
139 changes: 51 additions & 88 deletions avail-js/src/sdk/transactions/balances.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,35 @@
import { ApiPromise } from "@polkadot/api"
import { ISubmittableResult } from "@polkadot/types/types/extrinsic"
import { H256, EventRecord } from "@polkadot/types/interfaces/types"
import { EventRecord } from "@polkadot/types/interfaces/types"
import { BN } from "@polkadot/util"
import { KeyringPair } from "@polkadot/keyring/types"
import { err, Result } from "neverthrow"
import { err, Result, ok } from "neverthrow"

import { SignerOptions } from "@polkadot/api/types"
import { decodeError } from "../../helpers"
import { WaitFor, GenericFailure, standardCallback, getBlockHashAndTxHash } from "./common"

type TransferKeepAliveTxSuccess = {
isErr: false
event: Events.TransferEvent
events: EventRecord[]
txHash: H256
txIndex: number
blockHash: H256
blockNumber: number
import { WaitFor, standardCallback, TransactionFailed } from "./common"
import { parseTransactionResult, TxResultDetails } from "../utils"

export class TransferKeepAliveTx {
constructor(
public event: Events.TransferEvent,
public details: TxResultDetails,
) {}
}
type TransferAllowDeathTxSuccess = {
isErr: false
event: Events.TransferEvent
event2?: Events.KilledAccount
events: EventRecord[]
txHash: H256
txIndex: number
blockHash: H256
blockNumber: number

export class TransferAllowDeathTx {
constructor(
public event: Events.TransferEvent,
public event2: Events.KilledAccount | undefined,
public details: TxResultDetails,
) {}
}
type TransferAllTxSuccess = {
isErr: false
event: Events.TransferEvent
event2?: Events.KilledAccount
events: EventRecord[]
txHash: H256
txIndex: number
blockHash: H256
blockNumber: number

export class TransferAllTx {
constructor(
public event: Events.TransferEvent,
public event2: Events.KilledAccount | undefined,
public details: TxResultDetails,
) {}
}

export class Balances {
Expand All @@ -52,7 +45,7 @@ export class Balances {
waitFor: WaitFor,
account: KeyringPair,
options?: Partial<SignerOptions>,
): Promise<TransferAllTxSuccess | GenericFailure> {
): Promise<Result<TransferAllTx, TransactionFailed>> {
const optionWrapper = options || {}
const maybeTxResult = await new Promise<Result<ISubmittableResult, string>>((res, _) => {
this.api.tx.balances
Expand All @@ -66,29 +59,22 @@ export class Balances {
})

if (maybeTxResult.isErr()) {
return { isErr: true, reason: maybeTxResult.error } as GenericFailure
return err(new TransactionFailed(maybeTxResult.error, null))
}
const txResult = maybeTxResult.value

if (txResult.isError) {
return { isErr: true, reason: "The transaction was dropped or something." } as GenericFailure
const maybeParsed = await parseTransactionResult(this.api, txResult, waitFor)
if (maybeParsed.isErr()) {
return err(maybeParsed.error)
}
const details = maybeParsed.value

const failed = txResult.events.find((e) => this.api.events.system.ExtrinsicFailed.is(e.event))
if (failed != undefined) {
return { isErr: true, reason: decodeError(this.api, failed.event.data[0]) } as GenericFailure
}

const event = Events.TransferEvent.New(txResult.events)
const event = Events.TransferEvent.New(details.events)
if (event == undefined) {
return { isErr: true, reason: "Failed to find Transfer event." } as GenericFailure
return err(new TransactionFailed("Failed to find Transfer event", details))
}
const event2 = Events.KilledAccount.New(txResult.events)

const events = txResult.events
const [txHash, txIndex, blockHash, blockNumber] = await getBlockHashAndTxHash(txResult, waitFor, this.api)

return { isErr: false, event, event2, events, txHash, txIndex, blockHash, blockNumber } as TransferAllTxSuccess
return ok(new TransferAllTx(event, event2, details))
}

async transferAllowDeath(
Expand All @@ -97,7 +83,7 @@ export class Balances {
waitFor: WaitFor,
account: KeyringPair,
options?: Partial<SignerOptions>,
): Promise<TransferAllowDeathTxSuccess | GenericFailure> {
): Promise<Result<TransferAllowDeathTx, TransactionFailed>> {
const optionWrapper = options || {}
const maybeTxResult = await new Promise<Result<ISubmittableResult, string>>((res, _) => {
this.api.tx.balances
Expand All @@ -111,38 +97,22 @@ export class Balances {
})

if (maybeTxResult.isErr()) {
return { isErr: true, reason: maybeTxResult.error } as GenericFailure
return err(new TransactionFailed(maybeTxResult.error, null))
}
const txResult = maybeTxResult.value

if (txResult.isError) {
return { isErr: true, reason: "The transaction was dropped or something." } as GenericFailure
const maybeParsed = await parseTransactionResult(this.api, txResult, waitFor)
if (maybeParsed.isErr()) {
return err(maybeParsed.error)
}
const details = maybeParsed.value

const failed = txResult.events.find((e) => this.api.events.system.ExtrinsicFailed.is(e.event))
if (failed != undefined) {
return { isErr: true, reason: decodeError(this.api, failed.event.data[0]) } as GenericFailure
}

const event = Events.TransferEvent.New(txResult.events)
const event = Events.TransferEvent.New(details.events)
if (event == undefined) {
return { isErr: true, reason: "Failed to find Transfer event." } as GenericFailure
return err(new TransactionFailed("Failed to find Transfer event", details))
}
const event2 = Events.KilledAccount.New(txResult.events)

const events = txResult.events
const [txHash, txIndex, blockHash, blockNumber] = await getBlockHashAndTxHash(txResult, waitFor, this.api)

return {
isErr: false,
event,
event2,
events,
txHash,
txIndex,
blockHash,
blockNumber,
} as TransferAllowDeathTxSuccess
return ok(new TransferAllowDeathTx(event, event2, details))
}

async transferKeepAlive(
Expand All @@ -151,7 +121,7 @@ export class Balances {
waitFor: WaitFor,
account: KeyringPair,
options?: Partial<SignerOptions>,
): Promise<TransferKeepAliveTxSuccess | GenericFailure> {
): Promise<Result<TransferKeepAliveTx, TransactionFailed>> {
const optionWrapper = options || {}
const maybeTxResult = await new Promise<Result<ISubmittableResult, string>>((res, _) => {
this.api.tx.balances
Expand All @@ -165,28 +135,21 @@ export class Balances {
})

if (maybeTxResult.isErr()) {
return { isErr: true, reason: maybeTxResult.error } as GenericFailure
return err(new TransactionFailed(maybeTxResult.error, null))
}
const txResult = maybeTxResult.value

if (txResult.isError) {
return { isErr: true, reason: "The transaction was dropped or something." } as GenericFailure
const maybeParsed = await parseTransactionResult(this.api, txResult, waitFor)
if (maybeParsed.isErr()) {
return err(maybeParsed.error)
}
const details = maybeParsed.value

const failed = txResult.events.find((e) => this.api.events.system.ExtrinsicFailed.is(e.event))
if (failed != undefined) {
return { isErr: true, reason: decodeError(this.api, failed.event.data[0]) } as GenericFailure
}

const event = Events.TransferEvent.New(txResult.events)
const event = Events.TransferEvent.New(details.events)
if (event == undefined) {
return { isErr: true, reason: "Failed to find Transfer event." } as GenericFailure
return err(new TransactionFailed("Failed to find Transfer event", details))
}

const events = txResult.events
const [txHash, txIndex, blockHash, blockNumber] = await getBlockHashAndTxHash(txResult, waitFor, this.api)

return { isErr: false, event, events, txHash, txIndex, blockHash, blockNumber } as TransferKeepAliveTxSuccess
return ok(new TransferKeepAliveTx(event, details))
}
}

Expand Down
8 changes: 7 additions & 1 deletion avail-js/src/sdk/transactions/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ApiPromise } from "@polkadot/api"
import { ISubmittableResult } from "@polkadot/types/types/extrinsic"
import { H256 } from "@polkadot/types/interfaces/types"
import { ok, Result } from "neverthrow"
import { TxResultDetails } from "../utils"

export enum WaitFor {
BlockInclusion,
Expand Down Expand Up @@ -46,4 +47,9 @@ export async function getBlockHashAndTxHash(
return [txHash, txIndex, blockHash, blockNumber]
}

export type GenericFailure = { isErr: true; reason: string }
export class TransactionFailed {
constructor(
public reason: string,
public details: TxResultDetails | null,
) {}
}
Loading

0 comments on commit 4cdf3a9

Please sign in to comment.