Esempio n. 1
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)
Esempio n. 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)
Esempio n. 3
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)
Esempio n. 4
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)
Esempio n. 5
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)
Esempio n. 6
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)
Esempio n. 7
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)
Esempio n. 8
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)
Esempio n. 9
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)
Esempio n. 10
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,
    )
Esempio n. 11
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)
Esempio n. 12
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)
Esempio n. 13
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)
Esempio n. 14
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)
Esempio n. 15
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,
    )
Esempio n. 16
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)