Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 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
10 changes: 9 additions & 1 deletion .github/workflows/contracts-e2e-tests-and-deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,15 @@ jobs:
target-key: e2e-contracts
cargo-key: e2e-contracts
cache-version: v3
cargo-targets: e2e-tests-contracts/target/
cargo-targets: |
e2e-tests/target/
contracts/access_control/target/
contracts/button/target/
contracts/game_token/target/
contracts/marketplace/target/
contracts/simple_dex/target/
contracts/ticket_token/target/
contracts/wrapped_azero/target/

- name: Install cargo-contract
run: |
Expand Down
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion aleph-client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "aleph_client"
version = "1.9.0"
version = "1.10.0"
edition = "2021"
license = "Apache 2.0"

Expand Down
2 changes: 1 addition & 1 deletion aleph-client/src/contract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub struct ContractInstance {

impl ContractInstance {
const MAX_READ_GAS: u64 = 500000000000u64;
const MAX_GAS: u64 = 10000000000u64;
const MAX_GAS: u64 = 100000000000u64;
const PAYABLE_VALUE: u64 = 0u64;
const STORAGE_FEE_LIMIT: Option<u128> = None;

Expand Down
18 changes: 18 additions & 0 deletions aleph-client/src/contract/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,21 @@ pub fn to_account_id(value: Value) -> Result<AccountId> {
_ => Err(anyhow!("Expected {:?} to be a string", value)),
}
}

/// Returns `Ok(bool)` if the given `Value` represents one, or `Err(_)` otherwise.
/// ```
/// # #![feature(assert_matches)]
/// # use std::assert_matches::assert_matches;
/// # use anyhow::anyhow;
/// # use aleph_client::contract::util::to_bool;
/// use contract_transcode::Value;
///
/// assert_matches!(to_bool(Value::Bool(true)), Ok(true));
/// assert_matches!(to_bool(Value::UInt(42)), Err(_));
/// ```
pub fn to_bool(value: Value) -> Result<bool> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be more "rusty" to implement TryFrom fro such a conversion and then use .into() for a short & sweet syntax

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I implement TryFrom for a type from another crate?

@fbielejec fbielejec Nov 2, 2022

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, as long as either the trait you're implementing or the struct it's being implemented for come from the current crate. It is kinda possible to go around that with: https://doc.rust-lang.org/rust-by-example/generics/new_types.html (but you likely won't even need to do it here)

@obrok obrok Nov 2, 2022

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I knew about that, just thought it would be too much cruft. Hm, I guess it could be something like:

// Instead of
to_bool(result)
// do
ValueConvert(result).into()

at the call site? Honestly, I don't think this is very clean, but can change if you think it's better, let me know.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that I'm such a big rust pro, but I think it's more idiomatic... I'll leave the decision to you and the other reviewer (@maciejzelaszczyk )

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I found a way to make this nice at the call site, take a look.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not have a problem with these conversions in their current form.

match value {
Value::Bool(value) => Ok(value),
_ => Err(anyhow!("Expected {:?} to be a boolean", value)),
}
}
1 change: 1 addition & 0 deletions aleph-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use ac_primitives::{PlainTipExtrinsicParamsBuilder, SubstrateDefaultSignedExtra}
pub use account::{get_free_balance, locks};
pub use balances::total_issuance;
use codec::{Decode, Encode};
pub use contract_transcode;
pub use debug::print_storages;
pub use elections::{
get_committee_seats, get_current_era_non_reserved_validators,
Expand Down
2 changes: 1 addition & 1 deletion bin/cliain/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct ContractOptions {
#[clap(long, default_value = "0")]
pub balance: u128,
/// The gas limit enforced when executing the constructor
#[clap(long, default_value = "1_000_000_000")]
#[clap(long, default_value = "1000000000")]
pub gas_limit: u64,
/// The maximum amount of balance that can be charged/reserved from the caller to pay for the storage consumed
#[clap(long)]
Expand Down
2 changes: 1 addition & 1 deletion contracts/button/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod errors;
use ink_lang as ink;

#[ink::contract]
mod button_game {
pub mod button_game {
use access_control::{roles::Role, traits::AccessControlled, ACCESS_CONTROL_PUBKEY};
use game_token::MINT_SELECTOR;
use ink_env::{
Expand Down
2 changes: 1 addition & 1 deletion contracts/env/dev
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export AUTHORITY_SEED=//Alice
export LIFETIME=20

# mint this many ticket tokens
export TICKET_BALANCE=100
export TICKET_BALANCE=100000000

# initial price of ticket on the marketplace
export INITIAL_PRICE=69
1 change: 1 addition & 0 deletions contracts/scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ function deploy_button_game {
cd "$CONTRACTS_PATH"/access_control

cargo contract call --url "$NODE" --contract "$ACCESS_CONTROL" --message grant_role --args "$AUTHORITY" 'Owner('"$contract_address"')' --suri "$AUTHORITY_SEED" --skip-confirm
cargo contract call --url "$NODE" --contract "$ACCESS_CONTROL" --message grant_role --args "$AUTHORITY" 'Minter('"$game_token"')' --suri "$AUTHORITY_SEED" --skip-confirm
Comment thread
obrok marked this conversation as resolved.
Outdated
cargo contract call --url "$NODE" --contract "$ACCESS_CONTROL" --message grant_role --args "$contract_address" 'Admin('"$marketplace"')' --suri "$AUTHORITY_SEED" --skip-confirm
cargo contract call --url "$NODE" --contract "$ACCESS_CONTROL" --message grant_role --args "$contract_address" 'Minter('"$game_token"')' --suri "$AUTHORITY_SEED" --skip-confirm

Expand Down
91 changes: 20 additions & 71 deletions contracts/scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,77 +2,26 @@

set -euo pipefail

# --- FUNCTIONS

function play {

local contract_name=$1
local contract_address=$(cat "$CONTRACTS_PATH"/addresses.json | jq --raw-output ".$contract_name")
local ticket_address=$(cat "$CONTRACTS_PATH"/addresses.json | jq --raw-output ".${contract_name}_ticket")

# airdrop initial tickets

cd "$CONTRACTS_PATH"/ticket_token

echo "sending ticket token" ${contract_name}_ticket "["$ticket_address"]" "to " $PLAYER1

cargo contract call --url $NODE --contract $ticket_address --message PSP22::transfer --args $PLAYER1 1 "[0]" --suri $AUTHORITY_SEED --skip-confirm

echo "sending ticket token" ${contract_name}_ticket "["$ticket_address"]" "to " $PLAYER2

cargo contract call --url $NODE --contract $ticket_address --message PSP22::transfer --args $PLAYER2 1 "[0]" --suri $AUTHORITY_SEED --skip-confirm

# give allowance for spending tickets to the game contract

echo "allowing" $contract_name "["$contract_address"]" "to spend up to" $TICKET_BALANCE "of" ${contract_name}_ticket "["$ticket_address"]" "on behalf of" $PLAYER1

cargo contract call --url $NODE --contract $ticket_address --message PSP22::approve --args $contract_address $TICKET_BALANCE --suri $PLAYER1_SEED --skip-confirm

echo "allowing" $contract_name "["$contract_address"]" "to spend up to" $TICKET_BALANCE "of" ${contract_name}_ticket "["$ticket_address"]" "on behalf of" $PLAYER2

cargo contract call --url $NODE --contract $ticket_address --message PSP22::approve --args $contract_address $TICKET_BALANCE --suri $PLAYER2_SEED --skip-confirm

# TODO: uncomment when cargo contract doesn't break on parsing "foreign" events
#
# # play the game
#
# cd "$CONTRACTS_PATH"/button
#
# echo "calling press for" $contract_name "["$contract_address"]" "by" $PLAYER1_SEED
#
# cargo contract call --url $NODE --contract $contract_address --message press --suri $PLAYER1_SEED --skip-confirm
#
# sleep 1
#
# echo "calling press for" $contract_name "["$contract_address "]" "by" $PLAYER2_SEED
#
# cargo contract call --url $NODE --contract $contract_address --message press --suri $PLAYER2_SEED --skip-confirm
#
# # --- WAIT FOR THE BUTTON DEATH
#
# sleep $(($LIFETIME + 1))
#
# # --- TRIGGER GAME RESET
#
# cargo contract call --url $NODE --contract $contract_address --message reset --suri $AUTHORITY_SEED --skip-confirm
#
# echo "Done playing" $contract_name
}

# --- ARGUMENTS

E2E_PATH=$(pwd)/e2e-tests
CONTRACTS_PATH=$(pwd)/contracts

PLAYER1=5D34dL5prEUaGNQtPPZ3yN5Y6BnkfXunKXXz6fo7ZJbLwRRH
PLAYER1_SEED=//0
PLAYER2=5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY
PLAYER2_SEED=//Alice

GAMES=(early_bird_special back_to_the_future the_pressiah_cometh)
for GAME in "${GAMES[@]}"; do
(
play $GAME
)&
done
EARLY_BIRD_SPECIAL=$(jq --raw-output ".early_bird_special" < "$CONTRACTS_PATH"/addresses.json)
THE_PRESSIAH_COMETH=$(jq --raw-output ".the_pressiah_cometh" < "$CONTRACTS_PATH"/addresses.json)
BACK_TO_THE_FUTURE=$(jq --raw-output ".back_to_the_future" < "$CONTRACTS_PATH"/addresses.json)

pushd "$E2E_PATH"

RUST_LOG="aleph_e2e_client=info" cargo run --release -- \
--test-cases marketplace \
--test-cases button_game_reset \
--test-cases early_bird_special \
--test-cases the_pressiah_cometh \
--test-cases back_to_the_future \
--early-bird-special "$EARLY_BIRD_SPECIAL" \
--the-pressiah-cometh "$THE_PRESSIAH_COMETH" \
--back-to-the-future "$BACK_TO_THE_FUTURE" \
--button-game-metadata ../contracts/button/target/ink/metadata.json \
--ticket-token-metadata ../contracts/ticket_token/target/ink/metadata.json \
--reward-token-metadata ../contracts/game_token/target/ink/metadata.json \
--marketplace-metadata ../contracts/marketplace/target/ink/metadata.json

exit $?
43 changes: 42 additions & 1 deletion e2e-tests/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion e2e-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ log = "0.4"
serde_json = "1.0"
codec = { package = 'parity-scale-codec', version = "3.0", default-features = false, features = ['derive'] }
rayon = "1.5"
rand = "0.8"
itertools = "0.10"
assert2 = "0.3"

sp-core = { git = "https://github.com/Cardinal-Cryptography/substrate.git", branch = "aleph-v0.9.26", default-features = false, features = ["full_crypto"] }
sp-runtime = { git = "https://github.com/Cardinal-Cryptography/substrate.git", branch = "aleph-v0.9.26", default-features = false }
Expand All @@ -30,5 +33,5 @@ default = ["std"]
std = [
"pallet-staking/std",
"pallet-balances/std",
"primitives/std"
"primitives/std",
]
16 changes: 12 additions & 4 deletions e2e-tests/src/cases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ use crate::{
config::Config,
test::{
authorities_are_staking as test_authorities_are_staking,
batch_transactions as test_batch_transactions,
back_to_the_future as test_back_to_the_future,
batch_transactions as test_batch_transactions, button_game_reset as test_button_game_reset,
change_stake_and_force_new_era as test_change_stake_and_force_new_era,
change_validators as test_change_validators,
channeling_fee_and_tip as test_channeling_fee_and_tip, disable_node as test_disable_node,
early_bird_special as test_early_bird_special,
era_payouts_calculated_correctly as test_era_payout, era_validators as test_era_validators,
fee_calculation as test_fee_calculation, finalization as test_finalization,
force_new_era as test_force_new_era, points_basic as test_points_basic,
points_stake_change as test_points_stake_change,
force_new_era as test_force_new_era, marketplace as test_marketplace,
points_basic as test_points_basic, points_stake_change as test_points_stake_change,
staking_era_payouts as test_staking_era_payouts,
staking_new_validator as test_staking_new_validator, token_transfer as test_token_transfer,
staking_new_validator as test_staking_new_validator,
the_pressiah_cometh as test_the_pressiah_cometh, token_transfer as test_token_transfer,
treasury_access as test_treasury_access, validators_rotate as test_validators_rotate,
},
};
Expand Down Expand Up @@ -55,5 +58,10 @@ pub fn possible_test_cases() -> PossibleTestCases {
"authorities_are_staking",
test_authorities_are_staking as TestCase,
),
("button_game_reset", test_button_game_reset as TestCase),
("early_bird_special", test_early_bird_special as TestCase),
("back_to_the_future", test_back_to_the_future as TestCase),
("the_pressiah_cometh", test_the_pressiah_cometh as TestCase),
("marketplace", test_marketplace as TestCase),
]
}
Loading