def test_market_offers_selling(marketaccounts): alice = marketaccounts.alice bob = marketaccounts.bob market.sell(3, bob.account1, bob.account2, price=7) time.sleep(cron_interval) server_id = bob.account1.server_id message = pyopentxs.otme.get_market_list(server_id, alice.account1.nym._id) assert pyopentxs.is_message_success(message) obj = opentxs.QueryObject(opentxs.STORED_OBJ_MARKET_LIST, "markets", server_id, "market_data.bin") market_list = opentxs.MarketList_ot_dynamic_cast(obj) market_data_count = market_list.GetMarketDataCount() assert market_data_count >= 1 market_data = [market_list.GetMarketData(m) for m in range(market_data_count)] matching_markets = list(filter(lambda m: (bob.account1.asset._id == m.instrument_definition_id and bob.account2.asset._id == m.currency_type_id), market_data)) assert len(matching_markets) == 1 c = matching_markets[0] assert bob.account1.asset._id == c.instrument_definition_id assert bob.account2.asset._id == c.currency_type_id assert '1' == c.scale assert '3' == c.total_assets assert '0' == c.number_bids assert '1' == c.number_asks assert '1' == c.last_sale_price assert '0' == c.current_bid assert '7' == c.current_ask assert '0' == c.volume_trades assert '0' == c.volume_assets assert '0' == c.volume_currency assert '0' == c.recent_highest_bid assert '0' == c.recent_lowest_ask assert '' == c.last_sale_date message = pyopentxs.otme.get_market_offers(server_id, alice.account1.nym._id, c.market_id, 20) assert pyopentxs.is_message_success(message) obj = opentxs.QueryObject(opentxs.STORED_OBJ_OFFER_LIST_MARKET, "markets", server_id, c.market_id + ".bin") tradeList = opentxs.OfferListMarket_ot_dynamic_cast(obj) assert 1 == tradeList.GetAskDataCount() assert 0 == tradeList.GetBidDataCount() c = tradeList.GetAskData(0) assert '' == c.gui_label # assert '' == c.transaction_id assert '7' == c.price_per_scale assert '3' == c.available_assets assert '1' == c.minimum_increment
def withdraw(self): """ Withdraw voucher """ message = otme.withdraw_voucher( self.server_id, self.sender_nym._id, self.sender_account._id, self.recipient_nym and self.recipient_nym._id or "", self.memo, self.amount) assert is_message_success(message) ledger = opentxs.OTAPI_Wrap_Message_GetLedger(message) transaction = opentxs.OTAPI_Wrap_Ledger_GetTransactionByIndex( self.server_id, self.sender_nym._id, self.sender_account._id, ledger, 0) output = opentxs.OTAPI_Wrap_Transaction_GetVoucher( self.server_id, self.sender_nym._id, self.sender_account._id, transaction) if output == '': raise ReturnValueError(output) self._body = output # save a copy for myself in outpayments box, so i can cancel later otme.send_user_payment(self.server_id, self.sender_nym._id, self.sender_nym._id, output) return output
def issue(self, nym=None, contract_stream=None, server_id=None, issue_for_nym=None): '''Issues a new asset type on the given server and nym. contract should be a string with the contract contents. nym must be registered. issue_for_nym is the nym to try to create the issuer account as (OT shouldn't allow if this isn't the same as the issuer nym) - for testing purposes ''' # first create the contract if necessary self.server_id = self.server_id or server_id or server.first_active_id( ) assert self.server_id if not self._id: self.create_contract(nym, contract_stream) signed_contract = opentxs.OTAPI_Wrap_GetAssetType_Contract(self._id) message = otme.issue_asset_type(self.server_id, (issue_for_nym and issue_for_nym._id) or nym._id, signed_contract) assert is_message_success(message) account_id = opentxs.OTAPI_Wrap_Message_GetNewIssuerAcctID(message) self.issuer_account = Account(asset=self, nym=self.issuer, server_id=self.server_id, _id=account_id) return self
def test_deposit_twice(self, prepared_accounts, amount, first_valid, later_income, second_valid): # create cheque and try to deposit it cheque = new_cheque(prepared_accounts.source, prepared_accounts.target, amount) with error.expected(None if first_valid else ReturnValueError): transfer(cheque, prepared_accounts.source, prepared_accounts.target) expected_source = 100 expected_target = 0 if (first_valid): expected_source -= amount expected_target += amount prepared_accounts.assert_balances(-100, expected_source, expected_target) # now transfer more funds to source if later_income != 0: income = new_cheque(prepared_accounts.issuer, prepared_accounts.source, later_income) transfer(income, prepared_accounts.issuer, prepared_accounts.source) expected_source += later_income # and repeat cheque deposit with error.expected(None if second_valid else ReturnValueError): deposit = cheque.deposit(prepared_accounts.target.nym, prepared_accounts.target) if second_valid: expected_source -= amount expected_target += amount assert is_message_success(deposit) prepared_accounts.assert_balances(-100 - later_income, expected_source, expected_target)
def send_transfer(server_id=None, acct_from=None, acct_to=None, note=None, amount=None): server_id = server_id or server.only_id() print("transferring {} from {} to {} on {}".format(amount, acct_from, acct_to, server_id)) message = otme.send_transfer(server_id, acct_from.nym._id, acct_from._id, acct_to._id, amount, note) assert is_message_success(message) # accept all inbox items in target account assert otme.accept_inbox_items(acct_to._id, 0, "") return message
def send_transfer(server_id=None, acct_from=None, acct_to=None, note=None, amount=None): server_id = server_id or server.first_id() # print("transferring {} from {} to {} on {}".format(amount, acct_from, acct_to, server_id)) message = otme.send_transfer(server_id, acct_from.nym._id, acct_from._id, acct_to._id, amount, note) assert is_message_success(message) # accept all inbox items in target account assert otme.accept_inbox_items(acct_to._id, 0, "") return message
def register(self, server_id=None): '''Registers the nym with the given server. If there is no nym id yet (this object is still empty), the nym data will be created with defaults first, and then registered to the server. Returns the nym object. ''' server_id = server_id or self.server_id or server.first_active_id() assert server_id, "Can't register a nym without a server id.'" self.server_id = server_id if not self._id: self.create() message = otme.register_nym(server_id, self._id) assert is_message_success(message) return self
def deposit(self, depositor_nym, depositor_account): '''Deposit the cheque, getting a written copy from the server first if we don't have one. The reason for having both the nym and account is that the server asks for both and we can test its behavior when they don't match. ''' if not self._body: self.write() result = otme.deposit_cheque(self.server_id, depositor_nym._id, depositor_account._id, self._body) # print("Deposit: %s" % result) assert is_message_success(result) # otme.accept_inbox_items(depositor_account._id, 0, "") return result
def deposit(self, depositor_nym, depositor_account): '''Deposit the cheque, getting a written copy from the server first if we don't have one. The reason for having both the nym and account is that the server asks for both and we can test its behavior when they don't match. ''' if not self._body: self.write() result = otme.deposit_cheque(self.server_id, depositor_nym._id, depositor_account._id, self._body) print("Deposit: %s" % result) assert is_message_success(result) # otme.accept_inbox_items(depositor_account._id, 0, "") return result
def withdraw(self): """ Withdraw voucher """ message = otme.withdraw_voucher(self.server_id, self.sender_nym._id, self.sender_account._id, self.recipient_nym._id, self.memo, self.amount) assert is_message_success(message) ledger = opentxs.OTAPI_Wrap_Message_GetLedger(message) transaction = opentxs.OTAPI_Wrap_Ledger_GetTransactionByIndex( self.server_id, self.sender_nym._id, self.sender_account._id, ledger, 0) output = opentxs.OTAPI_Wrap_Transaction_GetVoucher(self.server_id, self.sender_nym._id, self.sender_account._id, transaction) if output == '': raise ReturnValueError(output) self._body = output # save a copy for myself in outpayments box, so i can cancel later otme.send_user_payment(self.server_id, self.sender_nym._id, self.sender_nym._id, output) return output
def issue(self, nym=None, contract_stream=None, server_id=None, issue_for_nym=None): '''Issues a new asset type on the given server and nym. contract should be a string with the contract contents. nym must be registered. issue_for_nym is the nym to try to create the issuer account as (OT shouldn't allow if this isn't the same as the issuer nym) - for testing purposes ''' # first create the contract if necessary self.server_id = self.server_id or server_id or server.only_id() assert self.server_id if not self._id: self.create_contract(nym, contract_stream) signed_contract = opentxs.OTAPI_Wrap_GetAssetType_Contract(self._id) message = otme.issue_asset_type(self.server_id, (issue_for_nym and issue_for_nym._id) or nym._id, signed_contract) assert is_message_success(message) account_id = opentxs.OTAPI_Wrap_Message_GetNewIssuerAcctID(message) self.issuer_account = Account(asset=self, nym=self.issuer, server_id=self.server_id, _id=account_id) return self
def test_get_nym_market_offers_buying(marketaccounts): # alice = marketaccounts.alice bob = marketaccounts.bob server_id = bob.account1.server_id market.buy(3, bob.account1, bob.account2, price=7) time.sleep(cron_interval) message = pyopentxs.otme.get_nym_market_offers(server_id, bob.nym._id) assert pyopentxs.is_message_success(message) obj = opentxs.QueryObject(opentxs.STORED_OBJ_OFFER_LIST_NYM, "nyms", server_id, bob.nym._id + ".bin") offerList = opentxs.OfferListNym_ot_dynamic_cast(obj) assert 1 == offerList.GetOfferDataNymCount() c = offerList.GetOfferDataNym(0) assert "" == c.gui_label # assert "" == c.valid_from # assert "" == c.valid_to assert server_id == c.notary_id assert bob.account1.asset._id == c.instrument_definition_id assert bob.account1._id == c.asset_acct_id assert bob.account2.asset._id == c.currency_type_id assert bob.account2._id == c.currency_acct_id assert False == c.selling assert "1" == c.scale assert "7" == c.price_per_scale # assert "" == c.transaction_id assert "3" == c.total_assets assert "0" == c.finished_so_far assert "1" == c.minimum_increment assert "" == c.stop_sign assert "0" == c.stop_price
def send(self): if not self._body: self.write() result = otme.send_user_payment(self.server_id, self.sender_nym._id, self.recipient_nym._id, self._body) return is_message_success(result)
def test_market_offers_selling(marketaccounts): alice = marketaccounts.alice bob = marketaccounts.bob market.sell(3, bob.account1, bob.account2, price=7) time.sleep(cron_interval) server_id = bob.account1.server_id message = pyopentxs.otme.get_market_list(server_id, alice.account1.nym._id) assert pyopentxs.is_message_success(message) obj = opentxs.QueryObject(opentxs.STORED_OBJ_MARKET_LIST, "markets", server_id, "market_data.bin") market_list = opentxs.MarketList_ot_dynamic_cast(obj) market_data_count = market_list.GetMarketDataCount() assert market_data_count >= 1 market_data = [ market_list.GetMarketData(m) for m in range(market_data_count) ] matching_markets = list( filter( lambda m: (bob.account1.asset._id == m.instrument_definition_id and bob.account2.asset._id == m.currency_type_id), market_data)) assert len(matching_markets) == 1 c = matching_markets[0] assert bob.account1.asset._id == c.instrument_definition_id assert bob.account2.asset._id == c.currency_type_id assert '1' == c.scale assert '3' == c.total_assets assert '0' == c.number_bids assert '1' == c.number_asks assert '1' == c.last_sale_price assert '0' == c.current_bid assert '7' == c.current_ask assert '0' == c.volume_trades assert '0' == c.volume_assets assert '0' == c.volume_currency assert '0' == c.recent_highest_bid assert '0' == c.recent_lowest_ask assert '' == c.last_sale_date message = pyopentxs.otme.get_market_offers(server_id, alice.account1.nym._id, c.market_id, 20) assert pyopentxs.is_message_success(message) obj = opentxs.QueryObject(opentxs.STORED_OBJ_OFFER_LIST_MARKET, "markets", server_id, c.market_id + ".bin") tradeList = opentxs.OfferListMarket_ot_dynamic_cast(obj) assert 1 == tradeList.GetAskDataCount() assert 0 == tradeList.GetBidDataCount() c = tradeList.GetAskData(0) assert '' == c.gui_label # assert '' == c.transaction_id assert '7' == c.price_per_scale assert '3' == c.available_assets assert '1' == c.minimum_increment
def deposit(self, depositor_nym, depositor_account): '''Deposit the cheque, getting a written copy from the server first if we don't have one.''' deposit = otme.deposit_cheque(self.server_id, depositor_nym._id, depositor_account._id, self._body) assert is_message_success(deposit) return deposit