Skip to content

Commit

Permalink
feat: msgs generate and broadcast
Browse files Browse the repository at this point in the history
  • Loading branch information
Sai Kumar committed Aug 22, 2022
1 parent 2adfe7a commit 3c65025
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 0 deletions.
12 changes: 12 additions & 0 deletions msgs_broadcast/Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[[source]]
url = "https://pypi.org/msgs_broadcast"
verify_ssl = true
name = "pypi"

[packages]
pandas = "*"

[dev-packages]

[requires]
python_version = "3.8"
105 changes: 105 additions & 0 deletions msgs_broadcast/Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions msgs_broadcast/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generate sign messages and broadcast to rpc
## Install deps
```bash
## Install python3 and pip-env
$ pipenv shell
$ pipenv install
```
## CSV File Format
```csv
validator_address,amount
pasgvaloper1djp5ru64djz3g8ngf26tcw203xd6tq2auzu5vp,100
pasgvaloper1djp5ru64djz3g8ngf26tcw203xd6tq2auzu5vp,20
pasgvaloper1djp5ru64djz3g8ngf26tcw203xd6tq2auzu5vp,30
```
## Genetate unsigned message for delegations
```bash
$ python main.py inp.csv --from_addr {FROM_ADDR} --granter {GRANTER} --memo {MEMO} --output unsigned_message.json
```

## Sign the message
```
$ simd tx sign unsigned_message.json --from test --chain-id test-chain > sign.json
```

## Broadcast the sign message
```bash
$ simd tx broadcast signed_message.json
```
103 changes: 103 additions & 0 deletions msgs_broadcast/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#! /usr/bin/python

from operator import index
from textwrap import indent
import pandas as pd
import os
import sys
import json
import argparse
import datetime

gas_default = 0
message = {
"body": {
"messages": [
],
"memo": "",
"timeout_height": "0",
"extension_options": [],
"non_critical_extension_options": []
},
"auth_info": {
"signer_infos": [],
"fee": {
"amount": [],
"gas_limit": "108997",
"payer": "",
"granter": ""
}
},
"signatures": []
}


# READ sample csv
def read_csv(file_path):
if not os.path.exists(file_path):
print(f"File is not found %s", file_path)
sys.exit(0)

return pd.read_csv(file_path)


def write_file(file_path, data):
with open(file_path, "w") as f:
json.dump(data, f)
print(f"[!] file writed successfully %s", file_path)


def gen_unsig_mesg_delegate(args):
from_addr = args.from_addr
granter_addr = args.granter

df = read_csv(args.infile)
row_count, _ = df.shape
# automate gas calculation for delegaiton is 108997
gas_default = row_count * 108997
for row_index in range(0, row_count):
msg_dict = dict(
{
"@type": "/cosmos.staking.v1beta1.MsgDelegate",
"delegator_address": from_addr,
"validator_address": df["validator_address"][row_index],
"amount": {
"denom": "stake",
"amount": str(df["amount"][row_index])
}
}
)
message["body"]["messages"].append(msg_dict)

if granter_addr is not None:
message["auth_info"]["fee"]["granter"] = granter_addr

if args.memo is not None:
message["body"]["memo"] = args.memo

message["auth_info"]["fee"]["gas_limit"] = str(gas_default)

output_file = "unsigned_sign_{}.json".format(
datetime.datetime.now().strftime("%Y%m%d-%H%M%S"))
if args.output is not None:
output_file = args.output
# Write ouput to ...
write_file(output_file, message)


if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("infile", help="input csv file", type=str)
parser.add_argument("--from_addr", help="from address", type=str)
parser.add_argument("--granter", help="granter address", type=str)
parser.add_argument("--memo", help="memo", type=str)
parser.add_argument("--output", help="output file", type=str)

args = parser.parse_args()
if args.infile is None:
print("[X] input csv file is required")
sys.exit(1)
if args.from_addr is None:
print("[X] from address is required")
sys.exit(1)
gen_unsig_mesg_delegate(args)
4 changes: 4 additions & 0 deletions msgs_broadcast/sample.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
validator_address,amount
pasgvaloper1djp5ru64djz3g8ngf26tcw203xd6tq2auzu5vp,100
pasgvaloper1djp5ru64djz3g8ngf26tcw203xd6tq2auzu5vp,20
pasgvaloper1djp5ru64djz3g8ngf26tcw203xd6tq2auzu5vp,30

0 comments on commit 3c65025

Please sign in to comment.