Skip to content

Commit

Permalink
[proposal] properly use inheritence for proposals and show how to use…
Browse files Browse the repository at this point in the history
… them in an example script
  • Loading branch information
xeroc committed Jul 25, 2016
1 parent 30a90b5 commit 37c8310
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 34 deletions.
30 changes: 30 additions & 0 deletions examples/propose_pm_settlement.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from graphenebase.transactions import formatTimeFromNow
from grapheneapi import GrapheneClient
from grapheneextra.proposal import Proposal
from pprint import pprint


symbol = "MILLLIONTDD"
issuer = "jonnybitcoin"
pm_result = False
expiration = formatTimeFromNow(60 * 60 * 24)
proposer = "xeroc"


class Config():
wallet_host = "localhost"
wallet_port = 8092

if __name__ == '__main__':
graphene = Proposal(Config)
account = graphene.rpc.get_account(issuer)
asset = graphene.rpc.get_asset(symbol)
# Publish a price
settle_price = {"quote": {"asset_id": "1.3.0",
"amount": 1 if pm_result else 0},
"base": {"asset_id": asset["id"],
"amount": 1
}}
tx = graphene.rpc.global_settle_asset(symbol, settle_price, False)
tx = graphene.propose_operations(tx["operations"], expiration, proposer, broadcast=True)
pprint(tx)
69 changes: 35 additions & 34 deletions grapheneextra/proposal.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
from grapheneapi import GrapheneClient
from datetime import datetime
import time

# from graphenebase.transactions import operations


class Proposal(object) :
class Proposal(GrapheneClient) :
""" Manage Proposals
:param grapheneapi.GrapheneClient grapheneClient: Grapehen
Client instance with connection details for RPC
*and* websocket connection
"""
def __init__(self, grapheneClient) :
self.client = grapheneClient
def __init__(self, *args, **kwargs) :
super(Proposal, self).__init__(*args, **kwargs)

def approve_available_proposals(self, from_account, approving_account) :
""" Approve all proposals for a given account with given approver
Expand All @@ -22,20 +23,20 @@ def approve_available_proposals(self, from_account, approving_account) :
:param str approving_account: approving account
"""
fromAccount = self.client.rpc.get_account(from_account)
approving_account = self.client.rpc.get_account(approving_account)
proposals = self.client.ws.get_proposed_transactions(fromAccount["id"])
fromAccount = self.rpc.get_account(from_account)
approving_account = self.rpc.get_account(approving_account)
proposals = self.rpc.ws.get_proposed_transactions(fromAccount["id"])
for proposal in proposals :
if approving_account["id"] in proposal["available_active_approvals"] :
print("%s: Proposal %s already approved. Expires on %s UTC" %
(fromAccount["name"], proposal['id'], proposal["expiration_time"]))
else :
print("%s: Approving Proposal %s ..." %
(fromAccount["name"], proposal['id']))
self.client.rpc.approve_proposal(approving_account["name"],
proposal["id"],
{"active_approvals_to_add" : [approving_account["name"]]},
True)
self.rpc.approve_proposal(approving_account["name"],
proposal["id"],
{"active_approvals_to_add" : [approving_account["name"]]},
True)

def propose_transfer(self, proposer_account, from_account, to_account,
amount, asset, expiration=3600, broadcast=True):
Expand All @@ -54,24 +55,24 @@ def propose_transfer(self, proposer_account, from_account, to_account,
cli_wallet
"""
proposer = self.client.rpc.get_account(proposer_account)
fromAccount = self.client.rpc.get_account(from_account)
toAccount = self.client.rpc.get_account(to_account)
asset = self.client.rpc.get_asset(asset)
op = self.client.rpc.get_prototype_operation("transfer_operation")
proposer = self.rpc.get_account(proposer_account)
fromAccount = self.rpc.get_account(from_account)
toAccount = self.rpc.get_account(to_account)
asset = self.rpc.get_asset(asset)
op = self.rpc.get_prototype_operation("transfer_operation")

op[1]["amount"]["amount"] = int(amount * 10 ** asset["precision"])
op[1]["amount"]["asset_id"] = asset["id"]
op[1]["from"] = fromAccount["id"]
op[1]["to"] = toAccount["id"]

exp_time = datetime.utcfromtimestamp(time.time() + int(expiration)).strftime('%Y-%m-%dT%H:%M:%S')
buildHandle = self.client.rpc.begin_builder_transaction()
self.client.rpc.add_operation_to_builder_transaction(buildHandle, op)
self.client.rpc.set_fees_on_builder_transaction(buildHandle, asset["id"])
self.client.rpc.propose_builder_transaction2(buildHandle, proposer["name"], exp_time, 0, False)
self.client.rpc.set_fees_on_builder_transaction(buildHandle, asset["id"])
return self.client.rpc.sign_builder_transaction(buildHandle, broadcast)
buildHandle = self.rpc.begin_builder_transaction()
self.rpc.add_operation_to_builder_transaction(buildHandle, op)
self.rpc.set_fees_on_builder_transaction(buildHandle, asset["id"])
self.rpc.propose_builder_transaction2(buildHandle, proposer["name"], exp_time, 0, False)
self.rpc.set_fees_on_builder_transaction(buildHandle, asset["id"])
return self.rpc.sign_builder_transaction(buildHandle, broadcast)

def propose_operations(self, ops, expiration, proposer_account, preview=0, broadcast=False):
""" Propose several operations
Expand All @@ -92,32 +93,32 @@ def propose_operations(self, ops, expiration, proposer_account, preview=0, broad
print(rpc.get_transaction_id(tx))
"""

proposer = self.client.rpc.get_account(proposer_account)
buildHandle = self.client.rpc.begin_builder_transaction()
proposer = self.rpc.get_account(proposer_account)
buildHandle = self.rpc.begin_builder_transaction()
for op in ops:
self.client.rpc.add_operation_to_builder_transaction(buildHandle, op)
self.client.rpc.set_fees_on_builder_transaction(buildHandle, "1.3.0")
self.client.rpc.propose_builder_transaction2(buildHandle, proposer["name"], expiration, preview, False)
self.client.rpc.set_fees_on_builder_transaction(buildHandle, "1.3.0")
return self.client.rpc.sign_builder_transaction(buildHandle, broadcast)
self.rpc.add_operation_to_builder_transaction(buildHandle, op)
self.rpc.set_fees_on_builder_transaction(buildHandle, "1.3.0")
self.rpc.propose_builder_transaction2(buildHandle, proposer["name"], expiration, preview, False)
self.rpc.set_fees_on_builder_transaction(buildHandle, "1.3.0")
return self.rpc.sign_builder_transaction(buildHandle, broadcast)

# ## Alternative implementation building the transactions
# ## manually. Not yet working though
# op = self.client.rpc.get_prototype_operation("proposal_create_operation")
# op = self.rpc.get_prototype_operation("proposal_create_operation")
# for o in ops :
# op[1]["proposed_ops"].append(o)
# op[1]["expiration_time"] = expiration
# op[1]["fee_paying_account"] = payee_id
# op[1]["fee"] = self.get_operations_fee(op, "1.3.0")
# buildHandle = self.client.rpc.begin_builder_transaction()
# buildHandle = self.rpc.begin_builder_transaction()
# from pprint import pprint
# pprint(op)
# self.client.rpc.add_operation_to_builder_transaction(buildHandle, op)
# # print(self.client.rpc.preview_builder_transaction(buildHandle))
# return self.client.rpc.sign_builder_transaction(buildHandle, broadcast)
# self.rpc.add_operation_to_builder_transaction(buildHandle, op)
# # print(self.rpc.preview_builder_transaction(buildHandle))
# return self.rpc.sign_builder_transaction(buildHandle, broadcast)

# def get_operations_fee(self, op, asset_id):
# global_parameters = self.client.rpc.get_object("2.0.0")[0]["parameters"]["current_fees"]
# global_parameters = self.rpc.get_object("2.0.0")[0]["parameters"]["current_fees"]
# parameters = global_parameters["parameters"]
# scale = global_parameters["scale"] / 1e4
# opID = op[0]
Expand Down

0 comments on commit 37c8310

Please sign in to comment.