Skip to content

Instantly share code, notes, and snippets.

@dmarzzz
Last active February 2, 2024 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmarzzz/21827db7b9303088577dc1cca0dd23b4 to your computer and use it in GitHub Desktop.
Save dmarzzz/21827db7b9303088577dc1cca0dd23b4 to your computer and use it in GitHub Desktop.
Generate Access List Example
import requests
import json
def generate_access_list_from_statediff():
response = '''
{"jsonrpc": "2.0", "result": {"output": "0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000001f161421c8e0000000000000000000000000000000000000000000000000000057f864ee247d477", "stateDiff": {"0x9162accd2bf58dcf5f5d46e5818117e3f5cd8333": {"balance": {"*": {"from": "0x1e8b422a215aff2a", "to": "0x1c8a6a7b5aa9727a"}}, "code": "=", "nonce": {"*": {"from": "0x259", "to": "0x25a"}}, "storage": {}}, "0x95222290dd7278aa3ddd389cc1e1d165cc4bafe5": {"balance": {"*": {"from": "0xcbf2d42250f0eb19", "to": "0xcbf2e3a93cf93319"}}, "code": "=", "nonce": "=", "storage": {}}, "0xa65303cbe1186f58dda9cf96b29e1e89ae90b165": {"balance": "=", "code": "=", "nonce": "=", "storage": {"0x0000000000000000000000000000000000000000000000000000000000000008": {"*": {"from": "0x65bd127b000000000000b48e759d60c985d500000000000205fd0da75ab8d231", "to": "0x65bd127b000000000000b67fd6df7d5785d5000000000002007d87587870fdba"}}}}, "0xb39bc86ac75118011f646276fca48d56e54c4854": {"balance": "=", "code": "=", "nonce": "=", "storage": {"0x000000000000000000000000000000000000000000000000000000000000000d": {"*": {"from": "0x0000000000000000000000000000000000000000000000000000000000000eb1", "to": "0x0000000000000000000000000000000000000000000000000000000000000eb2"}}, "0x7e3bb96fe9c3e8bf8baff39136a5e5778a1f21be90df3270983ce535ed884516": {"*": {"from": "0x00000000000000000000000000000000000000000000000205fd0da75ab8d231", "to": "0x000000000000000000000000000000000000000000000002007d87587870fdba"}}, "0x9db3014a7ecc5c8baca43505a03b4e7e24c2ec389a973d5605a299f04defc730": {"*": {"from": "0x0000000000000000000000000000000000000000000000000000000000000000", "to": "0x000000000000000000000000000000000000000000000000057f864ee247d477"}}}}, "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2": {"balance": {"*": {"from": "0x2b89d1917f36cfaf9d57d", "to": "0x2b89d1b0954af1787d57d"}}, "code": "=", "nonce": "=", "storage": {"0x377eec8667584e30e85471441b46e69393e5f35d2b63a0b4c26a5b1f6d059aa5": {"*": {"from": "0x000000000000000000000000000000000000000000000000b48e759d60c985d5", "to": "0x000000000000000000000000000000000000000000000000b67fd6df7d5785d5"}}}}}, "trace": [], "vmTrace": "None"}, "id": 1}
'''
state_diff = json.loads(response)
access_list = []
# Iterate through each account in the stateDiff
for account, diff in state_diff['result']['stateDiff'].items():
# Initialize the list of storageKeys for this account
storage_keys = []
# Check if 'storage' changes are present for this account
if 'storage' in diff:
# Iterate through each storage slot change
for slot, change in diff['storage'].items():
# Add the slot to the list
storage_keys.append(slot)
# Append the account and its storage keys to the access list
if storage_keys:
access_list.append({"address": account, "storageKeys": storage_keys})
return access_list
try:
access_list = generate_access_list_from_statediff()
print(json.dumps(access_list, indent=2))
except Exception as e:
print(f"Error: {e}")
@dmarzzz
Copy link
Author

dmarzzz commented Feb 2, 2024

[
  {
    "address": "0xa65303cbe1186f58dda9cf96b29e1e89ae90b165",
    "storageKeys": [
      "0x0000000000000000000000000000000000000000000000000000000000000008"
    ]
  },
  {
    "address": "0xb39bc86ac75118011f646276fca48d56e54c4854",
    "storageKeys": [
      "0x000000000000000000000000000000000000000000000000000000000000000d",
      "0x7e3bb96fe9c3e8bf8baff39136a5e5778a1f21be90df3270983ce535ed884516",
      "0x9db3014a7ecc5c8baca43505a03b4e7e24c2ec389a973d5605a299f04defc730"
    ]
  },
  {
    "address": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
    "storageKeys": [
      "0x377eec8667584e30e85471441b46e69393e5f35d2b63a0b4c26a5b1f6d059aa5"
    ]
  }
]

@dmarzzz
Copy link
Author

dmarzzz commented Feb 2, 2024

state diff is generated via:

import requests
import json

def generate_access_list(node_url, tx_hash):
    headers = {'Content-Type': 'application/json'}
    payload = {
        "jsonrpc": "2.0",
        "method": "trace_replayTransaction",
        "params": [tx_hash, ["stateDiff"]],
        "id": 1,
    }

    response = requests.post(node_url, data=json.dumps(payload), headers=headers)
    if response.status_code == 200:
        data = response.json()
        if 'result' in data:
            print(data)
        else:
            print("Access list could not be generated. Response:", data)
    else:
        print(response.content)
        print("Failed to connect to the node or transaction tracing not supported.")

node_url = "URL"
tx_hash = "TX_HASH"
generate_access_list(node_url, tx_hash)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment