Ejemplo n.º 1
0
 def decode(cls, data: str, encoding: str):
     data_bytes = decode_byte_string(data, encoding)
     parsed = STAKE_STATE_LAYOUT.parse(data_bytes)
     return StakeState(
         state_type=parsed['state_type'],
         state=parsed['state'],
     )
Ejemplo n.º 2
0
    def _create_mint_info(self, info: RPCResponse) -> MintInfo:
        if not info:
            raise ValueError("Failed to find mint account")
        owner = info["result"]["value"]["owner"]
        if owner != str(self.program_id):
            raise AttributeError(f"Invalid mint owner: {owner}")

        bytes_data = decode_byte_string(info["result"]["value"]["data"][0])
        if len(bytes_data) != MINT_LAYOUT.sizeof():
            raise ValueError("Invalid mint size")

        decoded_data = MINT_LAYOUT.parse(bytes_data)
        decimals = decoded_data.decimals

        if decoded_data.mint_authority_option == 0:
            mint_authority = None
        else:
            mint_authority = PublicKey(decoded_data.mint_authority)

        supply = decoded_data.supply
        is_initialized = decoded_data.is_initialized != 0

        if decoded_data.freeze_authority_option == 0:
            freeze_authority = None
        else:
            freeze_authority = PublicKey(decoded_data.freeze_authority)

        return MintInfo(mint_authority, supply, decimals, is_initialized, freeze_authority)
Ejemplo n.º 3
0
def test_token(stubbed_sender, freeze_authority, test_http_client) -> Token:
    """Test create mint."""
    resp = test_http_client.request_airdrop(stubbed_sender.public_key,
                                            AIRDROP_AMOUNT)
    test_http_client.confirm_transaction(resp["result"])

    expected_decimals = 6
    token_client = Token.create_mint(
        test_http_client,
        stubbed_sender,
        stubbed_sender.public_key,
        expected_decimals,
        TOKEN_PROGRAM_ID,
        freeze_authority.public_key,
    )

    assert token_client.pubkey
    assert token_client.program_id == TOKEN_PROGRAM_ID
    assert token_client.payer.public_key == stubbed_sender.public_key

    resp = test_http_client.get_account_info(token_client.pubkey)
    assert_valid_response(resp)
    assert resp["result"]["value"]["owner"] == str(TOKEN_PROGRAM_ID)

    mint_data = layouts.MINT_LAYOUT.parse(
        decode_byte_string(resp["result"]["value"]["data"][0]))
    assert mint_data.is_initialized
    assert mint_data.decimals == expected_decimals
    assert mint_data.supply == 0
    assert PublicKey(mint_data.mint_authority) == stubbed_sender.public_key
    assert PublicKey(mint_data.freeze_authority) == freeze_authority.public_key
    return token_client
Ejemplo n.º 4
0
    def _create_account_info(self, info: RPCResponse) -> AccountInfo:
        if not info:
            raise ValueError("Invalid account owner")

        if info["result"]["value"]["owner"] != str(self.program_id):
            raise AttributeError("Invalid account owner")

        bytes_data = decode_byte_string(info["result"]["value"]["data"][0])
        if len(bytes_data) != ACCOUNT_LAYOUT.sizeof():
            raise ValueError("Invalid account size")

        decoded_data = ACCOUNT_LAYOUT.parse(bytes_data)

        mint = PublicKey(decoded_data.mint)
        owner = PublicKey(decoded_data.owner)
        amount = decoded_data.amount

        if decoded_data.delegate_option == 0:
            delegate = None
            delegated_amount = 0
        else:
            delegate = PublicKey(decoded_data.delegate)
            delegated_amount = decoded_data.delegated_amount

        is_initialized = decoded_data.state != 0
        is_frozen = decoded_data.state == 2

        if decoded_data.is_native_option == 1:
            rent_exempt_reserve = decoded_data.is_native
            is_native = True
        else:
            rent_exempt_reserve = None
            is_native = False

        if decoded_data.close_authority_option == 0:
            close_authority = None
        else:
            close_authority = PublicKey(decoded_data.owner)

        if mint != self.pubkey:
            raise AttributeError(f"Invalid account mint: {decoded_data.mint} != {self.pubkey}")

        return AccountInfo(
            mint,
            owner,
            amount,
            delegate,
            delegated_amount,
            is_initialized,
            is_frozen,
            is_native,
            rent_exempt_reserve,
            close_authority,
        )
Ejemplo n.º 5
0
 def decode(cls, data: str, encoding: str):
     data_bytes = decode_byte_string(data, encoding)
     parsed = DECODE_VALIDATOR_LIST_LAYOUT.parse(data_bytes)
     print(parsed)
     return ValidatorList(
         max_validators=parsed['max_validators'],
         validators=[
             ValidatorStakeInfo.decode_container(container)
             for container in parsed['validators']
         ],
     )
Ejemplo n.º 6
0
def test_create_multisig(stubbed_sender, stubbed_receiver, test_token,
                         test_http_client):  # pylint: disable=redefined-outer-name
    """Test creating a multisig account."""
    min_signers = 2
    multisig_pubkey = test_token.create_multisig(
        min_signers, [stubbed_sender.public_key, stubbed_receiver])
    resp = test_http_client.get_account_info(multisig_pubkey)
    assert_valid_response(resp)
    assert resp["result"]["value"]["owner"] == str(TOKEN_PROGRAM_ID)

    multisig_data = layouts.MULTISIG_LAYOUT.parse(
        decode_byte_string(resp["result"]["value"]["data"][0]))
    assert multisig_data.is_initialized
    assert multisig_data.m == min_signers
    assert PublicKey(multisig_data.signer1) == stubbed_sender.public_key
    assert PublicKey(multisig_data.signer2) == stubbed_receiver
Ejemplo n.º 7
0
 def decode(cls, data: str, encoding: str):
     data_bytes = decode_byte_string(data, encoding)
     parsed = DECODE_STAKE_POOL_LAYOUT.parse(data_bytes)
     return StakePool(
         manager=PublicKey(parsed['manager']),
         staker=PublicKey(parsed['staker']),
         stake_deposit_authority=PublicKey(
             parsed['stake_deposit_authority']),
         stake_withdraw_bump_seed=parsed['stake_withdraw_bump_seed'],
         validator_list=PublicKey(parsed['validator_list']),
         reserve_stake=PublicKey(parsed['reserve_stake']),
         pool_mint=PublicKey(parsed['pool_mint']),
         manager_fee_account=PublicKey(parsed['manager_fee_account']),
         token_program_id=PublicKey(parsed['token_program_id']),
         total_lamports=parsed['total_lamports'],
         pool_token_supply=parsed['pool_token_supply'],
         last_update_epoch=parsed['last_update_epoch'],
         lockup=Lockup.decode_container(parsed['lockup']),
         epoch_fee=Fee.decode_container(parsed['epoch_fee']),
         next_epoch_fee=Fee.decode_optional_container(
             parsed['next_epoch_fee']),
         preferred_deposit_validator=decode_optional_publickey(
             parsed['preferred_deposit_validator']),
         preferred_withdraw_validator=decode_optional_publickey(
             parsed['preferred_withdraw_validator']),
         stake_deposit_fee=Fee.decode_container(
             parsed['stake_deposit_fee']),
         stake_withdrawal_fee=Fee.decode_container(
             parsed['stake_withdrawal_fee']),
         next_stake_withdrawal_fee=Fee.decode_optional_container(
             parsed['next_stake_withdrawal_fee']),
         stake_referral_fee=parsed['stake_referral_fee'],
         sol_deposit_authority=decode_optional_publickey(
             parsed['sol_deposit_authority']),
         sol_deposit_fee=Fee.decode_container(parsed['sol_deposit_fee']),
         sol_referral_fee=parsed['sol_referral_fee'],
         sol_withdraw_authority=decode_optional_publickey(
             parsed['sol_withdraw_authority']),
         sol_withdrawal_fee=Fee.decode_container(
             parsed['sol_withdrawal_fee']),
         next_sol_withdrawal_fee=Fee.decode_optional_container(
             parsed['next_sol_withdrawal_fee']),
         last_epoch_pool_token_supply=parsed[
             'last_epoch_pool_token_supply'],
         last_epoch_total_lamports=parsed['last_epoch_total_lamports'],
     )
Ejemplo n.º 8
0
def test_new_account(stubbed_sender, test_http_client, test_token):  # pylint: disable=redefined-outer-name
    """Test creating a new token account."""
    token_account_pk = test_token.create_account(stubbed_sender.public_key)
    resp = test_http_client.get_account_info(token_account_pk)
    assert_valid_response(resp)
    assert resp["result"]["value"]["owner"] == str(TOKEN_PROGRAM_ID)

    account_data = layouts.ACCOUNT_LAYOUT.parse(
        decode_byte_string(resp["result"]["value"]["data"][0]))
    assert account_data.state
    assert not account_data.amount
    assert (not account_data.delegate_option
            and not account_data.delegated_amount
            and PublicKey(account_data.delegate) == PublicKey(0))
    assert not account_data.close_authority_option and PublicKey(
        account_data.close_authority) == PublicKey(0)
    assert not account_data.is_native_option and not account_data.is_native
    assert PublicKey(account_data.mint) == test_token.pubkey
    assert PublicKey(account_data.owner) == stubbed_sender.public_key