-
Notifications
You must be signed in to change notification settings - Fork 1
/
merge-split.py
147 lines (119 loc) · 5.15 KB
/
merge-split.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from dotenv import load_dotenv
from dataclasses import dataclass
import os
from web3 import Web3
from web3.middleware import geth_poa_middleware
load_dotenv()
NegRiskAdapterABI = """[{"inputs":[{"internalType":"bytes32","name":"_conditionId","type":"bytes32"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"splitPosition","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"_conditionId","type":"bytes32"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"mergePositions","outputs":[],"stateMutability":"nonpayable","type":"function"}]"""
ConditionalTokenABI = """[{"constant":"false","inputs":[{"name":"collateralToken","type":"address"},{"name":"parentCollectionId","type":"bytes32"},{"name":"CONDITION_ID","type":"bytes32"},{"name":"partition","type":"uint256[]"},{"name":"amount","type":"uint256"}],"name":"splitPosition","outputs":[],"payable":"false","stateMutability":"nonpayable","type":"function"},{"constant":"false","inputs":[{"name":"collateralToken","type":"address"},{"name":"parentCollectionId","type":"bytes32"},{"name":"CONDITION_ID","type":"bytes32"},{"name":"partition","type":"uint256[]"},{"name":"amount","type":"uint256"}],"name":"mergePositions","outputs":[],"payable":"false","stateMutability":"nonpayable","type":"function"}]"""
@dataclass
class ContractConfig:
neg_risk_adapter: str
conditional_tokens: str
collateral: str
def get_contract_config(chainID: int) -> ContractConfig:
"""
Get the contract configuration for the chain
"""
CONFIG = {
137: ContractConfig(
neg_risk_adapter="0xd91E80cF2E7be2e162c6513ceD06f1dD0dA35296",
collateral="0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174",
conditional_tokens="0x4D97DCd97eC945f40cF65F87097ACe5EA0476045",
),
80002: ContractConfig(
neg_risk_adapter="",
collateral="0x9c4e1703476e875070ee25b56a58b008cfb8fa78",
conditional_tokens="0x69308FB512518e39F9b16112fA8d994F4e2Bf8bB",
),
}
return CONFIG.get(chainID)
# wallet
ADDRESS = os.getenv("ADDRESS")
PK = os.getenv("PK")
# RPC_URL
RPC_URL = os.getenv("RPC_URL")
# contracts
CHAIN_ID = int(os.getenv("CHAIN_ID"))
CONTRACTS = get_contract_config(CHAIN_ID)
# market
CONDITION_ID = os.getenv("CONDITION_ID")
IS_NEG_RISK_MARKET = os.getenv("IS_NEG_RISK_MARKET") == "true"
w3 = Web3(Web3.HTTPProvider(RPC_URL))
w3.middleware_onion.inject(geth_poa_middleware, layer=0)
def main():
neg_risk_adapter = w3.eth.contract(
address=CONTRACTS.neg_risk_adapter, abi=NegRiskAdapterABI
)
conditional_tokens = w3.eth.contract(
address=CONTRACTS.conditional_tokens, abi=ConditionalTokenABI
)
split_position(neg_risk_adapter, conditional_tokens)
merge_positions(neg_risk_adapter, conditional_tokens)
def split_position(neg_risk_adapter, conditional_tokens):
nonce = w3.eth.getTransactionCount(ADDRESS)
if IS_NEG_RISK_MARKET:
transaction = neg_risk_adapter.functions.splitPosition(
CONDITION_ID, 10000000
).buildTransaction(
{
"chainId": CHAIN_ID,
"gas": 1000000,
"from": ADDRESS,
"nonce": nonce,
}
)
else:
transaction = conditional_tokens.functions.splitPosition(
CONTRACTS.collateral,
"0x0000000000000000000000000000000000000000000000000000000000000000",
CONDITION_ID,
[1, 2],
10000000,
).buildTransaction(
{
"chainId": CHAIN_ID,
"gas": 1000000,
"from": ADDRESS,
"nonce": nonce,
}
)
signed_txn = w3.eth.account.signTransaction(transaction, private_key=PK)
tx_hash = w3.toHex(w3.keccak(signed_txn.rawTransaction))
tx = w3.eth.send_raw_transaction(signed_txn.rawTransaction)
w3.eth.wait_for_transaction_receipt(tx, 600)
print("split position " + tx_hash)
def merge_positions(neg_risk_adapter, conditional_tokens):
nonce = w3.eth.getTransactionCount(ADDRESS)
if IS_NEG_RISK_MARKET:
transaction = neg_risk_adapter.functions.mergePositions(
CONDITION_ID, 10000000
).buildTransaction(
{
"chainId": CHAIN_ID,
"gas": 1000000,
"from": ADDRESS,
"nonce": nonce,
}
)
else:
transaction = conditional_tokens.functions.mergePositions(
CONTRACTS.collateral,
"0x0000000000000000000000000000000000000000000000000000000000000000",
CONDITION_ID,
[1, 2],
10000000,
).buildTransaction(
{
"chainId": CHAIN_ID,
"gas": 1000000,
"from": ADDRESS,
"nonce": nonce,
}
)
signed_txn = w3.eth.account.signTransaction(transaction, private_key=PK)
tx_hash = w3.toHex(w3.keccak(signed_txn.rawTransaction))
tx = w3.eth.send_raw_transaction(signed_txn.rawTransaction)
w3.eth.wait_for_transaction_receipt(tx, 600)
print("merge positions " + tx_hash)
main()