Skip to content

Commit

Permalink
balance checking bethods and convertors for accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
35359595 committed Nov 3, 2023
1 parent 436e666 commit bde49f8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ targets = ["x86_64-unknown-linux-gnu"]
bcs = { git = "https://github.com/eigerco/bcs.git", default-features = false, branch = "master" }
codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false, features = ["derive",] }
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }
arrayref = "^0.3"
frame-benchmarking = { version = "4.0.0-dev", default-features = false, optional = true, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
frame-support = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
frame-system = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
sp-std = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v1.0.0' }
sp-runtime = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v1.0.0' }

# MoveVM dependencies
move-core-types = { default-features = false, git = 'https://github.com/eigerco/substrate-move.git', features = ["address32"] }
Expand All @@ -32,6 +34,7 @@ hex = "0.4"
sp-core = { version = "21.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
sp-io = { version = "23.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
sp-runtime = { version = "24.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
pallet-balances = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }

[features]
default = ["std"]
Expand Down
40 changes: 40 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ pub mod pallet {
#[cfg(not(feature = "std"))]
use alloc::format;

use arrayref::array_ref;
use codec::{FullCodec, FullEncode};
use frame_support::{
dispatch::{DispatchResultWithPostInfo, PostDispatchInfo},
pallet_prelude::*,
traits::{Currency, ReservableCurrency},
};
use frame_system::pallet_prelude::*;
use move_core_types::account_address::AccountAddress;
Expand All @@ -47,8 +49,12 @@ pub mod pallet {
/// MoveVM pallet configuration trait
#[pallet::config]
pub trait Config: frame_system::Config {
/// The currency mechanism.
type Currency: ReservableCurrency<Self::AccountId>;

/// Because this pallet emits events, it depends on the runtime's definition of an event.
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

/// Type representing the weight of this pallet
type WeightInfo: WeightInfo;
}
Expand Down Expand Up @@ -155,6 +161,10 @@ pub mod pallet {
PublishModuleFailed,
/// Error returned when publishing Move module package failed.
PublishPackageFailed,
/// Native balance to u128 conversion failed
BalanceConversionFailed,
/// Invalid account size (expected 32 bytes)
InvalidAccountSize,
}

/// Prepare a storage adapter ready for the Virtual Machine.
Expand Down Expand Up @@ -210,5 +220,35 @@ pub mod pallet {
)
.map_err(|e| format!("error in get_resource: {:?}", e).into())
}

/// Get balance of given account in native currency converted to u128
pub fn get_balance(of: T::AccountId) -> Result<u128, Error<T>> {
let encoded_balance = T::Currency::free_balance(&of).encode();
if encoded_balance.len().ne(&16usize) {
return Err(Error::BalanceConversionFailed);
}
Ok(u128::from_be_bytes(
array_ref!(encoded_balance, 0, 16).to_owned(),
))
}

// Get balance of given Move account in native currecy converted to u128
pub fn get_move_balance(of: &AccountAddress) -> Result<u128, Error<T>> {
Self::get_balance(Self::move_to_native(of)?)
}

// Transparent conversion move -> native
pub fn move_to_native(of: &AccountAddress) -> Result<T::AccountId, Error<T>> {
T::AccountId::decode(&mut of.as_ref()).map_err(|_| Error::InvalidAccountSize)
}

// Transparent conversion native -> move
pub fn native_to_move(of: T::AccountId) -> Result<AccountAddress, Error<T>> {
let encoded = of.encode();
if encoded.len().ne(&32usize) {
return Err(Error::InvalidAccountSize);
}
Ok(AccountAddress::new(array_ref![encoded, 0, 32].to_owned()))
}
}
}

0 comments on commit bde49f8

Please sign in to comment.