Exemple #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,
    )
Exemple #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,
    )
Exemple #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,
    )
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,
    )
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,
    )
Exemple #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)))))
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,
    )
Exemple #8
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 = SYSTEM_INSTRUCTIONS_LAYOUT.parse(instruction.data)
    validate_instruction_type(data, expected_type)
    return data
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)
Exemple #10
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