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

Minor changes required for ink! integration #38

Merged
merged 8 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ homepage = "https://github.com/Cardinal-Cryptography/drink"
license = "Apache-2.0"
readme = "README.md"
repository = "https://github.com/Cardinal-Cryptography/drink"
version = "0.1.1"
version = "0.1.2"

[workspace.dependencies]
anyhow = { version = "1.0.71" }
Expand All @@ -43,4 +43,4 @@ sp-core = { version = "22.0.0" }

# Local dependencies

drink = { version = "0.1.1", path = "drink" }
drink = { version = "0.1.2", path = "drink" }
8 changes: 8 additions & 0 deletions drink/src/chain_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ pub trait ChainApi {

/// Add tokens to an account.
fn add_tokens(&mut self, address: AccountId32, amount: u128);

/// Return the balance of an account.
fn balance(&mut self, address: &AccountId32) -> u128;
}

impl<R: Runtime> ChainApi for Sandbox<R> {
Expand All @@ -45,4 +48,9 @@ impl<R: Runtime> ChainApi for Sandbox<R> {
let _ = pallet_balances::Pallet::<R>::deposit_creating(&address, amount);
});
}

fn balance(&mut self, address: &AccountId32) -> u128 {
self.externalities
.execute_with(|| pallet_balances::Pallet::<R>::free_balance(address))
}
}
45 changes: 40 additions & 5 deletions drink/src/contract_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,64 @@

use frame_support::{sp_runtime::AccountId32, weights::Weight};
use pallet_contracts::{CollectEvents, DebugInfo, Determinism};
use pallet_contracts_primitives::{Code, ContractExecResult, ContractInstantiateResult};
use pallet_contracts_primitives::{
Code, CodeUploadResult, ContractExecResult, ContractInstantiateResult,
};

use crate::{runtime::Runtime, EventRecordOf, Sandbox};

/// Interface for contract-related operations.
pub trait ContractApi<R: Runtime> {
/// Interface for `bare_instantiate` contract call.
#[allow(clippy::too_many_arguments)]
fn deploy_contract(
&mut self,
contract_bytes: Vec<u8>,
value: u128,
piotrMocz marked this conversation as resolved.
Show resolved Hide resolved
data: Vec<u8>,
salt: Vec<u8>,
origin: AccountId32,
gas_limit: Weight,
storage_deposit_limit: Option<u128>,
) -> ContractInstantiateResult<AccountId32, u128, EventRecordOf<R>>;

/// Interface for `bare_upload_code` contract call.
fn upload_contract(
&mut self,
contract_bytes: Vec<u8>,
origin: AccountId32,
storage_deposit_limit: Option<u128>,
) -> CodeUploadResult<<R as frame_system::Config>::Hash, u128>;

/// Interface for `bare_call` contract call.
fn call_contract(
&mut self,
address: AccountId32,
value: u128,
data: Vec<u8>,
origin: AccountId32,
gas_limit: Weight,
storage_deposit_limit: Option<u128>,
) -> ContractExecResult<u128, EventRecordOf<R>>;
}

impl<R: Runtime> ContractApi<R> for Sandbox<R> {
fn deploy_contract(
&mut self,
contract_bytes: Vec<u8>,
value: u128,
data: Vec<u8>,
salt: Vec<u8>,
origin: AccountId32,
gas_limit: Weight,
storage_deposit_limit: Option<u128>,
) -> ContractInstantiateResult<AccountId32, u128, EventRecordOf<R>> {
self.externalities.execute_with(|| {
pallet_contracts::Pallet::<R>::bare_instantiate(
origin,
0,
value,
gas_limit,
None,
storage_deposit_limit,
Code::Upload(contract_bytes),
data,
salt,
Expand All @@ -52,20 +69,38 @@ impl<R: Runtime> ContractApi<R> for Sandbox<R> {
})
}

fn upload_contract(
&mut self,
contract_bytes: Vec<u8>,
origin: AccountId32,
storage_deposit_limit: Option<u128>,
) -> CodeUploadResult<<R as frame_system::Config>::Hash, u128> {
self.externalities.execute_with(|| {
pallet_contracts::Pallet::<R>::bare_upload_code(
origin,
contract_bytes,
storage_deposit_limit,
Determinism::Enforced,
)
})
}

fn call_contract(
&mut self,
address: AccountId32,
value: u128,
data: Vec<u8>,
origin: AccountId32,
gas_limit: Weight,
storage_deposit_limit: Option<u128>,
) -> ContractExecResult<u128, EventRecordOf<R>> {
self.externalities.execute_with(|| {
pallet_contracts::Pallet::<R>::bare_call(
origin,
address,
0,
value,
gas_limit,
None,
storage_deposit_limit,
data,
DebugInfo::UnsafeDebug,
CollectEvents::UnsafeCollect,
Expand Down
21 changes: 18 additions & 3 deletions drink/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ use crate::{
Sandbox, DEFAULT_ACTOR, DEFAULT_GAS_LIMIT,
};

const ZERO_TRANSFER: u128 = 0;
const DEFAULT_STORAGE_DEPOSIT_LIMIT: Option<u128> = None;

/// Session specific errors.
#[derive(Error, Debug)]
pub enum SessionError {
Expand Down Expand Up @@ -165,6 +168,11 @@ impl<R: Runtime> Session<R> {
&mut self.sandbox
}

/// Returns a reference for basic contracts API.
pub fn contracts_api(&mut self) -> &mut impl ContractApi<R> {
&mut self.sandbox
}

/// Deploys a contract with a given constructor, arguments and salt. In case of a success,
/// returns `self`.
pub fn deploy_and(
Expand Down Expand Up @@ -196,10 +204,12 @@ impl<R: Runtime> Session<R> {

let result = self.sandbox.deploy_contract(
contract_bytes,
ZERO_TRANSFER,
data,
salt,
self.actor.clone(),
self.gas_limit,
DEFAULT_STORAGE_DEPOSIT_LIMIT,
);

let ret = match &result.result {
Expand Down Expand Up @@ -272,9 +282,14 @@ impl<R: Runtime> Session<R> {
.clone(),
};

let result = self
.sandbox
.call_contract(address, data, self.actor.clone(), self.gas_limit);
let result = self.sandbox.call_contract(
address,
ZERO_TRANSFER,
data,
self.actor.clone(),
self.gas_limit,
DEFAULT_STORAGE_DEPOSIT_LIMIT,
);

let ret = match &result.result {
Ok(exec_result) if exec_result.did_revert() => Err(SessionError::CallReverted),
Expand Down