Skip to content

Commit 1866aa7

Browse files
committed
chore: add contracts_utils
1 parent a3005d4 commit 1866aa7

File tree

1 file changed

+175
-0
lines changed

1 file changed

+175
-0
lines changed

scripts/contracts_utils.py

+175
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
import sys
2+
import os
3+
4+
sys.path.append(os.path.abspath("tests"))
5+
from consts import *
6+
from brownie import (
7+
accounts,
8+
KeyManager,
9+
Vault,
10+
network
11+
)
12+
from shared_tests import *
13+
import re
14+
15+
# These functions are utils functions to replay, simulate or decode transactions crafted by the State Chain. These can
16+
# be transactions that aborted, reverted or failed for any other reason. Also it can decode raw bytes calls.
17+
# It can be used for calls to the Vault and/or to the Key Manager.
18+
19+
# When making the calls you should replace the addresses of the contracts with the ones for the network you are using.
20+
# Also, insert the adequate values that you want to decode/broadcast.
21+
# Finally, you can add `.call` to simulate the transaction without broadcasting it. Remove it to broadcast the transaction.
22+
23+
AUTONOMY_SEED = os.environ["SEED"]
24+
cf_accs = accounts.from_mnemonic(AUTONOMY_SEED, count=10)
25+
DEPLOYER_ACCOUNT_INDEX = int(os.environ.get("DEPLOYER_ACCOUNT_INDEX") or 0)
26+
27+
DEPLOYER = cf_accs[DEPLOYER_ACCOUNT_INDEX]
28+
print(f"DEPLOYER = {DEPLOYER}")
29+
30+
31+
network.priority_fee("1 gwei")
32+
33+
# Decode raw bytes call to the Vault contract. This is useful when a broadcast aborts and we have the raw bytes.
34+
# However, the engine logs should also be reporting what the error was.
35+
def decode_vault_call():
36+
# Insert call bytes
37+
bytes = [82, 147, 23, 142, 164, 210, 192, 228, 179, 1, 116, 226, 90, 22, 145, 80, 166, 112, 58, 148, 164, 61, 228, 113, 164, 171, 32, 239, 118, 61, 127, 164, 63, 51, 33, 150, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 197, 48, 35, 241, 171, 236, 246, 79, 161, 249, 215, 252, 102, 95, 118, 247, 166, 59, 139, 55, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 175, 136, 208, 101, 231, 124, 140, 194, 35, 147, 39, 197, 237, 179, 164, 50, 38, 142, 88, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95, 136, 31, 60, 230, 174, 25, 70, 188, 193, 171, 127, 97, 189, 209, 71, 10, 112, 205, 23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 94, 109, 229, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 96, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 95, 136, 31, 60, 230, 174, 25, 70, 188, 193, 171, 127, 97, 189, 209, 71, 10, 112, 205, 23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 192, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 121, 0, 26, 94, 118, 47, 59, 239, 200, 229, 135, 27, 66, 246, 115, 78, 0, 73, 137, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 96, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 48, 120, 56, 50, 97, 70, 52, 57, 52, 52, 55, 68, 56, 97, 48, 55, 101, 51, 98, 100, 57, 53, 66, 68, 48, 100, 53, 54, 102, 51, 53, 50, 52, 49, 53, 50, 51, 102, 66, 97, 98, 49, 32, 44, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
38+
39+
hex_string = ''.join(hex(num)[2:].zfill(2) for num in bytes)
40+
# print("0x" + hex_string)
41+
print(Vault.decode_input(hex_string))
42+
43+
# Decode raw bytes call to the Key Manager contract: This is useful when a broadcast aborts and we have the raw bytes.
44+
# However, the engine logs should also be reporting what the error was.
45+
def decode_keymanager_call():
46+
# Insert SC bytes
47+
bytes = [193, 196, 161, 89, 183, 114, 41, 219, 249, 193, 93, 187, 161, 5, 114, 77, 41, 112, 252, 191, 182, 91, 201, 85, 74, 176, 209, 70, 4, 42, 82, 172, 142, 93, 189, 62, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 122, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 70, 224, 189, 84, 26, 200, 131, 123, 10, 200, 229, 60, 105, 40, 217, 141, 255, 139, 12, 1, 27, 68, 201, 193, 236, 7, 35, 11, 223, 163, 212, 220, 16, 24, 103, 89, 25, 204, 64, 220, 185, 58, 253, 221, 34, 215, 54, 236, 46, 156, 137, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
48+
49+
hex_string = ''.join(hex(num)[2:].zfill(2) for num in bytes)
50+
# print("0x" + hex_string)
51+
print(KeyManager.decode_input(hex_string))
52+
53+
54+
# Send a transfer fallback call to the Vault contract. That is to be used to replay a CCM message that has aborted.
55+
def send_transferFallback():
56+
# Insert the address of the vault contract for the network
57+
vault = Vault.at("0xF5e10380213880111522dd0efD3dbb45b9f62Bcc")
58+
59+
# Get the sig data from the signed tx. Will look like this and can be converted straight away to a uint.
60+
# sig: 105,443,778,774,230,036,821,222,798,164,959,664,336,048,410,063,990,219,863,188,006,721,837,015,465,009
61+
sig = 105443778774230036821222798164959664336048410063990219863188006721837015465009
62+
nonce = 7227
63+
kTimesGAddress = "0x6c87aef85c3342deaecc056aecaf62f61af62233"
64+
65+
# Insert [token, recipitent, amount]
66+
transferParams = [NATIVE_ADDR, "0xf112f5d0995e17bfe5021fea90c3a6fac875664e", 39085318502136655]
67+
68+
sigData = [
69+
sig,
70+
nonce,
71+
kTimesGAddress,
72+
]
73+
74+
tx = vault.transferFallback(sigData, transferParams, {"from": DEPLOYER})
75+
tx.info()
76+
77+
def send_allBatch():
78+
# Insert the address of the vault contract for the network
79+
vault = Vault.at("0x2bb150e6d4366A1BDBC4275D1F35892CD63F27e3")
80+
81+
# Get the sig data from the signed tx as in the send_transferFallback function.
82+
sigData = [
83+
53564996668499050857217723416332709030557380600265126346613536706207704396933,
84+
584,
85+
"0xd1714973cbd82edad9c1af48cb21abd54283e366",
86+
]
87+
# [token, recipitent, amount]
88+
transferParams = [NATIVE_ADDR, "0x8c952cc18969129a1c67297332a0b6558eb0bfec", 428007866314266787]
89+
90+
tx = vault.allBatch(sigData, [], [], [transferParams], {"from": DEPLOYER})
91+
tx.info()
92+
93+
94+
def send_rotation():
95+
# Insert the address of the Key Manager contract for the network
96+
keyManager = KeyManager.at("0x18195b0E3c33EeF3cA6423b1828E0FE0C03F32Fd")
97+
98+
# Get the sig data from the signed tx as in the send_transferFallback function.
99+
sigData = [
100+
15744132089818751460223478396489526259646805948031235684624463192235106336468,
101+
587,
102+
"0x9b5cc915cfb363c0dbf7ee25caee6261cfb8d4bd",
103+
]
104+
105+
tx = keyManager.setAggKeyWithAggKey(sigData, (19461722762821367710032909336806588856719894271958382684299865054750972487645, 0), {"from": DEPLOYER})
106+
tx.info()
107+
108+
109+
# Parse file with the `transactionOutIdToBroadcastId`
110+
# That should be a list like this [[[{s: string, kTimesGAddress: string}],[broadcastId, blockNumber]], ...]
111+
def parse_text():
112+
file_path = 'file.txt'
113+
pairs = []
114+
115+
with open(file_path, 'r') as file:
116+
s_value = None
117+
kTimesGAddress_value = None
118+
119+
for line in file:
120+
# Strip leading/trailing whitespace
121+
line = line.strip()
122+
123+
# Check for the 's:' line
124+
if line.startswith('s:'):
125+
s_value = line.split('s:')[1].strip()
126+
127+
# Check for the 'kTimesGAddress:' line
128+
elif 'kTimesGAddress:' in line:
129+
kTimesGAddress_value = line.split('kTimesGAddress:')[1].strip()
130+
131+
# Check for the line with the number (e.g., 500)
132+
elif line.isdigit():
133+
number = int(line)
134+
135+
# Ensure both s and kTimesGAddress have been captured
136+
if s_value and kTimesGAddress_value:
137+
pairs.append((number, {"s": s_value, "kTimesGAddress": kTimesGAddress_value}))
138+
139+
# Reset s_value and kTimesGAddress_value after pairing
140+
s_value = None
141+
kTimesGAddress_value = None
142+
143+
# Sort it by broadcast id
144+
pairs.sort(key=lambda x: x[0])
145+
146+
broadcastID = 502
147+
# Get nonce and newAggKey from pendingApiCalls for that broadcastId
148+
nonce = 564
149+
newAggKey = (16960693264591855940648150423704738328849124419851104914880108972622132112107, 0)
150+
151+
filtered_pairs = [pair for pair in pairs if pair[0] == broadcastID]
152+
153+
keyManager = KeyManager.at("0x18195b0E3c33EeF3cA6423b1828E0FE0C03F32Fd")
154+
155+
loop_counter = 0
156+
for _, data in filtered_pairs:
157+
loop_counter += 1
158+
print(f"Loop number: {loop_counter}")
159+
160+
# Convert hexadecimal 's' value to decimal and extract 'kTimesGAddress'
161+
s_decimal = int(data['s'], 16)
162+
kTimesGAddress = data['kTimesGAddress']
163+
164+
sigData = [s_decimal, nonce, kTimesGAddress]
165+
try:
166+
# Use "call" to simulate the transaction. Remove it to broadcast the transaction.
167+
tx = keyManager.setAggKeyWithAggKey.call(sigData, newAggKey, {"from": DEPLOYER})
168+
# tx = keyManager.setAggKeyWithAggKey(sigData, newAggKey, {"from": DEPLOYER})
169+
print("Transaction simulation succeeded")
170+
print(tx)
171+
break
172+
except Exception as e:
173+
print("Transaction simulation failed")
174+
print(f"Error: {e}")
175+

0 commit comments

Comments
 (0)