use subxt::ext::sp_runtime::MultiAddress;
use crate::{
api, connections::TxInfo, pallet_vesting::vesting_info::VestingInfo, AccountId, Balance,
BlockHash, BlockNumber, ConnectionApi, SignedConnectionApi, TxStatus,
};
#[async_trait::async_trait]
pub trait VestingApi {
async fn get_vesting(
&self,
who: AccountId,
at: Option<BlockHash>,
) -> Vec<VestingInfo<Balance, BlockNumber>>;
}
#[async_trait::async_trait]
pub trait VestingUserApi {
async fn vest(&self, status: TxStatus) -> anyhow::Result<TxInfo>;
async fn vest_other(&self, status: TxStatus, other: AccountId) -> anyhow::Result<TxInfo>;
async fn vested_transfer(
&self,
receiver: AccountId,
schedule: VestingInfo<Balance, BlockNumber>,
status: TxStatus,
) -> anyhow::Result<TxInfo>;
async fn merge_schedules(
&self,
idx1: u32,
idx2: u32,
status: TxStatus,
) -> anyhow::Result<TxInfo>;
}
#[async_trait::async_trait]
impl<C: ConnectionApi> VestingApi for C {
async fn get_vesting(
&self,
who: AccountId,
at: Option<BlockHash>,
) -> Vec<VestingInfo<Balance, BlockNumber>> {
let addrs = api::storage().vesting().vesting(who);
self.get_storage_entry(&addrs, at).await.0
}
}
#[async_trait::async_trait]
impl<S: SignedConnectionApi> VestingUserApi for S {
async fn vest(&self, status: TxStatus) -> anyhow::Result<TxInfo> {
let tx = api::tx().vesting().vest();
self.send_tx(tx, status).await
}
async fn vest_other(&self, status: TxStatus, other: AccountId) -> anyhow::Result<TxInfo> {
let tx = api::tx().vesting().vest_other(MultiAddress::Id(other));
self.send_tx(tx, status).await
}
async fn vested_transfer(
&self,
receiver: AccountId,
schedule: VestingInfo<Balance, BlockNumber>,
status: TxStatus,
) -> anyhow::Result<TxInfo> {
let tx = api::tx()
.vesting()
.vested_transfer(MultiAddress::Id(receiver), schedule);
self.send_tx(tx, status).await
}
async fn merge_schedules(
&self,
idx1: u32,
idx2: u32,
status: TxStatus,
) -> anyhow::Result<TxInfo> {
let tx = api::tx().vesting().merge_schedules(idx1, idx2);
self.send_tx(tx, status).await
}
}