michaelhly / solana-py

Solana Python SDK
https://michaelhly.github.io/solana-py
MIT License
982 stars 254 forks source link

compile_message Error #153

Closed xyq1393342748 closed 2 years ago

xyq1393342748 commented 2 years ago

image

When I use the transaction.serialize_message(), there are two different results

michaelhly commented 2 years ago

Can you provide more info on how you're creating your transaction?

xyq1393342748 commented 2 years ago

ok , This is my code


import spl.token.instructions as spl_token
import solana.system_program as sp
import re
import base58
from hashlib import sha256
from solana.publickey import PublicKey
from solana.transaction import Transaction, AccountMeta, TransactionInstruction
from solana.rpc.api import Client
from spl.token.constants import ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID
from solana.blockhash import Blockhash
from spl.token._layouts import ACCOUNT_LAYOUT, MINT_LAYOUT
from solana.system_program import SYS_PROGRAM_ID
from solana.sysvar import SYSVAR_RENT_PUBKEY, SYSVAR_CLOCK_PUBKEY

http_client = Client("https://solana-mainnet.phantom.tech")

def to_snake_case(name):  # global state
    name = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
    name = re.sub('__([A-Z])', r'_\1', name)
    name = re.sub('([a-z0-9])([A-Z])', r'\1_\2', name)
    return name.lower()

def build_method(ix_name):
    ix_name = to_snake_case(ix_name)
    namespace = 'global'
    formatted_str = f"{namespace}:{ix_name}"
    return (sha256(formatted_str.encode()).digest()[:8])

def MintNft(_key1: PublicKey, _key2: PublicKey, _key3: PublicKey):
    config = PublicKey("DNnzE5TunUhgZz9JogFtQqrDFn1DqscDVLga8KmrnZKt")
    candyMachineId = PublicKey("FJPBA6WtXhB2M2p83eE1brevRJjGBsAQAW5Sf6soBG9e")
    treasury = PublicKey("564fBJG35QTuFxsJat6ynnJPBXjb7EUbXbgCSw6HvWUk")
    TOKEN_METADATA_PROGRAM_ID = PublicKey('metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s')
    CANDY_MACHINE_PROGRAM = PublicKey('CMX5tvuWs2rBUL3vqVWiARfcDoCKjdeSinCsZdxJmYoF')

    Metadata = PublicKey.find_program_address([b'metadata', bytes(TOKEN_METADATA_PROGRAM_ID), bytes(_key2)],
                                              TOKEN_METADATA_PROGRAM_ID)[0]
    masterEdition = PublicKey.find_program_address(
        [b'metadata', bytes(TOKEN_METADATA_PROGRAM_ID), bytes(_key2), b'edition'],
        TOKEN_METADATA_PROGRAM_ID)[0]

    keys1 = []
    keys1.append(AccountMeta(pubkey=config, is_signer=False,
                             is_writable=False))
    keys1.append(AccountMeta(pubkey=candyMachineId, is_signer=False,
                             is_writable=True))
    keys1.append(AccountMeta(pubkey=_key1, is_signer=True,
                             is_writable=True))
    keys1.append(AccountMeta(pubkey=treasury, is_signer=False,
                             is_writable=True))
    keys1.append(AccountMeta(pubkey=Metadata, is_signer=False,
                             is_writable=True))
    keys1.append(AccountMeta(pubkey=_key2, is_signer=False,
                             is_writable=True))
    keys1.append(AccountMeta(pubkey=_key1, is_signer=True,
                             is_writable=False))
    keys1.append(AccountMeta(pubkey=_key1, is_signer=True,
                             is_writable=False))
    keys1.append(AccountMeta(pubkey=masterEdition, is_signer=False,
                             is_writable=True))
    keys1.append(AccountMeta(pubkey=TOKEN_METADATA_PROGRAM_ID, is_signer=False,
                             is_writable=False))
    keys1.append(AccountMeta(pubkey=TOKEN_PROGRAM_ID, is_signer=False,
                             is_writable=False))
    keys1.append(AccountMeta(pubkey=SYS_PROGRAM_ID, is_signer=False,
                             is_writable=False))
    keys1.append(AccountMeta(pubkey=SYSVAR_RENT_PUBKEY, is_signer=False,
                             is_writable=False))
    keys1.append(AccountMeta(pubkey=SYSVAR_CLOCK_PUBKEY, is_signer=False,
                             is_writable=False))
    keys1.append(AccountMeta(pubkey=SYS_PROGRAM_ID, is_signer=False,
                             is_writable=True))
    keys1.append(AccountMeta(pubkey=SYS_PROGRAM_ID, is_signer=False,
                             is_writable=False))
    keys1.append(AccountMeta(pubkey=_key3, is_signer=True,
                             is_writable=False))
    return TransactionInstruction(
        keys=keys1,
        program_id=CANDY_MACHINE_PROGRAM,
        data=build_method('mintNft')
    )

def Demo():
    txn = Transaction()
    _keypair_publicKey1 = PublicKey("GPa8HDZQK7Rk4S5UQ7g9Rs4w1uPHEGQXYe1gYMQvmiUW")
    _keypair_publicKey2 = PublicKey('FTnJ4ugzfMjgWDyEoDvb3hFofvNM3cuEEMpRHwCk1a9Y')
    _keypair_sign_publicKey = PublicKey("5wz3MP6ZRStprgvosAJHLRPNDA4zaErJT1Mka9F5GTrv")
    txn.recent_blockhash = Blockhash("BNFXNGPGkE9bpGiZN9gFWvLMyEDjksoujpkjMhcEmVaP")
    txn.fee_payer = _keypair_publicKey1
    txn.add(
        sp.create_account(
            sp.CreateAccountParams(
                from_pubkey=_keypair_publicKey1,
                new_account_pubkey=_keypair_publicKey2,
                lamports=http_client.get_minimum_balance_for_rent_exemption(MINT_LAYOUT.sizeof())['result'],
                space=MINT_LAYOUT.sizeof(),
                program_id=TOKEN_PROGRAM_ID
            )
        )
    )
    txn.add(
        spl_token.initialize_mint(
            spl_token.InitializeMintParams(decimals=0,
                                           freeze_authority=_keypair_publicKey1,
                                           mint=_keypair_publicKey2,
                                           mint_authority=_keypair_publicKey1,
                                           program_id=TOKEN_PROGRAM_ID,
                                           )
        )
    )
    txn.add(
        spl_token.create_associated_token_account(
            payer=_keypair_publicKey1,
            owner=_keypair_publicKey1,
            mint=_keypair_publicKey2
        )
    )
    txn.add(
        spl_token.mint_to(
            spl_token.MintToParams(
                amount=1,
                dest=spl_token.get_associated_token_address(_keypair_publicKey1, _keypair_publicKey2),
                mint=_keypair_publicKey2,
                mint_authority=_keypair_publicKey1,
                program_id=TOKEN_PROGRAM_ID
            )
        )
    )
    txn.add(
        MintNft(_keypair_publicKey1, _keypair_publicKey2, _keypair_sign_publicKey)
    )
    message = base58.b58encode(txn.serialize_message()).decode()
    print(message)

Demo()
midomidox commented 2 years ago

same issue.. any updates?

michaelhly commented 2 years ago

I think this issue was addressed in https://github.com/michaelhly/solana-py/commit/07ec29e51d47b3989f3d516032b76098b9f950e3. Would you mind installing the latest version and try again?

abacus-x commented 2 years ago

@xyq1393342748, @midomidox this should be fixed now in #228 - please let us know if it is still an issue