Skip to content

Commit

Permalink
[ink_e2e] update to new drink API (#2005)
Browse files Browse the repository at this point in the history
* Update to latest drink version

* Update add_tokens to mint_into

* free_balance

* Trait bounds and generic balance

* Update API

* CHANGELOG.md

* Redundant From

* Redundant Send bounds

* Link to PR

* Clippy

* Clippy

* Fix
  • Loading branch information
ascjones committed Nov 28, 2023
1 parent 89c62d7 commit 73b24c5
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 37 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Messages return `TypeSpec` directly - #[1999](https://github.com/paritytech/ink/pull/1999)
- Fail when decoding from storage and not all bytes consumed - [#1897](https://github.com/paritytech/ink/pull/1897)
- [E2E] resolve DispatchError error details for dry-runs - [#1944](https://github.com/paritytech/ink/pull/1994)
- [E2E] update to new `drink` API - [#2005](https://github.com/paritytech/ink/pull/2005)


## Version 5.0.0-alpha
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ cfg-if = { version = "1.0" }
contract-build = { package = "contract-build", git = "https://github.com/paritytech/cargo-contract", branch = "at/riscv" }
darling = { version = "0.20.3" }
derive_more = { version = "0.99.17", default-features = false }
drink = { version = "=0.6.0" }
drink = { version = "=0.8.2" }
either = { version = "1.5", default-features = false }
funty = { version = "2.0.0" }
heck = { version = "0.4.0" }
Expand Down
4 changes: 2 additions & 2 deletions crates/e2e/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ pub trait ChainBackend {
amount: Self::Balance,
) -> Keypair;

/// Returns the balance of `actor`.
async fn balance(
/// Returns the free balance of `account`.
async fn free_balance(
&mut self,
account: Self::AccountId,
) -> Result<Self::Balance, Self::Error>;
Expand Down
81 changes: 54 additions & 27 deletions crates/e2e/src/drink_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@ use crate::{
UploadResult,
};
use drink::{
chain_api::{
ChainApi,
RuntimeCall,
frame_support::traits::fungible::Inspect,
pallet_balances,
pallet_contracts,
runtime::{
AccountIdFor,
Runtime as RuntimeT,
},
contract_api::ContractApi,
runtime::Runtime as RuntimeT,
BalanceOf,
RuntimeCall,
Sandbox,
Weight,
DEFAULT_GAS_LIMIT,
Expand Down Expand Up @@ -70,6 +73,9 @@ use subxt::{
tx::TxPayload,
};
use subxt_signer::sr25519::Keypair;

type ContractsBalanceOf<R> =
<<R as pallet_contracts::Config>::Currency as Inspect<AccountIdFor<R>>>::Balance;
pub struct Client<AccountId, Hash, Runtime: RuntimeT> {
sandbox: Sandbox<Runtime>,
contracts: ContractsRegistry,
Expand All @@ -83,12 +89,11 @@ unsafe impl<AccountId, Hash, Runtime: RuntimeT> Send
for Client<AccountId, Hash, Runtime>
{
}

type RuntimeAccountId<R> = <R as drink::runtime::frame_system::Config>::AccountId;

impl<AccountId, Hash, Runtime: RuntimeT> Client<AccountId, Hash, Runtime>
impl<AccountId, Hash, Runtime> Client<AccountId, Hash, Runtime>
where
RuntimeAccountId<Runtime>: From<[u8; 32]>,
Runtime: RuntimeT + pallet_balances::Config + pallet_contracts::Config,
AccountIdFor<Runtime>: From<[u8; 32]>,
BalanceOf<Runtime>: From<u128>,
{
pub fn new<P: Into<PathBuf>>(contracts: impl IntoIterator<Item = P>) -> Self {
let mut sandbox = Sandbox::new().expect("Failed to initialize Drink! sandbox");
Expand Down Expand Up @@ -117,19 +122,22 @@ where
.map(|kp| kp.public_key().0)
.map(From::from);
for account in accounts.into_iter() {
sandbox.add_tokens(account, TOKENS);
sandbox
.mint_into(account, TOKENS.into())
.unwrap_or_else(|_| panic!("Failed to mint {} tokens", TOKENS));
}
}
}

#[async_trait]
impl<AccountId: AsRef<[u8; 32]> + Send, Hash, Runtime: RuntimeT> ChainBackend
impl<AccountId: AsRef<[u8; 32]> + Send, Hash, Runtime> ChainBackend
for Client<AccountId, Hash, Runtime>
where
RuntimeAccountId<Runtime>: From<[u8; 32]>,
Runtime: RuntimeT + pallet_balances::Config,
AccountIdFor<Runtime>: From<[u8; 32]>,
{
type AccountId = AccountId;
type Balance = u128;
type Balance = BalanceOf<Runtime>;
type Error = DrinkErr;
type EventLog = ();

Expand All @@ -140,17 +148,19 @@ where
) -> Keypair {
let (pair, seed) = Pair::generate();

self.sandbox.add_tokens(pair.public().0.into(), amount);
self.sandbox
.mint_into(pair.public().0.into(), amount)
.expect("Failed to mint tokens");

Keypair::from_seed(seed).expect("Failed to create keypair")
}

async fn balance(
async fn free_balance(
&mut self,
account: Self::AccountId,
) -> Result<Self::Balance, Self::Error> {
let account = RuntimeAccountId::<Runtime>::from(*account.as_ref());
Ok(self.sandbox.balance(&account))
let account = AccountIdFor::<Runtime>::from(*account.as_ref());
Ok(self.sandbox.free_balance(&account))
}

async fn runtime_call<'a>(
Expand Down Expand Up @@ -199,11 +209,16 @@ where
impl<
AccountId: Clone + Send + Sync + From<[u8; 32]> + AsRef<[u8; 32]>,
Hash: Copy + From<[u8; 32]>,
Runtime: RuntimeT,
E: Environment<AccountId = AccountId, Balance = u128, Hash = Hash> + 'static,
Runtime: RuntimeT + pallet_balances::Config + pallet_contracts::Config,
E: Environment<
AccountId = AccountId,
Balance = ContractsBalanceOf<Runtime>,
Hash = Hash,
> + 'static,
> BuilderClient<E> for Client<AccountId, Hash, Runtime>
where
RuntimeAccountId<Runtime>: From<[u8; 32]> + AsRef<[u8; 32]>,
AccountIdFor<Runtime>: From<[u8; 32]> + AsRef<[u8; 32]>,
ContractsBalanceOf<Runtime>: Send + Sync,
{
async fn bare_instantiate<Contract: Clone, Args: Send + Sync + Encode + Clone, R>(
&mut self,
Expand Down Expand Up @@ -300,6 +315,7 @@ where
code,
keypair_to_account(caller),
storage_deposit_limit,
pallet_contracts::Determinism::Enforced,
) {
Ok(result) => result,
Err(err) => {
Expand Down Expand Up @@ -351,6 +367,7 @@ where
keypair_to_account(caller),
DEFAULT_GAS_LIMIT,
storage_deposit_limit,
pallet_contracts::Determinism::Enforced,
)
.result
.is_err()
Expand Down Expand Up @@ -383,6 +400,7 @@ where
keypair_to_account(caller),
DEFAULT_GAS_LIMIT,
storage_deposit_limit,
pallet_contracts::Determinism::Enforced,
)
});
Ok(CallDryRunResult {
Expand All @@ -402,11 +420,16 @@ where
impl<
AccountId: Clone + Send + Sync + From<[u8; 32]> + AsRef<[u8; 32]>,
Hash: Copy + From<[u8; 32]>,
Runtime: RuntimeT,
E: Environment<AccountId = AccountId, Balance = u128, Hash = Hash> + 'static,
Runtime: RuntimeT + pallet_balances::Config + pallet_contracts::Config,
E: Environment<
AccountId = AccountId,
Balance = ContractsBalanceOf<Runtime>,
Hash = Hash,
> + 'static,
> E2EBackend<E> for Client<AccountId, Hash, Runtime>
where
RuntimeAccountId<Runtime>: From<[u8; 32]> + AsRef<[u8; 32]>,
AccountIdFor<Runtime>: From<[u8; 32]> + AsRef<[u8; 32]>,
ContractsBalanceOf<Runtime>: Send + Sync,
{
}

Expand All @@ -418,11 +441,15 @@ fn keypair_to_account<AccountId: From<[u8; 32]>>(keypair: &Keypair) -> AccountId
impl<
AccountId: Clone + Send + Sync + From<[u8; 32]> + AsRef<[u8; 32]>,
Hash: Copy + From<[u8; 32]>,
Runtime: RuntimeT,
E: Environment<AccountId = AccountId, Balance = u128, Hash = Hash> + 'static,
Runtime: RuntimeT + pallet_balances::Config + pallet_contracts::Config,
E: Environment<
AccountId = AccountId,
Balance = ContractsBalanceOf<Runtime>,
Hash = Hash,
> + 'static,
> ContractsBackend<E> for Client<AccountId, Hash, Runtime>
where
RuntimeAccountId<Runtime>: From<[u8; 32]> + AsRef<[u8; 32]>,
AccountIdFor<Runtime>: From<[u8; 32]> + AsRef<[u8; 32]>,
{
type Error = DrinkErr;
type EventLog = ();
Expand Down
2 changes: 1 addition & 1 deletion crates/e2e/src/subxt_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ where
keypair
}

async fn balance(
async fn free_balance(
&mut self,
account: Self::AccountId,
) -> Result<Self::Balance, Self::Error> {
Expand Down
8 changes: 4 additions & 4 deletions integration-tests/call-runtime/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,11 @@ mod runtime_call {
let receiver: AccountId = default_accounts::<DefaultEnvironment>().bob;

let contract_balance_before = client
.balance(contract.account_id)
.free_balance(contract.account_id)
.await
.expect("Failed to get account balance");
let receiver_balance_before = client
.balance(receiver)
.free_balance(receiver)
.await
.expect("Failed to get account balance");

Expand All @@ -189,11 +189,11 @@ mod runtime_call {

// then
let contract_balance_after = client
.balance(contract.account_id)
.free_balance(contract.account_id)
.await
.expect("Failed to get account balance");
let receiver_balance_after = client
.balance(receiver)
.free_balance(receiver)
.await
.expect("Failed to get account balance");

Expand Down
4 changes: 2 additions & 2 deletions integration-tests/contract-transfer/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ pub mod give_me {
let mut call = contract.call::<GiveMe>();

let balance_before: Balance = client
.balance(contract.account_id.clone())
.free_balance(contract.account_id.clone())
.await
.expect("getting balance failed");

Expand All @@ -253,7 +253,7 @@ pub mod give_me {
assert!(call_res.debug_message().contains("requested value: 120\n"));

let balance_after: Balance = client
.balance(contract.account_id.clone())
.free_balance(contract.account_id.clone())
.await
.expect("getting balance failed");
assert_eq!(balance_before - balance_after, 120);
Expand Down

0 comments on commit 73b24c5

Please sign in to comment.