use super::{GearApi, Result};
use crate::Error;
use gear_core::ids::ActorId;
use gsdk::{
AsGear,
ext::subxt::utils::H256,
gear::{
Event,
runtime_types::pallet_gear_voucher::{internal::VoucherId, pallet::Event as VoucherEvent},
},
};
impl GearApi {
pub async fn issue_voucher(
&self,
spender: ActorId,
balance: u128,
programs: Option<Vec<ActorId>>,
code_uploading: bool,
duration: u32,
) -> Result<(VoucherId, H256)> {
let spender: [u8; 32] = spender.into();
let tx = self
.0
.calls()
.issue_voucher(spender, balance, programs, code_uploading, duration)
.await?;
for event in tx.wait_for_success().await?.iter() {
if let Event::GearVoucher(VoucherEvent::VoucherIssued { voucher_id, .. }) =
event?.as_gear()?
{
return Ok((voucher_id, tx.block_hash()));
}
}
Err(Error::EventNotFound)
}
#[allow(clippy::too_many_arguments)]
pub async fn update_voucher(
&self,
spender: ActorId,
voucher_id: VoucherId,
move_ownership: Option<ActorId>,
balance_top_up: Option<u128>,
append_programs: Option<Option<Vec<ActorId>>>,
code_uploading: Option<bool>,
prolong_duration: u32,
) -> Result<(VoucherId, H256)> {
let spender: [u8; 32] = spender.into();
let move_ownership: Option<[u8; 32]> = move_ownership.map(|v| v.into());
let tx = self
.0
.calls()
.update_voucher(
spender,
voucher_id,
move_ownership,
balance_top_up,
append_programs,
code_uploading,
prolong_duration,
)
.await?;
for event in tx.wait_for_success().await?.iter() {
if let Event::GearVoucher(VoucherEvent::VoucherUpdated { voucher_id, .. }) =
event?.as_gear()?
{
return Ok((voucher_id, tx.block_hash()));
}
}
Err(Error::EventNotFound)
}
pub async fn revoke_voucher(
&self,
spender: ActorId,
voucher_id: VoucherId,
) -> Result<(VoucherId, H256)> {
let spender: [u8; 32] = spender.into();
let tx = self.0.calls().revoke_voucher(spender, voucher_id).await?;
for event in tx.wait_for_success().await?.iter() {
if let Event::GearVoucher(VoucherEvent::VoucherRevoked { voucher_id, .. }) =
event?.as_gear()?
{
return Ok((voucher_id, tx.block_hash()));
}
}
Err(Error::EventNotFound)
}
pub async fn decline_voucher(&self, voucher_id: VoucherId) -> Result<(VoucherId, H256)> {
let tx = self.0.calls().decline_voucher(voucher_id).await?;
for event in tx.wait_for_success().await?.iter() {
if let Event::GearVoucher(VoucherEvent::VoucherDeclined { voucher_id, .. }) =
event?.as_gear()?
{
return Ok((voucher_id, tx.block_hash()));
}
}
Err(Error::EventNotFound)
}
pub async fn decline_voucher_with_voucher(
&self,
voucher_id: VoucherId,
) -> Result<(VoucherId, H256)> {
let tx = self
.0
.calls()
.decline_voucher_with_voucher(voucher_id)
.await?;
for event in tx.wait_for_success().await?.iter() {
if let Event::GearVoucher(VoucherEvent::VoucherDeclined { voucher_id, .. }) =
event?.as_gear()?
{
return Ok((voucher_id, tx.block_hash()));
}
}
Err(Error::EventNotFound)
}
}