Skip to content

Instantly share code, notes, and snippets.

@cinquemb
Created July 19, 2024 15:37
Show Gist options
  • Save cinquemb/2d8548606e1753864045412e1f12f26d to your computer and use it in GitHub Desktop.
Save cinquemb/2d8548606e1753864045412e1f12f26d to your computer and use it in GitHub Desktop.
Sample User Deposits/Withdrawl info Directly From Contract Storage - Rethink
from web3 import Web3
import json
import sys
import eth_utils
GovernableFundContract = json.loads(open('./build/contracts/GovernableFund.json', 'r+').read())
RethinkFundGovernorContract = json.loads(open('./build/contracts/RethinkFundGovernor.json', 'r+').read())
PermissionsContract = json.loads(open('./build/contracts/Permissions.json', 'r+').read())
ERC20MockContract = json.loads(open('./build/contracts/ERC20Mock.json', 'r+').read())
RethinkReaderContract = json.loads(open('./build/contracts/RethinkReader.json', 'r+').read())
RolesFull = json.loads(open('../rethink-frontend/src/contracts/zodiac/RolesFull.json', 'r+').read())
#provider = Web3.HTTPProvider('https://goerli-rollup.arbitrum.io/rpc', request_kwargs={"timeout": 60*300})
provider = Web3.HTTPProvider('https://polygon-bor.publicnode.com', request_kwargs={"timeout": 60*300})
#provider = Web3.HTTPProvider('https://eth.drpc.org', request_kwargs={"timeout": 60*300})
#provider = Web3.HTTPProvider('https://arbitrum.meowrpc.com', request_kwargs={"timeout": 60*300})
w3 = Web3(provider)
from web3.middleware import geth_poa_middleware
w3.middleware_onion.inject(geth_poa_middleware, layer=0)
#print(w3.to_hex(0))
#sys.exit()
c_addr = "0x1673458dDf6C0ea24ce5598918F3cA1e58f2d795"
u_addr_eoa = "0x6EC175951624e1E1e6367Fa3dB90a1829E032Ec3"
deposit_slot_idx = 273
withdraw_slot_idx = 274
def get_address_mapping_storage_key_at_index(addr, slot_no):
#https://ethereum.stackexchange.com/questions/49873/how-to-derive-the-storage-key-of-mapping-to-an-account
pos = eth_utils.remove_0x_prefix(hex(slot_no)).rjust(64, '0')
key = eth_utils.remove_0x_prefix(addr).rjust(64, '0')#.lower()
storage_key = w3.to_hex(w3.keccak(hexstr=key + pos))
return storage_key
def increment_storage_key(storage_key):
return w3.to_hex(w3.to_int(hexstr=eth_utils.remove_0x_prefix(storage_key)) + 1)
#https://medium.com/@dariusdev/how-to-read-ethereum-contract-storage-44252c8af925
#https://medium.com/coinmonks/decoding-the-memory-of-an-ethereum-contract-52c256f83f07
#https://docs.soliditylang.org/en/v0.4.20/miscellaneous.html#layout-of-state-variables-in-storage
print("#user deposits slot:", deposit_slot_idx,c_addr,u_addr_eoa)
dkey2 = get_address_mapping_storage_key_at_index(u_addr_eoa, deposit_slot_idx)
dkey2_1 = increment_storage_key(dkey2)
print("dkey2->", dkey2_1)
print(
"value -> (deposit amount)", w3.to_hex(
w3.eth.get_storage_at(
Web3.to_checksum_address(c_addr),
dkey2
)
)
)
print("dkey2+1->", increment_storage_key(dkey2))
print(
"value -> (deposit request time)", w3.to_hex(
w3.eth.get_storage_at(
Web3.to_checksum_address(c_addr),
dkey2_1
)
)
)
print("#user withdrawal, slot:", withdraw_slot_idx, c_addr)
wkey2 = get_address_mapping_storage_key_at_index(u_addr_eoa, withdraw_slot_idx)
wkey2_1 = increment_storage_key(wkey2)
print("wkey2->", wkey2)
print(
"value -> (withdrawal amount)", w3.to_hex(
w3.eth.get_storage_at(
Web3.to_checksum_address(c_addr),
wkey2
)
)
)
print("wkey2+1->", wkey2_1)
print(
"value -> (withdrawal request time)", w3.to_hex(
w3.eth.get_storage_at(
Web3.to_checksum_address(c_addr),
wkey2_1
)
)
)
print(
"MAX_BPS:", w3.to_hex(
w3.eth.get_storage_at(
Web3.to_checksum_address(c_addr),
w3.to_hex(259)
)
)
)
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment