def tx_grant(tx, cursor): payload = json.loads(tx.payload) grantee = '' if 'grantee' in payload: grantee = payload['grantee'] elif 'recipient' in payload: grantee = payload['recipient'] parcel = models.Parcel(tx.chain_id, payload['target'], None, cursor) storage = models.Storage(tx.chain_id, parcel.storage_id, None, cursor) host = models.Account(tx.chain_id, storage.owner, cursor) owner = models.Account(tx.chain_id, parcel.owner, cursor) # NOTE: This is line is for creating buyer account in s_accounts table. It # is necessary to satisfy the foreign key constraint. Even if we create a # row for each tx sender, it is not the case for the recipient for # `request` or `grant` tx. So, we need to explicitly create a grantee or # recipient account in s_accounts table. buyer = models.Account(tx.chain_id, grantee, cursor) buyer.save(cursor) request = models.Request(tx.chain_id, payload['target'], grantee, cursor) usage = models.Usage(tx.chain_id, payload['target'], grantee, cursor) usage.custody = payload['custody'] usage.extra = json.dumps(payload.get('extra', {})) usage.save(cursor) if tx.sender not in yappers: rel = models.RelParcelTx(tx.chain_id, parcel.parcel_id, tx.height, tx.index) rel.save(cursor) owner.balance += request.payment if request.dealer is not None: dealer = models.Account(tx.chain_id, request.dealer, cursor) dealer.balance += request.dealer_fee dealer.save(cursor) owner.balance -= storage.hosting_fee owner.save(cursor) if storage.hosting_fee > 0: rel = models.RelAccountTx(tx.chain_id, owner.address, tx.height, tx.index, cursor) rel.amount -= storage.hosting_fee rel.save(cursor) host.balance += storage.hosting_fee host.save(cursor) rel = models.RelAccountTx(tx.chain_id, host.address, tx.height, tx.index, cursor) rel.amount += storage.hosting_fee rel.save(cursor) request.delete(cursor)
def tx_retract(tx, cursor): payload = json.loads(tx.payload) payload['amount'] = int(payload['amount']) sender = models.Account(tx.chain_id, tx.sender, cursor) asset_stat = stats.Asset(tx.chain_id, cursor) sender.delegate -= payload['amount'] sender.balance += payload['amount'] del_addr = sender.del_addr if sender.delegate == 0: sender.del_addr = None sender.save(cursor) rel = models.RelAccountTx(tx.chain_id, tx.sender, tx.height, tx.index, cursor) rel.amount += payload['amount'] rel.save(cursor) delegatee = models.Account(tx.chain_id, del_addr, cursor) delegatee.eff_stake -= payload['amount'] delegatee.save(cursor) asset_stat.active_coins += payload['amount'] asset_stat.delegates -= payload['amount'] asset_stat.save(cursor)
def tx_request(tx, cursor): payload = json.loads(tx.payload) payload['payment'] = int(payload['payment']) payload['dealer_fee'] = int(payload.get('dealer_fee', '0')) buyer = models.Account(tx.chain_id, tx.sender, cursor) parcel = models.Parcel(tx.chain_id, payload['target'], None, cursor) request = models.Request(tx.chain_id, parcel.parcel_id, buyer.address, cursor) request.payment = payload['payment'] request.dealer = payload.get('dealer', None) request.dealer_fee = payload['dealer_fee'] request.extra = json.dumps(payload.get('extra', {})) request.save(cursor) if tx.sender not in yappers: rel = models.RelParcelTx(tx.chain_id, parcel.parcel_id, tx.height, tx.index) rel.save(cursor) if request.dealer is not None: buyer.balance -= request.dealer_fee buyer.balance -= request.payment buyer.save(cursor) rel = models.RelAccountTx(tx.chain_id, buyer.address, tx.height, tx.index, cursor) rel.amount -= request.payment rel.save(cursor)
def tx_transfer(tx, cursor): payload = json.loads(tx.payload) payload['amount'] = int(payload['amount']) sender = models.Account(tx.chain_id, tx.sender, cursor) sender.balance -= payload['amount'] sender.save(cursor) rel = models.RelAccountTx(tx.chain_id, tx.sender, tx.height, tx.index, cursor) rel.amount -= payload['amount'] rel.save(cursor) recp = models.Account(tx.chain_id, payload['to'], cursor) recp.balance += payload['amount'] recp.save(cursor) rel = models.RelAccountTx(tx.chain_id, payload['to'], tx.height, tx.index, cursor) rel.amount += payload['amount'] rel.save(cursor)
def tx_grant(tx, cursor): payload = json.loads(tx.payload) parcel = models.Parcel(tx.chain_id, payload['target'], None, cursor) storage = models.Storage(tx.chain_id, parcel.storage_id, None, cursor) host = models.Account(tx.chain_id, storage.owner, cursor) owner = models.Account(tx.chain_id, parcel.owner, cursor) request = models.Request(tx.chain_id, payload['target'], payload['grantee'], cursor) usage = models.Usage(tx.chain_id, payload['target'], payload['grantee'], cursor) usage.custody = payload['custody'] usage.extra = json.dumps(payload.get('extra', {})) usage.save(cursor) # rel = models.RelParcelTx(tx.chain_id, parcel.parcel_id, tx.height, # tx.index, cursor) # rel.save(cursor) owner.balance += request.payment if request.dealer is not None: dealer = models.Account(tx.chain_id, request.dealer, cursor) dealer.balance += request.dealer_fee dealer.save(cursor) owner.balance -= storage.hosting_fee owner.save(cursor) if storage.hosting_fee > 0: rel = models.RelAccountTx(tx.chain_id, owner.address, tx.height, tx.index, cursor) rel.amount -= storage.hosting_fee rel.save(cursor) host.balance += storage.hosting_fee host.save(cursor) rel = models.RelAccountTx(tx.chain_id, host.address, tx.height, tx.index, cursor) rel.amount += storage.hosting_fee rel.save(cursor) request.delete(cursor)
def tx_register(tx, cursor): payload = json.loads(tx.payload) owner = models.Account(tx.chain_id, tx.sender, cursor) parcel = models.Parcel(tx.chain_id, payload['target'], owner.address, cursor) storage = models.Storage(tx.chain_id, parcel.storage_id, None, cursor) parcel.custody = payload['custody'] if parcel.custody is not None and len(parcel.custody) > 100: parcel.custody = parcel.custody[:100] parcel.proxy_account = payload.get('proxy_account', None) if parcel.proxy_account is not None and len(parcel.proxy_account) > 40: parcel.proxy_account = parcel.proxy_account[:40] parcel.extra = json.dumps(payload.get('extra', {})) parcel.on_sale = True parcel.save(cursor) if tx.sender not in yappers: rel = models.RelParcelTx(tx.chain_id, parcel.parcel_id, tx.height, tx.index) rel.save(cursor) if storage.registration_fee > 0: owner.balance -= storage.registration_fee owner.save(cursor) rel = models.RelAccountTx(tx.chain_id, owner.address, tx.height, tx.index, cursor) rel.amount -= storage.registration_fee rel.save(cursor) host = models.Account(tx.chain_id, storage.owner, cursor) host.balance += storage.registration_fee host.save(cursor) rel = models.RelAccountTx(tx.chain_id, host.address, tx.height, tx.index, cursor) rel.amount += storage.registration_fee rel.save(cursor)
def tx_cancel(tx, cursor): payload = json.loads(tx.payload) buyer = models.Account(tx.chain_id, tx.sender, cursor) request = models.Request(tx.chain_id, payload['target'], tx.sender, cursor) buyer.balance += request.payment buyer.balance += request.dealer_fee buyer.save(cursor) rel = models.RelAccountTx(tx.chain_id, buyer.address, tx.height, tx.index, cursor) rel.amount += request.payment + request.dealer_fee rel.save(cursor) request.delete(cursor)
def play_events(self, cursor): for ev in self.events: ev = util.parse_event(ev) # TODO: refactor if ev['type'] == 'draft': draft = models.Draft(self.chain_id, int(ev['attr']['id']), None, cursor) util.from_dict(draft, json.loads(ev['attr']['draft'])) draft.deposit = int(draft.deposit) draft.tally_quorum = int(draft.tally_quorum) draft.tally_approve = int(draft.tally_approve) draft.tally_reject = int(draft.tally_reject) draft.save(cursor) # TODO: use another events regarding balance change proposer = models.Account(self.chain_id, draft.proposer, cursor) proposer.balance -= draft.deposit proposer.save(cursor) rel = models.RelAccountTx(self.chain_id, draft.proposer, self.height, self.index, cursor) rel.amount -= draft.deposit rel.save(cursor)
def tx_withdraw(tx, cursor): payload = json.loads(tx.payload) payload['amount'] = int(payload['amount']) sender = models.Account(tx.chain_id, tx.sender, cursor) sender.stake -= payload['amount'] sender.eff_stake -= payload['amount'] sender.balance += payload['amount'] if sender.stake == 0: sender.val_addr = None sender.val_pubkey = None sender.val_power = 0 sender.save(cursor) rel = models.RelAccountTx(tx.chain_id, tx.sender, tx.height, tx.index, cursor) rel.amount += payload['amount'] rel.save(cursor) asset_stat = stats.Asset(tx.chain_id, cursor) asset_stat.active_coins += payload['amount'] asset_stat.stakes -= payload['amount'] asset_stat.save(cursor)
def tx_stake(tx, cursor): payload = json.loads(tx.payload) payload['amount'] = int(payload['amount']) sender = models.Account(tx.chain_id, tx.sender, cursor) sender.stake += payload['amount'] sender.stake_locked += payload['amount'] sender.eff_stake += payload['amount'] sender.balance -= payload['amount'] sender.val_pubkey = payload['validator'] b = bytearray.fromhex(sender.val_pubkey) sender.val_addr = sha256(b).hexdigest()[:40].upper() sender.save(cursor) rel = models.RelAccountTx(tx.chain_id, tx.sender, tx.height, tx.index, cursor) rel.amount -= payload['amount'] rel.save(cursor) asset_stat = stats.Asset(tx.chain_id, cursor) asset_stat.active_coins -= payload['amount'] asset_stat.stakes += payload['amount'] asset_stat.save(cursor)
def tx_transfer(tx, cursor): payload = json.loads(tx.payload) # NOTE: This is line is for creating buyer account in s_accounts table. It # is necessary to satisfy the foreign key constraint. Even if we create a # row for each tx sender, it is not the case for the recipient for # `request` or `grant` tx. So, we need to explicitly create a grantee or # recipient account in s_accounts table. recp = models.Account(tx.chain_id, payload['to'], cursor) recp.save(cursor) if payload.get('parcel'): owner = models.Account(tx.chain_id, tx.sender, cursor) parcel = models.Parcel(tx.chain_id, payload['target'], owner.address, cursor) recp = models.Account(tx.chain_id, payload['to'], cursor) parcel.owner = recp.address parcel.save(cursor) if tx.sender not in yappers: rel = models.RelParcelTx(tx.chain_id, parcel.parcel_id, tx.height, tx.index) rel.save(cursor) else: amount = int(payload['amount']) udc = payload.get('udc') if udc is None: sender = models.Account(tx.chain_id, tx.sender, cursor) sender.balance -= amount sender.save(cursor) rel = models.RelAccountTx(tx.chain_id, tx.sender, tx.height, tx.index, cursor) rel.amount -= amount rel.save(cursor) recp = models.Account(tx.chain_id, payload['to'], cursor) recp.balance += amount recp.save(cursor) rel = models.RelAccountTx(tx.chain_id, payload['to'], tx.height, tx.index, cursor) rel.amount += amount rel.save(cursor) if recp.address == '000000000000000000000000000000000000DEAD': # This account is used for ad-hoc burning process, the balance # of this account is considered out of normal operation from # now. So let's decrease the amount of active coins # permanently. asset_stat = stats.Asset(tx.chain_id, cursor) asset_stat.active_coins -= amount asset_stat.save(cursor) else: sender = models.UDCBalance(tx.chain_id, udc, tx.sender, cursor) sender.balance -= amount sender.save(cursor) rel = models.RelBalanceTx(tx.chain_id, udc, tx.sender, tx.height, tx.index, cursor) rel.amount -= amount rel.save(cursor) recp = models.UDCBalance(tx.chain_id, udc, payload['to'], cursor) recp.balance += amount recp.save(cursor) rel = models.RelBalanceTx(tx.chain_id, udc, payload['to'], tx.height, tx.index, cursor) rel.amount += amount rel.save(cursor)