| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- // SPDX-License-Identifier: Apache-2.0
- use crate::{
- account_new, build_solidity, build_solidity_with_cache, AccountMeta, AccountState, BorshToken,
- Pubkey,
- };
- use borsh::BorshSerialize;
- use num_bigint::BigInt;
- use solang::file_resolver::FileResolver;
- #[test]
- fn use_authority() {
- let mut vm = build_solidity(include_str!("../../docs/examples/solana/use_authority.sol"));
- let authority = account_new();
- vm.account_data.insert(
- authority,
- AccountState {
- data: vec![],
- owner: Some([0u8; 32]),
- lamports: 0,
- },
- );
- let data_account = vm.initialize_data_account();
- vm.function("new")
- .arguments(&[BorshToken::Address(authority)])
- .accounts(vec![("dataAccount", data_account)])
- .call();
- let res = vm
- .function("inc")
- .accounts(vec![("dataAccount", data_account)])
- .must_fail()
- .unwrap();
- assert_ne!(res, 0);
- let res = vm
- .function("get")
- .accounts(vec![("dataAccount", data_account)])
- .call()
- .unwrap();
- assert_eq!(
- res,
- BorshToken::Uint {
- width: 64,
- value: 0.into()
- }
- );
- vm.function("inc")
- .accounts(vec![("dataAccount", data_account)])
- .remaining_accounts(&[AccountMeta {
- pubkey: Pubkey(authority),
- is_signer: true,
- is_writable: false,
- }])
- .call();
- let res = vm
- .function("get")
- .accounts(vec![("dataAccount", data_account)])
- .call()
- .unwrap();
- assert_eq!(
- res,
- BorshToken::Uint {
- width: 64,
- value: 1.into()
- }
- );
- }
- #[test]
- fn token_account() {
- let mut cache = FileResolver::default();
- cache.set_file_contents(
- "spl_token.sol",
- include_str!("../../solana-library/spl_token.sol").to_string(),
- );
- let src = r#"
- import './spl_token.sol';
- contract Foo {
- function token_account(address add) public returns (SplToken.TokenAccountData) {
- return SplToken.get_token_account_data(add);
- }
- }
- "#;
- cache.set_file_contents("test.sol", src.to_string());
- let mut vm = build_solidity_with_cache(cache);
- #[derive(BorshSerialize)]
- struct TokenAccount {
- mint_account: [u8; 32],
- owner: [u8; 32],
- balance: u64,
- delegate_present: u32,
- delegate: [u8; 32],
- state: u8,
- is_native_present: u32,
- is_native: u64,
- delegated_amount: u64,
- close_authority_present: u32,
- close_authority: [u8; 32],
- }
- let mut data = TokenAccount {
- mint_account: account_new(),
- owner: account_new(),
- balance: 234,
- delegate_present: 0,
- delegate: account_new(),
- state: 1,
- is_native_present: 0,
- is_native: 234,
- delegated_amount: 1346,
- close_authority_present: 0,
- close_authority: account_new(),
- };
- let encoded = data.try_to_vec().unwrap();
- let account = account_new();
- vm.account_data.insert(
- account,
- AccountState {
- owner: None,
- lamports: 0,
- data: encoded,
- },
- );
- let data_account = vm.initialize_data_account();
- vm.function("new")
- .accounts(vec![("dataAccount", data_account)])
- .call();
- let res = vm
- .function("token_account")
- .arguments(&[BorshToken::Address(account)])
- .accounts(vec![("dataAccount", data_account)])
- .remaining_accounts(&[AccountMeta {
- pubkey: Pubkey(account),
- is_signer: false,
- is_writable: false,
- }])
- .call()
- .unwrap()
- .unwrap_tuple();
- assert_eq!(
- res,
- vec![
- BorshToken::Address(data.mint_account),
- BorshToken::Address(data.owner),
- BorshToken::Uint {
- width: 64,
- value: BigInt::from(data.balance)
- },
- BorshToken::Bool(data.delegate_present > 0),
- BorshToken::Address(data.delegate),
- BorshToken::Uint {
- width: 8,
- value: BigInt::from(data.state)
- },
- BorshToken::Bool(data.is_native_present > 0),
- BorshToken::Uint {
- width: 64,
- value: BigInt::from(data.is_native)
- },
- BorshToken::Uint {
- width: 64,
- value: BigInt::from(data.delegated_amount)
- },
- BorshToken::Bool(data.close_authority_present > 0),
- BorshToken::Address(data.close_authority)
- ]
- );
- data.delegate_present = 1;
- data.is_native_present = 1;
- data.close_authority_present = 1;
- let encoded = data.try_to_vec().unwrap();
- vm.account_data.get_mut(&account).unwrap().data = encoded;
- let res = vm
- .function("token_account")
- .arguments(&[BorshToken::Address(account)])
- .accounts(vec![("dataAccount", data_account)])
- .remaining_accounts(&[AccountMeta {
- pubkey: Pubkey(account),
- is_signer: false,
- is_writable: false,
- }])
- .call()
- .unwrap()
- .unwrap_tuple();
- assert_eq!(
- res,
- vec![
- BorshToken::Address(data.mint_account),
- BorshToken::Address(data.owner),
- BorshToken::Uint {
- width: 64,
- value: BigInt::from(data.balance)
- },
- BorshToken::Bool(data.delegate_present > 0),
- BorshToken::Address(data.delegate),
- BorshToken::Uint {
- width: 8,
- value: BigInt::from(data.state)
- },
- BorshToken::Bool(data.is_native_present > 0),
- BorshToken::Uint {
- width: 64,
- value: BigInt::from(data.is_native)
- },
- BorshToken::Uint {
- width: 64,
- value: BigInt::from(data.delegated_amount)
- },
- BorshToken::Bool(data.close_authority_present > 0),
- BorshToken::Address(data.close_authority)
- ]
- );
- }
- #[test]
- fn mint_account() {
- let mut cache = FileResolver::default();
- cache.set_file_contents(
- "spl_token.sol",
- include_str!("../../solana-library/spl_token.sol").to_string(),
- );
- let src = r#"
- import './spl_token.sol';
- contract Foo {
- function mint_account(address add) public returns (SplToken.MintAccountData) {
- return SplToken.get_mint_account_data(add);
- }
- }
- "#;
- cache.set_file_contents("test.sol", src.to_string());
- let mut vm = build_solidity_with_cache(cache);
- #[derive(BorshSerialize)]
- struct MintAccountData {
- authority_present: u32,
- mint_authority: [u8; 32],
- supply: u64,
- decimals: u8,
- is_initialized: bool,
- freeze_authority_present: u32,
- freeze_authority: [u8; 32],
- }
- let mut data = MintAccountData {
- authority_present: 0,
- mint_authority: account_new(),
- supply: 450,
- decimals: 4,
- is_initialized: false,
- freeze_authority_present: 0,
- freeze_authority: account_new(),
- };
- let encoded = data.try_to_vec().unwrap();
- let account = account_new();
- vm.account_data.insert(
- account,
- AccountState {
- owner: None,
- lamports: 0,
- data: encoded,
- },
- );
- let data_account = vm.initialize_data_account();
- vm.function("new")
- .accounts(vec![("dataAccount", data_account)])
- .call();
- let res = vm
- .function("mint_account")
- .accounts(vec![("dataAccount", data_account)])
- .arguments(&[BorshToken::Address(account)])
- .remaining_accounts(&[AccountMeta {
- pubkey: Pubkey(account),
- is_writable: false,
- is_signer: false,
- }])
- .call()
- .unwrap()
- .unwrap_tuple();
- assert_eq!(
- res,
- vec![
- BorshToken::Bool(data.authority_present > 0),
- BorshToken::Address(data.mint_authority),
- BorshToken::Uint {
- width: 64,
- value: BigInt::from(data.supply)
- },
- BorshToken::Uint {
- width: 8,
- value: BigInt::from(data.decimals)
- },
- BorshToken::Bool(data.is_initialized),
- BorshToken::Bool(data.freeze_authority_present > 0),
- BorshToken::Address(data.freeze_authority)
- ]
- );
- data.authority_present = 1;
- data.is_initialized = true;
- data.freeze_authority_present = 1;
- let encoded = data.try_to_vec().unwrap();
- vm.account_data.get_mut(&account).unwrap().data = encoded;
- let res = vm
- .function("mint_account")
- .accounts(vec![("dataAccount", data_account)])
- .arguments(&[BorshToken::Address(account)])
- .remaining_accounts(&[AccountMeta {
- pubkey: Pubkey(account),
- is_writable: false,
- is_signer: false,
- }])
- .call()
- .unwrap()
- .unwrap_tuple();
- assert_eq!(
- res,
- vec![
- BorshToken::Bool(data.authority_present > 0),
- BorshToken::Address(data.mint_authority),
- BorshToken::Uint {
- width: 64,
- value: BigInt::from(data.supply)
- },
- BorshToken::Uint {
- width: 8,
- value: BigInt::from(data.decimals)
- },
- BorshToken::Bool(data.is_initialized),
- BorshToken::Bool(data.freeze_authority_present > 0),
- BorshToken::Address(data.freeze_authority)
- ]
- );
- }
|