def compareNewWire(self): # def test_online(self): # self.maxDiff = None from grapheneapi.grapheneapi import GrapheneAPI rpc = GrapheneAPI("localhost", 8092) tx = rpc.create_account("xeroc", "fsafaasf", "", False) pprint(tx) compare = rpc.serialize_transaction(tx) ref_block_num = tx["ref_block_num"] ref_block_prefix = tx["ref_block_prefix"] expiration = tx["expiration"] ops = [ transactions.Operation( transactions.Account_create(**tx["operations"][0][1])) ] tx = transactions.Signed_Transaction(ref_block_num=ref_block_num, ref_block_prefix=ref_block_prefix, expiration=expiration, operations=ops) tx = tx.sign([wif], chain=prefix) tx.verify([PrivateKey(wif).pubkey], "BTS") txWire = hexlify(bytes(tx)).decode("ascii") print("\n") print(txWire[:-130]) print(compare[:-130])
def test_limit_order_create(self): s = { "fee": { "amount": 100, "asset_id": "1.3.0" }, "seller": "1.2.29", "amount_to_sell": { "amount": 100000, "asset_id": "1.3.0" }, "min_to_receive": { "amount": 10000, "asset_id": "1.3.105" }, "expiration": "2016-05-18T09:22:05", "fill_or_kill": False, "extensions": [] } ops = [transactions.Operation(transactions.Limit_order_create(**s))] tx = transactions.Signed_Transaction(ref_block_num=ref_block_num, ref_block_prefix=ref_block_prefix, expiration=expiration, operations=ops) tx = tx.sign([wif], chain=prefix) tx.verify([PrivateKey(wif).pubkey], "BTS") txWire = hexlify(bytes(tx)).decode("ascii") compare = "f68585abf4dce7c8045701016400000000000000001da086010000000000001027000000000000693d343c57000000011f75cbfd49ae8d9b04af76cc0a7de8b6e30b71167db7fe8e2197ef9d858df1877043493bc24ffdaaffe592357831c978fd8a296b913979f106debe940d60d77b50" self.assertEqual(compare[:-130], txWire[:-130])
def test_call_update(self): s = { 'fee': { 'amount': 100, 'asset_id': '1.3.0' }, 'delta_debt': { 'amount': 10000, 'asset_id': '1.3.22' }, 'delta_collateral': { 'amount': 100000000, 'asset_id': '1.3.0' }, 'funding_account': '1.2.29', 'extensions': [] } ops = [transactions.Operation(transactions.Call_order_update(**s))] tx = transactions.Signed_Transaction(ref_block_num=ref_block_num, ref_block_prefix=ref_block_prefix, expiration=expiration, operations=ops) tx = tx.sign([wif], chain=prefix) tx.verify([PrivateKey(wif).pubkey], "BTS") txWire = hexlify(bytes(tx)).decode("ascii") compare = "f68585abf4dce7c8045701036400000000000000001d00e1f50500000000001027000000000000160000011f2627efb5c5144440e06ff567f1a09928d699ac6f5122653cd7173362a1ae20205952c874ed14ccec050be1c86c1a300811763ef3b481e562e0933c09b40e31fb" self.assertEqual(compare[:-130], txWire[:-130])
def test_override_transfer(self): s = { "fee": { "amount": 0, "asset_id": "1.3.0" }, "issuer": "1.2.29", "from": "1.2.104", "to": "1.2.29", "amount": { "amount": 100000, "asset_id": "1.3.105" }, "extensions": [] } op = transactions.Override_transfer(**s) ops = [transactions.Operation(op)] tx = transactions.Signed_Transaction(ref_block_num=ref_block_num, ref_block_prefix=ref_block_prefix, expiration=expiration, operations=ops) tx = tx.sign([wif], chain=prefix) tx.verify([PrivateKey(wif).pubkey], "BTS") txWire = hexlify(bytes(tx)).decode("ascii") compare = "f68585abf4dce7c8045701260000000000000000001d681da08601000000000069000000012030cc81722c3e67442d2f59deba188f6079c8ba2d8318a642e6a70a125655515f20e2bd3adb2ea886cdbc7f6590c7f8c80818d9176d9085c176c736686ab6c9fd" self.assertEqual(compare[:-130], txWire[:-130])
def compareConstructedTX(self): # def test_online(self): # self.maxDiff = None op = transactions.Override_transfer( **{ "fee": { "amount": 0, "asset_id": "1.3.0" }, "issuer": "1.2.29", "from": "1.2.104", "to": "1.2.29", "amount": { "amount": 100000, "asset_id": "1.3.105" }, "extensions": [] }) ops = [transactions.Operation(op)] tx = transactions.Signed_Transaction(ref_block_num=ref_block_num, ref_block_prefix=ref_block_prefix, expiration=expiration, operations=ops) tx = tx.sign([wif], chain=prefix) txWire = hexlify(bytes(tx)).decode("ascii") from grapheneapi.grapheneapi import GrapheneAPI rpc = GrapheneAPI("localhost", 8092) compare = rpc.serialize_transaction(transactions.JsonObj(tx)) print(compare[:-130]) print(txWire[:-130]) print(txWire[:-130] == compare[:-130]) self.assertEqual(compare[:-130], txWire[:-130])
def test_pricefeed(self): feed = transactions.PriceFeed( **{ "settlement_price": transactions.Price( base=transactions.Asset(amount=214211, asset_id="1.3.0"), quote=transactions.Asset(amount=1241, asset_id="1.3.14"), ), "core_exchange_rate": transactions.Price( base=transactions.Asset(amount=1241, asset_id="1.3.0"), quote=transactions.Asset(amount=6231, asset_id="1.3.14"), ), "maximum_short_squeeze_ratio": 1100, "maintenance_collateral_ratio": 1750, }) pFeed = transactions.Asset_publish_feed(fee=transactions.Asset( amount=100, asset_id="1.3.0"), publisher="1.2.0", asset_id="1.3.3", feed=feed) ops = [transactions.Operation(pFeed)] tx = transactions.Signed_Transaction(ref_block_num=ref_block_num, ref_block_prefix=ref_block_prefix, expiration=expiration, operations=ops) tx = tx.sign([wif], chain=prefix) txWire = hexlify(bytes(tx)).decode("ascii") compare = "f68585abf4dce7c8045701136400000000000000000003c34403000000000000d9040000000000000ed6064c04d9040000000000000057180000000000000e0000012009e13f9066fedc3c8c1eb2ac33b15dc67ecebf708890d0f8ab62ec8283d1636002315a189f1f5aa8497b41b8e6bb7c4dc66044510fae25d8f6aebb02c7cdef10" self.assertEqual(compare[:-130], txWire[:-130])
def test_create_account(self): s = { "fee": { "amount": 1467634, "asset_id": "1.3.0" }, "registrar": "1.2.33", "referrer": "1.2.27", "referrer_percent": 3, "name": "foobar-f124", "owner": { "weight_threshold": 1, "account_auths": [], 'key_auths': [['BTS6pbVDAjRFiw6fkiKYCrkz7PFeL7XNAfefrsREwg8MKpJ9VYV9x', 1], ['BTS6zLNtyFVToBsBZDsgMhgjpwysYVbsQD6YhP3kRkQhANUB4w7Qp', 1]], "address_auths": [] }, "active": { "weight_threshold": 1, "account_auths": [], 'key_auths': [['BTS6pbVDAjRFiw6fkiKYCrkz7PFeL7XNAfefrsREwg8MKpJ9VYV9x', 1], ['BTS6zLNtyFVToBsBZDsgMhgjpwysYVbsQD6YhP3kRkQhANUB4w7Qp', 1], ['BTS8CemMDjdUWSV5wKotEimhK6c4dY7p2PdzC2qM1HpAP8aLtZfE7', 1]], "address_auths": [] }, "options": { "memo_key": "BTS5TPTziKkLexhVKsQKtSpo4bAv5RnB8oXcG4sMHEwCcTf3r7dqE", "voting_account": "1.2.5", "num_witness": 0, "num_committee": 0, "votes": [], "extensions": [] }, "extensions": {} } op = transactions.Account_create(**s) ops = [transactions.Operation(op)] tx = transactions.Signed_Transaction(ref_block_num=ref_block_num, ref_block_prefix=ref_block_prefix, expiration=expiration, operations=ops) tx = tx.sign([wif], chain=prefix) tx.verify([PrivateKey(wif).pubkey], "BTS") txWire = hexlify(bytes(tx)).decode("ascii") compare = "f68585abf4dce7c804570105f26416000000000000211b03000b666f6f6261722d6631323401000000000202fe8cc11cc8251de6977636b55c1ab8a9d12b0b26154ac78e56e7c4257d8bcf6901000314aa202c9158990b3ec51a1aa49b2ab5d300c97b391df3beb34bb74f3c62699e01000001000000000303b453f46013fdbccb90b09ba169c388c34d84454a3b9fbec68d5a7819a734fca0010002fe8cc11cc8251de6977636b55c1ab8a9d12b0b26154ac78e56e7c4257d8bcf6901000314aa202c9158990b3ec51a1aa49b2ab5d300c97b391df3beb34bb74f3c62699e010000024ab336b4b14ba6d881675d1c782912783c43dbbe31693aa710ac1896bd7c3d61050000000000000000011f61ad276120bc3f1892962bfff7db5e8ce04d5adec9309c80529e3a978a4fa1073225a6d56929e34c9d2a563e67a8f4a227e4fadb4a3bb6ec91bfdf4e57b80efd" self.assertEqual(compare[:-130], txWire[:-130])
def test_proposal_update(self): s = { 'fee_paying_account': "1.2.1", 'proposal': "1.10.90", 'active_approvals_to_add': ["1.2.5"], "fee": transactions.Asset(amount=12512, asset_id="1.3.0"), } op = transactions.Proposal_update(**s) ops = [transactions.Operation(op)] tx = transactions.Signed_Transaction(ref_block_num=ref_block_num, ref_block_prefix=ref_block_prefix, expiration=expiration, operations=ops) tx = tx.sign([wif], chain=prefix) txWire = hexlify(bytes(tx)).decode("ascii") compare = "f68585abf4dce7c804570117e03000000000000000015a01050000000000000001203255378db6dc19443e74421c954ad7fdcf23f4ea45fe4fe5a1b078a0f94fb529594819c9799d68efa5cfb5b271a9333a2f516ca4fb5093226275f48a42d9e8cf" self.assertEqual(compare[:-130], txWire[:-130])
def test_limit_order_cancel(self): s = { "fee": { "amount": 0, "asset_id": "1.3.0" }, "fee_paying_account": "1.2.104", "order": "1.7.51840", "extensions": [] } ops = [transactions.Operation(transactions.Limit_order_cancel(**s))] tx = transactions.Signed_Transaction(ref_block_num=ref_block_num, ref_block_prefix=ref_block_prefix, expiration=expiration, operations=ops) tx = tx.sign([wif], chain=prefix) txWire = hexlify(bytes(tx)).decode("ascii") compare = "f68585abf4dce7c804570102000000000000000000688095030000011f3fb754814f3910c1a8845486b86057d2b4588ae559b4c3810828c0d4cbec0e5b23517937cd7e0cc5ee8999d0777af7fe56d3c4b2e587421bfb7400d4efdae97a" self.assertEqual(compare[:-130], txWire[:-130])
def test_Transfer(self): pub = format(account.PrivateKey(wif).pubkey, prefix) from_account_id = "1.2.0" to_account_id = "1.2.1" amount = 1000000 asset_id = "1.3.4" message = "abcdefgABCDEFG0123456789" nonce = "5862723643998573708" fee = transactions.Asset(amount=0, asset_id="1.3.0") amount = transactions.Asset(amount=int(amount), asset_id=asset_id) encrypted_memo = memo.encode_memo( account.PrivateKey(wif), account.PublicKey(pub, prefix=prefix), nonce, message) memoStruct = { "from": pub, "to": pub, "nonce": nonce, "message": encrypted_memo, "chain": prefix } memoObj = transactions.Memo(**memoStruct) transferStruct = { "fee": fee, "from": from_account_id, "to": to_account_id, "amount": amount, "memo": memoObj } transfer = transactions.Transfer(**transferStruct) ops = [transactions.Operation(transfer)] tx = transactions.Signed_Transaction(ref_block_num=ref_block_num, ref_block_prefix=ref_block_prefix, expiration=expiration, operations=ops) tx = tx.sign([wif], chain=prefix) tx.verify([PrivateKey(wif).pubkey], "BTS") txWire = hexlify(bytes(tx)).decode("ascii") compare = "f68585abf4dce7c804570100000000000000000000000140420f0000000000040102c0ded2bc1f1305fb0faac5e6c03ee3a1924234985427b6167ca569d13df435cf02c0ded2bc1f1305fb0faac5e6c03ee3a1924234985427b6167ca569d13df435cf8c94d19817945c5120fa5b6e83079a878e499e2e52a76a7739e9de40986a8e3bd8a68ce316cee50b210000011f39e3fa7071b795491e3b6851d61e7c959be92cc7deb5d8491cf1c3c8c99a1eb44553c348fb8f5001a78b18233ac66727e32fc776d48e92d9639d64f68e641948" self.assertEqual(compare[:-130], txWire[:-130])
def test_fee_pool(self): s = { "fee": { "amount": 10001, "asset_id": "1.3.0" }, "from_account": "1.2.282", "asset_id": "1.3.32", "amount": 15557238, "extensions": [] } op = transactions.Asset_fund_fee_pool(**s) ops = [transactions.Operation(op)] tx = transactions.Signed_Transaction(ref_block_num=ref_block_num, ref_block_prefix=ref_block_prefix, expiration=expiration, operations=ops) tx = tx.sign([wif], chain=prefix) txWire = hexlify(bytes(tx)).decode("ascii") compare = "f68585abf4dce7c8045701101127000000000000009a02207662ed00000000000000011f39f7dc7745076c9c7e612d40c68ee92d3f4b2696b1838037ce2a35ac259883ba6c6c49d91ad05a7e78d80bb83482c273dbbc911587487bf468b85fb4f537da3d" self.assertEqual(compare[:-130], txWire[:-130])
def compareConstructedTX(self): # def test_online(self): # self.maxDiff = None op = transactions.Account_create( **{ "fee": { "amount": 1467634, "asset_id": "1.3.0" }, "registrar": "1.2.33", "referrer": "1.2.27", "referrer_percent": 3, "name": "foobar-f124", "owner": { "weight_threshold": 1, "account_auths": [], "key_auths": [[ "BTS5TPTziKkLexhVKsQKtSpo4bAv5RnB8oXcG4sMHEwCcTf3r7dqE", 1 ]], "address_auths": [] }, "active": { "weight_threshold": 1, "account_auths": [], "key_auths": [[ "BTS5TPTziKkLexhVKsQKtSpo4bAv5RnB8oXcG4sMHEwCcTf3r7dqE", 1 ]], "address_auths": [] }, "options": { "memo_key": "BTS5TPTziKkLexhVKsQKtSpo4bAv5RnB8oXcG4sMHEwCcTf3r7dqE", "voting_account": "1.2.5", "num_witness": 26, "num_committee": 8, "votes": [ "1:22", "1:23", "1:24", "1:25", "1:26", "1:27", "1:28", "1:30", "1:31", "1:32", "1:34", "1:35", "1:36", "1:37", "1:38", "1:40", "1:41", "1:44", "1:45", "1:49", "1:51", "1:56", "1:60", "0:76", "0:84", "0:87", "0:88", "0:91", "0:141", "1:143", "0:147", "2:148", "2:150", "1:165", "1:166", "2:171", "0:173", "2:179" ], "extensions": [] }, "extensions": {} }) ops = [transactions.Operation(op)] tx = transactions.Signed_Transaction(ref_block_num=ref_block_num, ref_block_prefix=ref_block_prefix, expiration=expiration, operations=ops) tx = tx.sign([wif], chain=prefix) tx.verify([PrivateKey(wif).pubkey], "BTS") txWire = hexlify(bytes(tx)).decode("ascii") pprint(transactions.JsonObj(tx)) from grapheneapi.grapheneapi import GrapheneAPI rpc = GrapheneAPI("localhost", 8092) compare = rpc.serialize_transaction(transactions.JsonObj(tx)) print(compare[:-130]) print(txWire[:-130]) print(txWire[:-130] == compare[:-130]) self.assertEqual(compare[:-130], txWire[:-130])
class Config(): witness_url = witness_url if __name__ == '__main__': config = Config client = GrapheneClient(config) issuer = client.ws.get_account(issuer) from_account = client.ws.get_account(from_account) to_account = client.ws.get_account(to_account) asset = client.ws.get_asset(asset) amount = int(amount * 10 ** asset["precision"]) ops = [] op = transactions.Override_transfer(**{ "fee": {"amount": 0, "asset_id": "1.3.0"}, "issuer": issuer["id"], "from": from_account["id"], "to": to_account["id"], "amount": {"amount": amount, "asset_id": asset["id"]}, "extensions": [] }) ops.append(transactions.Operation(op)) tx = constructSignedTransaction(ops) pprint(transactions.JsonObj(tx)) print(client.ws.broadcast_transaction(transactions.JsonObj(tx), api="network_broadcast"))
def main() : global args global rpc config = Configuration() if "node" not in config or not config["node"]: config["node"] = "wss://bitshares.openledger.info/ws" parser = argparse.ArgumentParser( formatter_class=argparse.RawDescriptionHelpFormatter, description="Command line tool to interact with the BitShares network" ) """ Default settings for all tools """ parser.add_argument( '--node', type=str, default=config["node"], help='Websocket URL for public BitShares API (default: "wss://bitshares.openledger.info/ws")' ) parser.add_argument( '--rpcuser', type=str, default=config["rpcuser"], help='Websocket user if authentication is required' ) parser.add_argument( '--rpcpassword', type=str, default=config["rpcpassword"], help='Websocket password if authentication is required' ) parser.add_argument( '--nobroadcast', action='store_true', help='Do not broadcast anything' ) subparsers = parser.add_subparsers(help='sub-command help') parser.set_defaults(command=None) """ Command "set" """ setconfig = subparsers.add_parser('set', help='Set configuration') setconfig.add_argument( 'key', type=str, choices=["node", "rpcuser", "rpcpassword", "account" ], help='Configuration key' ) setconfig.add_argument( 'value', type=str, help='Configuration value' ) setconfig.set_defaults(command="set") """ Command "addkey" """ addkey = subparsers.add_parser('addkey', help='Add a new key to the wallet') addkey.add_argument( 'wifkeys', nargs='*', type=str, help='the private key in wallet import format (wif)' ) addkey.set_defaults(command="addkey") """ Command "listkeys" """ listkeys = subparsers.add_parser('listkeys', help='List available keys in your wallet') listkeys.set_defaults(command="listkeys") """ Command "listaccounts" """ listaccounts = subparsers.add_parser('listaccounts', help='List available accounts in your wallet') listaccounts.set_defaults(command="listaccounts") """ Command "getbalance" """ getbalance = subparsers.add_parser('getbalance', help='Get balances of available account(s)') getbalance.set_defaults(command="getbalance") getbalance.add_argument( 'account', type=str, nargs="*", default=[config["account"]], help='Accounts for which to retrieve the balance', ) """ Command "transfer" """ transfer = subparsers.add_parser('transfer', help='Transfer funds from your wallet to someone else') transfer.set_defaults(command="transfer") transfer.add_argument( '--from', type=str, help='Transfer from this account', default=config["account"], ) transfer.add_argument( '--to', type=str, help='Transfer to this account', required=True, ) transfer.add_argument( '--amount', type=str, help='Transfer this amount (format: "amount SYMBOL")', action="append", required=True, ) transfer.add_argument( '--memo', default='', type=str, help='Memo', ) """ Command "approve" """ approve = subparsers.add_parser('approve', help='approve funds from your wallet to someone else') approve.set_defaults(command="approve") approve.add_argument( '--account', type=str, help='Approve with this account', default=config["account"], ) approve.add_argument( 'proposal', type=str, help='Proposal to approve', ) """ Parse Arguments """ args = parser.parse_args() rpc_not_required = ["set", ""] if args.command not in rpc_not_required and args.command: rpc = GrapheneWebsocketRPC(args.node, args.rpcuser, args.rpcpassword) if args.command == "set": config[args.key] = args.value elif args.command == "addkey": wallet = Wallet(rpc) if len(args.wifkeys): for wifkey in args.wifkeys: pub = (wallet.addPrivateKey(wifkey)) if pub: print(pub) else: import getpass wifkey = "" while True: wifkey = getpass.getpass('Private Key (wif) [Enter to quit]:') if not wifkey: break pub = (wallet.addPrivateKey(wifkey)) if pub: print(pub) elif args.command == "listkeys": t = PrettyTable(["Available Key"]) t.align = "l" for key in Wallet(rpc).getPublicKeys(): t.add_row([key]) print(t) elif args.command == "listaccounts": t = PrettyTable(["Name", "Available Key"]) t.align = "l" for account in Wallet(rpc).getAccounts(): t.add_row(account) print(t) elif args.command == "getbalance": if args.account: accounts = [a for a in args.account] else: accounts = [a[0] for a in Wallet(rpc).getAccounts()] for account_name in accounts: account = rpc.get_account(account_name) t = PrettyTable(["Amount", "Asset"]) t.align = "l" balances = rpc.get_account_balances(account["id"], []) if not balances: continue print(account["name"] + ":") for balance in balances: asset = rpc.get_objects([balance["asset_id"]])[0] amount = int(balance["amount"]) / 10 ** asset["precision"] if amount: t.add_row([amount, asset["symbol"]]) print(t) elif args.command == "transfer": wallet = Wallet(rpc) ops = [] for amountStr in args.amount: amount, symbol = amountStr.split(" ") amount = float(amount) asset = rpc.get_asset(symbol) from_account = rpc.get_account(getattr(args, "from")) to_account = rpc.get_account(getattr(args, "to")) transferObj = { "fee": {"amount": 0, "asset_id": "1.3.0" }, "from": from_account["id"], "to": to_account["id"], "amount": {"amount": int(amount * 10 ** asset["precision"]), "asset_id": asset["id"] } } if args.memo: memo_key = wallet.getMemoKeyForAccount(getattr(args, "from")) if not memo_key: print("Missing memo private key!") return import random nonce = str(random.getrandbits(64)) encrypted_memo = memo.encode_memo(PrivateKey(memo_key), PublicKey(to_account["options"]["memo_key"]), nonce, args.memo) memoStruct = {"from": from_account["options"]["memo_key"], "to": to_account["options"]["memo_key"], "nonce": nonce, "message": encrypted_memo, "chain": "BTS"} transferObj["memo"] = transactions.Memo(**memoStruct) transfer = transactions.Transfer(**transferObj) ops.append(transactions.Operation(transfer)) wif = wallet.getActiveKeyForAccount(getattr(args, "from")) executeOps(ops, [wif]) elif args.command == "approve": wallet = Wallet(rpc) account = rpc.get_account(args.account) s = {'fee_paying_account': account["id"], 'proposal': args.proposal, 'active_approvals_to_add': [account["id"]], "fee": transactions.Asset(amount=0, asset_id="1.3.0"), } op = transactions.Proposal_update(**s) wif = wallet.getActiveKeyForAccount(args.account) executeOps([transactions.Operation(op)], [wif]) else: print("No valid command given")
# convert into a fraction denominator = int(10 ** base["precision"]) numerator = int(price * 10 ** base["precision"]) ops = [] for producer in producers: account = client.ws.get_account(producer) feed = transactions.PriceFeed(**{ "settlement_price" : transactions.Price( base=transactions.Asset(amount=numerator, asset_id=asset["id"]), quote=transactions.Asset(amount=denominator, asset_id=base["id"]), ), "core_exchange_rate" : transactions.Price( base=transactions.Asset(amount=numerator, asset_id=asset["id"]), quote=transactions.Asset(amount=denominator * scale_cer, asset_id="1.3.0"), ), "maximum_short_squeeze_ratio" : 1200, "maintenance_collateral_ratio" : 1100, }) pFeed = transactions.Asset_publish_feed( fee=transactions.Asset(amount=0, asset_id="1.3.0"), publisher=account["id"], asset_id=asset["id"], feed=feed ) ops.append(transactions.Operation(pFeed)) tx = constructSignedTransaction(ops) print(tx) print(client.ws.broadcast_transaction(transactions.JsonObj(tx), api="network_broadcast"))