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

PassAll gas option for program to program calls #1417

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
7 changes: 2 additions & 5 deletions x/contracts/examples/automated-market-maker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@ state_schema! {
LiquidityToken => Address,
}

const MAX_GAS: Gas = 10_000_000;
const ZERO: u64 = 0;

fn call_args_from_address(address: Address) -> ExternalCallArgs {
ExternalCallArgs {
contract_address: address,
max_units: MAX_GAS,
value: ZERO,
max_units: Gas::PassAll,
value: 0,
}
}

Expand Down
122 changes: 49 additions & 73 deletions x/contracts/examples/automated-market-maker/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use wasmlanche::{
simulator::{Error, SimpleState, Simulator},
Address,
};

const CONTRACT_PATH: &str = env!("CONTRACT_PATH");
const MAX_GAS: u64 = 1000000000;

#[test]
fn init_contract() -> Result<(), Error> {
Expand Down Expand Up @@ -41,35 +41,30 @@ fn add_liquidity_same_ratio() {
let amount: u64 = 100;

simulator
.call_contract::<(), _>(token_x, "mint", (alice, amount), MAX_GAS)
.call_contract::<(), _>(token_x, "mint", (alice, amount))
.unwrap();
simulator
.call_contract::<(), _>(token_y, "mint", (alice, amount), MAX_GAS)
.call_contract::<(), _>(token_y, "mint", (alice, amount))
.unwrap();

let balance: u64 = simulator
.call_contract(lt, "balance_of", alice, MAX_GAS)
.unwrap();
let balance: u64 = simulator.call_contract(lt, "balance_of", alice).unwrap();
assert_eq!(balance, 0, "Balance of liquidity token is incorrect");

simulator
.call_contract::<(), _>(token_x, "approve", (amm, amount), MAX_GAS)
.call_contract::<(), _>(token_x, "approve", (amm, amount))
.unwrap();
simulator
.call_contract::<(), _>(token_y, "approve", (amm, amount), MAX_GAS)
.call_contract::<(), _>(token_y, "approve", (amm, amount))
.unwrap();

let result =
simulator.call_contract::<Units, _>(amm, "add_liquidity", (amount, amount), MAX_GAS);
let result = simulator.call_contract::<Units, _>(amm, "add_liquidity", (amount, amount));
assert!(
result.is_ok(),
"Add liquidity errored {:?}",
result.unwrap_err()
);

let balance: u64 = simulator
.call_contract(lt, "balance_of", alice, MAX_GAS)
.unwrap();
let balance: u64 = simulator.call_contract(lt, "balance_of", alice).unwrap();
assert!(balance > 0, "Balance of liquidity token is incorrect");
}

Expand All @@ -84,19 +79,16 @@ fn add_liquidity_without_approval() {
let amount: u64 = 100;

simulator
.call_contract::<(), _>(token_x, "mint", (alice, amount), MAX_GAS)
.call_contract::<(), _>(token_x, "mint", (alice, amount))
.unwrap();
simulator
.call_contract::<(), _>(token_y, "mint", (alice, amount), MAX_GAS)
.call_contract::<(), _>(token_y, "mint", (alice, amount))
.unwrap();

let balance: u64 = simulator
.call_contract(lt, "balance_of", alice, MAX_GAS)
.unwrap();
let balance: u64 = simulator.call_contract(lt, "balance_of", alice).unwrap();
assert_eq!(balance, 0, "Balance of liquidity token is incorrect");

let result =
simulator.call_contract::<Units, _>(amm, "add_liquidity", (amount, amount), MAX_GAS);
let result = simulator.call_contract::<Units, _>(amm, "add_liquidity", (amount, amount));
assert!(result.is_err(), "Add liquidity did not error");
}

Expand All @@ -112,45 +104,44 @@ fn swap_changes_ratio() {
let amount_x_swap: u64 = 50;

simulator
.call_contract::<(), _>(token_x, "mint", (alice, amount + amount_x_swap), MAX_GAS)
.call_contract::<(), _>(token_x, "mint", (alice, amount + amount_x_swap))
.unwrap();
simulator
.call_contract::<(), _>(token_y, "mint", (alice, amount), MAX_GAS)
.call_contract::<(), _>(token_y, "mint", (alice, amount))
.unwrap();

simulator
.call_contract::<(), _>(token_x, "approve", (amm, amount + amount_x_swap), MAX_GAS)
.call_contract::<(), _>(token_x, "approve", (amm, amount + amount_x_swap))
.unwrap();
simulator
.call_contract::<(), _>(token_y, "approve", (amm, amount), MAX_GAS)
.call_contract::<(), _>(token_y, "approve", (amm, amount))
.unwrap();

let result =
simulator.call_contract::<Units, _>(amm, "add_liquidity", (amount, amount), MAX_GAS);
let result = simulator.call_contract::<Units, _>(amm, "add_liquidity", (amount, amount));
assert!(
result.is_ok(),
"Add liquidity errored {:?}",
result.unwrap_err()
);

let balance_y: u64 = simulator
.call_contract(token_y, "balance_of", alice, MAX_GAS)
.call_contract(token_y, "balance_of", alice)
.unwrap();
assert_eq!(balance_y, 0, "Balance of token y is incorrect");

let swap: u64 = simulator
.call_contract(amm, "swap", (token_x, amount_x_swap), MAX_GAS)
.call_contract(amm, "swap", (token_x, amount_x_swap))
.unwrap();

assert!(swap > 0, "Swap did not return any tokens");

let balance_x: Units = simulator
.call_contract(token_x, "balance_of", alice, MAX_GAS)
.call_contract(token_x, "balance_of", alice)
.unwrap();
assert_eq!(balance_x, 0, "Balance of token x is incorrect");

let balance_y: u64 = simulator
.call_contract(token_y, "balance_of", alice, MAX_GAS)
.call_contract(token_y, "balance_of", alice)
.unwrap();
assert!(balance_y > 0, "Balance of token y is incorrect");
}
Expand All @@ -167,34 +158,33 @@ fn swap_insufficient_funds() {
let amount_x_swap: u64 = 50;

simulator
.call_contract::<(), _>(token_x, "mint", (alice, amount), MAX_GAS)
.call_contract::<(), _>(token_x, "mint", (alice, amount))
.unwrap();
simulator
.call_contract::<(), _>(token_y, "mint", (alice, amount), MAX_GAS)
.call_contract::<(), _>(token_y, "mint", (alice, amount))
.unwrap();

simulator
.call_contract::<(), _>(
token_x,
"approve",
(amm, amount + amount_x_swap + amount_x_swap),
MAX_GAS,
)
.unwrap();
simulator
.call_contract::<(), _>(token_y, "approve", (amm, amount), MAX_GAS)
.call_contract::<(), _>(token_y, "approve", (amm, amount))
.unwrap();

let _result: Units = simulator
.call_contract(amm, "add_liquidity", (amount, amount), MAX_GAS)
.call_contract(amm, "add_liquidity", (amount, amount))
.unwrap();

let balance_y: Units = simulator
.call_contract(token_y, "balance_of", alice, MAX_GAS)
.call_contract(token_y, "balance_of", alice)
.unwrap();
assert_eq!(balance_y, 0, "Balance of token y is incorrect");

let swap = simulator.call_contract::<Units, _>(amm, "swap", (token_x, amount_x_swap), MAX_GAS);
let swap = simulator.call_contract::<Units, _>(amm, "swap", (token_x, amount_x_swap));
assert!(swap.is_err(), "Swap succeeded with insufficient funds");
}

Expand All @@ -216,29 +206,27 @@ fn swap_incorrect_token() {
let amount_x_swap: u64 = 50;

simulator
.call_contract::<(), _>(token_x, "mint", (alice, amount + amount_x_swap), MAX_GAS)
.call_contract::<(), _>(token_x, "mint", (alice, amount + amount_x_swap))
.unwrap();
simulator
.call_contract::<(), _>(token_y, "mint", (alice, amount), MAX_GAS)
.call_contract::<(), _>(token_y, "mint", (alice, amount))
.unwrap();

simulator
.call_contract::<(), _>(token_x, "approve", (amm, amount + amount_x_swap), MAX_GAS)
.call_contract::<(), _>(token_x, "approve", (amm, amount + amount_x_swap))
.unwrap();
simulator
.call_contract::<(), _>(token_y, "approve", (amm, amount), MAX_GAS)
.call_contract::<(), _>(token_y, "approve", (amm, amount))
.unwrap();

let result =
simulator.call_contract::<Units, _>(amm, "add_liquidity", (amount, amount), MAX_GAS);
let result = simulator.call_contract::<Units, _>(amm, "add_liquidity", (amount, amount));
assert!(
result.is_ok(),
"Add liquidity errored {:?}",
result.unwrap_err()
);

let swap =
simulator.call_contract::<Units, _>(amm, "swap", (wrong_token, amount_x_swap), MAX_GAS);
let swap = simulator.call_contract::<Units, _>(amm, "swap", (wrong_token, amount_x_swap));
assert!(swap.is_err(), "Swap succeeded with incorrect token");
}

Expand All @@ -253,56 +241,49 @@ fn remove_liquidity() {
let amount: u64 = 100;

simulator
.call_contract::<(), _>(token_x, "mint", (alice, amount), MAX_GAS)
.call_contract::<(), _>(token_x, "mint", (alice, amount))
.unwrap();
simulator
.call_contract::<(), _>(token_y, "mint", (alice, amount), MAX_GAS)
.call_contract::<(), _>(token_y, "mint", (alice, amount))
.unwrap();

let balance: u64 = simulator
.call_contract(lt, "balance_of", alice, MAX_GAS)
.unwrap();
let balance: u64 = simulator.call_contract(lt, "balance_of", alice).unwrap();
assert_eq!(balance, 0, "Balance of liquidity token is incorrect");

simulator
.call_contract::<(), _>(token_x, "approve", (amm, amount), MAX_GAS)
.call_contract::<(), _>(token_x, "approve", (amm, amount))
.unwrap();
simulator
.call_contract::<(), _>(token_y, "approve", (amm, amount), MAX_GAS)
.call_contract::<(), _>(token_y, "approve", (amm, amount))
.unwrap();

let result =
simulator.call_contract::<Units, _>(amm, "add_liquidity", (amount, amount), MAX_GAS);
let result = simulator.call_contract::<Units, _>(amm, "add_liquidity", (amount, amount));
assert!(
result.is_ok(),
"Add liquidity errored {:?}",
result.unwrap_err()
);

let balance: Units = simulator
.call_contract(lt, "balance_of", alice, MAX_GAS)
.unwrap();
let balance: Units = simulator.call_contract(lt, "balance_of", alice).unwrap();
assert!(balance > 0, "Balance of liquidity token is incorrect");

let (token_x_balance, token_y_balance): (u64, u64) = simulator
.call_contract(amm, "remove_liquidity", balance, MAX_GAS)
.call_contract(amm, "remove_liquidity", balance)
.unwrap();

assert_eq!(token_x_balance, amount, "Token x balance is incorrect");
assert_eq!(token_y_balance, amount, "Token y balance is incorrect");

let balance: Units = simulator
.call_contract(lt, "balance_of", alice, MAX_GAS)
.unwrap();
let balance: Units = simulator.call_contract(lt, "balance_of", alice).unwrap();
assert_eq!(balance, 0, "Balance of liquidity token is incorrect");

let balance_x: Units = simulator
.call_contract(token_x, "balance_of", alice, MAX_GAS)
.call_contract(token_x, "balance_of", alice)
.unwrap();
assert_eq!(balance_x, amount, "Balance of token x is incorrect");

let balance_y: Units = simulator
.call_contract(token_y, "balance_of", alice, MAX_GAS)
.call_contract(token_y, "balance_of", alice)
.unwrap();
assert_eq!(balance_y, amount, "Balance of token y is incorrect");
}
Expand All @@ -322,28 +303,23 @@ fn init_amm(simulator: &mut Simulator) -> (Address, Address, Address, Address) {

// initialize tokens
simulator
.call_contract::<(), _>(token_x, "init", ("CoinX", "CX"), MAX_GAS)
.call_contract::<(), _>(token_x, "init", ("CoinX", "CX"))
.unwrap();
simulator
.call_contract::<(), _>(token_y, "init", ("YCoin", "YC"), MAX_GAS)
.call_contract::<(), _>(token_y, "init", ("YCoin", "YC"))
.unwrap();
let amm_contract = simulator.create_contract(CONTRACT_PATH).unwrap().address;

simulator
.call_contract::<(), _>(
amm_contract,
"init",
(token_x, token_y, token_contract_id),
MAX_GAS,
)
.call_contract::<(), _>(amm_contract, "init", (token_x, token_y, token_contract_id))
.unwrap();

// Check if the liquidity token was created
let lt: Address = simulator
.call_contract(amm_contract, "get_liquidity_token", (), MAX_GAS)
.call_contract(amm_contract, "get_liquidity_token", ())
.unwrap();
// grab the name of the liquidity token
let lt_name: String = simulator.call_contract(lt, "symbol", (), MAX_GAS).unwrap();
let lt_name: String = simulator.call_contract(lt, "symbol", ()).unwrap();

assert_eq!(lt_name, "LT", "Liquidity token name is incorrect");

Expand Down
6 changes: 3 additions & 3 deletions x/contracts/examples/counter-external/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Copyright (C) 2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

use wasmlanche::{public, Address, Context, ExternalCallArgs};
use wasmlanche::{public, Address, Context, ExternalCallArgs, Gas};

#[public]
pub fn inc(ctx: &mut Context, contract_address: Address, of: Address) {
let args = ExternalCallArgs {
contract_address,
max_units: 1_000_000,
max_units: Gas::PassAll,
value: 0,
};

Expand All @@ -20,7 +20,7 @@ pub fn inc(ctx: &mut Context, contract_address: Address, of: Address) {
pub fn get_value(ctx: &mut Context, contract_address: Address, of: Address) -> u64 {
let args = ExternalCallArgs {
contract_address,
max_units: 1_000_000,
max_units: Gas::PassAll,
value: 0,
};

Expand Down
4 changes: 2 additions & 2 deletions x/contracts/examples/counter-external/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ fn inc_and_get_value() {
let counter = simulator.create_contract(&counter_path).unwrap().address;

simulator
.call_contract::<(), _>(counter_external, "inc", (counter, owner), 100_000_000)
.call_contract::<(), _>(counter_external, "inc", (counter, owner))
.unwrap();

let response: u64 = simulator
.call_contract(counter_external, "get_value", (counter, owner), 100_000_000)
.call_contract(counter_external, "get_value", (counter, owner))
.unwrap();

assert_eq!(response, 1);
Expand Down
5 changes: 2 additions & 3 deletions x/contracts/examples/counter/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@ fn init_contract() -> Result<(), Error> {
fn increment() {
let mut state = SimpleState::new();
let simulator = Simulator::new(&mut state);
let gas = 100000000;
let bob = Address::new([1; 33]);
let counter_address = simulator.create_contract(CONTRACT_PATH).unwrap().address;

simulator
.call_contract::<bool, _>(counter_address, "inc", (bob, 10u64), gas)
.call_contract::<bool, _>(counter_address, "inc", (bob, 10u64))
.unwrap();
let value: u64 = simulator
.call_contract(counter_address, "get_value", ((bob),), gas)
.call_contract(counter_address, "get_value", ((bob),))
.unwrap();

assert_eq!(value, 10);
Expand Down
Loading
Loading