示例#1
0
def initialize_account(params: InitializeAccountParams) -> TransactionInstruction:
    """Creates a transaction instruction to initialize a new account to hold tokens.

    This instruction requires no signers and MUST be included within the same Transaction as
    the system program's `CreateInstruction` that creates the account being initialized.
    Otherwise another party can acquire ownership of the uninitialized account.

    >>> account, mint, owner, token = PublicKey(1), PublicKey(2), PublicKey(3), PublicKey(4)
    >>> params = InitializeAccountParams(
    ...     account=account,
    ...     mint=mint,
    ...     owner=owner,
    ...     program_id=token,
    ... )
    >>> type(initialize_account(params))
    <class 'solana.transaction.TransactionInstruction'>
    """
    data = INSTRUCTIONS_LAYOUT.build(dict(instruction_type=InstructionType.InitializeAccount, args=None))
    return TransactionInstruction(
        keys=[
            AccountMeta(pubkey=params.account, is_signer=False, is_writable=True),
            AccountMeta(pubkey=params.mint, is_signer=False, is_writable=False),
            AccountMeta(pubkey=params.owner, is_signer=False, is_writable=False),
            AccountMeta(pubkey=SYSVAR_RENT_PUBKEY, is_signer=False, is_writable=False),
        ],
        program_id=params.program_id,
        data=data,
    )
示例#2
0
def approve(params: ApproveParams) -> TransactionInstruction:
    """Creates a transaction instruction to approves a delegate.

    >>> delegate, owner, source, token = PublicKey(1), PublicKey(2), PublicKey(3), PublicKey(4)
    >>> params = ApproveParams(
    ...     amount=123,
    ...     delegate=delegate,
    ...     owner=owner,
    ...     program_id=token,
    ...     source=source
    ... )
    >>> type(approve(params))
    <class 'solana.transaction.TransactionInstruction'>
    """
    data = INSTRUCTIONS_LAYOUT.build(
        dict(instruction_type=InstructionType.APPROVE,
             args=dict(amount=params.amount)))
    keys = [
        AccountMeta(pubkey=params.source, is_signer=False, is_writable=True),
        AccountMeta(pubkey=params.delegate, is_signer=False,
                    is_writable=False),
    ]
    __add_signers(keys, params.owner, params.signers)

    return TransactionInstruction(keys=keys,
                                  program_id=params.program_id,
                                  data=data)
示例#3
0
def revoke(params: RevokeParams) -> TransactionInstruction:
    """Creates a transaction instruction that revokes delegate authority for a given account.

    Example:

        >>> account, owner, token = PublicKey(1), PublicKey(2), PublicKey(3)
        >>> params = RevokeParams(
        ...     account=account, owner=owner, program_id=token
        ... )
        >>> type(revoke(params))
        <class 'solana.transaction.TransactionInstruction'>

    Returns:
        The revoke instruction.
    """
    data = INSTRUCTIONS_LAYOUT.build(
        dict(instruction_type=InstructionType.REVOKE, args=None))
    keys = [
        AccountMeta(pubkey=params.account, is_signer=False, is_writable=True)
    ]
    __add_signers(keys, params.owner, params.signers)

    return TransactionInstruction(keys=keys,
                                  program_id=params.program_id,
                                  data=data)
示例#4
0
def close_account(params: CloseAccountParams) -> TransactionInstruction:
    """Creates a transaction instruction to close an account by transferring all its SOL to the destination account.

    Non-native accounts may only be closed if its token amount is zero.

    Example:

        >>> account, dest, owner, token = PublicKey(1), PublicKey(2), PublicKey(3), PublicKey(4)
        >>> params = CloseAccountParams(
        ...     account=account, dest=dest, owner=owner, program_id=token)
        >>> type(close_account(params))
        <class 'solana.transaction.TransactionInstruction'>

    Returns:
        The close-account instruction.
    """
    data = INSTRUCTIONS_LAYOUT.build(
        dict(instruction_type=InstructionType.CLOSE_ACCOUNT, args=None))
    keys = [
        AccountMeta(pubkey=params.account, is_signer=False, is_writable=True),
        AccountMeta(pubkey=params.dest, is_signer=False, is_writable=True),
    ]
    __add_signers(keys, params.owner, params.signers)

    return TransactionInstruction(keys=keys,
                                  program_id=params.program_id,
                                  data=data)
示例#5
0
def transfer_checked(params: TransferCheckedParams) -> TransactionInstruction:
    """This instruction differs from `transfer` in that the token mint and decimals value is asserted by the caller.

    Example:

        >>> dest, mint, owner, source, token = PublicKey(1), PublicKey(2), PublicKey(3), PublicKey(4), PublicKey(5)
        >>> params = TransferCheckedParams(
        ...     amount=1000,
        ...     decimals=6,
        ...     dest=dest,
        ...     mint=mint,
        ...     owner=owner,
        ...     program_id=token,
        ...     source=source,
        ... )
        >>> type(transfer_checked(params))
        <class 'solana.transaction.TransactionInstruction'>

    Returns:
        The transfer-checked instruction.
    """
    data = INSTRUCTIONS_LAYOUT.build(
        dict(instruction_type=InstructionType.TRANSFER2,
             args=dict(amount=params.amount, decimals=params.decimals)))
    keys = [
        AccountMeta(pubkey=params.source, is_signer=False, is_writable=True),
        AccountMeta(pubkey=params.mint, is_signer=False, is_writable=False),
        AccountMeta(pubkey=params.dest, is_signer=False, is_writable=True),
    ]
    __add_signers(keys, params.owner, params.signers)

    return TransactionInstruction(keys=keys,
                                  program_id=params.program_id,
                                  data=data)
示例#6
0
def set_authority(params: SetAuthorityParams) -> TransactionInstruction:
    """Creates a transaction instruction to sets a new authority of a mint or account.

    >>> account, current_authority, new_authority, token = (
    ...     PublicKey(1), PublicKey(2), PublicKey(3), PublicKey(4)
    ... )
    >>> params = SetAuthorityParams(
    ...     account=account,
    ...     authority=AuthorityType.ACCOUNT_OWNER,
    ...     current_authority=current_authority,
    ...     new_authority=new_authority,
    ...     program_id=token,
    ... )
    >>> type(set_authority(params))
    <class 'solana.transaction.TransactionInstruction'>
    """
    new_authority, opt = (params.new_authority,
                          1) if params.new_authority else (PublicKey(0), 0)
    data = INSTRUCTIONS_LAYOUT.build(
        dict(
            instruction_type=InstructionType.SET_AUTHORITY,
            args=dict(authority_type=params.authority,
                      new_authority_option=opt,
                      new_authority=bytes(new_authority)),
        ))
    keys = [
        AccountMeta(pubkey=params.account, is_signer=False, is_writable=True)
    ]
    __add_signers(keys, params.current_authority, params.signers)

    return TransactionInstruction(keys=keys,
                                  program_id=params.program_id,
                                  data=data)
示例#7
0
def approve2(params: Approve2Params) -> TransactionInstruction:
    """This instruction differs from `approve` in that the token mint and decimals value is asserted by the caller.

    >>> delegate, mint, owner, source, token = PublicKey(1), PublicKey(2), PublicKey(3), PublicKey(4), PublicKey(5)
    >>> params = Approve2Params(
    ...     amount=1000,
    ...     decimals=6,
    ...     delegate=delegate,
    ...     mint=mint,
    ...     owner=owner,
    ...     program_id=token,
    ...     source=source,
    ... )
    >>> type(approve2(params))
    <class 'solana.transaction.TransactionInstruction'>
    """
    data = INSTRUCTIONS_LAYOUT.build(
        dict(instruction_type=InstructionType.APPROVE2,
             args=dict(amount=params.amount, decimals=params.decimals)))
    keys = [
        AccountMeta(pubkey=params.source, is_signer=False, is_writable=True),
        AccountMeta(pubkey=params.mint, is_signer=False, is_writable=False),
        AccountMeta(pubkey=params.delegate, is_signer=False,
                    is_writable=False),
    ]
    __add_signers(keys, params.owner, params.signers)

    return TransactionInstruction(keys=keys,
                                  program_id=params.program_id,
                                  data=data)
示例#8
0
def initialize_multisig(params: InitializeMultisigParams) -> TransactionInstruction:
    """Creates a transaction instruction to initialize a multisignature account with N provided signers.

    This instruction requires no signers and MUST be included within the same Transaction as
    the system program's `CreateInstruction` that creates the account being initialized.
    Otherwise another party can acquire ownership of the uninitialized account.

    >>> m = 2   # Two signers
    >>> signers = [PublicKey(i) for i in range(m)]
    >>> multisig_account, token = PublicKey(1), PublicKey(2)
    >>> params = InitializeMultisigParams(
    ...     m=m,
    ...     multisig=multisig_account,
    ...     signers=signers,
    ...     program_id=token,
    ... )
    >>> type(initialize_multisig(params))
    <class 'solana.transaction.TransactionInstruction'>
    """
    data = INSTRUCTIONS_LAYOUT.build(dict(instruction_type=InstructionType.InitializeMultisig, args=dict(m=params.m)))
    keys = [
        AccountMeta(pubkey=params.multisig, is_signer=False, is_writable=True),
        AccountMeta(pubkey=SYSVAR_RENT_PUBKEY, is_signer=False, is_writable=False),
    ]
    for signer in params.signers:
        keys.append(AccountMeta(pubkey=signer, is_signer=False, is_writable=False))

    return TransactionInstruction(keys=keys, program_id=params.program_id, data=data)
示例#9
0
def transfer(params: TransferParams) -> TransactionInstruction:
    """Creates a transaction instruction to transfers tokens from one account to another.

    Either directly or via a delegate.

    >>> dest, owner, source, token = PublicKey(1), PublicKey(2), PublicKey(3), PublicKey(4)
    >>> params = TransferParams(
    ...     amount=1000,
    ...     dest=dest,
    ...     owner=owner,
    ...     program_id=token,
    ...     source=source,
    ... )
    >>> type(transfer(params))
    <class 'solana.transaction.TransactionInstruction'>
    """
    data = INSTRUCTIONS_LAYOUT.build(
        dict(instruction_type=InstructionType.TRANSFER,
             args=dict(amount=params.amount)))
    keys = [
        AccountMeta(pubkey=params.source, is_signer=False, is_writable=True),
        AccountMeta(pubkey=params.dest, is_signer=False, is_writable=True),
    ]
    __add_signers(keys, params.owner, params.signers)

    return TransactionInstruction(keys=keys,
                                  program_id=params.program_id,
                                  data=data)
示例#10
0
def mint_to(params: MintToParams) -> TransactionInstruction:
    """Creates a transaction instruction to mint new tokens to an account.

    The native mint does not support minting.

    Example:

        >>> dest, mint, mint_authority, token = PublicKey(1), PublicKey(2), PublicKey(3), PublicKey(4)
        >>> params = MintToParams(
        ...     amount=123,
        ...     dest=dest,
        ...     mint=mint,
        ...     mint_authority=mint_authority,
        ...     program_id=token,
        ... )
        >>> type(mint_to(params))
        <class 'solana.transaction.TransactionInstruction'>

    Returns:
        The mint-to instruction.
    """
    data = INSTRUCTIONS_LAYOUT.build(
        dict(instruction_type=InstructionType.MINT_TO,
             args=dict(amount=params.amount)))
    return __mint_to_instruction(params, data)
示例#11
0
def __parse_and_validate_instruction(
    instruction: TransactionInstruction,
    expected_keys: int,
    expected_type: InstructionType,
) -> Any:  # Returns a Construct container.
    validate_instruction_keys(instruction, expected_keys)
    data = INSTRUCTIONS_LAYOUT.parse(instruction.data)
    validate_instruction_type(data, expected_type)
    return data
示例#12
0
def __freeze_or_thaw_instruction(
    params: Union[FreezeAccountParams, ThawAccountParams], instruction_type: InstructionType
) -> TransactionInstruction:
    data = INSTRUCTIONS_LAYOUT.build(dict(instruction_type=instruction_type, args=None))
    keys = [
        AccountMeta(pubkey=params.account, is_signer=False, is_writable=True),
        AccountMeta(pubkey=params.mint, is_signer=False, is_writable=False),
    ]
    __add_signers(keys, params.owner, params.signers)

    return TransactionInstruction(keys=keys, program_id=params.program_id, data=data)
示例#13
0
def burn(params: BurnParams) -> TransactionInstruction:
    """Creates a transaction instruction to burns tokens by removing them from an account.

    >>> account, mint, owner, token = PublicKey(1), PublicKey(2), PublicKey(3), PublicKey(4)
    >>> params = BurnParams(
    ...     amount=123, account=account, mint=mint, owner=owner, program_id=token,
    ... )
    >>> type(burn(params))
    <class 'solana.transaction.TransactionInstruction'>
    """
    data = INSTRUCTIONS_LAYOUT.build(dict(instruction_type=InstructionType.Burn, args=dict(amount=params.amount)))
    return __burn_instruction(params, data)
示例#14
0
def burn2(params: Burn2Params) -> TransactionInstruction:
    """This instruction differs from `burn` in that the decimals value is asserted by the caller.

    >>> account, mint, owner, token = PublicKey(1), PublicKey(2), PublicKey(3), PublicKey(4)
    >>> params = Burn2Params(
    ...     amount=123, account=account, decimals=6, mint=mint, owner=owner, program_id=token,
    ... )
    >>> type(burn2(params))
    <class 'solana.transaction.TransactionInstruction'>
    """
    data = INSTRUCTIONS_LAYOUT.build(
        dict(instruction_type=InstructionType.BURN2,
             args=dict(amount=params.amount, decimals=params.decimals)))
    return __burn_instruction(params, data)
示例#15
0
def revoke(params: RevokeParams) -> TransactionInstruction:
    """Creates a transaction instruction to revokes the delegate's authority.

    >>> delegate, owner, token = PublicKey(1), PublicKey(2), PublicKey(3)
    >>> params = RevokeParams(
    ...     delegate=delegate, owner=owner, program_id=token
    ... )
    >>> type(revoke(params))
    <class 'solana.transaction.TransactionInstruction'>
    """
    data = INSTRUCTIONS_LAYOUT.build(dict(instruction_type=InstructionType.Revoke, args=None))
    keys = [AccountMeta(pubkey=params.delegate, is_signer=False, is_writable=False)]
    __add_signers(keys, params.owner, params.signers)

    return TransactionInstruction(keys=keys, program_id=params.program_id, data=data)
示例#16
0
def initialize_mint(params: InitializeMintParams) -> TransactionInstruction:
    """Creates a transaction instruction to initialize a new mint newly.

    This instruction requires no signers and MUST be included within the same Transaction as
    the system program's `CreateInstruction` that creates the account being initialized.
    Otherwise another party can acquire ownership of the uninitialized account.

    Example:

        >>> from spl.token.constants import TOKEN_PROGRAM_ID
        >>> mint_account, mint_authority, freeze_authority, owner = PublicKey(1), PublicKey(2), PublicKey(3), PublicKey(4)
        >>> params = InitializeMintParams(
        ...     decimals=6,
        ...     freeze_authority=freeze_authority,
        ...     mint=mint_account,
        ...     mint_authority=mint_authority,
        ...     program_id=TOKEN_PROGRAM_ID,
        ... )
        >>> type(initialize_mint(params))
        <class 'solana.transaction.TransactionInstruction'>

    Returns:
        The instruction to initialize the mint.
    """  # noqa: E501 # pylint: disable=line-too-long
    freeze_authority, opt = (params.freeze_authority,
                             1) if params.freeze_authority else (PublicKey(0),
                                                                 0)
    data = INSTRUCTIONS_LAYOUT.build(
        dict(
            instruction_type=InstructionType.INITIALIZE_MINT,
            args=dict(
                decimals=params.decimals,
                mint_authority=bytes(params.mint_authority),
                freeze_authority_option=opt,
                freeze_authority=bytes(freeze_authority),
            ),
        ))
    return TransactionInstruction(
        keys=[
            AccountMeta(pubkey=params.mint, is_signer=False, is_writable=True),
            AccountMeta(pubkey=SYSVAR_RENT_PUBKEY,
                        is_signer=False,
                        is_writable=False),
        ],
        program_id=params.program_id,
        data=data,
    )
示例#17
0
def mint_to2(params: MintTo2Params) -> TransactionInstruction:
    """This instruction differs from `mint_to` in that the decimals value is asserted by the caller.

    >>> dest, mint, mint_authority, token = PublicKey(1), PublicKey(2), PublicKey(3), PublicKey(4)
    >>> params = MintTo2Params(
    ...     amount=123,
    ...     decimals=6,
    ...     dest=dest,
    ...     mint=mint,
    ...     mint_authority=mint_authority,
    ...     program_id=token,
    ... )
    >>> type(mint_to2(params))
    <class 'solana.transaction.TransactionInstruction'>
    """
    data = INSTRUCTIONS_LAYOUT.build(
        dict(instruction_type=InstructionType.MINT_TO2,
             args=dict(amount=params.amount, decimals=params.decimals)))
    return __mint_to_instruction(params, data)
示例#18
0
solana_client = Client("https://devnet.solana.com")
address = ""
transactions = solana_client.get_confirmed_signature_for_address2(address)["result"]
for tx in transactions:
	tx_result = solana_client.get_confirmed_transaction(tx_sig=tx["signature"], encoding="base64")

	raw_tx_str = tx_result['result']['transaction'][0]

	raw_tx_base64_bytes = raw_tx_str.encode('ascii')
	raw_tx_bytes = base64.b64decode(raw_tx_base64_bytes)

	des_tx: Transaction = Transaction.deserialize(raw_tx_bytes)
	tx_instruction: TransactionInstruction = des_tx.instructions.pop()
	# program id will be a bunch of ones if it's a transaction involving SOL
	if tx_instruction.program_id.__str__() == "11111111111111111111111111111111":
		if SYSTEM_INSTRUCTIONS_LAYOUT.parse(tx_instruction.data).instruction_type == SOLInstructionType.Transfer:
			transfer_params: SOLTransferParams = sol_decode_transfer(tx_instruction)
			print(tx["slot"]) # blockheight
			print(f'from:{transfer_params.from_pubkey}') # from
			print(f'to:{transfer_params.to_pubkey}') # to
			print(f'amount:{transfer_params.lamports * .000000001}') # amount
	# program id will be Token... if it's a transaction involving tokens
	if tx_instruction.program_id.__str__() == "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA":
		if INSTRUCTIONS_LAYOUT.parse(tx_instruction.data).instruction_type == SPLInstructionType.Transfer:
			transfer_params: SPLTransferParams = spl_decode_transfer(tx_instruction)
			print(tx["slot"]) # blockheight
			print(f'from:{transfer_params.source}') # from
			print(f'to:{transfer_params.dest}') # to
			print(f'token:{None}') # TODO token
			print(f'amount:{transfer_params.amount *.000001}') # amount