Ejemplo n.º 1
0
def transfer(params: TransferParams) -> TransactionInstruction:
    """Generate an instruction that transfers lamports from one account to another.

    >>> from solana.publickey import PublicKey
    >>> sender, reciever = PublicKey(1), PublicKey(2)
    >>> instruction = transfer(
    ...     TransferParams(from_pubkey=sender, to_pubkey=reciever, lamports=1000)
    ... )
    >>> type(instruction)
    <class 'solana.transaction.TransactionInstruction'>
    """
    data = SYSTEM_INSTRUCTIONS_LAYOUT.build(
        dict(instruction_type=InstructionType.Transfer,
             args=dict(lamports=params.lamports)))

    return TransactionInstruction(
        keys=[
            AccountMeta(pubkey=params.from_pubkey,
                        is_signer=True,
                        is_writable=True),
            AccountMeta(pubkey=params.to_pubkey,
                        is_signer=False,
                        is_writable=True),
        ],
        program_id=SYS_PROGRAM_ID,
        data=data,
    )
Ejemplo n.º 2
0
def create_account(params: CreateAccountParams) -> TransactionInstruction:
    """Generate an instruction that creates a new account.

    >>> from solana.publickey import PublicKey
    >>> from_account, new_account, program_id = PublicKey(1), PublicKey(2), PublicKey(3)
    >>> instruction = create_account(
    ...     CreateAccountParams(
    ...         from_pubkey=from_account, new_account_pubkey=new_account,
    ...         lamports=1, space=1, program_id=program_id)
    ... )
    >>> type(instruction)
    <class 'solana.transaction.TransactionInstruction'>
    """
    data = SYSTEM_INSTRUCTIONS_LAYOUT.build(
        dict(
            instruction_type=InstructionType.CreateAccount,
            args=dict(lamports=params.lamports,
                      space=params.space,
                      program_id=bytes(params.program_id)),
        ))

    return TransactionInstruction(
        keys=[
            AccountMeta(pubkey=params.from_pubkey,
                        is_signer=True,
                        is_writable=True),
            AccountMeta(pubkey=params.new_account_pubkey,
                        is_signer=False,
                        is_writable=True),
        ],
        program_id=SYS_PROGRAM_ID,
        data=data,
    )
Ejemplo n.º 3
0
def assign(
    params: Union[AssignParams,
                  AssignWithSeedParams]) -> TransactionInstruction:
    """Generate an instruction that assigns an account to a program.

    >>> from solana.publickey import PublicKey
    >>> account, program_id = PublicKey(1), PublicKey(2)
    >>> instruction = assign(
    ...     AssignParams(account_pubkey=account, program_id=program_id)
    ... )
    >>> type(instruction)
    <class 'solana.transaction.TransactionInstruction'>
    """
    if isinstance(params, AssignWithSeedParams):
        raise NotImplementedError("assign with key is not implemented")
    else:
        data = SYSTEM_INSTRUCTIONS_LAYOUT.build(
            dict(instruction_type=InstructionType.Assign,
                 args=dict(program_id=bytes(params.program_id))))

    return TransactionInstruction(
        keys=[
            AccountMeta(pubkey=params.account_pubkey,
                        is_signer=True,
                        is_writable=True),
        ],
        program_id=SYS_PROGRAM_ID,
        data=data,
    )
Ejemplo n.º 4
0
def nonce_advance(params: AdvanceNonceParams) -> TransactionInstruction:
    """Generate an instruction to advance the nonce in a Nonce account.

    Args:
        params: The advance nonce params

    Returns:
        The instruction to advance the nonce.
    """
    data = SYSTEM_INSTRUCTIONS_LAYOUT.build(
        dict(
            instruction_type=InstructionType.ADVANCE_NONCE_ACCOUNT,
            args={},
        ))

    return TransactionInstruction(
        keys=[
            AccountMeta(pubkey=params.nonce_pubkey,
                        is_signer=False,
                        is_writable=True),
            AccountMeta(pubkey=sysvar.SYSVAR_RECENT_BLOCKHASHES_PUBKEY,
                        is_signer=False,
                        is_writable=False),
            AccountMeta(pubkey=params.authorized_pubkey,
                        is_signer=True,
                        is_writable=True),
        ],
        program_id=SYS_PROGRAM_ID,
        data=data,
    )
Ejemplo n.º 5
0
def nonce_initialization(
        params: InitializeNonceParams) -> TransactionInstruction:
    """Generate an instruction to initialize a Nonce account.

    Args:
        params: The nonce initialization params.

    Returns:
        The instruction to initialize the nonce account.

    """
    data = SYSTEM_INSTRUCTIONS_LAYOUT.build(
        dict(
            instruction_type=InstructionType.INITIALIZE_NONCE_ACCOUNT,
            args=dict(authorized=bytes(params.authorized_pubkey), ),
        ))

    return TransactionInstruction(
        keys=[
            AccountMeta(pubkey=params.nonce_pubkey,
                        is_signer=True,
                        is_writable=True),
            AccountMeta(pubkey=sysvar.SYSVAR_RECENT_BLOCKHASHES_PUBKEY,
                        is_signer=False,
                        is_writable=False),
            AccountMeta(pubkey=sysvar.SYSVAR_RENT_PUBKEY,
                        is_signer=False,
                        is_writable=False),
        ],
        program_id=SYS_PROGRAM_ID,
        data=data,
    )
Ejemplo n.º 6
0
def create_account_with_seed_layout(base, seed, lamports, space):
    return SYSTEM_INSTRUCTIONS_LAYOUT.build(
        dict(instruction_type=InstructionType.CREATE_ACCOUNT_WITH_SEED,
             args=dict(base=bytes(base),
                       seed=dict(length=len(seed), chars=seed),
                       lamports=lamports,
                       space=space,
                       program_id=bytes(PublicKey(EVM_LOADER_ID)))))
Ejemplo n.º 7
0
def allocate(
    params: Union[AllocateParams, AllocateWithSeedParams]
) -> TransactionInstruction:
    """Generate an instruction that allocates space in an account without funding.

    Args:
        params: The allocate params.

    Example:

        >>> from solana.publickey import PublicKey
        >>> allocator = PublicKey(1)
        >>> instruction = allocate(
        ...     AllocateParams(account_pubkey=allocator, space=65537)
        ... )
        >>> type(instruction)
        <class 'solana.transaction.TransactionInstruction'>

    Returns:
        The allocate instruction.
    """
    if isinstance(params, AllocateWithSeedParams):
        data = SYSTEM_INSTRUCTIONS_LAYOUT.build(
            dict(
                instruction_type=InstructionType.ALLOCATE_WITH_SEED,
                args=dict(
                    base=bytes(params.base_pubkey),
                    seed=params.seed,
                    space=params.space,
                    program_id=bytes(params.program_id),
                ),
            ))
    else:
        data = SYSTEM_INSTRUCTIONS_LAYOUT.build(
            dict(instruction_type=InstructionType.ALLOCATE,
                 args=dict(space=params.space)))

    return TransactionInstruction(
        keys=[
            AccountMeta(pubkey=params.account_pubkey,
                        is_signer=True,
                        is_writable=True),
        ],
        program_id=SYS_PROGRAM_ID,
        data=data,
    )
Ejemplo n.º 8
0
def create_account_with_seed(
    params: CreateAccountWithSeedParams, ) -> TransactionInstruction:
    """Generate a instruction that creates a new account at an address generated with `from`, a seed, and programId.

    Args:
        params: account creation params.

    Returns:
        The instruction to create the account.
    """
    seed = {
        "length": len(params.seed),
        "chars": params.seed
    } if isinstance(params.seed, str) else params.seed
    data = SYSTEM_INSTRUCTIONS_LAYOUT.build(
        dict(
            instruction_type=InstructionType.CREATE_ACCOUNT_WITH_SEED,
            args=dict(
                base=bytes(params.base_pubkey),
                seed=seed,
                lamports=params.lamports,
                space=params.space,
                program_id=bytes(params.program_id),
            ),
        ))

    keys = [
        AccountMeta(pubkey=params.from_pubkey,
                    is_signer=True,
                    is_writable=True),
        AccountMeta(pubkey=params.new_account_pubkey,
                    is_signer=False,
                    is_writable=True),
    ]

    if params.base_pubkey != params.from_pubkey:
        keys.append(
            AccountMeta(pubkey=params.base_pubkey,
                        is_signer=True,
                        is_writable=False))

    return TransactionInstruction(keys=keys,
                                  program_id=SYS_PROGRAM_ID,
                                  data=data)