fun p<T>(s: &T) {
debug::print(s);
}
fun p_vector(s: vector<u8>) {
debug::print(&string::utf8(s));
}
#[test(deployer = @pizza_drop, user1 = @0x123, user2 = @0x456, framework = @0x1)]
#[expected_failure(abort_code = E_INSUFFICIENT_FUND)]
fun test_get_random_slice_exceeding_pool_funds(
deployer: &signer,
user1: &signer,
user2: &signer,
framework: &signer
) acquires State, ModuleData {
use aptos_framework::account;
use aptos_framework::timestamp;
use aptos_framework::aptos_coin;
timestamp::set_time_has_started_for_testing(framework);
let now_us = timestamp::now_microseconds();
p_vector(b"now_us");
p(&now_us);
let (burn_cap, mint_cap) = aptos_coin::initialize_for_test(framework);
account::create_account_for_test(@pizza_drop);
account::create_account_for_test(signer::address_of(user1));
account::create_account_for_test(signer::address_of(user2));
init_module(deployer);
let funding_amount = 100000;
let deployer_coins = coin::mint<AptosCoin>(funding_amount, &mint_cap);
coin::register<AptosCoin>(deployer);
coin::deposit<AptosCoin>(@pizza_drop, deployer_coins);
let deployer_balance = coin::balance<AptosCoin>(@pizza_drop);
p_vector(b"Deployer APT balance: ");
debug::print(&deployer_balance);
let contract_funding = 100;
fund_pizza_drop(deployer, contract_funding);
let contract_balance = get_actual_apt_balance();
p_vector(b"Contract APT balance: ");
debug::print(&contract_balance);
let user1_addr = signer::address_of(user1);
register_pizza_lover(deployer, user1_addr);
let user2_addr = signer::address_of(user2);
get_random_slice(user2_addr);
let user1_claimed_amount = get_claimed_amount(user1_addr);
p_vector(b"user1_claimed_amount");
p(&user1_claimed_amount);
let user2_claimed_amount = get_claimed_amount(user2_addr);
p_vector(b"user2_claimed_amount");
p(&user2_claimed_amount);
claim_pizza_slice(user2);
assert!(has_claimed_slice(user2_addr), 1);
claim_pizza_slice(user1);
coin::destroy_burn_cap(burn_cap);
coin::destroy_mint_cap(mint_cap);
}
Enforce that there are sufficient funds in the PizzaDrop pool before registering a user with the PizzaDrop slice.
#[randomness]
entry fun get_random_slice(user_addr: address) acquires ModuleData, State {
let state = borrow_global_mut<State>(get_resource_address());
let time = timestamp::now_microseconds();
let random_val = time % 401;
let random_amount = 100 + random_val; // 100-500 APT (in Octas: 10^8 smallest unit)
+ // Check if contract has sufficient balance
+ assert!(state.balance >= random_amount, E_INSUFFICIENT_FUND);
table::add(&mut state.users_claimed_amount, user_addr, random_amount);
}