from web3.auto import w3
from eth_account.messages import encode_structured_data
import time
from loguru import logger
import aiohttp
import asyncio
from config import TIME, TIMEMAX, TIME_ERROR
import random
import ast
import json
logger.add(f'log.log')
def validation_type(type, choise):
if type == 'uint32':
return int(choise)
if type == 'string':
return json.dumps(dict(choise))
choise = [int(ch) for ch in choise]
return choise
# region form
def forma(address, signature, space, proposal, choice, timestamp, type_choice="uint32"):
forma = {
"address": address,
"sig": signature,
"data": {
"domain": {
"name": "snapshot",
"version": "0.1.4"
},
"types": {
"Vote": [{
"name": "from",
"type": "address"
},
{
"name": "space",
"type": "string"
},
{
"name": "timestamp",
"type": "uint64"
},
{
"name": "proposal",
"type": "bytes32"
},
{
"name": "choice",
"type": type_choice
},
{
"name": "reason",
"type": "string"
},
{
"name": "app",
"type": "string"
},
{
"name": "metadata",
"type": "string"
}
]
},
"message": {
"space": space,
"proposal": proposal,
"choice": validation_type(type_choice,choice),
"app": "snapshot",
"reason": "",
"from": address,
"timestamp": timestamp,
'metadata': "{}"
}
}
}
return forma
# endregion
# region signature
def signature(address, space, proposal, choice, timestamp, key, type_choice="uint32"):
sig_signature = {
"domain": {
"name": "snapshot",
"version": "0.1.4"
},
"types": {
"Vote": [
{
"name": "from",
"type": "address"
},
{
"name": "space",
"type": "string"
},
{
"name": "timestamp",
"type": "uint64"
},
{
"name": "proposal",
"type": "bytes32"
},
{
"name": "choice",
"type": type_choice
},
{
"name": "reason",
"type": "string"
},
{
"name": "app",
"type": "string"
},
{
"name": "metadata",
"type": "string"
}
],
'EIP712Domain': [{'name': 'name', 'type': 'string'}, {'name': 'version', 'type': 'string'}]
},
'primaryType': "Vote",
"message": {
"space": space,
"proposal": w3.toBytes(hexstr=proposal),
"choice": validation_type(type_choice,choice),
"app": "snapshot",
"reason": "",
"from": address,
"timestamp": timestamp,
'metadata': "{}"
}
}
signature = (w3.eth.account.sign_message(encode_structured_data(primitive=sig_signature), key))['signature'].hex()
return signature