diff --git a/avail-js/docs/cookbook/src/rpc/payment.ts b/avail-js/docs/cookbook/src/rpc/payment.ts index 5427207e0..17d1bcca8 100644 --- a/avail-js/docs/cookbook/src/rpc/payment.ts +++ b/avail-js/docs/cookbook/src/rpc/payment.ts @@ -17,7 +17,7 @@ const main = async () => { // Payment Query Free Details const mtx2 = await alice.balanceTransfer("5HGjWAeFDfFCWPsjFQdVV2Msvz2XtMktvgocEZcCj68kUMaw", SDK.oneAvail()) const tx2 = mtx2._unsafeUnwrap() - const gtx2 = await tx2.details.fetchGenericTransaction(api) + const gtx2 = (await tx2.details.fetchBlock(api)).transactionByIndex(tx2.details.txIndex)._unsafeUnwrap() const queryFeeDetails: any = await api.call.transactionPaymentApi.queryFeeDetails(gtx2.toHex(), null) const baseFee: BN = queryFeeDetails.inclusionFee.__internal__raw.baseFee diff --git a/avail-js/src/sdk/transactions/balances.ts b/avail-js/src/sdk/transactions/balances.ts index 2d91f6a9b..f59899117 100644 --- a/avail-js/src/sdk/transactions/balances.ts +++ b/avail-js/src/sdk/transactions/balances.ts @@ -5,7 +5,7 @@ import { KeyringPair } from "@polkadot/keyring/types" import { WaitFor, TransactionOptions, - singAndSendAndParseTransaction, + signAndSendAndParseTransaction, TxResultDetails, TransactionFailed, } from "./common" @@ -49,7 +49,7 @@ export class Balances { options?: TransactionOptions, ): Promise> { const tx = this.api.tx.balances.transferAll(dest, keepAlive) - const maybeParsed = await singAndSendAndParseTransaction(this.api, tx, account, waitFor, options) + const maybeParsed = await signAndSendAndParseTransaction(this.api, tx, account, waitFor, options) if (maybeParsed.isErr()) return err(maybeParsed.error) const details = maybeParsed.value @@ -78,7 +78,7 @@ export class Balances { options?: TransactionOptions, ): Promise> { const tx = this.api.tx.balances.transferAllowDeath(dest, value) - const maybeParsed = await singAndSendAndParseTransaction(this.api, tx, account, waitFor, options) + const maybeParsed = await signAndSendAndParseTransaction(this.api, tx, account, waitFor, options) if (maybeParsed.isErr()) return err(maybeParsed.error) const details = maybeParsed.value @@ -107,7 +107,7 @@ export class Balances { options?: TransactionOptions, ): Promise> { const tx = this.api.tx.balances.transferKeepAlive(dest, value) - const maybeParsed = await singAndSendAndParseTransaction(this.api, tx, account, waitFor, options) + const maybeParsed = await signAndSendAndParseTransaction(this.api, tx, account, waitFor, options) if (maybeParsed.isErr()) return err(maybeParsed.error) const details = maybeParsed.value diff --git a/avail-js/src/sdk/transactions/common.ts b/avail-js/src/sdk/transactions/common.ts index e81692a70..90ec82fcc 100644 --- a/avail-js/src/sdk/transactions/common.ts +++ b/avail-js/src/sdk/transactions/common.ts @@ -51,7 +51,7 @@ export async function signAndSendTransaction( }) } -export async function singAndSendAndParseTransaction( +export async function signAndSendAndParseTransaction( api: ApiPromise, tx: SubmittableExtrinsic<"promise">, account: KeyringPair, diff --git a/avail-js/src/sdk/transactions/da.ts b/avail-js/src/sdk/transactions/da.ts index a11768b95..37a1e8251 100644 --- a/avail-js/src/sdk/transactions/da.ts +++ b/avail-js/src/sdk/transactions/da.ts @@ -7,7 +7,7 @@ import { decodeError, fromHexToAscii } from "../../helpers" import { WaitFor, TransactionOptions, - singAndSendAndParseTransaction, + signAndSendAndParseTransaction, TxResultDetails, TransactionFailed, } from "./common" @@ -73,7 +73,7 @@ export class DataAvailability { const appId = optionWrapper.app_id || 0 const tx = this.api.tx.dataAvailability.submitData(data) - const maybeParsed = await singAndSendAndParseTransaction(this.api, tx, account, waitFor, options) + const maybeParsed = await signAndSendAndParseTransaction(this.api, tx, account, waitFor, options) if (maybeParsed.isErr()) return err(maybeParsed.error) const details = maybeParsed.value @@ -99,7 +99,7 @@ export class DataAvailability { options?: TransactionOptions, ): Promise> { const tx = this.api.tx.dataAvailability.createApplicationKey(key) - const maybeParsed = await singAndSendAndParseTransaction(this.api, tx, account, waitFor, options) + const maybeParsed = await signAndSendAndParseTransaction(this.api, tx, account, waitFor, options) if (maybeParsed.isErr()) return err(maybeParsed.error) const details = maybeParsed.value @@ -125,7 +125,7 @@ export class DataAvailability { ): Promise> { const call = this.api.tx.dataAvailability.setApplicationKey(oldKey, newKey) const tx = this.api.tx.sudo.sudo(call) - const maybeParsed = await singAndSendAndParseTransaction(this.api, tx, account, waitFor, options) + const maybeParsed = await signAndSendAndParseTransaction(this.api, tx, account, waitFor, options) if (maybeParsed.isErr()) return err(maybeParsed.error) const details = maybeParsed.value @@ -151,7 +151,7 @@ export class DataAvailability { ): Promise> { const call = this.api.tx.dataAvailability.submitBlockLengthProposal(rows, cols) const tx = this.api.tx.sudo.sudo(call) - const maybeParsed = await singAndSendAndParseTransaction(this.api, tx, account, waitFor, options) + const maybeParsed = await signAndSendAndParseTransaction(this.api, tx, account, waitFor, options) if (maybeParsed.isErr()) return err(maybeParsed.error) const details = maybeParsed.value @@ -177,7 +177,7 @@ export class DataAvailability { ): Promise> { const call = this.api.tx.dataAvailability.setSubmitDataFeeModifier(modifier) const tx = this.api.tx.sudo.sudo(call) - const maybeParsed = await singAndSendAndParseTransaction(this.api, tx, account, waitFor, options) + const maybeParsed = await signAndSendAndParseTransaction(this.api, tx, account, waitFor, options) if (maybeParsed.isErr()) return err(maybeParsed.error) const details = maybeParsed.value diff --git a/avail-js/src/sdk/transactions/multisig.ts b/avail-js/src/sdk/transactions/multisig.ts index b1931c5ab..0512b3aa2 100644 --- a/avail-js/src/sdk/transactions/multisig.ts +++ b/avail-js/src/sdk/transactions/multisig.ts @@ -8,7 +8,7 @@ import { TransactionOptions, TxResultDetails, MultisigTimepoint, - singAndSendAndParseTransaction, + signAndSendAndParseTransaction, } from "./common" export class AsMultiTx { @@ -45,7 +45,7 @@ export class Multisig { options?: TransactionOptions, ): Promise> { const tx = this.api.tx.multisig.asMulti(threshold, otherSignatures, timepoint, call, maxWeight) - const maybeParsed = await singAndSendAndParseTransaction(this.api, tx, account, waitFor, options) + const maybeParsed = await signAndSendAndParseTransaction(this.api, tx, account, waitFor, options) if (maybeParsed.isErr()) return err(maybeParsed.error) const details = maybeParsed.value @@ -67,7 +67,7 @@ export class Multisig { options?: TransactionOptions, ): Promise> { const tx = this.api.tx.multisig.approveAsMulti(threshold, otherSignatures, timepoint, callHash, maxWeight) - const maybeParsed = await singAndSendAndParseTransaction(this.api, tx, account, waitFor, options) + const maybeParsed = await signAndSendAndParseTransaction(this.api, tx, account, waitFor, options) if (maybeParsed.isErr()) return err(maybeParsed.error) const details = maybeParsed.value diff --git a/avail-js/src/sdk/transactions/nomination_pools.ts b/avail-js/src/sdk/transactions/nomination_pools.ts index 6cfdf6231..6ec27527b 100644 --- a/avail-js/src/sdk/transactions/nomination_pools.ts +++ b/avail-js/src/sdk/transactions/nomination_pools.ts @@ -5,7 +5,7 @@ import { KeyringPair } from "@polkadot/keyring/types" import { WaitFor, TransactionOptions, - singAndSendAndParseTransaction, + signAndSendAndParseTransaction, TxResultDetails, TransactionFailed, } from "./common" @@ -137,7 +137,7 @@ export class NominationPools { options?: TransactionOptions, ): Promise> { const tx = this.api.tx.nominationPools.create(amount, root, nominator, bouncer) - const maybeParsed = await singAndSendAndParseTransaction(this.api, tx, account, waitFor, options) + const maybeParsed = await signAndSendAndParseTransaction(this.api, tx, account, waitFor, options) if (maybeParsed.isErr()) return err(maybeParsed.error) const details = maybeParsed.value @@ -162,7 +162,7 @@ export class NominationPools { options?: TransactionOptions, ): Promise> { const tx = this.api.tx.nominationPools.createWithPoolId(amount, root, nominator, bouncer, poolId) - const maybeParsed = await singAndSendAndParseTransaction(this.api, tx, account, waitFor, options) + const maybeParsed = await signAndSendAndParseTransaction(this.api, tx, account, waitFor, options) if (maybeParsed.isErr()) return err(maybeParsed.error) const details = maybeParsed.value @@ -184,7 +184,7 @@ export class NominationPools { options?: TransactionOptions, ): Promise> { const tx = this.api.tx.nominationPools.join(amount, poolId) - const maybeParsed = await singAndSendAndParseTransaction(this.api, tx, account, waitFor, options) + const maybeParsed = await signAndSendAndParseTransaction(this.api, tx, account, waitFor, options) if (maybeParsed.isErr()) return err(maybeParsed.error) const details = maybeParsed.value @@ -203,7 +203,7 @@ export class NominationPools { options?: TransactionOptions, ): Promise> { const tx = this.api.tx.nominationPools.nominate(poolId, validators) - const maybeParsed = await singAndSendAndParseTransaction(this.api, tx, account, waitFor, options) + const maybeParsed = await signAndSendAndParseTransaction(this.api, tx, account, waitFor, options) if (maybeParsed.isErr()) return err(maybeParsed.error) const details = maybeParsed.value @@ -218,7 +218,7 @@ export class NominationPools { options?: TransactionOptions, ): Promise> { const tx = this.api.tx.nominationPools.bondExtra(extra) - const maybeParsed = await singAndSendAndParseTransaction(this.api, tx, account, waitFor, options) + const maybeParsed = await signAndSendAndParseTransaction(this.api, tx, account, waitFor, options) if (maybeParsed.isErr()) return err(maybeParsed.error) const details = maybeParsed.value @@ -237,7 +237,7 @@ export class NominationPools { options?: TransactionOptions, ): Promise> { const tx = this.api.tx.nominationPools.setMetadata(poolId, metadata) - const maybeParsed = await singAndSendAndParseTransaction(this.api, tx, account, waitFor, options) + const maybeParsed = await signAndSendAndParseTransaction(this.api, tx, account, waitFor, options) if (maybeParsed.isErr()) return err(maybeParsed.error) const details = maybeParsed.value @@ -253,7 +253,7 @@ export class NominationPools { options?: TransactionOptions, ): Promise> { const tx = this.api.tx.nominationPools.unbond(memberAccount, unbondingPoints) - const maybeParsed = await singAndSendAndParseTransaction(this.api, tx, account, waitFor, options) + const maybeParsed = await signAndSendAndParseTransaction(this.api, tx, account, waitFor, options) if (maybeParsed.isErr()) return err(maybeParsed.error) const details = maybeParsed.value @@ -269,7 +269,7 @@ export class NominationPools { options?: TransactionOptions, ): Promise> { const tx = this.api.tx.nominationPools.chill(poolId) - const maybeParsed = await singAndSendAndParseTransaction(this.api, tx, account, waitFor, options) + const maybeParsed = await signAndSendAndParseTransaction(this.api, tx, account, waitFor, options) if (maybeParsed.isErr()) return err(maybeParsed.error) const details = maybeParsed.value @@ -284,7 +284,7 @@ export class NominationPools { options?: TransactionOptions, ): Promise> { const tx = this.api.tx.nominationPools.claimCommission(poolId) - const maybeParsed = await singAndSendAndParseTransaction(this.api, tx, account, waitFor, options) + const maybeParsed = await signAndSendAndParseTransaction(this.api, tx, account, waitFor, options) if (maybeParsed.isErr()) return err(maybeParsed.error) const details = maybeParsed.value @@ -301,7 +301,7 @@ export class NominationPools { options?: TransactionOptions, ): Promise> { const tx = this.api.tx.nominationPools.claimPayout() - const maybeParsed = await singAndSendAndParseTransaction(this.api, tx, account, waitFor, options) + const maybeParsed = await signAndSendAndParseTransaction(this.api, tx, account, waitFor, options) if (maybeParsed.isErr()) return err(maybeParsed.error) const details = maybeParsed.value @@ -317,7 +317,7 @@ export class NominationPools { options?: TransactionOptions, ): Promise> { const tx = this.api.tx.nominationPools.claimPayoutOther(other) - const maybeParsed = await singAndSendAndParseTransaction(this.api, tx, account, waitFor, options) + const maybeParsed = await signAndSendAndParseTransaction(this.api, tx, account, waitFor, options) if (maybeParsed.isErr()) return err(maybeParsed.error) const details = maybeParsed.value @@ -333,7 +333,7 @@ export class NominationPools { options?: TransactionOptions, ): Promise> { const tx = this.api.tx.nominationPools.setClaimPermission(permission) - const maybeParsed = await singAndSendAndParseTransaction(this.api, tx, account, waitFor, options) + const maybeParsed = await signAndSendAndParseTransaction(this.api, tx, account, waitFor, options) if (maybeParsed.isErr()) return err(maybeParsed.error) const details = maybeParsed.value @@ -357,7 +357,7 @@ export class NominationPools { } const tx = this.api.tx.nominationPools.setCommission(poolId, commission) - const maybeParsed = await singAndSendAndParseTransaction(this.api, tx, account, waitFor, options) + const maybeParsed = await signAndSendAndParseTransaction(this.api, tx, account, waitFor, options) if (maybeParsed.isErr()) return err(maybeParsed.error) const details = maybeParsed.value @@ -376,7 +376,7 @@ export class NominationPools { options?: TransactionOptions, ): Promise> { const tx = this.api.tx.nominationPools.withdrawUnbonded(memberAccount, numSlashingSpans) - const maybeParsed = await singAndSendAndParseTransaction(this.api, tx, account, waitFor, options) + const maybeParsed = await signAndSendAndParseTransaction(this.api, tx, account, waitFor, options) if (maybeParsed.isErr()) return err(maybeParsed.error) const details = maybeParsed.value @@ -395,7 +395,7 @@ export class NominationPools { options?: TransactionOptions, ): Promise> { const tx = this.api.tx.nominationPools.setState(poolId, state) - const maybeParsed = await singAndSendAndParseTransaction(this.api, tx, account, waitFor, options) + const maybeParsed = await signAndSendAndParseTransaction(this.api, tx, account, waitFor, options) if (maybeParsed.isErr()) return err(maybeParsed.error) const details = maybeParsed.value diff --git a/avail-js/src/sdk/transactions/staking.ts b/avail-js/src/sdk/transactions/staking.ts index 6176fb50f..5566fce58 100644 --- a/avail-js/src/sdk/transactions/staking.ts +++ b/avail-js/src/sdk/transactions/staking.ts @@ -6,7 +6,7 @@ import { err, Result, ok } from "neverthrow" import { WaitFor, TransactionOptions, - singAndSendAndParseTransaction, + signAndSendAndParseTransaction, TxResultDetails, TransactionFailed, } from "./common" @@ -79,7 +79,7 @@ export class Staking { options?: TransactionOptions, ): Promise> { const tx = this.api.tx.staking.bond(value, payee) - const maybeParsed = await singAndSendAndParseTransaction(this.api, tx, account, waitFor, options) + const maybeParsed = await signAndSendAndParseTransaction(this.api, tx, account, waitFor, options) if (maybeParsed.isErr()) return err(maybeParsed.error) const details = maybeParsed.value @@ -97,7 +97,7 @@ export class Staking { options?: TransactionOptions, ): Promise> { const tx = this.api.tx.staking.bondExtra(maxAdditional) - const maybeParsed = await singAndSendAndParseTransaction(this.api, tx, account, waitFor, options) + const maybeParsed = await signAndSendAndParseTransaction(this.api, tx, account, waitFor, options) if (maybeParsed.isErr()) return err(maybeParsed.error) const details = maybeParsed.value @@ -114,7 +114,7 @@ export class Staking { options?: TransactionOptions, ): Promise> { const tx = this.api.tx.staking.chill() - const maybeParsed = await singAndSendAndParseTransaction(this.api, tx, account, waitFor, options) + const maybeParsed = await signAndSendAndParseTransaction(this.api, tx, account, waitFor, options) if (maybeParsed.isErr()) return err(maybeParsed.error) const details = maybeParsed.value @@ -132,7 +132,7 @@ export class Staking { options?: TransactionOptions, ): Promise> { const tx = this.api.tx.staking.chillOther(stash) - const maybeParsed = await singAndSendAndParseTransaction(this.api, tx, account, waitFor, options) + const maybeParsed = await signAndSendAndParseTransaction(this.api, tx, account, waitFor, options) if (maybeParsed.isErr()) return err(maybeParsed.error) const details = maybeParsed.value @@ -150,7 +150,7 @@ export class Staking { options?: TransactionOptions, ): Promise> { const tx = this.api.tx.staking.nominate(targets) - const maybeParsed = await singAndSendAndParseTransaction(this.api, tx, account, waitFor, options) + const maybeParsed = await signAndSendAndParseTransaction(this.api, tx, account, waitFor, options) if (maybeParsed.isErr()) return err(maybeParsed.error) const details = maybeParsed.value @@ -168,7 +168,7 @@ export class Staking { options?: TransactionOptions, ): Promise> { const tx = this.api.tx.staking.unbond(value) - const maybeParsed = await singAndSendAndParseTransaction(this.api, tx, account, waitFor, options) + const maybeParsed = await signAndSendAndParseTransaction(this.api, tx, account, waitFor, options) if (maybeParsed.isErr()) return err(maybeParsed.error) const details = maybeParsed.value @@ -191,7 +191,7 @@ export class Staking { const validatorPerfs = { commission: maybeCommission.value, blocked } as ValidatorPerfs const tx = this.api.tx.staking.validate(validatorPerfs) - const maybeParsed = await singAndSendAndParseTransaction(this.api, tx, account, waitFor, options) + const maybeParsed = await signAndSendAndParseTransaction(this.api, tx, account, waitFor, options) if (maybeParsed.isErr()) return err(maybeParsed.error) const details = maybeParsed.value diff --git a/avail-rust/docs/extrinsics/README.md b/avail-rust/docs/extrinsics/README.md index 7d81178ec..962316b10 100644 --- a/avail-rust/docs/extrinsics/README.md +++ b/avail-rust/docs/extrinsics/README.md @@ -11,7 +11,7 @@ Origin Level: Signed ### Interface ```rust -async fn create_application_key(&self, key: Key, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; +async fn create_application_key(&self, key: Key, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; ``` #### Parameters @@ -79,10 +79,10 @@ Error: "Runtime error: Pallet error: DataAvailability::AppKeyAlreadyExists" #### On Success -If the operation is successful, the function will return a object of type `CreateApplicationKeyTxSuccess`. +If the operation is successful, the function will return a object of type `CreateApplicationKeyTx`. ```rust -CreateApplicationKeyTxSuccess { +CreateApplicationKeyTx { event: ApplicationKeyCreated { key: BoundedVec(...), owner: AccountId32(...), @@ -111,7 +111,7 @@ Origin Level: Signed ### Interface ```rust -async fn submit_data(&self, data: Data, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; +async fn submit_data(&self, data: Data, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; ``` #### Parameters @@ -175,10 +175,10 @@ If the operation fails, the function will return an error message indicating the #### On Success -If the operation is successful, the function will return a object of type `SubmitDataTxSuccess`. +If the operation is successful, the function will return a object of type `SubmitDataTx`. ```rust -SubmitDataTxSuccess { +SubmitDataTx { event: DataSubmitted { who: AccountId32(...), data_hash: 0x8846d900ea89aab9bce96402846c0ac74a853acc00cb99ff5ddb1a0f052594bd, @@ -209,7 +209,7 @@ Origin Level: Root ### Interface ```rust -async fn submit_block_length_proposal(&self, rows: u32, cols: u32, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; +async fn submit_block_length_proposal(&self, rows: u32, cols: u32, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; ``` #### Parameters @@ -278,7 +278,7 @@ Origin Level: Root ### Interface ```rust -async fn set_application_key(&self, old_key: Key, new_key: Key, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; +async fn set_application_key(&self, old_key: Key, new_key: Key, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; ``` #### Parameters @@ -352,7 +352,7 @@ Origin Level: Root ### Interface ```rust -async fn set_submit_data_fee_modifier(&self, modifier: DispatchFeeModifier, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; +async fn set_submit_data_fee_modifier(&self, modifier: DispatchFeeModifier, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; ``` #### Parameters @@ -420,10 +420,10 @@ If the operation fails, the function will return an error message indicating the #### On Success -If the operation is successful, the function will return a object of type `SetSubmitDataFeeModifierTxSuccess`. +If the operation is successful, the function will return a object of type `SetSubmitDataFeeModifierTx`. ```rust -SetSubmitDataFeeModifierTxSuccess { +SetSubmitDataFeeModifierTx { event: SubmitDataFeeModifierSet { value: DispatchFeeModifier { weight_maximum_fee: None, @@ -462,7 +462,7 @@ Origin Level: Signed ### Interface ```rust -async fn transfer_keep_alive(&self, dest: &str, value: u128, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; +async fn transfer_keep_alive(&self, dest: &str, value: u128, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; ``` #### Parameters @@ -527,10 +527,10 @@ If the operation fails, the function will return an error message indicating the #### On Success -If the operation is successful, the function will return a object of type `TransferKeepAliveTxSuccess`. +If the operation is successful, the function will return a object of type `TransferKeepAliveTx`. ```rust -TransferKeepAliveTxSuccess { +TransferKeepAliveTx { event: Transfer { from: AccountId32(...), to: AccountId32(...), @@ -559,7 +559,7 @@ Origin Level: Signed ### Interface ```rust -async fn transfer_allow_death(&self, dest: &str, value: u128, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; +async fn transfer_allow_death(&self, dest: &str, value: u128, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; ``` #### Parameters @@ -628,10 +628,10 @@ If the operation fails, the function will return an error message indicating the #### On Success -If the operation is successful, the function will return a object of type `TransferAllowDeathTxSuccess`. +If the operation is successful, the function will return a object of type `TransferAllowDeathTx`. ```rust -TransferAllowDeathTxSuccess { +TransferAllowDeathTx { event: Transfer { from: AccountId32(...), to: AccountId32(...), @@ -661,7 +661,7 @@ Origin Level: Signed ### Interface ```rust -async fn transfer_all(&self, dest: &str, keep_alive: bool, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; +async fn transfer_all(&self, dest: &str, keep_alive: bool, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; ``` #### Parameters @@ -730,10 +730,10 @@ If the operation fails, the function will return an error message indicating the #### On Success -If the operation is successful, the function will return a object of type `TransferAllTxSuccess`. +If the operation is successful, the function will return a object of type `TransferAllTx`. ```rust -TransferAllTxSuccess { +TransferAllTx { event: Transfer { from: AccountId32(...), to: AccountId32(...), @@ -1536,7 +1536,7 @@ Origin Level: Signed ### Interface ```rust -async fn create(&self, amount: u128, root: &str, nominator: &str, bouncer: &str, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; +async fn create(&self, amount: u128, root: &str, nominator: &str, bouncer: &str, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; ``` #### Parameters @@ -1613,10 +1613,10 @@ If the operation fails, the function will return an error message indicating the #### On Success -If the operation is successful, the function will return a object of type `PoolCreateTxSuccess`. +If the operation is successful, the function will return a object of type `CreateTx`. ```rust -PoolCreateTxSuccess { +CreateTx { event: Created { depositor: AccountId32(...), pool_id: 1, @@ -1650,7 +1650,7 @@ Origin Level: Signed ### Interface ```rust -async fn create_with_pool_id(&self, amount: u128, root: &str, nominator: &str, bouncer: &str, pool_id: u32, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; +async fn create_with_pool_id(&self, amount: u128, root: &str, nominator: &str, bouncer: &str, pool_id: u32, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; ``` #### Parameters @@ -1730,10 +1730,10 @@ If the operation fails, the function will return an error message indicating the #### On Success -If the operation is successful, the function will return a object of type `PoolCreateWithPoolIdTxSuccess`. +If the operation is successful, the function will return a object of type `CreateWithPoolIdTx`. ```rust -PoolCreateWithPoolIdTxSuccess { +CreateWithPoolIdTx { event: Created { depositor: AccountId32(...), pool_id: 0, @@ -1767,7 +1767,7 @@ Origin Level: Signed ### Interface ```rust -async fn join(&self, amount: u128, pool_id: u32, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; +async fn join(&self, amount: u128, pool_id: u32, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; ``` #### Parameters @@ -1832,10 +1832,10 @@ If the operation fails, the function will return an error message indicating the #### On Success -If the operation is successful, the function will return a object of type `PoolJoinTxSuccess`. +If the operation is successful, the function will return a object of type `JoinTx`. ```rust -PoolJoinTxSuccess { +JoinTx { event: Bonded { member: AccountId32(...), pool_id: 1, @@ -1865,7 +1865,7 @@ Origin Level: Signed ### Interface ```rust -async fn nominate(&self, pool_id: u32, validators: Vec, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; +async fn nominate(&self, pool_id: u32, validators: Vec, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; ``` #### Parameters @@ -1933,10 +1933,10 @@ If the operation fails, the function will return an error message indicating the #### On Success -If the operation is successful, the function will return a object of type `PoolNominateTxSuccess`. +If the operation is successful, the function will return a object of type `NominateTx`. ```rust -PoolNominateTxSuccess { +NominateTx { events: ExtrinsicEvents { ext_hash: 0xde74e9df59143b84ed216e4e52fd58ec8bd557fae4b54d992a9abb1adf750446, idx: 1, @@ -1967,7 +1967,7 @@ Origin Level: Signed ### Interface ```rust -async fn bond_extra(&self, extra: BondExtra, wait_for: WaitFor, account: &Keypair, options: Option,) -> Result; +async fn bond_extra(&self, extra: BondExtra, wait_for: WaitFor, account: &Keypair, options: Option,) -> Result; ``` #### Parameters @@ -2030,10 +2030,10 @@ If the operation fails, the function will return an error message indicating the #### On Success -If the operation is successful, the function will return a object of type `PoolBondExtraTxSuccess`. +If the operation is successful, the function will return a object of type `BondExtraTx`. ```rust -PoolBondExtraTxSuccess { +BondExtraTx { event: Bonded { member: AccountId32(...), pool_id: 1, @@ -2063,7 +2063,7 @@ Origin Level: Signed ### Interface ```rust -async fn set_commission(&self, pool_id: u32, new_commission: Option, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; +async fn set_commission(&self, pool_id: u32, new_commission: Option, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; ``` #### Parameters @@ -2137,10 +2137,10 @@ If the operation fails, the function will return an error message indicating the #### On Success -If the operation is successful, the function will return a object of type `PoolSetCommissionTxSuccess`. +If the operation is successful, the function will return a object of type `SetCommissionTx`. ```rust -PoolSetCommissionTxSuccess { +SetCommissionTx { event: PoolCommissionUpdated { pool_id: 1, current: Some( @@ -2175,7 +2175,7 @@ Origin Level: Signed ### Interface ```rust -async fn set_metadata(&self, pool_id: u32, metadata: Vec, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; +async fn set_metadata(&self, pool_id: u32, metadata: Vec, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; ``` #### Parameters @@ -2240,10 +2240,10 @@ If the operation fails, the function will return an error message indicating the #### On Success -If the operation is successful, the function will return a object of type `PoolSetMetadataTxSuccess`. +If the operation is successful, the function will return a object of type `SetMetadataTx`. ```rust -PoolSetMetadataTxSuccess { +SetMetadataTx { events: ExtrinsicEvents { ext_hash: 0x0b4c5b4dbc573e88fa96729622c8f3a303ae35db2144365ed951b55c9a9a0f9e, idx: 1, @@ -2267,7 +2267,7 @@ Origin Level: Signed ### Interface ```rust -async fn set_claim_permission(&self, permission: Permission, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; +async fn set_claim_permission(&self, permission: Permission, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; ``` #### Parameters @@ -2332,10 +2332,10 @@ If the operation fails, the function will return an error message indicating the #### On Success -If the operation is successful, the function will return a object of type `PoolSetClaimPermissionTxSuccess`. +If the operation is successful, the function will return a object of type `SetClaimPermissionTx`. ```rust -PoolSetClaimPermissionTxSuccess { +SetClaimPermissionTx { events: ExtrinsicEvents { ext_hash: 0xf69cea124fe7823532821f73d3cb4c93dac58951b3bc28b770c54fc323b94bc0, idx: 1, @@ -2359,7 +2359,7 @@ Origin Level: Signed ### Interface ```rust -async fn set_state(&self, pool_id: u32, state: State, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; +async fn set_state(&self, pool_id: u32, state: State, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; ``` #### Parameters @@ -2424,10 +2424,10 @@ If the operation fails, the function will return an error message indicating the #### On Success -If the operation is successful, the function will return a object of type `PoolSetStateTxSuccess`. +If the operation is successful, the function will return a object of type `SetStateTx`. ```rust -PoolSetStateTxSuccess { +SetStateTx { event: None, events: ExtrinsicEvents { ext_hash: 0xa5745e02a0a257e79b193efc66c9ac85138cb2a454eb52235687013430e1b932, @@ -2452,7 +2452,7 @@ Origin Level: Signed ### Interface ```rust -async fn unbond(&self, member_account: &str, unbonding_points: u128, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; +async fn unbond(&self, member_account: &str, unbonding_points: u128, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; ``` #### Parameters @@ -2523,10 +2523,10 @@ If the operation fails, the function will return an error message indicating the #### On Success -If the operation is successful, the function will return a object of type `PoolUnbondTxSuccess`. +If the operation is successful, the function will return a object of type `UnbondTx`. ```rust -PoolUnbondTxSuccess { +UnbondTx { event: Some( Unbonded { member: AccountId32(...), @@ -2559,7 +2559,7 @@ Origin Level: Signed ### Interface ```rust -async fn withdraw_unbonded(&self, member_account: &str, num_slashing_spans: u32, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; +async fn withdraw_unbonded(&self, member_account: &str, num_slashing_spans: u32, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; ``` #### Parameters @@ -2630,10 +2630,10 @@ If the operation fails, the function will return an error message indicating the #### On Success -If the operation is successful, the function will return a object of type `PoolWithdrawUnbondedTxSuccess`. +If the operation is successful, the function will return a object of type `WithdrawUnbondedTx`. ```rust -PoolWithdrawUnbondedTxSuccess { +WithdrawUnbondedTx { event: Some( Withdrawn { member: AccountId32(...), @@ -2665,7 +2665,7 @@ Origin Level: Signed ### Interface ```rust -async fn chill(&self, pool_id: u32, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; +async fn chill(&self, pool_id: u32, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; ``` #### Parameters @@ -2728,10 +2728,10 @@ If the operation fails, the function will return an error message indicating the #### On Success -If the operation is successful, the function will return a object of type `PoolChillTxSuccess`. +If the operation is successful, the function will return a object of type `ChillTx`. ```rust -PoolChillTxSuccess { +ChillTx { events: ExtrinsicEvents { ext_hash: 0xef47e23b303005a010bfc854a73143596849fc6e2b3db4b01d4b1f53800cde94, idx: 1, @@ -2755,7 +2755,7 @@ Origin Level: Signed ### Interface ```rust -async fn claim_payout(&self, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; +async fn claim_payout(&self, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; ``` #### Parameters @@ -2816,10 +2816,10 @@ If the operation fails, the function will return an error message indicating the #### On Success -If the operation is successful, the function will return a object of type `PoolClaimPayoutTxSuccess`. +If the operation is successful, the function will return a object of type `ClaimPayoutTx`. ```rust -PoolClaimPayoutTxSuccess { +ClaimPayoutTx { event: Some( PaidOut { member: AccountId32(...), @@ -2850,7 +2850,7 @@ Origin Level: Signed ### Interface ```rust -async fn claim_commission(&self, pool_id: u32, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; +async fn claim_commission(&self, pool_id: u32, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; ``` #### Parameters @@ -2913,10 +2913,10 @@ If the operation fails, the function will return an error message indicating the #### On Success -If the operation is successful, the function will return a object of type `PoolClaimCommissionTxSuccess`. +If the operation is successful, the function will return a object of type `ClaimCommissionTx`. ```rust -PoolClaimCommissionTxSuccess { +ClaimCommissionTx { event: PoolCommissionClaimed { pool_id: 1, commission: 2952048955361375559, @@ -2944,7 +2944,7 @@ Origin Level: Signed ### Interface ```rust -async fn claim_payout_other(&self, other: &str, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; +async fn claim_payout_other(&self, other: &str, wait_for: WaitFor, account: &Keypair, options: Option) -> Result; ``` #### Parameters @@ -3007,10 +3007,10 @@ If the operation fails, the function will return an error message indicating the #### On Success -If the operation is successful, the function will return a object of type `PoolClaimPayoutOtherTxSuccess`. +If the operation is successful, the function will return a object of type `ClaimPayoutOtherTx`. ```rust -PoolClaimPayoutOtherTxSuccess { +ClaimPayoutOtherTx { event: Some( PaidOut { member: AccountId32(...), diff --git a/avail-rust/docs/utils/README.md b/avail-rust/docs/utils/README.md index 1c322e6a0..58659b591 100644 --- a/avail-rust/docs/utils/README.md +++ b/avail-rust/docs/utils/README.md @@ -5,7 +5,7 @@ ### Interface ```rust -async fn fetch_transaction(&self, block_hash: H256, tx_hash: H256) -> Result, FetchTransactionError>; +async fn fetch_transaction(&self, block_hash: H256, tx_hash: H256) -> Result, FetchTransactionError>; ``` #### Parameters @@ -75,7 +75,7 @@ async fn main() -> Result<(), String> { ### Interface ```rust -async fn fetch_transactions(&self, block_hash: H256) -> Result, FetchTransactionError> ; +async fn fetch_transactions(&self, block_hash: H256) -> Result, FetchTransactionError> ; ``` #### Parameters @@ -153,14 +153,14 @@ async fn main() -> Result<(), String> { ### Interface ```rust -async fn progress_transaction(&self, maybe_tx_progress: Result, subxt::Error>, wait_for: WaitFor) -> Result; +async fn progress_transaction(&self, maybe_tx_progress: Result, subxt::Error>, wait_for: WaitFor) -> Result; ``` #### Parameters | parameter | type | optional | description | | ----------------- | -------------------------------------------------- | -------- | ---------------------------------------- | -| maybe_tx_progress | Result, subxt::Error> | false | transaction in progress | +| maybe_tx_progress | Result, subxt::Error> | false | transaction in progress | | waitFor | WaitFor | false | wait for block inclusion or finalization | #### Return value diff --git a/avail-rust/src/block.rs b/avail-rust/src/block.rs new file mode 100644 index 000000000..f0db9e001 --- /dev/null +++ b/avail-rust/src/block.rs @@ -0,0 +1,147 @@ +use primitive_types::H256; +use subxt::blocks::StaticExtrinsic; + +use crate::{ABlock, ABlocksClient, AExtrinsicDetails, AExtrinsics, AFoundExtrinsic}; + +pub struct Block { + pub block: ABlock, + pub transactions: AExtrinsics, +} + +impl Block { + pub async fn new(client: &ABlocksClient, block_hash: H256) -> Result { + let (block, transactions) = block_transactions(client, block_hash).await?; + Ok(Self { + block, + transactions, + }) + } + + pub fn transaction_count(&self) -> usize { + return transaction_count(&self.transactions); + } + + pub fn transaction_by_signer(&self, signer: &str) -> Vec { + return transaction_by_signer(&self.transactions, signer); + } + + pub fn transaction_by_signer_static( + &self, + signer: &str, + ) -> Vec> { + transaction_by_signer_static::(&self.transactions, signer) + } + + pub fn transaction_by_index(&self, tx_index: u32) -> Result { + return transaction_by_index(&self.transactions, tx_index); + } + + pub fn transaction_by_index_static( + &self, + tx_index: u32, + ) -> Result, String> { + transaction_by_index_static::(&self.transactions, tx_index) + } + + pub fn transaction_by_hash(&self, tx_hash: H256) -> Vec { + return transaction_by_hash(&self.transactions, tx_hash); + } + + pub fn transaction_by_hash_static( + &self, + tx_hash: H256, + ) -> Vec> { + transaction_by_hash_static::(&self.transactions, tx_hash) + } +} + +pub async fn block_transactions( + client: &ABlocksClient, + block_hash: H256, +) -> Result<(ABlock, AExtrinsics), subxt::Error> { + let block = client.at(block_hash).await?; + let transactions = block.extrinsics().await?; + Ok((block, transactions)) +} + +pub fn transaction_count(transactions: &AExtrinsics) -> usize { + return transactions.len(); +} + +pub fn transaction_by_signer(transactions: &AExtrinsics, signer: &str) -> Vec { + let mut result: Vec = Vec::new(); + for tx in transactions.iter() { + if tx.signature_bytes() == Some(signer.as_bytes()) { + result.push(tx) + } + } + + result +} + +pub fn transaction_by_signer_static( + transactions: &AExtrinsics, + signer: &str, +) -> Vec> { + let mut result: Vec> = Vec::new(); + for tx in transactions.find::() { + if let Ok(tx) = tx { + if tx.details.signature_bytes() == Some(signer.as_bytes()) { + result.push(tx) + } + } + } + + result +} + +pub fn transaction_by_index( + transactions: &AExtrinsics, + tx_index: u32, +) -> Result { + transactions + .iter() + .skip(tx_index as usize) + .next() + .ok_or(String::from("Transaction not found")) +} + +pub fn transaction_by_index_static( + transactions: &AExtrinsics, + tx_index: u32, +) -> Result, String> { + let tx = transactions + .find::() + .skip(tx_index as usize) + .next() + .ok_or(String::from("Transaction not found"))?; + + tx.map_err(|_| String::from("Transaction has different type")) +} + +pub fn transaction_by_hash(transactions: &AExtrinsics, tx_hash: H256) -> Vec { + let mut result: Vec = Vec::new(); + for tx in transactions.iter() { + if tx.hash() == tx_hash { + result.push(tx) + } + } + + result +} + +pub fn transaction_by_hash_static( + transactions: &AExtrinsics, + tx_hash: H256, +) -> Vec> { + let mut result: Vec> = Vec::new(); + for tx in transactions.find::() { + if let Ok(tx) = tx { + if tx.details.hash() == tx_hash { + result.push(tx); + } + } + } + + result +} diff --git a/avail-rust/src/config.rs b/avail-rust/src/config.rs index ba5b1b17e..b86bc7859 100644 --- a/avail-rust/src/config.rs +++ b/avail-rust/src/config.rs @@ -1,7 +1,10 @@ use crate::{AvailHeader, DefaultExtrinsicParams, DefaultExtrinsicParamsBuilder}; use subxt::{ backend::legacy::rpc_methods::{Block as BlockRPC, BlockDetails as BlockDetailsRPC}, - blocks::BlocksClient, + blocks::{ + Block, BlocksClient, ExtrinsicDetails, ExtrinsicEvents, Extrinsics, FoundExtrinsic, + StaticExtrinsic, + }, config::substrate::BlakeTwo256, tx::{TxClient, TxInBlock}, utils::{AccountId32, MultiAddress, MultiSignature, H256}, @@ -17,12 +20,17 @@ pub type BlockNumber = u32; pub type BlockHash = H256; /// Clients -pub type Api = OnlineClient; -pub type AvailBlocksClient = BlocksClient; -pub type TxApi = TxClient; +pub type AOnlineClient = OnlineClient; +pub type ABlocksClient = BlocksClient; +pub type ABlock = Block; +pub type AExtrinsicEvents = ExtrinsicEvents; +pub type AExtrinsicDetails = ExtrinsicDetails; +pub type AExtrinsics = Extrinsics; +pub type AFoundExtrinsic = FoundExtrinsic; +pub type ATxClient = TxClient; /// TX status -pub type TransactionInBlock = TxInBlock; +pub type TransactionInBlock = TxInBlock; /// Used only when chain_getBlock RPC is called. This is part of legacy baggage. pub type AvailBlockDetailsRPC = BlockDetailsRPC; diff --git a/avail-rust/src/lib.rs b/avail-rust/src/lib.rs index 8b9468749..50cf222e0 100644 --- a/avail-rust/src/lib.rs +++ b/avail-rust/src/lib.rs @@ -6,6 +6,7 @@ mod sdk; mod utils; // Export types for internal and external consumption +pub mod block; pub mod primitives; pub mod transactions; diff --git a/avail-rust/src/rpcs.rs b/avail-rust/src/rpcs.rs index 4137a51b1..f18a6b089 100644 --- a/avail-rust/src/rpcs.rs +++ b/avail-rust/src/rpcs.rs @@ -142,11 +142,7 @@ impl System { } pub async fn account_next_index(&self, account: String) -> Result { - let value: u32 = self - .client - .request("system_accountNextIndex", rpc_params![account]) - .await?; - Ok(value) + account_next_index(&self.client, account).await } pub async fn chain(&self) -> Result { @@ -223,6 +219,13 @@ impl System { } } +pub async fn account_next_index(client: &RpcClient, account: String) -> Result { + let value: u32 = client + .request("system_accountNextIndex", rpc_params![account]) + .await?; + Ok(value) +} + #[derive(Clone)] pub struct Chain { client: RpcClient, @@ -237,41 +240,58 @@ impl Chain { &self, at: Option, ) -> Result { - let value: AvailBlockDetailsRPC = self - .client - .request("chain_getBlock", rpc_params![at]) - .await?; - Ok(value) + get_block(&self.client, at).await } pub async fn get_block_hash( &self, block_number: Option, ) -> Result { - let value: BlockHash = self - .client - .request("chain_getBlockHash", rpc_params![block_number]) - .await?; - Ok(value) + get_block_hash(&self.client, block_number).await } pub async fn get_finalized_head(&self) -> Result { - let value: BlockHash = self - .client - .request("chain_getFinalizedHead", rpc_params![]) - .await?; - Ok(value) + get_finalized_head(&self.client).await } pub async fn get_header(&self, at: Option) -> Result { - let value: AvailHeader = self - .client - .request("chain_getHeader", rpc_params![at]) - .await?; - Ok(value) + get_header(&self.client, at).await } } +pub async fn get_block( + client: &RpcClient, + at: Option, +) -> Result { + let value: AvailBlockDetailsRPC = client.request("chain_getBlock", rpc_params![at]).await?; + Ok(value) +} + +pub async fn get_block_hash( + client: &RpcClient, + block_number: Option, +) -> Result { + let value: BlockHash = client + .request("chain_getBlockHash", rpc_params![block_number]) + .await?; + Ok(value) +} + +pub async fn get_finalized_head(client: &RpcClient) -> Result { + let value: BlockHash = client + .request("chain_getFinalizedHead", rpc_params![]) + .await?; + Ok(value) +} + +pub async fn get_header( + client: &RpcClient, + at: Option, +) -> Result { + let value: AvailHeader = client.request("chain_getHeader", rpc_params![at]).await?; + Ok(value) +} + #[derive(Clone)] pub struct Author { client: RpcClient, diff --git a/avail-rust/src/sdk.rs b/avail-rust/src/sdk.rs index 7f7461afc..169d68fa8 100644 --- a/avail-rust/src/sdk.rs +++ b/avail-rust/src/sdk.rs @@ -1,8 +1,8 @@ -use crate::{rpcs::Rpc, transactions::Transactions, utils::Util, Api}; +use crate::{rpcs::Rpc, transactions::Transactions, utils::Util, AOnlineClient}; #[derive(Clone)] pub struct SDK { - pub api: Api, + pub api: AOnlineClient, pub tx: Transactions, pub util: Util, pub rpc: Rpc, @@ -12,7 +12,7 @@ impl SDK { pub async fn new(endpoint: &str) -> Result> { let rpc = Rpc::new(endpoint, true).await?; // Cloning RpcClient is cheaper and doesn't create a new WS connection. - let api = Api::from_rpc_client(rpc.client.clone()).await?; + let api = AOnlineClient::from_rpc_client(rpc.client.clone()).await?; Ok(SDK { tx: Transactions::new(api.clone(), rpc.clone()), @@ -24,7 +24,7 @@ impl SDK { pub async fn new_insecure(endpoint: &str) -> Result> { let rpc = Rpc::new(endpoint, false).await?; - let api = Api::from_rpc_client(rpc.client.clone()).await?; + let api = AOnlineClient::from_rpc_client(rpc.client.clone()).await?; Ok(SDK { tx: Transactions::new(api.clone(), rpc.clone()), diff --git a/avail-rust/src/transactions/balances.rs b/avail-rust/src/transactions/balances.rs index d65b4941b..13b2c1472 100644 --- a/avail-rust/src/transactions/balances.rs +++ b/avail-rust/src/transactions/balances.rs @@ -1,62 +1,48 @@ -use crate::rpcs::Rpc; use crate::sdk::WaitFor; -use crate::{avail, AccountId, AvailBlocksClient, AvailConfig, TxApi, H256}; - -use std::str::FromStr; -use subxt::blocks::ExtrinsicEvents; -use subxt_signer::sr25519::Keypair; +use crate::{avail, ABlocksClient, ATxClient, AccountId}; use avail::balances::events as BalancesEvents; use avail::system::events as SystemEvents; +use std::str::FromStr; +use subxt::backend::rpc::RpcClient; +use subxt_signer::sr25519::Keypair; -use super::options::{from_options_to_params, Options}; -use super::progress_transaction_ex; +use super::options::Options; +use super::{sign_and_submit_and_progress_transaction, TxResultDetails}; #[derive(Debug)] -pub struct TransferAllTxSuccess { +pub struct TransferAllTx { pub event: BalancesEvents::Transfer, pub event2: Option, - pub events: ExtrinsicEvents, - pub tx_hash: H256, - pub tx_index: u32, - pub block_hash: H256, - pub block_number: u32, + pub details: TxResultDetails, } #[derive(Debug)] -pub struct TransferAllowDeathTxSuccess { +pub struct TransferAllowDeathTx { pub event: BalancesEvents::Transfer, pub event2: Option, - pub events: ExtrinsicEvents, - pub tx_hash: H256, - pub tx_index: u32, - pub block_hash: H256, - pub block_number: u32, + pub details: TxResultDetails, } #[derive(Debug)] -pub struct TransferKeepAliveTxSuccess { +pub struct TransferKeepAliveTx { pub event: BalancesEvents::Transfer, - pub events: ExtrinsicEvents, - pub tx_hash: H256, - pub tx_index: u32, - pub block_hash: H256, - pub block_number: u32, + pub details: TxResultDetails, } #[derive(Clone)] pub struct Balances { - api: TxApi, - rpc_client: Rpc, - blocks: AvailBlocksClient, + tx_client: ATxClient, + blocks_client: ABlocksClient, + rpc_client: RpcClient, } impl Balances { - pub fn new(api: TxApi, rpc_client: Rpc, blocks: AvailBlocksClient) -> Self { + pub fn new(tx_client: ATxClient, rpc_client: RpcClient, blocks_client: ABlocksClient) -> Self { Self { - api, + tx_client, + blocks_client, rpc_client, - blocks, } } @@ -67,42 +53,36 @@ impl Balances { wait_for: WaitFor, account: &Keypair, options: Option, - ) -> Result { + ) -> Result { let dest = match AccountId::from_str(dest) { Ok(dest) => dest, Err(error) => return Err(std::format!("{:?}", error)), }; - let account_id = account.public_key().to_account_id(); - let params = - from_options_to_params(options, &self.rpc_client, account_id, &self.blocks).await?; let call = avail::tx().balances().transfer_all(dest.into(), keep_alive); - - let maybe_tx_progress = self - .api - .sign_and_submit_then_watch(&call, account, params) - .await; - - let (events, data) = - progress_transaction_ex(maybe_tx_progress, wait_for, &self.blocks).await?; - let (block_hash, block_number, tx_hash, tx_index) = data; - - let event = events.find_first::(); + let details = sign_and_submit_and_progress_transaction( + &self.tx_client, + &self.blocks_client, + &self.rpc_client, + account, + call, + wait_for, + options, + ) + .await?; + + let event = details.events.find_first::(); let Some(event) = event.ok().flatten() else { return Err(String::from("Failed to find Transfer event")); }; - let event2 = events.find_first::(); + let event2 = details.events.find_first::(); let event2 = event2.ok().flatten(); - Ok(TransferAllTxSuccess { + Ok(TransferAllTx { event, event2, - events, - tx_hash, - tx_index, - block_hash, - block_number, + details, }) } @@ -113,44 +93,38 @@ impl Balances { wait_for: WaitFor, account: &Keypair, options: Option, - ) -> Result { + ) -> Result { let dest = match AccountId::from_str(dest) { Ok(dest) => dest, Err(error) => return Err(std::format!("{:?}", error)), }; - let account_id = account.public_key().to_account_id(); - let params = - from_options_to_params(options, &self.rpc_client, account_id, &self.blocks).await?; let call = avail::tx() .balances() .transfer_allow_death(dest.into(), amount); - - let maybe_tx_progress = self - .api - .sign_and_submit_then_watch(&call, account, params) - .await; - - let (events, data) = - progress_transaction_ex(maybe_tx_progress, wait_for, &self.blocks).await?; - let (block_hash, block_number, tx_hash, tx_index) = data; - - let event = events.find_first::(); + let details = sign_and_submit_and_progress_transaction( + &self.tx_client, + &self.blocks_client, + &self.rpc_client, + account, + call, + wait_for, + options, + ) + .await?; + + let event = details.events.find_first::(); let Some(event) = event.ok().flatten() else { return Err(String::from("Failed to find Transfer event")); }; - let event2 = events.find_first::(); + let event2 = details.events.find_first::(); let event2 = event2.ok().flatten(); - Ok(TransferAllowDeathTxSuccess { + Ok(TransferAllowDeathTx { event, event2, - events, - tx_hash, - tx_index, - block_hash, - block_number, + details, }) } @@ -161,40 +135,31 @@ impl Balances { wait_for: WaitFor, account: &Keypair, options: Option, - ) -> Result { + ) -> Result { let dest = match AccountId::from_str(dest) { Ok(dest) => dest, Err(error) => return Err(std::format!("{:?}", error)), }; - let account_id = account.public_key().to_account_id(); - let params = - from_options_to_params(options, &self.rpc_client, account_id, &self.blocks).await?; let call = avail::tx() .balances() .transfer_keep_alive(dest.into(), value); - - let maybe_tx_progress = self - .api - .sign_and_submit_then_watch(&call, account, params) - .await; - - let (events, data) = - progress_transaction_ex(maybe_tx_progress, wait_for, &self.blocks).await?; - let (block_hash, block_number, tx_hash, tx_index) = data; - - let event = events.find_first::(); + let details = sign_and_submit_and_progress_transaction( + &self.tx_client, + &self.blocks_client, + &self.rpc_client, + account, + call, + wait_for, + options, + ) + .await?; + + let event = details.events.find_first::(); let Some(event) = event.ok().flatten() else { return Err(String::from("Failed to find Transfer event")); }; - Ok(TransferKeepAliveTxSuccess { - event, - events, - tx_hash, - tx_index, - block_hash, - block_number, - }) + Ok(TransferKeepAliveTx { event, details }) } } diff --git a/avail-rust/src/transactions/da.rs b/avail-rust/src/transactions/da.rs index a2039fa2b..6d4750992 100644 --- a/avail-rust/src/transactions/da.rs +++ b/avail-rust/src/transactions/da.rs @@ -2,85 +2,64 @@ use crate::api_dev::api::data_availability::calls::types::create_application_key use crate::api_dev::api::data_availability::calls::types::submit_data::Data; use crate::api_dev::api::runtime_types::frame_support::dispatch::DispatchFeeModifier; use crate::api_dev::api::Call; -use crate::rpcs::Rpc; use crate::sdk::WaitFor; -use crate::utils_raw::{fetch_transaction, progress_transaction}; -use crate::{avail, AvailBlocksClient, AvailConfig, BlockHash, TxApi}; - -use subxt::blocks::ExtrinsicEvents; -use subxt_signer::sr25519::Keypair; +use crate::utils_raw::fetch_transaction; +use crate::{avail, ABlocksClient, ATxClient}; use avail::data_availability::calls::types as DataAvailabilityCalls; use avail::data_availability::events as DataAvailabilityEvents; use avail::sudo::events as SudoEvents; +use primitive_types::H256; +use subxt::backend::rpc::RpcClient; +use subxt_signer::sr25519::Keypair; -use super::options::{from_options_to_params, Options}; -use super::{block_and_tx_hash, progress_transaction_ex}; +use super::options::Options; +use super::{sign_and_submit_and_progress_transaction, TxResultDetails}; #[derive(Debug)] -pub struct SubmitDataTxSuccess { +pub struct SubmitDataTx { pub event: DataAvailabilityEvents::DataSubmitted, - pub events: ExtrinsicEvents, - pub tx_data: DataAvailabilityCalls::SubmitData, - pub tx_hash: BlockHash, - pub tx_index: u32, - pub block_hash: BlockHash, - pub block_number: u32, + pub data: DataAvailabilityCalls::SubmitData, + pub details: TxResultDetails, } #[derive(Debug)] -pub struct CreateApplicationKeyTxSuccess { +pub struct CreateApplicationKeyTx { pub event: DataAvailabilityEvents::ApplicationKeyCreated, - pub events: ExtrinsicEvents, - pub tx_hash: BlockHash, - pub tx_index: u32, - pub block_hash: BlockHash, - pub block_number: u32, + pub details: TxResultDetails, } #[derive(Debug)] -pub struct SetApplicationKeyTxSuccess { +pub struct SetApplicationKeyTx { pub event: DataAvailabilityEvents::ApplicationKeySet, - pub events: ExtrinsicEvents, - pub tx_hash: BlockHash, - pub tx_index: u32, - pub block_hash: BlockHash, - pub block_number: u32, + pub details: TxResultDetails, } #[derive(Debug)] -pub struct SubmitBlockLengthProposalTxSuccess { +pub struct SubmitBlockLengthProposalTx { pub event: DataAvailabilityEvents::BlockLengthProposalSubmitted, - pub events: ExtrinsicEvents, - pub tx_hash: BlockHash, - pub tx_index: u32, - pub block_hash: BlockHash, - pub block_number: u32, + pub details: TxResultDetails, } #[derive(Debug)] -pub struct SetSubmitDataFeeModifierTxSuccess { +pub struct SetSubmitDataFeeModifierTx { pub event: DataAvailabilityEvents::SubmitDataFeeModifierSet, - pub events: ExtrinsicEvents, - pub tx_hash: BlockHash, - pub tx_index: u32, - pub block_hash: BlockHash, - pub block_number: u32, + pub details: TxResultDetails, } #[derive(Clone)] pub struct DataAvailability { - api: TxApi, - rpc_client: Rpc, - blocks: AvailBlocksClient, + tx_client: ATxClient, + blocks_client: ABlocksClient, + rpc_client: RpcClient, } impl DataAvailability { - pub fn new(api: TxApi, rpc_client: Rpc, blocks: AvailBlocksClient) -> Self { + pub fn new(tx_client: ATxClient, rpc_client: RpcClient, blocks_client: ABlocksClient) -> Self { Self { - api, + tx_client, + blocks_client, rpc_client, - blocks, } } @@ -90,36 +69,41 @@ impl DataAvailability { wait_for: WaitFor, account: &Keypair, options: Option, - ) -> Result { - let account_id = account.public_key().to_account_id(); - let params = - from_options_to_params(options, &self.rpc_client, account_id, &self.blocks).await?; + ) -> Result { let call = avail::tx().data_availability().submit_data(data); - - let maybe_tx_progress = self - .api - .sign_and_submit_then_watch(&call, account, params) - .await; - - let (events, data) = - progress_transaction_ex(maybe_tx_progress, wait_for, &self.blocks).await?; - let (block_hash, block_number, tx_hash, tx_index) = data; - - let event = events.find_first::(); + let details = sign_and_submit_and_progress_transaction( + &self.tx_client, + &self.blocks_client, + &self.rpc_client, + account, + call, + wait_for, + options, + ) + .await?; + + let event = details + .events + .find_first::(); let Some(event) = event.ok().flatten() else { return Err(String::from("Failed to find DataSubmitted event")); }; - let tx_data = tx_data_da_submit_data(block_hash, tx_hash, &self.blocks).await?; - - Ok(SubmitDataTxSuccess { + let block = details + .fetch_block(&self.blocks_client) + .await + .map_err(|e| e.to_string())?; + let mut data = + block.transaction_by_hash_static::(details.tx_hash); + let data = data + .pop() + .ok_or(String::from("Failed to find transaction data"))? + .value; + + Ok(SubmitDataTx { event, - events, - tx_data, - tx_hash, - tx_index, - block_hash, - block_number, + data, + details, }) } @@ -129,34 +113,27 @@ impl DataAvailability { wait_for: WaitFor, account: &Keypair, options: Option, - ) -> Result { - let account_id = account.public_key().to_account_id(); - let params = - from_options_to_params(options, &self.rpc_client, account_id, &self.blocks).await?; + ) -> Result { let call = avail::tx().data_availability().create_application_key(key); - - let maybe_tx_progress = self - .api - .sign_and_submit_then_watch(&call, account, params) - .await; - - let (events, data) = - progress_transaction_ex(maybe_tx_progress, wait_for, &self.blocks).await?; - let (block_hash, block_number, tx_hash, tx_index) = data; - - let event = events.find_first::(); + let details = sign_and_submit_and_progress_transaction( + &self.tx_client, + &self.blocks_client, + &self.rpc_client, + account, + call, + wait_for, + options, + ) + .await?; + + let event = details + .events + .find_first::(); let Some(event) = event.ok().flatten() else { return Err(String::from("Failed to find ApplicationKeyCreated event")); }; - Ok(CreateApplicationKeyTxSuccess { - event, - events, - tx_hash, - tx_index, - block_hash, - block_number, - }) + Ok(CreateApplicationKeyTx { event, details }) } pub async fn set_application_key( @@ -166,10 +143,7 @@ impl DataAvailability { wait_for: WaitFor, account: &Keypair, options: Option, - ) -> Result { - let account_id = account.public_key().to_account_id(); - let params = - from_options_to_params(options, &self.rpc_client, account_id, &self.blocks).await?; + ) -> Result { let call = Call::DataAvailability( avail::runtime_types::da_control::pallet::Call::set_application_key { old_key, @@ -177,24 +151,18 @@ impl DataAvailability { }, ); let sudo = avail::tx().sudo().sudo(call); - - let maybe_tx_progress = self - .api - .sign_and_submit_then_watch(&sudo, account, params) - .await; - - let transaction = progress_transaction(maybe_tx_progress, wait_for).await; - let tx_in_block = match transaction { - Ok(tx_in_block) => tx_in_block, - Err(message) => return Err(message), - }; - - let events = match tx_in_block.wait_for_success().await { - Ok(e) => e, - Err(error) => return Err(error.to_string()), - }; - - let sudo_event = events.find_first::(); + let details = sign_and_submit_and_progress_transaction( + &self.tx_client, + &self.blocks_client, + &self.rpc_client, + account, + sudo, + wait_for, + options, + ) + .await?; + + let sudo_event = details.events.find_first::(); let Some(sudo_event) = sudo_event.ok().flatten() else { return Err(String::from("Failed to find Sudid event")); }; @@ -203,22 +171,14 @@ impl DataAvailability { return Err(std::format!("{:?}", error)); } - let event = events.find_first::(); + let event = details + .events + .find_first::(); let Some(event) = event.ok().flatten() else { return Err(String::from("Failed to find ApplicationKeySet event")); }; - let (block_hash, block_number, tx_hash, tx_index) = - block_and_tx_hash(&tx_in_block, &events, &self.blocks).await?; - - Ok(SetApplicationKeyTxSuccess { - event, - events, - tx_hash, - tx_index, - block_hash, - block_number, - }) + Ok(SetApplicationKeyTx { event, details }) } pub async fn submit_block_length_proposal( @@ -228,10 +188,7 @@ impl DataAvailability { wait_for: WaitFor, account: &Keypair, options: Option, - ) -> Result { - let account_id = account.public_key().to_account_id(); - let params = - from_options_to_params(options, &self.rpc_client, account_id, &self.blocks).await?; + ) -> Result { let call = Call::DataAvailability( avail::runtime_types::da_control::pallet::Call::submit_block_length_proposal { rows, @@ -239,24 +196,18 @@ impl DataAvailability { }, ); let sudo = avail::tx().sudo().sudo(call); - - let maybe_tx_progress = self - .api - .sign_and_submit_then_watch(&sudo, account, params) - .await; - - let transaction = progress_transaction(maybe_tx_progress, wait_for).await; - let tx_in_block = match transaction { - Ok(tx_in_block) => tx_in_block, - Err(message) => return Err(message), - }; - - let events = match tx_in_block.wait_for_success().await { - Ok(e) => e, - Err(error) => return Err(error.to_string()), - }; - - let sudo_event = events.find_first::(); + let details = sign_and_submit_and_progress_transaction( + &self.tx_client, + &self.blocks_client, + &self.rpc_client, + account, + sudo, + wait_for, + options, + ) + .await?; + + let sudo_event = details.events.find_first::(); let Some(sudo_event) = sudo_event.ok().flatten() else { return Err(String::from("Failed to find Sudid event")); }; @@ -265,24 +216,16 @@ impl DataAvailability { return Err(std::format!("{:?}", error)); } - let event = events.find_first::(); + let event = details + .events + .find_first::(); let Some(event) = event.ok().flatten() else { return Err(String::from( "Failed to find BlockLengthProposalSubmitted event", )); }; - let (block_hash, block_number, tx_hash, tx_index) = - block_and_tx_hash(&tx_in_block, &events, &self.blocks).await?; - - Ok(SubmitBlockLengthProposalTxSuccess { - event, - events, - tx_hash, - tx_index, - block_hash, - block_number, - }) + Ok(SubmitBlockLengthProposalTx { event, details }) } pub async fn set_submit_data_fee_modifier( @@ -291,34 +234,25 @@ impl DataAvailability { wait_for: WaitFor, account: &Keypair, options: Option, - ) -> Result { - let account_id = account.public_key().to_account_id(); - let params = - from_options_to_params(options, &self.rpc_client, account_id, &self.blocks).await?; + ) -> Result { let call = Call::DataAvailability( avail::runtime_types::da_control::pallet::Call::set_submit_data_fee_modifier { modifier, }, ); let sudo = avail::tx().sudo().sudo(call); - - let maybe_tx_progress = self - .api - .sign_and_submit_then_watch(&sudo, account, params) - .await; - - let transaction = progress_transaction(maybe_tx_progress, wait_for).await; - let tx_in_block = match transaction { - Ok(tx_in_block) => tx_in_block, - Err(message) => return Err(message), - }; - - let events = match tx_in_block.wait_for_success().await { - Ok(e) => e, - Err(error) => return Err(error.to_string()), - }; - - let sudo_event = events.find_first::(); + let details = sign_and_submit_and_progress_transaction( + &self.tx_client, + &self.blocks_client, + &self.rpc_client, + account, + sudo, + wait_for, + options, + ) + .await?; + + let sudo_event = details.events.find_first::(); let Some(sudo_event) = sudo_event.ok().flatten() else { return Err(String::from("Failed to find Sudid event")); }; @@ -327,34 +261,15 @@ impl DataAvailability { return Err(std::format!("{:?}", error)); } - let event = events.find_first::(); + let event = details + .events + .find_first::(); let Some(event) = event.ok().flatten() else { return Err(String::from( "Failed to find SubmitDataFeeModifierSet event", )); }; - let (block_hash, block_number, tx_hash, tx_index) = - block_and_tx_hash(&tx_in_block, &events, &self.blocks).await?; - - Ok(SetSubmitDataFeeModifierTxSuccess { - event, - events, - tx_hash, - tx_index, - block_hash, - block_number, - }) + Ok(SetSubmitDataFeeModifierTx { event, details }) } } - -pub async fn tx_data_da_submit_data( - block_hash: BlockHash, - tx_hash: BlockHash, - blocks: &AvailBlocksClient, -) -> Result { - let transaction = - fetch_transaction::(block_hash, tx_hash, blocks).await; - let transaction = transaction.map_err(|err| err.to_string())?; - Ok(transaction.value) -} diff --git a/avail-rust/src/transactions/mod.rs b/avail-rust/src/transactions/mod.rs index 127c43515..728563db8 100644 --- a/avail-rust/src/transactions/mod.rs +++ b/avail-rust/src/transactions/mod.rs @@ -8,15 +8,22 @@ mod staking; pub use balances::*; pub use da::*; pub use nom_pools::*; +use options::from_options_to_params; pub use options::{Mortality, Nonce, Options}; pub use session::*; pub use staking::*; +use subxt_signer::sr25519::Keypair; use crate::{ - rpcs::Rpc, utils_raw::progress_transaction, Api, AvailBlocksClient, AvailConfig, - TransactionInBlock, WaitFor, H256, + rpcs::Rpc, utils_raw::progress_transaction, ABlock, ABlocksClient, AExtrinsicDetails, + AOnlineClient, ATxClient, AvailConfig, TransactionInBlock, WaitFor, H256, +}; +use subxt::{ + backend::rpc::RpcClient, + blocks::{ExtrinsicEvents, StaticExtrinsic}, + ext::scale_encode::EncodeAsFields, + tx::{DefaultPayload, TxProgress}, }; -use subxt::{blocks::ExtrinsicEvents, tx::TxProgress}; pub type Params = <::ExtrinsicParams as subxt::config::ExtrinsicParams< @@ -33,20 +40,24 @@ pub struct Transactions { } impl Transactions { - pub fn new(api: Api, rpc_client: Rpc) -> Self { + pub fn new(api: AOnlineClient, rpc_client: Rpc) -> Self { let tx = api.tx(); let blocks = api.blocks(); Self { - balances: Balances::new(tx.clone(), rpc_client.clone(), blocks.clone()), - staking: Staking::new(tx.clone(), rpc_client.clone(), blocks.clone()), + balances: Balances::new(tx.clone(), rpc_client.client.clone(), blocks.clone()), + staking: Staking::new(tx.clone(), rpc_client.client.clone(), blocks.clone()), data_availability: DataAvailability::new( tx.clone(), - rpc_client.clone(), + rpc_client.client.clone(), + blocks.clone(), + ), + session: Session::new(tx.clone(), rpc_client.client.clone(), blocks.clone()), + nomination_pools: NominationPools::new( + tx.clone(), + rpc_client.client.clone(), blocks.clone(), ), - session: Session::new(tx.clone(), rpc_client.clone(), blocks.clone()), - nomination_pools: NominationPools::new(tx.clone(), rpc_client.clone(), blocks.clone()), } } } @@ -54,7 +65,7 @@ impl Transactions { async fn block_and_tx_hash( tx_in_block: &TransactionInBlock, events: &ExtrinsicEvents, - blocks: &AvailBlocksClient, + blocks: &ABlocksClient, ) -> Result<(H256, u32, H256, u32), String> { let tx_hash = tx_in_block.extrinsic_hash(); let tx_index = events.extrinsic_index(); @@ -64,7 +75,7 @@ async fn block_and_tx_hash( Ok((block_hash, block_number, tx_hash, tx_index)) } -async fn get_block_number(blocks: &AvailBlocksClient, block_hash: H256) -> Result { +async fn get_block_number(blocks: &ABlocksClient, block_hash: H256) -> Result { let block_number = blocks .at(block_hash) .await @@ -74,11 +85,34 @@ async fn get_block_number(blocks: &AvailBlocksClient, block_hash: H256) -> Resul Ok(block_number) } +async fn sign_and_submit_and_progress_transaction( + tx_client: &ATxClient, + blocks_client: &ABlocksClient, + rpc_client: &RpcClient, + account: &Keypair, + call: DefaultPayload, + wait_for: WaitFor, + options: Option, +) -> Result +where + T: StaticExtrinsic + EncodeAsFields, +{ + let account_id = account.public_key().to_account_id(); + let params = from_options_to_params(rpc_client, blocks_client, &account_id, options).await?; + + let maybe_tx_progress = tx_client + .sign_and_submit_then_watch(&call, account, params) + .await; + let details = progress_transaction_ex(blocks_client, maybe_tx_progress, wait_for).await?; + + Ok(details) +} + async fn progress_transaction_ex( - maybe_tx_progress: Result, subxt::Error>, + client: &ABlocksClient, + maybe_tx_progress: Result, subxt::Error>, wait_for: WaitFor, - blocks: &AvailBlocksClient, -) -> Result<(ExtrinsicEvents, (H256, u32, H256, u32)), String> { +) -> Result { let transaction = progress_transaction(maybe_tx_progress, wait_for).await; let tx_in_block = match transaction { Ok(tx_in_block) => tx_in_block, @@ -90,9 +124,55 @@ async fn progress_transaction_ex( Err(error) => return Err(error.to_string()), }; - let data = block_and_tx_hash(&tx_in_block, &events, blocks).await?; + let (block_hash, block_number, tx_hash, tx_index) = + block_and_tx_hash(&tx_in_block, &events, client).await?; + let result = TxResultDetails::new( + tx_in_block, + events, + tx_hash, + tx_index, + block_hash, + block_number, + ); + + Ok(result) +} - Ok((events, data)) +#[derive(Debug)] +pub struct TxResultDetails { + pub tx_result: TransactionInBlock, + pub events: ExtrinsicEvents, + pub tx_hash: H256, + pub tx_index: u32, + pub block_hash: H256, + pub block_number: u32, +} + +impl TxResultDetails { + pub fn new( + tx_result: TransactionInBlock, + events: ExtrinsicEvents, + tx_hash: H256, + tx_index: u32, + block_hash: H256, + block_number: u32, + ) -> Self { + Self { + tx_result, + events, + tx_hash, + tx_index, + block_hash, + block_number, + } + } + + pub async fn fetch_block( + &self, + block_client: &ABlocksClient, + ) -> Result { + crate::block::Block::new(block_client, self.block_hash).await + } } #[cfg(test)] diff --git a/avail-rust/src/transactions/nom_pools.rs b/avail-rust/src/transactions/nom_pools.rs index e1e288188..dc431194c 100644 --- a/avail-rust/src/transactions/nom_pools.rs +++ b/avail-rust/src/transactions/nom_pools.rs @@ -1,7 +1,8 @@ -use super::options::{from_options_to_params, Options}; -use super::progress_transaction_ex; +use super::options::Options; +use super::{sign_and_submit_and_progress_transaction, TxResultDetails}; + use crate::avail::{ - nomination_pools::calls::types as NominationPoolsCalls, + self, nomination_pools::calls::types as NominationPoolsCalls, nomination_pools::calls::types::set_claim_permission::Permission, nomination_pools::calls::types::set_commission::NewCommission as NewCommissionOriginal, nomination_pools::calls::types::set_state::State, @@ -9,161 +10,101 @@ use crate::avail::{ runtime_types::pallet_nomination_pools::BondExtra, runtime_types::sp_arithmetic::per_things::Perbill, }; -use crate::rpcs::Rpc; use crate::sdk::WaitFor; use crate::utils_raw::fetch_transaction; -use crate::{avail, AccountId, AvailBlocksClient, AvailConfig, TxApi, H256}; +use crate::{ABlocksClient, ATxClient, AccountId, H256}; + use std::str::FromStr; -use subxt::blocks::ExtrinsicEvents; +use subxt::backend::rpc::RpcClient; use subxt_signer::sr25519::Keypair; #[derive(Debug)] -pub struct PoolCreateTxSuccess { +pub struct CreateTx { pub event: NominationPoolsEvents::Created, pub event2: NominationPoolsEvents::Bonded, - pub events: ExtrinsicEvents, - pub tx_hash: H256, - pub tx_index: u32, - pub block_hash: H256, - pub block_number: u32, + pub details: TxResultDetails, } #[derive(Debug)] -pub struct PoolCreateWithPoolIdTxSuccess { +pub struct CreateWithPoolIdTx { pub event: NominationPoolsEvents::Created, pub event2: NominationPoolsEvents::Bonded, - pub events: ExtrinsicEvents, - pub tx_hash: H256, - pub tx_index: u32, - pub block_hash: H256, - pub block_number: u32, + pub details: TxResultDetails, } #[derive(Debug)] -pub struct PoolJoinTxSuccess { +pub struct JoinTx { pub event: NominationPoolsEvents::Bonded, - pub events: ExtrinsicEvents, - pub tx_hash: H256, - pub tx_index: u32, - pub block_hash: H256, - pub block_number: u32, + pub details: TxResultDetails, } #[derive(Debug)] -pub struct PoolNominateTxSuccess { - pub events: ExtrinsicEvents, - pub tx_data: NominationPoolsCalls::Nominate, - pub tx_hash: H256, - pub tx_index: u32, - pub block_hash: H256, - pub block_number: u32, +pub struct NominateTx { + pub data: NominationPoolsCalls::Nominate, + pub details: TxResultDetails, } #[derive(Debug)] -pub struct PoolBondExtraTxSuccess { +pub struct BondExtraTx { pub event: NominationPoolsEvents::Bonded, - pub events: ExtrinsicEvents, - pub tx_hash: H256, - pub tx_index: u32, - pub block_hash: H256, - pub block_number: u32, + pub details: TxResultDetails, } #[derive(Debug)] -pub struct PoolSetCommissionTxSuccess { +pub struct SetCommissionTx { pub event: NominationPoolsEvents::PoolCommissionUpdated, - pub events: ExtrinsicEvents, - pub tx_hash: H256, - pub tx_index: u32, - pub block_hash: H256, - pub block_number: u32, + pub details: TxResultDetails, } #[derive(Debug)] -pub struct PoolSetStateTxSuccess { +pub struct SetStateTx { pub event: Option, - pub events: ExtrinsicEvents, - pub tx_hash: H256, - pub tx_index: u32, - pub block_hash: H256, - pub block_number: u32, + pub details: TxResultDetails, } #[derive(Debug)] -pub struct PoolClaimPayoutTxSuccess { +pub struct ClaimPayoutTx { pub event: Option, - pub events: ExtrinsicEvents, - pub tx_hash: H256, - pub tx_index: u32, - pub block_hash: H256, - pub block_number: u32, + pub details: TxResultDetails, } #[derive(Debug)] -pub struct PoolChillTxSuccess { - pub events: ExtrinsicEvents, - pub tx_hash: H256, - pub tx_index: u32, - pub block_hash: H256, - pub block_number: u32, +pub struct ChillTx { + pub details: TxResultDetails, } #[derive(Debug)] -pub struct PoolSetClaimPermissionTxSuccess { - pub events: ExtrinsicEvents, - pub tx_hash: H256, - pub tx_index: u32, - pub block_hash: H256, - pub block_number: u32, +pub struct SetClaimPermissionTx { + pub details: TxResultDetails, } #[derive(Debug)] -pub struct PoolClaimCommissionTxSuccess { +pub struct ClaimCommissionTx { pub event: NominationPoolsEvents::PoolCommissionClaimed, - pub events: ExtrinsicEvents, - pub tx_hash: H256, - pub tx_index: u32, - pub block_hash: H256, - pub block_number: u32, + pub details: TxResultDetails, } #[derive(Debug)] -pub struct PoolClaimPayoutOtherTxSuccess { +pub struct ClaimPayoutOtherTx { pub event: Option, - pub events: ExtrinsicEvents, - pub tx_hash: H256, - pub tx_index: u32, - pub block_hash: H256, - pub block_number: u32, + pub details: TxResultDetails, } #[derive(Debug)] -pub struct PoolUnbondTxSuccess { +pub struct UnbondTx { pub event: Option, - pub events: ExtrinsicEvents, - pub tx_hash: H256, - pub tx_index: u32, - pub block_hash: H256, - pub block_number: u32, + pub details: TxResultDetails, } #[derive(Debug)] -pub struct PoolSetMetadataTxSuccess { - pub events: ExtrinsicEvents, - pub tx_hash: H256, - pub tx_index: u32, - pub block_hash: H256, - pub block_number: u32, +pub struct SetMetadataTx { + pub details: TxResultDetails, } #[derive(Debug)] -pub struct PoolWithdrawUnbondedTxSuccess { +pub struct WithdrawUnbondedTx { pub event: Option, - pub events: ExtrinsicEvents, - pub tx_hash: H256, - pub tx_index: u32, - pub block_hash: H256, - pub block_number: u32, + pub details: TxResultDetails, } #[derive(Debug, Clone)] @@ -174,17 +115,17 @@ pub struct NewCommission { #[derive(Clone)] pub struct NominationPools { - api: TxApi, - rpc_client: Rpc, - blocks: AvailBlocksClient, + tx_client: ATxClient, + blocks_client: ABlocksClient, + rpc_client: RpcClient, } impl NominationPools { - pub fn new(api: TxApi, rpc_client: Rpc, blocks: AvailBlocksClient) -> Self { + pub fn new(tx_client: ATxClient, rpc_client: RpcClient, blocks_client: ABlocksClient) -> Self { Self { - api, + tx_client, + blocks_client, rpc_client, - blocks, } } @@ -195,37 +136,29 @@ impl NominationPools { wait_for: WaitFor, account: &Keypair, options: Option, - ) -> Result { + ) -> Result { let validators: Result, _> = validators .into_iter() .map(|v| AccountId::from_str(&v)) .collect(); let validators = validators.map_err(|e| e.to_string())?; - let account_id = account.public_key().to_account_id(); - let params = - from_options_to_params(options, &self.rpc_client, account_id, &self.blocks).await?; let call = avail::tx().nomination_pools().nominate(pool_id, validators); - - let maybe_tx_progress = self - .api - .sign_and_submit_then_watch(&call, account, params) - .await; - - let (events, data) = - progress_transaction_ex(maybe_tx_progress, wait_for, &self.blocks).await?; - let (block_hash, block_number, tx_hash, tx_index) = data; - - let tx_data = tx_data_pool_nominate(block_hash, tx_hash, &self.blocks).await?; - - Ok(PoolNominateTxSuccess { - events, - tx_data, - tx_hash, - tx_index, - block_hash, - block_number, - }) + let details = sign_and_submit_and_progress_transaction( + &self.tx_client, + &self.blocks_client, + &self.rpc_client, + account, + call, + wait_for, + options, + ) + .await?; + + let data = + tx_data_pool_nominate(&self.blocks_client, details.block_hash, details.tx_hash).await?; + + Ok(NominateTx { data, details }) } pub async fn join( @@ -235,34 +168,25 @@ impl NominationPools { wait_for: WaitFor, account: &Keypair, options: Option, - ) -> Result { - let account_id = account.public_key().to_account_id(); - let params = - from_options_to_params(options, &self.rpc_client, account_id, &self.blocks).await?; + ) -> Result { let call = avail::tx().nomination_pools().join(amount, pool_id); - - let maybe_tx_progress = self - .api - .sign_and_submit_then_watch(&call, account, params) - .await; - - let (events, data) = - progress_transaction_ex(maybe_tx_progress, wait_for, &self.blocks).await?; - let (block_hash, block_number, tx_hash, tx_index) = data; - - let event = events.find_first::(); + let details = sign_and_submit_and_progress_transaction( + &self.tx_client, + &self.blocks_client, + &self.rpc_client, + account, + call, + wait_for, + options, + ) + .await?; + + let event = details.events.find_first::(); let Some(event) = event.ok().flatten() else { return Err(String::from("Failed to find Bonded event")); }; - Ok(PoolJoinTxSuccess { - event, - events, - tx_hash, - tx_index, - block_hash, - block_number, - }) + Ok(JoinTx { event, details }) } pub async fn create_with_pool_id( @@ -275,14 +199,11 @@ impl NominationPools { wait_for: WaitFor, account: &Keypair, options: Option, - ) -> Result { + ) -> Result { let root = AccountId::from_str(root).map_err(|e| e.to_string())?; let nominator = AccountId::from_str(nominator).map_err(|e| e.to_string())?; let bouncer = AccountId::from_str(bouncer).map_err(|e| e.to_string())?; - let account_id = account.public_key().to_account_id(); - let params = - from_options_to_params(options, &self.rpc_client, account_id, &self.blocks).await?; let call = avail::tx().nomination_pools().create_with_pool_id( amount, root.into(), @@ -290,34 +211,33 @@ impl NominationPools { bouncer.into(), pool_id, ); - - let maybe_tx_progress = self - .api - .sign_and_submit_then_watch(&call, account, params) - .await; - - let (events, data) = - progress_transaction_ex(maybe_tx_progress, wait_for, &self.blocks).await?; - let (block_hash, block_number, tx_hash, tx_index) = data; - - let event = events.find_first::(); + let details = sign_and_submit_and_progress_transaction( + &self.tx_client, + &self.blocks_client, + &self.rpc_client, + account, + call, + wait_for, + options, + ) + .await?; + + let event = details + .events + .find_first::(); let Some(event) = event.ok().flatten() else { return Err(String::from("Failed to find Created event")); }; - let event2 = events.find_first::(); + let event2 = details.events.find_first::(); let Some(event2) = event2.ok().flatten() else { return Err(String::from("Failed to find Bonded event")); }; - Ok(PoolCreateWithPoolIdTxSuccess { + Ok(CreateWithPoolIdTx { event, event2, - events, - tx_hash, - tx_index, - block_hash, - block_number, + details, }) } @@ -330,48 +250,44 @@ impl NominationPools { wait_for: WaitFor, account: &Keypair, options: Option, - ) -> Result { + ) -> Result { let root = AccountId::from_str(root).map_err(|e| e.to_string())?; let nominator = AccountId::from_str(nominator).map_err(|e| e.to_string())?; let bouncer = AccountId::from_str(bouncer).map_err(|e| e.to_string())?; - let account_id = account.public_key().to_account_id(); - let params = - from_options_to_params(options, &self.rpc_client, account_id, &self.blocks).await?; let call = avail::tx().nomination_pools().create( amount, root.into(), nominator.into(), bouncer.into(), ); - - let maybe_tx_progress = self - .api - .sign_and_submit_then_watch(&call, account, params) - .await; - - let (events, data) = - progress_transaction_ex(maybe_tx_progress, wait_for, &self.blocks).await?; - let (block_hash, block_number, tx_hash, tx_index) = data; - - let event = events.find_first::(); + let details = sign_and_submit_and_progress_transaction( + &self.tx_client, + &self.blocks_client, + &self.rpc_client, + account, + call, + wait_for, + options, + ) + .await?; + + let event = details + .events + .find_first::(); let Some(event) = event.ok().flatten() else { return Err(String::from("Failed to find Created event")); }; - let event2 = events.find_first::(); + let event2 = details.events.find_first::(); let Some(event2) = event2.ok().flatten() else { return Err(String::from("Failed to find Bonded event")); }; - Ok(PoolCreateTxSuccess { + Ok(CreateTx { event, event2, - events, - tx_hash, - tx_index, - block_hash, - block_number, + details, }) } @@ -381,34 +297,25 @@ impl NominationPools { wait_for: WaitFor, account: &Keypair, options: Option, - ) -> Result { - let account_id = account.public_key().to_account_id(); - let params = - from_options_to_params(options, &self.rpc_client, account_id, &self.blocks).await?; + ) -> Result { let call = avail::tx().nomination_pools().bond_extra(extra); - - let maybe_tx_progress = self - .api - .sign_and_submit_then_watch(&call, account, params) - .await; - - let (events, data) = - progress_transaction_ex(maybe_tx_progress, wait_for, &self.blocks).await?; - let (block_hash, block_number, tx_hash, tx_index) = data; - - let event = events.find_first::(); + let details = sign_and_submit_and_progress_transaction( + &self.tx_client, + &self.blocks_client, + &self.rpc_client, + account, + call, + wait_for, + options, + ) + .await?; + + let event = details.events.find_first::(); let Some(event) = event.ok().flatten() else { return Err(String::from("Failed to find Bonded event")); }; - Ok(PoolBondExtraTxSuccess { - event, - events, - tx_hash, - tx_index, - block_hash, - block_number, - }) + Ok(BondExtraTx { event, details }) } pub async fn set_commission( @@ -418,7 +325,7 @@ impl NominationPools { wait_for: WaitFor, account: &Keypair, options: Option, - ) -> Result { + ) -> Result { let new_commission: NewCommissionOriginal = match new_commission { Some(x) => { let account = AccountId::from_str(&x.payee).map_err(|e| e.to_string())?; @@ -427,35 +334,28 @@ impl NominationPools { None => None, }; - let account_id = account.public_key().to_account_id(); - let params = - from_options_to_params(options, &self.rpc_client, account_id, &self.blocks).await?; let call = avail::tx() .nomination_pools() .set_commission(pool_id, new_commission); - - let maybe_tx_progress = self - .api - .sign_and_submit_then_watch(&call, account, params) - .await; - - let (events, data) = - progress_transaction_ex(maybe_tx_progress, wait_for, &self.blocks).await?; - let (block_hash, block_number, tx_hash, tx_index) = data; - - let event = events.find_first::(); + let details = sign_and_submit_and_progress_transaction( + &self.tx_client, + &self.blocks_client, + &self.rpc_client, + account, + call, + wait_for, + options, + ) + .await?; + + let event = details + .events + .find_first::(); let Some(event) = event.ok().flatten() else { return Err(String::from("Failed to find PoolCommissionUpdated event")); }; - Ok(PoolSetCommissionTxSuccess { - event, - events, - tx_hash, - tx_index, - block_hash, - block_number, - }) + Ok(SetCommissionTx { event, details }) } pub async fn set_state( @@ -465,32 +365,25 @@ impl NominationPools { wait_for: WaitFor, account: &Keypair, options: Option, - ) -> Result { - let account_id = account.public_key().to_account_id(); - let params = - from_options_to_params(options, &self.rpc_client, account_id, &self.blocks).await?; + ) -> Result { let call = avail::tx().nomination_pools().set_state(pool_id, state); - - let maybe_tx_progress = self - .api - .sign_and_submit_then_watch(&call, account, params) - .await; - - let (events, data) = - progress_transaction_ex(maybe_tx_progress, wait_for, &self.blocks).await?; - let (block_hash, block_number, tx_hash, tx_index) = data; - - let event = events.find_first::(); + let details = sign_and_submit_and_progress_transaction( + &self.tx_client, + &self.blocks_client, + &self.rpc_client, + account, + call, + wait_for, + options, + ) + .await?; + + let event = details + .events + .find_first::(); let event = event.ok().flatten(); - Ok(PoolSetStateTxSuccess { - event, - events, - tx_hash, - tx_index, - block_hash, - block_number, - }) + Ok(SetStateTx { event, details }) } pub async fn claim_payout( @@ -498,32 +391,25 @@ impl NominationPools { wait_for: WaitFor, account: &Keypair, options: Option, - ) -> Result { - let account_id = account.public_key().to_account_id(); - let params = - from_options_to_params(options, &self.rpc_client, account_id, &self.blocks).await?; + ) -> Result { let call = avail::tx().nomination_pools().claim_payout(); - - let maybe_tx_progress = self - .api - .sign_and_submit_then_watch(&call, account, params) - .await; - - let (events, data) = - progress_transaction_ex(maybe_tx_progress, wait_for, &self.blocks).await?; - let (block_hash, block_number, tx_hash, tx_index) = data; - - let event = events.find_first::(); + let details = sign_and_submit_and_progress_transaction( + &self.tx_client, + &self.blocks_client, + &self.rpc_client, + account, + call, + wait_for, + options, + ) + .await?; + + let event = details + .events + .find_first::(); let event = event.ok().flatten(); - Ok(PoolClaimPayoutTxSuccess { - event, - events, - tx_hash, - tx_index, - block_hash, - block_number, - }) + Ok(ClaimPayoutTx { event, details }) } pub async fn chill( @@ -532,28 +418,20 @@ impl NominationPools { wait_for: WaitFor, account: &Keypair, options: Option, - ) -> Result { - let account_id = account.public_key().to_account_id(); - let params = - from_options_to_params(options, &self.rpc_client, account_id, &self.blocks).await?; + ) -> Result { let call = avail::tx().nomination_pools().chill(pool_id); - - let maybe_tx_progress = self - .api - .sign_and_submit_then_watch(&call, account, params) - .await; - - let (events, data) = - progress_transaction_ex(maybe_tx_progress, wait_for, &self.blocks).await?; - let (block_hash, block_number, tx_hash, tx_index) = data; - - Ok(PoolChillTxSuccess { - events, - tx_hash, - tx_index, - block_hash, - block_number, - }) + let details = sign_and_submit_and_progress_transaction( + &self.tx_client, + &self.blocks_client, + &self.rpc_client, + account, + call, + wait_for, + options, + ) + .await?; + + Ok(ChillTx { details }) } pub async fn set_claim_permission( @@ -562,30 +440,22 @@ impl NominationPools { wait_for: WaitFor, account: &Keypair, options: Option, - ) -> Result { - let account_id = account.public_key().to_account_id(); - let params = - from_options_to_params(options, &self.rpc_client, account_id, &self.blocks).await?; + ) -> Result { let call = avail::tx() .nomination_pools() .set_claim_permission(permission); - - let maybe_tx_progress = self - .api - .sign_and_submit_then_watch(&call, account, params) - .await; - - let (events, data) = - progress_transaction_ex(maybe_tx_progress, wait_for, &self.blocks).await?; - let (block_hash, block_number, tx_hash, tx_index) = data; - - Ok(PoolSetClaimPermissionTxSuccess { - events, - tx_hash, - tx_index, - block_hash, - block_number, - }) + let details = sign_and_submit_and_progress_transaction( + &self.tx_client, + &self.blocks_client, + &self.rpc_client, + account, + call, + wait_for, + options, + ) + .await?; + + Ok(SetClaimPermissionTx { details }) } pub async fn claim_commission( @@ -594,34 +464,26 @@ impl NominationPools { wait_for: WaitFor, account: &Keypair, options: Option, - ) -> Result { - let account_id = account.public_key().to_account_id(); - let params = - from_options_to_params(options, &self.rpc_client, account_id, &self.blocks).await?; + ) -> Result { let call = avail::tx().nomination_pools().claim_commission(pool_id); - - let maybe_tx_progress = self - .api - .sign_and_submit_then_watch(&call, account, params) - .await; - - let (events, data) = - progress_transaction_ex(maybe_tx_progress, wait_for, &self.blocks).await?; - let (block_hash, block_number, tx_hash, tx_index) = data; - - let event = events.find_first::(); + let details = sign_and_submit_and_progress_transaction( + &self.tx_client, + &self.blocks_client, + &self.rpc_client, + account, + call, + wait_for, + options, + ) + .await?; + let event = details + .events + .find_first::(); let Some(event) = event.ok().flatten() else { return Err(String::from("Failed to find PoolCommissionClaimed event")); }; - Ok(PoolClaimCommissionTxSuccess { - event, - events, - tx_hash, - tx_index, - block_hash, - block_number, - }) + Ok(ClaimCommissionTx { event, details }) } pub async fn claim_payout_other( @@ -630,35 +492,28 @@ impl NominationPools { wait_for: WaitFor, account: &Keypair, options: Option, - ) -> Result { + ) -> Result { let other = AccountId::from_str(other).map_err(|e| e.to_string())?; - let account_id = account.public_key().to_account_id(); - let params = - from_options_to_params(options, &self.rpc_client, account_id, &self.blocks).await?; let call = avail::tx() .nomination_pools() .claim_payout_other(other.into()); - - let maybe_tx_progress = self - .api - .sign_and_submit_then_watch(&call, account, params) - .await; - - let (events, data) = - progress_transaction_ex(maybe_tx_progress, wait_for, &self.blocks).await?; - let (block_hash, block_number, tx_hash, tx_index) = data; - - let event = events.find_first::(); + let details = sign_and_submit_and_progress_transaction( + &self.tx_client, + &self.blocks_client, + &self.rpc_client, + account, + call, + wait_for, + options, + ) + .await?; + + let event = details + .events + .find_first::(); let event = event.ok().flatten(); - Ok(PoolClaimPayoutOtherTxSuccess { - event, - events, - tx_hash, - tx_index, - block_hash, - block_number, - }) + Ok(ClaimPayoutOtherTx { event, details }) } pub async fn unbond( @@ -668,35 +523,27 @@ impl NominationPools { wait_for: WaitFor, account: &Keypair, options: Option, - ) -> Result { + ) -> Result { let member_account = AccountId::from_str(member_account).map_err(|e| e.to_string())?; - let account_id = account.public_key().to_account_id(); - let params = - from_options_to_params(options, &self.rpc_client, account_id, &self.blocks).await?; let call = avail::tx() .nomination_pools() .unbond(member_account.into(), unbonding_points); - - let maybe_tx_progress = self - .api - .sign_and_submit_then_watch(&call, account, params) - .await; - - let (events, data) = - progress_transaction_ex(maybe_tx_progress, wait_for, &self.blocks).await?; - let (block_hash, block_number, tx_hash, tx_index) = data; - - let event = events.find_first::(); + let details = sign_and_submit_and_progress_transaction( + &self.tx_client, + &self.blocks_client, + &self.rpc_client, + account, + call, + wait_for, + options, + ) + .await?; + let event = details + .events + .find_first::(); let event = event.ok().flatten(); - Ok(PoolUnbondTxSuccess { - event, - events, - tx_hash, - tx_index, - block_hash, - block_number, - }) + Ok(UnbondTx { event, details }) } pub async fn set_metadata( @@ -706,30 +553,22 @@ impl NominationPools { wait_for: WaitFor, account: &Keypair, options: Option, - ) -> Result { - let account_id = account.public_key().to_account_id(); - let params = - from_options_to_params(options, &self.rpc_client, account_id, &self.blocks).await?; + ) -> Result { let call = avail::tx() .nomination_pools() .set_metadata(pool_id, metadata); - - let maybe_tx_progress = self - .api - .sign_and_submit_then_watch(&call, account, params) - .await; - - let (events, data) = - progress_transaction_ex(maybe_tx_progress, wait_for, &self.blocks).await?; - let (block_hash, block_number, tx_hash, tx_index) = data; - - Ok(PoolSetMetadataTxSuccess { - events, - tx_hash, - tx_index, - block_hash, - block_number, - }) + let details = sign_and_submit_and_progress_transaction( + &self.tx_client, + &self.blocks_client, + &self.rpc_client, + account, + call, + wait_for, + options, + ) + .await?; + + Ok(SetMetadataTx { details }) } pub async fn withdraw_unbonded( @@ -739,45 +578,38 @@ impl NominationPools { wait_for: WaitFor, account: &Keypair, options: Option, - ) -> Result { + ) -> Result { let member_account = AccountId::from_str(member_account).map_err(|e| e.to_string())?; - let account_id = account.public_key().to_account_id(); - let params = - from_options_to_params(options, &self.rpc_client, account_id, &self.blocks).await?; let call = avail::tx() .nomination_pools() .withdraw_unbonded(member_account.into(), num_slashing_spans); - - let maybe_tx_progress = self - .api - .sign_and_submit_then_watch(&call, account, params) - .await; - - let (events, data) = - progress_transaction_ex(maybe_tx_progress, wait_for, &self.blocks).await?; - let (block_hash, block_number, tx_hash, tx_index) = data; - - let event = events.find_first::(); + let details = sign_and_submit_and_progress_transaction( + &self.tx_client, + &self.blocks_client, + &self.rpc_client, + account, + call, + wait_for, + options, + ) + .await?; + + let event = details + .events + .find_first::(); let event = event.ok().flatten(); - Ok(PoolWithdrawUnbondedTxSuccess { - event, - events, - tx_hash, - tx_index, - block_hash, - block_number, - }) + Ok(WithdrawUnbondedTx { event, details }) } } pub async fn tx_data_pool_nominate( + client: &ABlocksClient, block_hash: H256, tx_hash: H256, - blocks: &AvailBlocksClient, ) -> Result { let transaction = - fetch_transaction::(block_hash, tx_hash, blocks).await; + fetch_transaction::(client, block_hash, tx_hash).await; let transaction = transaction.map_err(|err| err.to_string())?; Ok(transaction.value) } diff --git a/avail-rust/src/transactions/options.rs b/avail-rust/src/transactions/options.rs index 2e136b927..972f3cde3 100644 --- a/avail-rust/src/transactions/options.rs +++ b/avail-rust/src/transactions/options.rs @@ -1,5 +1,7 @@ -use crate::rpcs::Rpc; -use crate::{AccountId, AvailBlocksClient, AvailExtrinsicParamsBuilder, BlockHash}; +use subxt::backend::rpc::RpcClient; + +use crate::rpcs::{account_next_index, get_block_hash, get_finalized_head, get_header}; +use crate::{ABlocksClient, AccountId, AvailExtrinsicParamsBuilder, BlockHash}; use super::Params; @@ -61,10 +63,10 @@ pub enum Nonce { } pub async fn from_options_to_params( + rpc_client: &RpcClient, + blocks_client: &ABlocksClient, + account: &AccountId, options: Option, - client: &Rpc, - account: AccountId, - blocks: &AvailBlocksClient, ) -> Result { let Some(options) = options else { return Ok(AvailExtrinsicParamsBuilder::new().build()); @@ -78,15 +80,15 @@ pub async fn from_options_to_params( period: 32, block_hash: None, }); - let header = client.chain.get_header(mortality.block_hash); + let header = get_header(rpc_client, mortality.block_hash); let header = header.await.map_err(|e| e.to_string())?; builder = builder.mortal(&header, mortality.period); if let Some(nonce) = options.nonce { builder = match nonce { Nonce::BestBlock => { - let hash = client.chain.get_block_hash(None).await.unwrap(); - let block = blocks.at(hash).await.map_err(|e| e.to_string())?; + let hash = get_block_hash(rpc_client, None).await.unwrap(); + let block = blocks_client.at(hash).await.map_err(|e| e.to_string())?; let nonce = block .account_nonce(&account) .await @@ -94,8 +96,8 @@ pub async fn from_options_to_params( builder.nonce(nonce) }, Nonce::FinalizedBlock => { - let hash = client.chain.get_finalized_head().await.unwrap(); - let block = blocks.at(hash).await.map_err(|e| e.to_string())?; + let hash = get_finalized_head(rpc_client).await.unwrap(); + let block = blocks_client.at(hash).await.map_err(|e| e.to_string())?; let nonce = block .account_nonce(&account) .await @@ -103,9 +105,7 @@ pub async fn from_options_to_params( builder.nonce(nonce) }, Nonce::BestBlockAndTxPool => { - let nonce = client - .system - .account_next_index(account.to_string()) + let nonce = account_next_index(rpc_client, account.to_string()) .await .map_err(|e| e.to_string())?; builder.nonce(nonce as u64) diff --git a/avail-rust/src/transactions/session.rs b/avail-rust/src/transactions/session.rs index 8ed0003e2..5dc7d090d 100644 --- a/avail-rust/src/transactions/session.rs +++ b/avail-rust/src/transactions/session.rs @@ -1,40 +1,35 @@ use crate::avail::runtime_types::da_runtime::primitives::SessionKeys; -use crate::rpcs::Rpc; use crate::sdk::WaitFor; use crate::utils_raw::fetch_transaction; -use crate::{avail, AvailBlocksClient, AvailConfig, TxApi, H256}; +use crate::{avail, ABlocksClient, ATxClient, H256}; -use subxt::blocks::ExtrinsicEvents; +use subxt::backend::rpc::RpcClient; use subxt_signer::sr25519::Keypair; -use super::options::from_options_to_params; -use super::{options::Options, progress_transaction_ex}; +use super::options::Options; +use super::{sign_and_submit_and_progress_transaction, TxResultDetails}; use avail::session::calls::types as SessionCalls; #[derive(Debug)] pub struct SetKeysTxSuccess { - pub events: ExtrinsicEvents, - pub tx_data: SessionCalls::SetKeys, - pub tx_hash: H256, - pub tx_index: u32, - pub block_hash: H256, - pub block_number: u32, + pub data: SessionCalls::SetKeys, + pub details: TxResultDetails, } #[derive(Clone)] pub struct Session { - api: TxApi, - rpc_client: Rpc, - blocks: AvailBlocksClient, + tx_client: ATxClient, + blocks_client: ABlocksClient, + rpc_client: RpcClient, } impl Session { - pub fn new(api: TxApi, rpc_client: Rpc, blocks: AvailBlocksClient) -> Self { + pub fn new(tx_client: ATxClient, rpc_client: RpcClient, blocks_client: ABlocksClient) -> Self { Self { - api, + tx_client, + blocks_client, rpc_client, - blocks, } } @@ -45,39 +40,32 @@ impl Session { account: &Keypair, options: Option, ) -> Result { - let account_id = account.public_key().to_account_id(); - let params = - from_options_to_params(options, &self.rpc_client, account_id, &self.blocks).await?; let call = avail::tx().session().set_keys(keys, vec![]); + let details = sign_and_submit_and_progress_transaction( + &self.tx_client, + &self.blocks_client, + &self.rpc_client, + account, + call, + wait_for, + options, + ) + .await?; - let maybe_tx_progress = self - .api - .sign_and_submit_then_watch(&call, account, params) - .await; + let data = + tx_data_session_set_keys(&self.blocks_client, details.block_hash, details.tx_hash) + .await?; - let (events, data) = - progress_transaction_ex(maybe_tx_progress, wait_for, &self.blocks).await?; - let (block_hash, block_number, tx_hash, tx_index) = data; - - let tx_data = tx_data_session_set_keys(block_hash, tx_hash, &self.blocks).await?; - - Ok(SetKeysTxSuccess { - events, - tx_data, - tx_hash, - tx_index, - block_hash, - block_number, - }) + Ok(SetKeysTxSuccess { data, details }) } } pub async fn tx_data_session_set_keys( + client: &ABlocksClient, block_hash: H256, tx_hash: H256, - blocks: &AvailBlocksClient, ) -> Result { - let transaction = fetch_transaction::(block_hash, tx_hash, blocks).await; + let transaction = fetch_transaction::(client, block_hash, tx_hash).await; let transaction = transaction.map_err(|err| err.to_string())?; Ok(transaction.value) } diff --git a/avail-rust/src/transactions/staking.rs b/avail-rust/src/transactions/staking.rs index b3270b67f..f5a46953a 100644 --- a/avail-rust/src/transactions/staking.rs +++ b/avail-rust/src/transactions/staking.rs @@ -1,12 +1,11 @@ use crate::api_dev::api::runtime_types::pallet_staking::ValidatorPrefs; use crate::api_dev::api::runtime_types::sp_arithmetic::per_things::Perbill; -use crate::rpcs::Rpc; use crate::sdk::WaitFor; use crate::utils_raw::fetch_transaction; -use crate::{avail, AccountId, AvailBlocksClient, AvailConfig, RewardDestination, TxApi, H256}; +use crate::{avail, ABlocksClient, ATxClient, AccountId, RewardDestination, H256}; use std::str::FromStr; -use subxt::blocks::ExtrinsicEvents; +use subxt::backend::rpc::RpcClient; use subxt_core::utils::MultiAddress; use subxt_signer::sr25519::Keypair; @@ -14,91 +13,63 @@ use avail::staking::calls::types as StakingCalls; use avail::staking::events as StakingEvents; use super::options::{from_options_to_params, Options}; -use super::progress_transaction_ex; +use super::{progress_transaction_ex, sign_and_submit_and_progress_transaction, TxResultDetails}; #[derive(Debug)] pub struct BondTxSuccess { pub event: StakingEvents::Bonded, - pub events: ExtrinsicEvents, - pub tx_hash: H256, - pub tx_index: u32, - pub block_hash: H256, - pub block_number: u32, + pub details: TxResultDetails, } #[derive(Debug)] pub struct BondExtraTxSuccess { pub event: StakingEvents::Bonded, - pub events: ExtrinsicEvents, - pub tx_hash: H256, - pub tx_index: u32, - pub block_hash: H256, - pub block_number: u32, + pub details: TxResultDetails, } #[derive(Debug)] pub struct ChillTxSuccess { pub event: Option, - pub events: ExtrinsicEvents, - pub tx_hash: H256, - pub tx_index: u32, - pub block_hash: H256, - pub block_number: u32, + pub details: TxResultDetails, } #[derive(Debug)] pub struct ChillOtherTxSuccess { pub event: StakingEvents::Chilled, - pub events: ExtrinsicEvents, - pub tx_hash: H256, - pub tx_index: u32, - pub block_hash: H256, - pub block_number: u32, + pub details: TxResultDetails, } #[derive(Debug)] pub struct NominateTxSuccess { - pub events: ExtrinsicEvents, - pub tx_data: StakingCalls::Nominate, - pub tx_hash: H256, - pub tx_index: u32, - pub block_hash: H256, - pub block_number: u32, + pub data: StakingCalls::Nominate, + pub details: TxResultDetails, } #[derive(Debug)] pub struct UnbondTxSuccess { pub event: StakingEvents::Unbonded, - pub events: ExtrinsicEvents, - pub tx_hash: H256, - pub tx_index: u32, - pub block_hash: H256, - pub block_number: u32, + pub details: TxResultDetails, } #[derive(Debug)] pub struct ValidateTxSuccess { pub event: StakingEvents::ValidatorPrefsSet, - pub events: ExtrinsicEvents, - pub tx_hash: H256, - pub tx_index: u32, - pub block_hash: H256, - pub block_number: u32, + pub details: TxResultDetails, } #[derive(Clone)] pub struct Staking { - api: TxApi, - rpc_client: Rpc, - blocks: AvailBlocksClient, + tx_client: ATxClient, + blocks_client: ABlocksClient, + rpc_client: RpcClient, } impl Staking { - pub fn new(api: TxApi, rpc_client: Rpc, blocks: AvailBlocksClient) -> Self { + pub fn new(tx_client: ATxClient, rpc_client: RpcClient, blocks_client: ABlocksClient) -> Self { Self { - api, + tx_client, + blocks_client, rpc_client, - blocks, } } @@ -110,33 +81,24 @@ impl Staking { account: &Keypair, options: Option, ) -> Result { - let account_id = account.public_key().to_account_id(); - let params = - from_options_to_params(options, &self.rpc_client, account_id, &self.blocks).await?; let call = avail::tx().staking().bond(value, payee); - - let maybe_tx_progress = self - .api - .sign_and_submit_then_watch(&call, account, params) - .await; - - let (events, data) = - progress_transaction_ex(maybe_tx_progress, wait_for, &self.blocks).await?; - let (block_hash, block_number, tx_hash, tx_index) = data; - - let event = events.find_first::(); + let details = sign_and_submit_and_progress_transaction( + &self.tx_client, + &self.blocks_client, + &self.rpc_client, + account, + call, + wait_for, + options, + ) + .await?; + + let event = details.events.find_first::(); let Some(event) = event.ok().flatten() else { return Err(String::from("Failed to find Bonded event")); }; - Ok(BondTxSuccess { - event, - events, - tx_hash, - tx_index, - block_hash, - block_number, - }) + Ok(BondTxSuccess { event, details }) } pub async fn bond_extra( @@ -146,33 +108,24 @@ impl Staking { account: &Keypair, options: Option, ) -> Result { - let account_id = account.public_key().to_account_id(); - let params = - from_options_to_params(options, &self.rpc_client, account_id, &self.blocks).await?; let call = avail::tx().staking().bond_extra(max_additional); - - let maybe_tx_progress = self - .api - .sign_and_submit_then_watch(&call, account, params) - .await; - - let (events, data) = - progress_transaction_ex(maybe_tx_progress, wait_for, &self.blocks).await?; - let (block_hash, block_number, tx_hash, tx_index) = data; - - let event = events.find_first::(); + let details = sign_and_submit_and_progress_transaction( + &self.tx_client, + &self.blocks_client, + &self.rpc_client, + account, + call, + wait_for, + options, + ) + .await?; + + let event = details.events.find_first::(); let Some(event) = event.ok().flatten() else { return Err(String::from("Failed to find Bonded event")); }; - Ok(BondExtraTxSuccess { - event, - events, - tx_hash, - tx_index, - block_hash, - block_number, - }) + Ok(BondExtraTxSuccess { event, details }) } pub async fn chill( @@ -181,30 +134,25 @@ impl Staking { account: &Keypair, options: Option, ) -> Result { - let account_id = account.public_key().to_account_id(); - let params = - from_options_to_params(options, &self.rpc_client, account_id, &self.blocks).await?; let call = avail::tx().staking().chill(); - - let maybe_tx_progress = self - .api - .sign_and_submit_then_watch(&call, account, params) - .await; - - let (events, data) = - progress_transaction_ex(maybe_tx_progress, wait_for, &self.blocks).await?; - let (block_hash, block_number, tx_hash, tx_index) = data; - - let event = events.find_first::().ok().flatten(); - - Ok(ChillTxSuccess { - event, - events, - tx_hash, - tx_index, - block_hash, - block_number, - }) + let details = sign_and_submit_and_progress_transaction( + &self.tx_client, + &self.blocks_client, + &self.rpc_client, + account, + call, + wait_for, + options, + ) + .await?; + + let event = details + .events + .find_first::() + .ok() + .flatten(); + + Ok(ChillTxSuccess { event, details }) } pub async fn chill_other( @@ -219,33 +167,23 @@ impl Staking { Err(error) => return Err(std::format!("{:?}", error)), }; - let account_id = account.public_key().to_account_id(); - let params = - from_options_to_params(options, &self.rpc_client, account_id, &self.blocks).await?; let call = avail::tx().staking().chill_other(stash); - - let maybe_tx_progress = self - .api - .sign_and_submit_then_watch(&call, account, params) - .await; - - let (events, data) = - progress_transaction_ex(maybe_tx_progress, wait_for, &self.blocks).await?; - let (block_hash, block_number, tx_hash, tx_index) = data; - - let event = events.find_first::(); + let details = sign_and_submit_and_progress_transaction( + &self.tx_client, + &self.blocks_client, + &self.rpc_client, + account, + call, + wait_for, + options, + ) + .await?; + let event = details.events.find_first::(); let Some(event) = event.ok().flatten() else { return Err(String::from("Failed to find Chilled event")); }; - Ok(ChillOtherTxSuccess { - event, - events, - tx_hash, - tx_index, - block_hash, - block_number, - }) + Ok(ChillOtherTxSuccess { event, details }) } pub async fn nominate( @@ -262,30 +200,23 @@ impl Staking { let targets = targets.map_err(|e| std::format!("{:?}", e))?; let targets = targets.into_iter().map(|a| MultiAddress::Id(a)).collect(); - let account_id = account.public_key().to_account_id(); - let params = - from_options_to_params(options, &self.rpc_client, account_id, &self.blocks).await?; let call = avail::tx().staking().nominate(targets); - - let maybe_tx_progress = self - .api - .sign_and_submit_then_watch(&call, account, params) - .await; - - let (events, data) = - progress_transaction_ex(maybe_tx_progress, wait_for, &self.blocks).await?; - let (block_hash, block_number, tx_hash, tx_index) = data; - - let tx_data = tx_data_staking_nominate(block_hash, tx_hash, &self.blocks).await?; - - Ok(NominateTxSuccess { - events, - tx_data, - tx_hash, - tx_index, - block_hash, - block_number, - }) + let details = sign_and_submit_and_progress_transaction( + &self.tx_client, + &self.blocks_client, + &self.rpc_client, + account, + call, + wait_for, + options, + ) + .await?; + + let data = + tx_data_staking_nominate(&self.blocks_client, details.block_hash, details.tx_hash) + .await?; + + Ok(NominateTxSuccess { data, details }) } pub async fn unbond( @@ -296,32 +227,23 @@ impl Staking { options: Option, ) -> Result { let call = avail::tx().staking().unbond(value); - - let account_id = account.public_key().to_account_id(); - let params = - from_options_to_params(options, &self.rpc_client, account_id, &self.blocks).await?; - let maybe_tx_progress = self - .api - .sign_and_submit_then_watch(&call, account, params) - .await; - - let (events, data) = - progress_transaction_ex(maybe_tx_progress, wait_for, &self.blocks).await?; - let (block_hash, block_number, tx_hash, tx_index) = data; - - let event = events.find_first::(); + let details = sign_and_submit_and_progress_transaction( + &self.tx_client, + &self.blocks_client, + &self.rpc_client, + account, + call, + wait_for, + options, + ) + .await?; + + let event = details.events.find_first::(); let Some(event) = event.ok().flatten() else { return Err(String::from("Failed to find Unbonded event")); }; - Ok(UnbondTxSuccess { - event, - events, - tx_hash, - tx_index, - block_hash, - block_number, - }) + Ok(UnbondTxSuccess { event, details }) } pub async fn validate( @@ -342,43 +264,36 @@ impl Staking { blocked, }; - let account_id = account.public_key().to_account_id(); - let params = - from_options_to_params(options, &self.rpc_client, account_id, &self.blocks).await?; let call = avail::tx().staking().validate(perfs); - - let maybe_tx_progress = self - .api - .sign_and_submit_then_watch(&call, account, params) - .await; - - let (events, data) = - progress_transaction_ex(maybe_tx_progress, wait_for, &self.blocks).await?; - let (block_hash, block_number, tx_hash, tx_index) = data; - - let event = events.find_first::(); + let details = sign_and_submit_and_progress_transaction( + &self.tx_client, + &self.blocks_client, + &self.rpc_client, + account, + call, + wait_for, + options, + ) + .await?; + + let event = details + .events + .find_first::(); let Some(event) = event.ok().flatten() else { return Err(String::from("Failed to find ValidatorPrefsSet event")); }; - Ok(ValidateTxSuccess { - event, - events, - tx_hash, - tx_index, - block_hash, - block_number, - }) + Ok(ValidateTxSuccess { event, details }) } } pub async fn tx_data_staking_nominate( + client: &ABlocksClient, block_hash: H256, tx_hash: H256, - blocks: &AvailBlocksClient, ) -> Result { let transaction = - fetch_transaction::(block_hash, tx_hash, blocks).await; + fetch_transaction::(client, block_hash, tx_hash).await; let transaction = transaction.map_err(|err| err.to_string())?; Ok(transaction.value) } diff --git a/avail-rust/src/utils.rs b/avail-rust/src/utils.rs index dc229d89e..bcd3e054f 100644 --- a/avail-rust/src/utils.rs +++ b/avail-rust/src/utils.rs @@ -5,8 +5,8 @@ use subxt::{ }; use crate::{ - avail::runtime_types::da_runtime::primitives::SessionKeys, Api, AppUncheckedExtrinsic, - AvailBlocksClient, AvailConfig, BlockHash, TransactionInBlock, WaitFor, + avail::runtime_types::da_runtime::primitives::SessionKeys, ABlocksClient, AOnlineClient, + AppUncheckedExtrinsic, AvailConfig, BlockHash, TransactionInBlock, WaitFor, }; use utils_raw::*; @@ -33,33 +33,33 @@ impl FetchTransactionError { #[derive(Clone)] pub struct Util { - blocks_api: AvailBlocksClient, + blocks_client: ABlocksClient, } impl Util { - pub fn new(api: Api) -> Self { - let blocks_api = api.blocks(); - Self { blocks_api } + pub fn new(online_client: AOnlineClient) -> Self { + let blocks_client = online_client.blocks(); + Self { blocks_client } } pub async fn fetch_transactions( &self, block_hash: BlockHash, - ) -> Result, FetchTransactionError> { - fetch_transactions(block_hash, &self.blocks_api).await + ) -> Result, FetchTransactionError> { + fetch_transactions(&self.blocks_client, block_hash).await } pub async fn fetch_transaction( &self, block_hash: BlockHash, tx_hash: BlockHash, - ) -> Result, FetchTransactionError> { - fetch_transaction(block_hash, tx_hash, &self.blocks_api).await + ) -> Result, FetchTransactionError> { + fetch_transaction(&self.blocks_client, block_hash, tx_hash).await } pub async fn progress_transaction( &self, - maybe_tx_progress: Result, subxt::Error>, + maybe_tx_progress: Result, subxt::Error>, wait_for: WaitFor, ) -> Result { progress_transaction(maybe_tx_progress, wait_for).await @@ -118,10 +118,10 @@ pub mod utils_raw { pub use super::*; pub async fn fetch_transactions( + client: &ABlocksClient, block_hash: BlockHash, - blocks_api: &AvailBlocksClient, - ) -> Result, FetchTransactionError> { - let block = blocks_api.at(block_hash).await; + ) -> Result, FetchTransactionError> { + let block = client.at(block_hash).await; let block = match block { Ok(b) => b, Err(_) => return Err(FetchTransactionError::FailedToFetchBlock), @@ -137,11 +137,11 @@ pub mod utils_raw { } pub async fn fetch_transaction( + client: &ABlocksClient, block_hash: BlockHash, tx_hash: BlockHash, - blocks_api: &AvailBlocksClient, - ) -> Result, FetchTransactionError> { - let extrinsics = fetch_transactions(block_hash, blocks_api).await?; + ) -> Result, FetchTransactionError> { + let extrinsics = fetch_transactions(client, block_hash).await?; let found_extrinsics = extrinsics.find::(); @@ -164,7 +164,7 @@ pub mod utils_raw { } pub async fn progress_transaction( - maybe_tx_progress: Result, subxt::Error>, + maybe_tx_progress: Result, subxt::Error>, wait_for: WaitFor, ) -> Result { if let Err(error) = maybe_tx_progress {