def send_tokens(token, account_to, quantity, memo): contract_accounts = {'EOS': 'eosio.token', 'KNYGA': 'knygarium111'} ce = Cleos(url=eos_endpoint) arguments = { "from": bartender_account, # sender "to": account_to, # receiver "quantity": str(quantity) + ' ' + token, # In Token "memo": memo, } payload = { "account": contract_accounts[token], "name": 'transfer', "authorization": [{ "actor": bartender_account, "permission": 'active', }], } # Converting payload to binary data = ce.abi_json_to_bin(payload['account'], payload['name'], arguments) # Inserting payload binary form as "data" field in original payload payload['data'] = data['binargs'] # final transaction formed trx = {"actions": [payload]} import datetime as dt trx['expiration'] = str( (dt.datetime.utcnow() + dt.timedelta(seconds=60)).replace(tzinfo=pytz.UTC)) key = eospy.keys.EOSKey(active_privat_key) resp = ce.push_transaction(trx, key, broadcast=True) return 'transaction_id' in resp.keys()
def __send_block(self, memo: str): ce = Cleos(url=eos_endpoint) arguments = { "from": self.sender_account, # sender "to": self.account, # receiver "quantity": '0.0001 EOS', # In Token "memo": memo, } payload = { "account": 'eosio.token', "name": 'transfer', "authorization": [{ "actor": self.sender_account, "permission": 'active', }], } # Converting payload to binary data = ce.abi_json_to_bin(payload['account'], payload['name'], arguments) # Inserting payload binary form as "data" field in original payload payload['data'] = data['binargs'] # final transaction formed trx = {"actions": [payload]} import datetime as dt trx['expiration'] = str( (dt.datetime.utcnow() + dt.timedelta(seconds=60)).replace(tzinfo=pytz.UTC)) key = eospy.keys.EOSKey(self.sender_privat_key) resp = ce.push_transaction(trx, key, broadcast=True) if ('transaction_id' in resp.keys()): return resp else: return ''
def send_tokens(token, account_to, quantity, memo): # contract_accounts = {'EOS': 'eosio.token', 'KNYGA': 'knygarium111'} contract_accounts = { 'TNT': 'eosio.token', 'EOS': 'eosio.token', 'KNYGA': 'knygarium111' } ce = Cleos(url=eos_endpoint) quantity_str = str(quantity) qs_start = quantity_str[:quantity_str.find('.')] qs_end = quantity_str[quantity_str.find('.'):] needs_0 = 5 - len(qs_end) if needs_0 < 0: qs_end = qs_end[:5] n = 0 while n < needs_0: n += 1 qs_end = qs_end + '0' quantity_str = qs_start + qs_end arguments = { "from": bartender_account, # sender "to": account_to, # receiver "quantity": quantity_str + ' ' + token, # In Token "memo": memo, } payload = { "account": contract_accounts[token], "name": 'transfer', "authorization": [{ "actor": bartender_account, "permission": 'active', }], } # Converting payload to binary data = ce.abi_json_to_bin(payload['account'], payload['name'], arguments) # Inserting payload binary form as "data" field in original payload payload['data'] = data['binargs'] # final transaction formed trx = {"actions": [payload]} import datetime as dt trx['expiration'] = str( (dt.datetime.utcnow() + dt.timedelta(seconds=60)).replace(tzinfo=pytz.UTC)) key = eospy.keys.EOSKey(active_privat_key) resp = ce.push_transaction(trx, key, broadcast=True) if ('transaction_id' in resp.keys()): return float(quantity_str) else: return 0
def send_tokens(self, account_to: str, amount: float, token: str, memo: str): ce = Cleos(url=self._api_endpoint) quantity_str = str(float(amount)) qs_start = quantity_str[:quantity_str.find('.')] qs_end = quantity_str[quantity_str.find('.'):] needs_0 = 5 - len(qs_end) if needs_0 < 0: qs_end = qs_end[:5] n = 0 while n < needs_0: n += 1 qs_end = qs_end + '0' quantity_str = qs_start + qs_end arguments = { "from": self._account, # sender "to": account_to, # receiver "quantity": quantity_str + ' ' + token, # In Token "memo": memo, } payload = { "account": self._currencies[token], "name": 'transfer', "authorization": [{ "actor": self._account, "permission": 'active', }], } data = ce.abi_json_to_bin(payload['account'], payload['name'], arguments) # Inserting payload binary form as "data" field in original payload payload['data'] = data['binargs'] # final transaction formed trx = {"actions": [payload]} import datetime as dt trx['expiration'] = str( (dt.datetime.utcnow() + dt.timedelta(seconds=60)).replace(tzinfo=pytz.UTC)) key = eospy.keys.EOSKey(self._active_privat_key) resp = ce.push_transaction(trx, key, broadcast=True) if 'transaction_id' in resp.keys(): print( f'{quantity_str} {token} sent to {account_to} with memo: {memo}' ) return float(quantity_str) else: print(f'error sending {quantity_str} {token} to {account_to}') return 0
def refund(action, amount, memo): ce = Cleos(url=eos_endpoint) quantity_str = str(amount) qs_start = quantity_str[:quantity_str.find('.')] qs_end = quantity_str[quantity_str.find('.'):] needs_0 = 5 - len(qs_end) if needs_0 < 0: qs_end = qs_end[:5] n = 0 while n < needs_0: n += 1 qs_end = qs_end + '0' quantity_str = qs_start + qs_end arguments = { "from": action['to'], # sender "to": action['from'], # receiver "quantity": quantity_str + ' ' + action['quantity'].split(' ')[1], # In Token "memo": memo, } payload = { "account": action['account'], "name": 'transfer', "authorization": [{ "actor": bartender_account, "permission": 'active', }], } # Converting payload to binary data = ce.abi_json_to_bin(payload['account'], payload['name'], arguments) # Inserting payload binary form as "data" field in original payload payload['data'] = data['binargs'] # final transaction formed trx = {"actions": [payload]} import datetime as dt trx['expiration'] = str( (dt.datetime.utcnow() + dt.timedelta(seconds=60)).replace(tzinfo=pytz.UTC)) key = eospy.keys.EOSKey(active_privat_key) resp = ce.push_transaction(trx, key, broadcast=True) return 'transaction_id' in resp.keys()
def grant_achievement(category_id, achievement_id, user_id): cleos = Cleos(url=settings.WAX_URL) # void grantach(name ecosystem_owner, uint32_t ecosystem_id, uint32_t user_id, uint32_t category_id, uint32_t achievement_id, uint32_t timestamp) arguments = { "ecosystem_owner": settings.WAX_ACCOUNT_NAME, "ecosystem_id": settings.WAXBADGES_ECOSYSTEM_ID, "user_id": user_id, "category_id": category_id, "achievement_id": achievement_id, "timestamp": math.trunc(time.time()) } payload = { "account": settings.WAX_CONTRACT, "name": "grantach", "authorization": [{ "actor": settings.WAX_ACCOUNT_NAME, "permission": "active", }], } #Converting payload to binary data = cleos.abi_json_to_bin(payload['account'], payload['name'], arguments) #Inserting payload binary form as "data" field in original payload payload['data'] = data['binargs'] #final transaction formed trx = {"actions": [payload]} trx['expiration'] = str( (dt.datetime.utcnow() + dt.timedelta(seconds=60)).replace(tzinfo=pytz.UTC)) key = eospy.keys.EOSKey(settings.WAX_PRIVATE_KEY) resp = cleos.push_transaction(trx, key, broadcast=True) return resp
def add_user(userid, avatarurl): cleos = Cleos(url=settings.WAX_URL) # void adduser(name ecosystem_owner, uint32_t ecosystem_id, string user_name, string userid, string avatarurl) arguments = { "ecosystem_owner": settings.WAX_ACCOUNT_NAME, "ecosystem_id": settings.WAXBADGES_ECOSYSTEM_ID, "user_name": userid, "userid": userid, "avatarurl": avatarurl } payload = { "account": settings.WAX_CONTRACT, "name": "adduser", "authorization": [{ "actor": settings.WAX_ACCOUNT_NAME, "permission": "active", }], } #Converting payload to binary data = cleos.abi_json_to_bin(payload['account'], payload['name'], arguments) #Inserting payload binary form as "data" field in original payload payload['data'] = data['binargs'] #final transaction formed trx = {"actions": [payload]} trx['expiration'] = str( (dt.datetime.utcnow() + dt.timedelta(seconds=60)).replace(tzinfo=pytz.UTC)) key = eospy.keys.EOSKey(settings.WAX_PRIVATE_KEY) resp = cleos.push_transaction(trx, key, broadcast=True) return resp
"ecosystem_id": ecosystem_id, "user_name": at_twitter_username, "userid": at_twitter_username, "avatarurl": avatarurl } payload = { "account": WAX_CONTRACT, "name": "adduser", "authorization": [{ "actor": wax_account_name, "permission": "active", }], } #Converting payload to binary data = cleos.abi_json_to_bin(payload['account'],payload['name'],arguments) #Inserting payload binary form as "data" field in original payload payload['data']=data['binargs'] #final transaction formed trx = {"actions": [payload]} trx['expiration'] = str((dt.datetime.utcnow() + dt.timedelta(seconds=60)).replace(tzinfo=pytz.UTC)) key = eospy.keys.EOSKey(wax_private_key) resp = cleos.push_transaction(trx, key, broadcast=True) print(resp) # Wait a moment for the data to percolate time.sleep(1)
class EosNetwork: def __init__(self, endpoint): self.endpoint = endpoint self.cleos = Cleos(url=endpoint) def deploy_contract(self, contract_info, account_info): setcode_action = self.create_setcode_action(contract_info['code_file'], account_info['name']) setabi_action = self.create_setabi_action(contract_info['abi_file'], account_info['name']) transaction = {'actions': [setcode_action, setabi_action]} key = EOSKey(account_info['private_key']) tx_result = self.cleos.push_transaction(transaction, key) return tx_result def create_setcode_action(self, code_file, account_name): file_handle = open(code_file, 'rb') file_data = file_handle.read() hex_data = hexlify(file_data) arguments = { 'account': account_name, 'code': hex_data.decode('utf-8'), 'vmtype': 0, 'vmversion': 0 } payload = { 'account': 'eosio', 'name': 'setcode', 'authorization': [{ 'actor': account_name, 'permission': 'active' }] } # Converting payload to binary data. data = self.cleos.abi_json_to_bin(payload['account'], payload['name'], arguments) # Inserting payload binary form as 'data' field in original payload. payload['data'] = data['binargs'] return payload def create_setabi_action(self, abi_file, account_name): file_handle = open(abi_file) file_data = json.load(file_handle) abi_data = Abi(file_data) arguments = {'account': account_name, 'abi': abi_data.get_raw()} payload = { 'account': 'eosio', 'name': 'setabi', 'authorization': [{ 'actor': account_name, 'permission': 'active' }] } # Converting payload to binary data. data = self.cleos.abi_json_to_bin(payload['account'], payload['name'], arguments) # Inserting payload binary form as 'data' field in original payload. payload['data'] = data['binargs'] return payload def read_data(self, code, scope, table_name): return self.cleos.get_table(code, scope, table_name) def write_data(self, contract_name, action_name, account_name, private_key, params): payload = { 'account': contract_name, 'name': action_name, 'authorization': [{ 'actor': account_name, 'permission': 'active' }] } print(params) data = self.cleos.abi_json_to_bin(payload['account'], payload['name'], params) payload['data'] = data['binargs'] print(payload) transaction = {'actions': [payload]} key = EOSKey(private_key) tx_result = self.cleos.push_transaction(transaction, key) return tx_result
class Metagraph(): def __init__(self, config): self.config = config self.cleos = Cleos(url=config.eosurl) self.nodes = {} self.pull_metagraph() self.attributions = [(config.identity, 1.0)] # TODO(shibshib) this should be our own key. NOT EOSMAIN. self.eoskey = "5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3" def get_my_stake(self): return int(self.nodes[self.config.identity].stake) # TODO(shibshib): pull this from the eos chain under the var 'total stake' # instead of doing a sum. def get_total_stake(self): return int(sum([node.stake for node in self.nodes.values()])) def pull_metagraph(self): table = self.cleos.get_table('bittensoracc', 'bittensoracc', 'metagraph') for entry in table['rows']: next_node = Node(entry) self.nodes[entry['identity']] = next_node logger.debug(self.__str__()) # Push attribution scores. def publish_attributions(self): logger.debug('Publish attributions: ' + str(self.attributions)) transaction = self.publish_attributions_trx() try: #import pdb #pdb.set_trace() # TODO (shibshib) Rewrite the cleos library for our selves. resp = self.cleos.push_transaction(transaction, self.eoskey, broadcast=True) except: try: eoskey = eospy.keys.EOSKey(self.eoskey) resp = self.cleos.push_transaction(transaction, eoskey, broadcast=True) except Exception as e: logger.error('Failed to publish transaction "{}"'.format(e)) def publish_attributions_trx(self): arguments = { "this_user": self.config.identity, "this_edges": [(attr[0], float(attr[1])) for attr in self.attributions], } payload = { "account": "bittensoracc", "name": "emit", "authorization": [{ "actor": self.config.identity, "permission": "active", }], } #Converting payload to binary data = self.cleos.abi_json_to_bin(payload['account'], payload['name'], arguments) #Inserting payload binary form as "data" field in original payload payload['data'] = data['binargs'] #final transaction formed trx = {"actions": [payload]} return trx def __repr__(self): return self.__str__() def __str__(self): str_rep = "\nmetagraph = {\n" for node in self.nodes.values(): str_rep += ("\t" + str(node) + "\n") str_rep += "}." return str_rep