how to pass a long string to a starknet contract in python

121 Views Asked by At

For example, in the following transaction, https://starkscan.co/tx/0x012829d678c9b5bff701a6f2fed31744e14e736a209f61522fbc0bb65be98983, a long string

data:,{\"p\":\"stark-20\",\"op\":\"mint\",\"tick\":\"STRK\",\"amt\":\"1000\"}

is passed to the contract. I want to reproduce it with the following code:

async with aiohttp.TCPConnector(ssl=False) as tcpconnector:
    async with aiohttp.ClientSession(connector=tcpconnector,trust_env=True) as session:

        full_node_client = FullNodeClient(node_url=node_url,session=session)

        account = Account(
            client=full_node_client,
            address=address,
            key_pair=KeyPair.from_private_key(key=private_key),
            chain=StarknetChainId.MAINNET,
            )

                   
   
        call = Call(
            to_addr=0x07341189e3c96f636a4192cfba8c18deeee33a19c5d0425a26cf96ea42388c4e,
            selector=get_selector_from_name("inscribe"),
            calldata=["data:,{\"p\":\"stark-20\",\"op\":\"min","t\",\"tick\":\"STRK\",\"amt\":\"1000\"}"]
        )

        calls = [call]
        
        tx = await account.sign_invoke_transaction(
            calls=calls, max_fee=0
            )

        estimated_fee = await account.client.estimate_fee(
            tx=tx,
            )

the following error is returned:

{'jsonrpc': '2.0', 'id': 0, 'error': {'code': 40, 'data': {'revert_error': "Execution error at transaction index 0: Transaction validation has failed. Execution failed. Failure reason: 0x496e70757420746f6f206c6f6e6720666f7220617267756d656e7473 ('Input too long for arguments')."}, 'message': 'Contract error'}} 

So what is the right method to pass a long string to starknet contract?

1

There are 1 best solutions below

0
shramee On

Looking at the transaction call, the data needs to be an array of u64 (one character per u64).

In Starknet, when passing an array,

  1. You have to pass in the size of the array first.
  2. Pass the elements of the array

For example, if I want to send this array [8, 13, 21, 34] my calldata would look like 4, 8, 13, 21, 34,

    4, 8, 13, 21, 34
#   ↑  -------------
#   ↑       ↑
#   ↑     items
#   ↑
# length

Which translates to