Skip to content

Commit

Permalink
Limit, value
Browse files Browse the repository at this point in the history
  • Loading branch information
pmikolajczyk41 committed Aug 18, 2023
1 parent 2f5b017 commit 0d7d436
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
21 changes: 16 additions & 5 deletions drink/src/contract_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,55 @@ 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,
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 @@ -65,12 +73,13 @@ impl<R: Runtime> ContractApi<R> for Sandbox<R> {
&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,
None,
storage_deposit_limit,
Determinism::Enforced,
)
})
Expand All @@ -79,17 +88,19 @@ impl<R: Runtime> ContractApi<R> for Sandbox<R> {
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
16 changes: 13 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 @@ -201,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 @@ -277,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

0 comments on commit 0d7d436

Please sign in to comment.