|
| 1 | +#[test_only] |
| 2 | +module pyth_lazer::pyth_lazer_tests { |
| 3 | + use std::signer; |
| 4 | + use std::string; |
| 5 | + use aptos_framework::account; |
| 6 | + use aptos_framework::coin; |
| 7 | + use aptos_framework::timestamp; |
| 8 | + use aptos_framework::aptos_coin::AptosCoin; |
| 9 | + use aptos_std::ed25519; |
| 10 | + use pyth_lazer::pyth_lazer; |
| 11 | + |
| 12 | + // Test accounts |
| 13 | + const TOP_AUTHORITY: address = @0x123; |
| 14 | + const TREASURY: address = @0x456; |
| 15 | + const USER: address = @0x789; |
| 16 | + |
| 17 | + // Test data |
| 18 | + const TEST_PUBKEY: vector<u8> = x"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; |
| 19 | + const TEST_MESSAGE: vector<u8> = x"deadbeef"; |
| 20 | + const TEST_SIGNATURE: vector<u8> = x"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; |
| 21 | + |
| 22 | + #[test_only] |
| 23 | + fun setup_aptos_coin(framework: &signer): coin::MintCapability<AptosCoin> { |
| 24 | + let (burn_cap, freeze_cap, mint_cap) = coin::initialize<AptosCoin>( |
| 25 | + framework, |
| 26 | + std::string::utf8(b"Aptos Coin"), |
| 27 | + std::string::utf8(b"APT"), |
| 28 | + 8, |
| 29 | + false, |
| 30 | + ); |
| 31 | + coin::destroy_burn_cap(burn_cap); |
| 32 | + coin::destroy_freeze_cap(freeze_cap); |
| 33 | + mint_cap |
| 34 | + } |
| 35 | + |
| 36 | + fun setup(): (signer, signer, signer) { |
| 37 | + // Create test accounts |
| 38 | + let framework = account::create_account_for_test(@aptos_framework); |
| 39 | + let top_authority = account::create_account_for_test(TOP_AUTHORITY); |
| 40 | + let treasury = account::create_account_for_test(TREASURY); |
| 41 | + let user = account::create_account_for_test(USER); |
| 42 | + |
| 43 | + // Setup AptosCoin and get mint capability |
| 44 | + let mint_cap = setup_aptos_coin(&framework); |
| 45 | + |
| 46 | + // Register accounts for AptosCoin |
| 47 | + coin::register<AptosCoin>(&top_authority); |
| 48 | + coin::register<AptosCoin>(&treasury); |
| 49 | + coin::register<AptosCoin>(&user); |
| 50 | + |
| 51 | + // Give user some coins for fees |
| 52 | + let coins = coin::mint<AptosCoin>(1000, &mint_cap); |
| 53 | + coin::deposit(signer::address_of(&user), coins); |
| 54 | + coin::destroy_mint_cap(mint_cap); |
| 55 | + |
| 56 | + // Initialize timestamp for expiration tests |
| 57 | + timestamp::set_time_has_started_for_testing(&framework); |
| 58 | + |
| 59 | + // Initialize contract |
| 60 | + pyth_lazer::initialize(&top_authority, TOP_AUTHORITY, TREASURY); |
| 61 | + |
| 62 | + (top_authority, treasury, user) |
| 63 | + } |
| 64 | + |
| 65 | + #[test] |
| 66 | + fun test_initialize() { |
| 67 | + let (top_authority, _treasury, _) = setup(); |
| 68 | + // Contract is already initialized in setup |
| 69 | + } |
| 70 | + |
| 71 | + #[test] |
| 72 | + fun test_update_add_signer() { |
| 73 | + let (top_authority, _treasury, _) = setup(); |
| 74 | + |
| 75 | + // Add signer |
| 76 | + let expires_at = timestamp::now_seconds() + 1000; |
| 77 | + pyth_lazer::update_trusted_signer(&top_authority, TEST_PUBKEY, expires_at); |
| 78 | + |
| 79 | + // Update signer |
| 80 | + let new_expires_at = timestamp::now_seconds() + 2000; |
| 81 | + pyth_lazer::update_trusted_signer(&top_authority, TEST_PUBKEY, new_expires_at); |
| 82 | + |
| 83 | + // Remove signer |
| 84 | + pyth_lazer::update_trusted_signer(&top_authority, TEST_PUBKEY, 0); |
| 85 | + } |
| 86 | + |
| 87 | + #[test] |
| 88 | + #[expected_failure(abort_code = pyth_lazer::ENO_SPACE)] |
| 89 | + fun test_max_signers() { |
| 90 | + let (top_authority, _treasury, _) = setup(); |
| 91 | + |
| 92 | + let expires_at = timestamp::now_seconds() + 1000; |
| 93 | + let pubkey1 = x"1111111111111111111111111111111111111111111111111111111111111111"; |
| 94 | + let pubkey2 = x"2222222222222222222222222222222222222222222222222222222222222222"; |
| 95 | + let pubkey3 = x"3333333333333333333333333333333333333333333333333333333333333333"; |
| 96 | + |
| 97 | + pyth_lazer::update_trusted_signer(&top_authority, pubkey1, expires_at); |
| 98 | + pyth_lazer::update_trusted_signer(&top_authority, pubkey2, expires_at); |
| 99 | + // This should fail as we already have 2 signers |
| 100 | + pyth_lazer::update_trusted_signer(&top_authority, pubkey3, expires_at); |
| 101 | + } |
| 102 | + |
| 103 | + #[test] |
| 104 | + #[expected_failure(abort_code = pyth_lazer::EINVALID_SIGNER)] |
| 105 | + fun test_expired_signer() { |
| 106 | + let (top_authority, _treasury, user) = setup(); |
| 107 | + |
| 108 | + // Add signer that expires in 1000 seconds |
| 109 | + let expires_at = timestamp::now_seconds() + 1000; |
| 110 | + pyth_lazer::update_trusted_signer(&top_authority, TEST_PUBKEY, expires_at); |
| 111 | + |
| 112 | + // Move time forward past expiration |
| 113 | + timestamp::fast_forward_seconds(2000); |
| 114 | + |
| 115 | + // This should fail as the signer is expired |
| 116 | + pyth_lazer::verify_message(&user, TEST_MESSAGE, TEST_SIGNATURE, TEST_PUBKEY); |
| 117 | + } |
| 118 | + |
| 119 | + #[test] |
| 120 | + #[expected_failure(abort_code = pyth_lazer::EINSUFFICIENT_FEE)] |
| 121 | + fun test_insufficient_fee() { |
| 122 | + let (top_authority, _treasury, user) = setup(); |
| 123 | + |
| 124 | + // Drain user's balance |
| 125 | + let user_balance = coin::balance<AptosCoin>(signer::address_of(&user)); |
| 126 | + coin::transfer<AptosCoin>(&user, TREASURY, user_balance); |
| 127 | + |
| 128 | + // This should fail due to insufficient fee |
| 129 | + pyth_lazer::verify_message(&user, TEST_MESSAGE, TEST_SIGNATURE, TEST_PUBKEY); |
| 130 | + } |
| 131 | +} |
0 commit comments