Skip to content

Commit 7994d50

Browse files
authored
Merge pull request #121 from InjectiveLabs/f/binary-options
F/Binary-options
2 parents 044250d + 531e68c commit 7994d50

File tree

92 files changed

+17587
-2191
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+17587
-2191
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Copyright 2022 Injective Labs
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
import asyncio
17+
import logging
18+
19+
from pyinjective.composer import Composer as ProtoMsgComposer
20+
from pyinjective.async_client import AsyncClient
21+
from pyinjective.transaction import Transaction
22+
from pyinjective.constant import Network
23+
from pyinjective.wallet import PrivateKey
24+
25+
26+
async def main() -> None:
27+
# select network: local, testnet, mainnet
28+
network = Network.testnet()
29+
composer = ProtoMsgComposer(network=network.string())
30+
31+
# initialize grpc client
32+
client = AsyncClient(network, insecure=False)
33+
await client.sync_timeout_height()
34+
35+
# load account
36+
priv_key = PrivateKey.from_hex("5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e")
37+
pub_key = priv_key.to_public_key()
38+
address = await pub_key.to_address().async_init_num_seq(network.lcd_endpoint)
39+
subaccount_id = address.get_subaccount_id(index=0)
40+
41+
# prepare trade info
42+
market_id = "0x00617e128fdc0c0423dd18a1ff454511af14c4db6bdd98005a99cdf8fdbf74e9"
43+
fee_recipient = "inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r"
44+
45+
# prepare tx msg
46+
msg = composer.MsgCreateBinaryOptionsLimitOrder(
47+
sender=address.to_acc_bech32(),
48+
market_id=market_id,
49+
subaccount_id=subaccount_id,
50+
fee_recipient=fee_recipient,
51+
price=0.5,
52+
quantity=1,
53+
is_buy=True,
54+
is_reduce_only=False
55+
)
56+
# build sim tx
57+
tx = (
58+
Transaction()
59+
.with_messages(msg)
60+
.with_sequence(address.get_sequence())
61+
.with_account_num(address.get_number())
62+
.with_chain_id(network.chain_id)
63+
)
64+
sim_sign_doc = tx.get_sign_doc(pub_key)
65+
sim_sig = priv_key.sign(sim_sign_doc.SerializeToString())
66+
sim_tx_raw_bytes = tx.get_tx_data(sim_sig, pub_key)
67+
68+
# simulate tx
69+
(sim_res, success) = await client.simulate_tx(sim_tx_raw_bytes)
70+
if not success:
71+
print(sim_res)
72+
return
73+
74+
sim_res_msg = ProtoMsgComposer.MsgResponses(sim_res.result.data, simulation=True)
75+
print("---Simulation Response---")
76+
print(sim_res_msg)
77+
78+
# build tx
79+
gas_price = 500000000
80+
gas_limit = sim_res.gas_info.gas_used + 20000 # add 20k for gas, fee computation
81+
gas_fee = '{:.18f}'.format((gas_price * gas_limit) / pow(10, 18)).rstrip('0')
82+
fee = [composer.Coin(
83+
amount=gas_price * gas_limit,
84+
denom=network.fee_denom,
85+
)]
86+
tx = tx.with_gas(gas_limit).with_fee(fee).with_memo('').with_timeout_height(client.timeout_height)
87+
sign_doc = tx.get_sign_doc(pub_key)
88+
sig = priv_key.sign(sign_doc.SerializeToString())
89+
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
90+
91+
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
92+
res = await client.send_tx_sync_mode(tx_raw_bytes)
93+
print("---Transaction Response---")
94+
print(res)
95+
print("gas wanted: {}".format(gas_limit))
96+
print("gas fee: {} INJ".format(gas_fee))
97+
98+
if __name__ == "__main__":
99+
logging.basicConfig(level=logging.INFO)
100+
asyncio.get_event_loop().run_until_complete(main())
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Copyright 2022 Injective Labs
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
import asyncio
17+
import logging
18+
19+
from pyinjective.composer import Composer as ProtoMsgComposer
20+
from pyinjective.async_client import AsyncClient
21+
from pyinjective.transaction import Transaction
22+
from pyinjective.constant import Network
23+
from pyinjective.wallet import PrivateKey
24+
25+
26+
async def main() -> None:
27+
# select network: local, testnet, mainnet
28+
network = Network.testnet()
29+
composer = ProtoMsgComposer(network=network.string())
30+
31+
# initialize grpc client
32+
client = AsyncClient(network, insecure=False)
33+
await client.sync_timeout_height()
34+
35+
# load account
36+
priv_key = PrivateKey.from_hex("5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e")
37+
pub_key = priv_key.to_public_key()
38+
address = await pub_key.to_address().async_init_num_seq(network.lcd_endpoint)
39+
subaccount_id = address.get_subaccount_id(index=0)
40+
41+
# prepare trade info
42+
market_id = "0x00617e128fdc0c0423dd18a1ff454511af14c4db6bdd98005a99cdf8fdbf74e9"
43+
fee_recipient = "inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r"
44+
45+
# prepare tx msg
46+
msg = composer.MsgCreateBinaryOptionsMarketOrder(
47+
sender=address.to_acc_bech32(),
48+
market_id=market_id,
49+
subaccount_id=subaccount_id,
50+
fee_recipient=fee_recipient,
51+
price=0.5,
52+
quantity=1,
53+
is_buy=True,
54+
is_reduce_only=False
55+
)
56+
# build sim tx
57+
tx = (
58+
Transaction()
59+
.with_messages(msg)
60+
.with_sequence(address.get_sequence())
61+
.with_account_num(address.get_number())
62+
.with_chain_id(network.chain_id)
63+
)
64+
sim_sign_doc = tx.get_sign_doc(pub_key)
65+
sim_sig = priv_key.sign(sim_sign_doc.SerializeToString())
66+
sim_tx_raw_bytes = tx.get_tx_data(sim_sig, pub_key)
67+
68+
# simulate tx
69+
(sim_res, success) = await client.simulate_tx(sim_tx_raw_bytes)
70+
if not success:
71+
print(sim_res)
72+
return
73+
74+
sim_res_msg = ProtoMsgComposer.MsgResponses(sim_res.result.data, simulation=True)
75+
print("---Simulation Response---")
76+
print(sim_res_msg)
77+
78+
# build tx
79+
gas_price = 500000000
80+
gas_limit = sim_res.gas_info.gas_used + 20000 # add 20k for gas, fee computation
81+
gas_fee = '{:.18f}'.format((gas_price * gas_limit) / pow(10, 18)).rstrip('0')
82+
fee = [composer.Coin(
83+
amount=gas_price * gas_limit,
84+
denom=network.fee_denom,
85+
)]
86+
tx = tx.with_gas(gas_limit).with_fee(fee).with_memo('').with_timeout_height(client.timeout_height)
87+
sign_doc = tx.get_sign_doc(pub_key)
88+
sig = priv_key.sign(sign_doc.SerializeToString())
89+
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
90+
91+
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
92+
res = await client.send_tx_sync_mode(tx_raw_bytes)
93+
print("---Transaction Response---")
94+
print(res)
95+
print("gas wanted: {}".format(gas_limit))
96+
print("gas fee: {} INJ".format(gas_fee))
97+
98+
if __name__ == "__main__":
99+
logging.basicConfig(level=logging.INFO)
100+
asyncio.get_event_loop().run_until_complete(main())
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Copyright 2022 Injective Labs
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
import asyncio
17+
import logging
18+
19+
from pyinjective.composer import Composer as ProtoMsgComposer
20+
from pyinjective.async_client import AsyncClient
21+
from pyinjective.transaction import Transaction
22+
from pyinjective.constant import Network
23+
from pyinjective.wallet import PrivateKey
24+
25+
26+
async def main() -> None:
27+
# select network: local, testnet, mainnet
28+
network = Network.testnet()
29+
composer = ProtoMsgComposer(network=network.string())
30+
31+
# initialize grpc client
32+
client = AsyncClient(network, insecure=False)
33+
await client.sync_timeout_height()
34+
35+
# load account
36+
priv_key = PrivateKey.from_hex("5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e")
37+
pub_key = priv_key.to_public_key()
38+
address = await pub_key.to_address().async_init_num_seq(network.lcd_endpoint)
39+
subaccount_id = address.get_subaccount_id(index=0)
40+
41+
# prepare trade info
42+
market_id = "0x00617e128fdc0c0423dd18a1ff454511af14c4db6bdd98005a99cdf8fdbf74e9"
43+
order_hash = "a975fbd72b874bdbf5caf5e1e8e2653937f33ce6dd14d241c06c8b1f7b56be46"
44+
45+
# prepare tx msg
46+
msg = composer.MsgCancelBinaryOptionsOrder(
47+
sender=address.to_acc_bech32(),
48+
market_id=market_id,
49+
subaccount_id=subaccount_id,
50+
order_hash=order_hash
51+
)
52+
# build sim tx
53+
tx = (
54+
Transaction()
55+
.with_messages(msg)
56+
.with_sequence(address.get_sequence())
57+
.with_account_num(address.get_number())
58+
.with_chain_id(network.chain_id)
59+
)
60+
sim_sign_doc = tx.get_sign_doc(pub_key)
61+
sim_sig = priv_key.sign(sim_sign_doc.SerializeToString())
62+
sim_tx_raw_bytes = tx.get_tx_data(sim_sig, pub_key)
63+
64+
# simulate tx
65+
(sim_res, success) = await client.simulate_tx(sim_tx_raw_bytes)
66+
if not success:
67+
print(sim_res)
68+
return
69+
70+
sim_res_msg = ProtoMsgComposer.MsgResponses(sim_res.result.data, simulation=True)
71+
print("---Simulation Response---")
72+
print(sim_res_msg)
73+
74+
# build tx
75+
gas_price = 500000000
76+
gas_limit = sim_res.gas_info.gas_used + 20000 # add 20k for gas, fee computation
77+
gas_fee = '{:.18f}'.format((gas_price * gas_limit) / pow(10, 18)).rstrip('0')
78+
fee = [composer.Coin(
79+
amount=gas_price * gas_limit,
80+
denom=network.fee_denom,
81+
)]
82+
tx = tx.with_gas(gas_limit).with_fee(fee).with_memo('').with_timeout_height(client.timeout_height)
83+
sign_doc = tx.get_sign_doc(pub_key)
84+
sig = priv_key.sign(sign_doc.SerializeToString())
85+
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
86+
87+
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
88+
res = await client.send_tx_sync_mode(tx_raw_bytes)
89+
print("---Transaction Response---")
90+
print(res)
91+
print("gas wanted: {}".format(gas_limit))
92+
print("gas fee: {} INJ".format(gas_fee))
93+
94+
if __name__ == "__main__":
95+
logging.basicConfig(level=logging.INFO)
96+
asyncio.get_event_loop().run_until_complete(main())

0 commit comments

Comments
 (0)