Digine-Labs / rosettanet

Ethereum <> Starknet RPC middleware.
MIT License
34 stars 26 forks source link

Implement getTransactionByBlockNumberAndIndex Handler with tests #74

Closed AryanGodara closed 8 months ago

AryanGodara commented 8 months ago

Resolves #68 Some of the fields are still missing; and I'm going through both the starknet and Eth APIs to see what those corresponding fields are.

{
    "name": "starknet_getBlockWithTxs",
    "summary": "Get block information with full transactions given the block id",
    "params": [
        {
            "name": "block_id",
            "description": "The hash of the requested block, or number (height) of the requested block, or a block tag",
            "required": true,
            "schema": {
                "title": "Block id",
                "$ref": "#/components/schemas/BLOCK_ID"
            }
        }
    ],
    "result": {
        "name": "result",
        "description": "The resulting block information with full transactions",
        "schema": {
            "title": "Starknet get block with txs result",
            "oneOf": [
                {
                    "title": "Block with transactions",
                    "$ref": "#/components/schemas/BLOCK_WITH_TXS"
                },
                {
                    "title": "Pending block with transactions",
                    "$ref": "#/components/schemas/PENDING_BLOCK_WITH_TXS"
                }
            ]
        }
    },
    "errors": [
        {
            "$ref": "#/components/errors/BLOCK_NOT_FOUND"
        }
    ]
},
AryanGodara commented 8 months ago

@ermvrs, I need your help with this The resulting block may be pending and I can't figure out a way to check this in the response.

By comparing the openAPI specs of the two possible responses (given below); I wrote the following code, as these fields are present only in pending blocks (according to the openAPI doc here)

  // Check if the block is pending by looking for pending-specific properties
  const isPendingBlock = (block: any): boolean => {
    // Check for required pending block properties
    return 'parent_hash' in block && 'timestamp' in block && 'sequencer_address' in block &&
          'l1_gas_price' in block && 'starknet_version' in block;
  };

  if (isPendingBlock(response.result)) {
    // If it's a pending block, return an error response
    return {
      code: 7979,
      message: 'Starknet RPC error',
      data: 'The block is still pending. Pending blocks cannot be processed.',
    }
  }

And here are the specs for the Block Responses:

"PENDING_BLOCK_WITH_TXS": {
    "title": "Pending block with transactions",
    "description": "The dynamic block being constructed by the sequencer. Note that this object will be deprecated upon decentralization.",
    "allOf": [
        {
            "title": "Block body with transactions",
            "$ref": "#/components/schemas/BLOCK_BODY_WITH_TXS"
        },
        {
            "title": "Pending block header",
            "$ref": "#/components/schemas/PENDING_BLOCK_HEADER"
        }
    ],
    "additionalProperties": false
},
"BLOCK_WITH_TXS": {
    "title": "Block with transactions",
    "description": "The block object",
    "allOf": [
        {
            "title": "block with txs",
            "type": "object",
            "properties": {
                "status": {
                    "title": "Status",
                    "$ref": "#/components/schemas/BLOCK_STATUS"
                }
            },
            "required": [
                "status"
            ]
        },
        {
            "title": "Block header",
            "$ref": "#/components/schemas/BLOCK_HEADER"
        },
        {
            "title": "Block body with transactions",
            "$ref": "#/components/schemas/BLOCK_BODY_WITH_TXS"
        }
    ]
},
ermvrs commented 8 months ago

Please fix ci error

AryanGodara commented 8 months ago

Please fix ci error

It was a suggestion to change a var to const. I pushed new changes, but the CI isn't running after the new commit

ermvrs commented 8 months ago

Please do not use type any. and console

AryanGodara commented 8 months ago

Please do not use type any. and console

Please do not use type any. and console Removed both of those. Console was debug statement, forgot to remove it by mistake