wrm3 / sol_xfr

Solana & Token Transfer For Python
2 stars 1 forks source link

Proper formatting of Txn + Transfer protocol #2

Closed bennimen closed 4 months ago

bennimen commented 4 months ago

Thanks again for the help! Will possibly post on the discord.

When trying to format the transaction to send sol from one wallet to another, I'm getting an invalid instruction argument and it can't get past the following code snippet:

txn.add(
    transfer(
        TransferParams(
            from_pubkey   = src_pubkey,
            to_pubkey     = dest_pubkey,
            lamports      = send_amt_lamps
        )
    )
)

I placed debug statements before and after the above snippet, so it's having an error adding the transfer instruction to the txn object.

I was just trying to do a test send of 0.01 sol from one wallet to another. Here's the error output with my wallets removed:

ValueError: ('invalid instruction:', Instruction( Instruction { program_id: 11111111111111111111111111111111, accounts: [ AccountMeta { pubkey: , is_signer: true, is_writable: true, }, AccountMeta { pubkey: , is_signer: false, is_writable: true, }, ], data: [ 2, 0, 0, 0, 128, 150, 152, 0, 0, 0, 0, 0, ], }, ))

bennimen commented 4 months ago

Full error here:


ValueError Traceback (most recent call last) Cell In[205], line 6 1 # 2 # SEND SOL 3 # 4 # This example sends 0.1337 SOL from wallet_1 (src) to wallet_2 (dest) 5 sol_amt_2_xfr = 0.01 ## sol ----> 6 txn_hash = send_sol( 7 src_addr = src_addr, 8 src_key = src_key, 9 dest_addr = dest_addr, 10 amt_sol = sol_amt_2_xfr, 11 cu_prc = cu_prc, 12 cu_lmt = cu_lmt, 13 rpc_url = rpc_url, 14 show_details_yn = show_details_yn 15 ) 16 print(txn_hash) 19 print('')

Cell In[199], line 201, in send_sol(src_addr, src_key, dest_addr, amt_sol, cu_prc, cu_lmt, rpc_url, show_details_yn) 198 print(dir(txn.add)) 199 # print(txn) --> 201 txn.add( 202 transfer( 203 TransferParams( 204 from_pubkey = src_pubkey, 205 to_pubkey = dest_pubkey, 206 lamports = send_amt_lamps 207 ) 208 ) 209 ) 211 print(txn) 213 # Priority Fees, remove when no network congestion 214 # these were the seeings from 4/5/2024 for trades to do through 215 # adjust as needed

File ~\AppData\Roaming\Python\Python311\site-packages\solana\transaction.py:110, in Transaction.add(self, *args) 108 self.instructions.append(arg) 109 else: --> 110 raise ValueError("invalid instruction:", arg) 112 return self

ValueError: ('invalid instruction:', Instruction( Instruction { program_id: 11111111111111111111111111111111, accounts: [ AccountMeta { pubkey: , is_signer: true, is_writable: true, }, AccountMeta { pubkey: , is_signer: false, is_writable: true, }, ], data: [ 2, 0, 0, 0, 128, 150, 152, 0, 0, 0, 0, 0, ], }, ))

wrm3 commented 4 months ago

Are you able to import these ok? Are you using solders 0.21.0 or an earlier version? from what you posted you are running Python 3.11 correct? The guy I originally was helping when I created this was running 3.8.5, so I assumed it would be ok for earlier releases... are you using solders 0.20 or other earlier version?

from solders.system_program import transfer from solders.system_program import TransferParams

bennimen commented 4 months ago

Hey, thanks for the response!

I have solders 0.21.0 and solana 0.20.0

I did some digging and it seems that the referenced transaction.py and add() function requires the inputted argument (formatted transfer instruction) needs to be a Transfer class object

and upon doing some debugging, the following snippet formats them as an "Instruction" object

txn.add(
    transfer(
        TransferParams(
            from_pubkey   = src_pubkey,
            to_pubkey     = dest_pubkey,
            lamports      = send_amt_lamps
        )
    )
)
bennimen commented 4 months ago

this is what my add() function looks like:

def add(self, *args: Union[Transaction, TransactionInstruction]) -> Transaction:
    """Add one or more instructions to this Transaction."""
    for arg in args:
        if isinstance(arg, Transaction):
            self.instructions.extend(arg.instructions)
        elif isinstance(arg, TransactionInstruction):
            self.instructions.append(arg)
        else:
            raise ValueError("invalid instruction:", arg)

    return self
bennimen commented 4 months ago

Tried to send you a message on Moon Dev's discord but it requires us to be friends

bennimen commented 4 months ago

Here's the result of some onscreen debugging

testVTFXP = TransferParams(
            from_pubkey   = src_pubkey,
            to_pubkey     = dest_pubkey,
            lamports      = send_amt_lamps
        )
testTFR = transfer(testVTFXP)

   txn.add(testTFR)    <--- error arises here upon execution

print('\ntestVTFXp')
print(type(testVTFXP))
print(testVTFXP)

print('\ntestTFR')
print(type(testTFR))
print(testTFR)

testVTFXp <class 'dict'> {'from_pubkey': Pubkey( W1, ), 'to_pubkey': Pubkey( W2, ), 'lamports': 10000000}

testTFR <class 'solders.instruction.Instruction'> Instruction { program_id: 11111111111111111111111111111111, accounts: [AccountMeta { pubkey: W1, is_signer: true, is_writable: true }, AccountMeta { pubkey: W2, is_signer: false, is_writable: true }], data: [2, 0, 0, 0, 128, 150, 152, 0, 0, 0, 0, 0] }

wrm3 commented 4 months ago

send friend request and I will add you when I get back (running out)... looked at current solders in GH, it looks correct? sol_xfr2

wrm3 commented 4 months ago

Did Moon ever post the video, where he had it running in 3.8.5?

bennimen commented 4 months ago

Did Moon ever post the video, where he had it running in 3.8.5?

i didn't see that video

bennimen commented 4 months ago

send friend request and I will add you when I get back (running out)

done, thank you man

bennimen commented 4 months ago

i'll keep digging

it makes me think i need to somehow convert the Instruction Object to a TransactionObject for it to accept it

bennimen commented 4 months ago

i created a new environment and reinstalled solana to an updated version and with your original code it allowed me to send solana. solana == 0.34.2

bennimen commented 4 months ago

or at least it produced a tx hash. didn't seem to go through, haha. but the code stopped producing an error for me at least

wrm3 commented 4 months ago

just keep doubling the priority fees until it they go through. Last I heard Solana was just dropping any transaction that didn't have enough fees to go thru. So if it does not show on solscan within 60 seconds, increase your priority fees. The functions will default to what was required in April 2024 to always go through. Update these values to whatever is needed now (or future readers then, hello from the past!) cu_prc=400000, cu_lmt=200000, compute_price and compute_limit