def sell(self, account, amount, symbol, price): """Sell token for given price. :param str account: account name :param float amount: Amount to withdraw :param str symbol: symbol :param float price: price Sell example: .. code-block:: python from hiveengine.market import Market from beem import Steem active_wif = "5xxxx" stm = Steem(keys=[active_wif]) market = Market(blockchain_instance=stm) market.sell("test", 1, "BEE", 0.95) """ wallet = Wallet(account, api=self.api, blockchain_instance=self.blockchain) token_in_wallet = wallet.get_token(symbol) if token_in_wallet is None: raise TokenNotInWallet("%s is not in wallet." % symbol) if float(token_in_wallet["balance"]) < float(amount): raise InsufficientTokenAmount("Only %.3f in wallet" % float(token_in_wallet["balance"])) token = Token(symbol, api=self.api) quant_amount = token.quantize(amount) if quant_amount <= decimal.Decimal("0"): raise InvalidTokenAmount( "Amount to transfer is below token precision of %d" % token["precision"]) contract_payload = { "symbol": symbol.upper(), "quantity": str(quant_amount), "price": str(price) } json_data = { "contractName": "market", "contractAction": "sell", "contractPayload": contract_payload } assert self.blockchain.is_hive tx = self.blockchain.custom_json(self.ssc_id, json_data, required_auths=[account]) return tx
def transfer(self, to, amount, symbol, memo=""): """Transfer a token to another account. :param str to: Recipient :param float amount: Amount to transfer :param str symbol: Token to transfer :param str memo: (optional) Memo Transfer example: .. code-block:: python from hiveengine.wallet import Wallet from beem import Steem active_wif = "5xxxx" stm = Steem(keys=[active_wif]) wallet = Wallet("test", blockchain_instance=stm) wallet.transfer("test1", 1, "BEE", "test") """ token_in_wallet = self.get_token(symbol) if token_in_wallet is None: raise TokenNotInWallet("%s is not in wallet." % symbol) if float(token_in_wallet["balance"]) < float(amount): raise InsufficientTokenAmount("Only %.3f in wallet" % float(token_in_wallet["balance"])) token = Token(symbol, api=self.api) quant_amount = token.quantize(amount) if quant_amount <= decimal.Decimal("0"): raise InvalidTokenAmount( "Amount to transfer is below token precision of %d" % token["precision"]) check_to = Account(to, blockchain_instance=self.blockchain) contract_payload = { "symbol": symbol.upper(), "to": to, "quantity": str(quant_amount), "memo": memo } json_data = { "contractName": "tokens", "contractAction": "transfer", "contractPayload": contract_payload } assert self.blockchain.is_hive tx = self.blockchain.custom_json(self.ssc_id, json_data, required_auths=[self.account]) return tx
def issue(self, to, amount, symbol): """Issues a specific token amount. :param str to: Recipient :param float amount: Amount to issue :param str symbol: Token to issue Issue example: .. code-block:: python from hiveengine.wallet import Wallet from beem import Steem active_wif = "5xxxx" stm = Steem(keys=[active_wif]) wallet = Wallet("test", blockchain_instance=stm) wallet.issue(1, "my_token") """ token = Token(symbol, api=self.api) if token["issuer"] != self.account: raise TokenIssueNotPermitted("%s is not the issuer of token %s" % (self.account, symbol)) if token["maxSupply"] == token["supply"]: raise MaxSupplyReached("%s has reached is maximum supply of %d" % (symbol, token["maxSupply"])) quant_amount = token.quantize(amount) if quant_amount <= decimal.Decimal("0"): raise InvalidTokenAmount( "Amount to issue is below token precision of %d" % token["precision"]) check_to = Account(to, blockchain_instance=self.blockchain) contract_payload = { "symbol": symbol.upper(), "to": to, "quantity": str(quant_amount) } json_data = { "contractName": "tokens", "contractAction": "issue", "contractPayload": contract_payload } assert self.blockchain.is_hive tx = self.blockchain.custom_json(self.ssc_id, json_data, required_auths=[self.account]) return tx
def stake(self, amount, symbol, receiver=None): """Stake a token. :param float amount: Amount to stake :param str symbol: Token to stake Stake example: .. code-block:: python from hiveengine.wallet import Wallet from beem import Steem active_wif = "5xxxx" stm = Steem(keys=[active_wif]) wallet = Wallet("test", blockchain_instance=stm) wallet.stake(1, "BEE") """ token_in_wallet = self.get_token(symbol) if token_in_wallet is None: raise TokenNotInWallet("%s is not in wallet." % symbol) if float(token_in_wallet["balance"]) < float(amount): raise InsufficientTokenAmount("Only %.3f in wallet" % float(token_in_wallet["balance"])) token = Token(symbol, api=self.api) quant_amount = token.quantize(amount) if quant_amount <= decimal.Decimal("0"): raise InvalidTokenAmount( "Amount to stake is below token precision of %d" % token["precision"]) if receiver is None: receiver = self.account else: _ = Account(receiver, blockchain_instance=self.blockchain) contract_payload = { "symbol": symbol.upper(), "to": receiver, "quantity": str(quant_amount) } json_data = { "contractName": "tokens", "contractAction": "stake", "contractPayload": contract_payload } assert self.blockchain.is_hive tx = self.blockchain.custom_json(self.ssc_id, json_data, required_auths=[self.account]) return tx
def get_token(self, symbol): """Returns Token from given token symbol. Is None when token does not exists. """ for t in self: if t["symbol"].lower() == symbol.lower(): return Token(t, api=self.api) return None
def withdraw(self, account, amount): """Widthdraw SWAP.HIVE to account as HIVE. :param str account: account name :param float amount: Amount to withdraw Withdraw example: .. code-block:: python from hiveengine.market import Market from beem import Steem active_wif = "5xxxx" stm = Steem(keys=[active_wif]) market = Market(blockchain_instance=stm) market.withdraw("test", 1) """ wallet = Wallet(account, api=self.api, blockchain_instance=self.blockchain) token_in_wallet = wallet.get_token("SWAP.HIVE") if token_in_wallet is None: raise TokenNotInWallet("%s is not in wallet." % "SWAP.HIVE") if float(token_in_wallet["balance"]) < float(amount): raise InsufficientTokenAmount("Only %.3f in wallet" % float(token_in_wallet["balance"])) token = Token("SWAP.HIVE", api=self.api) quant_amount = token.quantize(amount) if quant_amount <= decimal.Decimal("0"): raise InvalidTokenAmount( "Amount to transfer is below token precision of %d" % token["precision"]) contract_payload = {"quantity": str(quant_amount)} json_data = { "contractName": "hivepegged", "contractAction": "withdraw", "contractPayload": contract_payload } assert self.blockchain.is_hive tx = self.blockchain.custom_json(self.ssc_id, json_data, required_auths=[account]) return tx
def test_token(self): eng = Token("BEE") self.assertTrue(eng is not None) self.assertTrue(eng["symbol"] == "BEE")