Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gsdk,gclient): Add transfer_all, transfer_allow_death #4027

Merged
merged 3 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions gclient/src/api/calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,61 @@ impl GearApi {
Err(Error::EventNotFound)
}

/// Transfer `value` to `destination`'s account.
///
/// Sends the
/// [`pallet_balances::transfer`](https://crates.parity.io/pallet_balances/pallet/struct.Pallet.html#method.transfer)
/// extrinsic.
///
/// This function returns a hash of the block with the transfer transaction.
pub async fn transfer_allow_death(&self, destination: ProgramId, value: u128) -> Result<H256> {
let destination: [u8; 32] = destination.into();

let tx = self
.0
.calls
.transfer_allow_death(destination, value)
.await?;

for event in tx.wait_for_success().await?.iter() {
if let Event::Balances(BalancesEvent::Transfer { .. }) =
event?.as_root_event::<Event>()?
{
return Ok(tx.block_hash());
}
}

// Sending zero value is a no-op, so now event occurs.
if value == 0 {
return Ok(tx.block_hash());
}

Err(Error::EventNotFound)
}

/// Transfer `value` to `destination`'s account.
///
/// Sends the
/// [`pallet_balances::transfer`](https://crates.parity.io/pallet_balances/pallet/struct.Pallet.html#method.transfer)
/// extrinsic.
///
/// This function returns a hash of the block with the transfer transaction.
pub async fn transfer_all(&self, destination: ProgramId, keep_alive: bool) -> Result<H256> {
let destination: [u8; 32] = destination.into();

let tx = self.0.calls.transfer_all(destination, keep_alive).await?;

for event in tx.wait_for_success().await?.iter() {
if let Event::Balances(BalancesEvent::Transfer { .. }) =
event?.as_root_event::<Event>()?
{
return Ok(tx.block_hash());
}
}

Err(Error::EventNotFound)
}

/// Create a new program from a previously uploaded code identified by
/// [`CodeId`](https://docs.gear.rs/gear_core/ids/struct.CodeId.html) and
/// initialize it with a byte slice `payload`.
Expand Down
34 changes: 34 additions & 0 deletions gsdk/src/signer/calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,23 @@ impl SignerCalls {
&self,
dest: impl Into<AccountId32>,
value: u128,
) -> Result<TxInBlock> {
self.0
.run_tx(
BalancesCall::TransferKeepAlive,
vec![
Value::unnamed_variant("Id", [Value::from_bytes(dest.into())]),
Value::u128(value),
],
)
.await
}

/// `pallet_balances::transfer_allow_death`
pub async fn transfer_allow_death(
&self,
dest: impl Into<AccountId32>,
value: u128,
) -> Result<TxInBlock> {
self.0
.run_tx(
Expand All @@ -56,6 +73,23 @@ impl SignerCalls {
)
.await
}

/// `pallet_balances::transfer_all`
pub async fn transfer_all(
&self,
dest: impl Into<AccountId32>,
keep_alive: bool,
) -> Result<TxInBlock> {
self.0
.run_tx(
BalancesCall::TransferAllowDeath,
vec![
Value::unnamed_variant("Id", [Value::from_bytes(dest.into())]),
Value::bool(keep_alive),
],
)
.await
}
}

// pallet-gear
Expand Down
9 changes: 7 additions & 2 deletions gsdk/src/signer/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,13 @@ impl Inner {
/// Logging balance spent
pub async fn log_balance_spent(&self, before: u128) -> Result<()> {
let signer_rpc = SignerRpc(Arc::new(self.clone()));
let after = before.saturating_sub(signer_rpc.get_balance().await?);
log::info!("\tBalance spent: {after}");
match signer_rpc.get_balance().await {
Ok(balance) => {
let after = before.saturating_sub(balance);
log::info!("\tBalance spent: {after}");
}
Err(e) => log::info!("\tAccount was removed from storage: {e}"),
}

Ok(())
}
Expand Down
Loading