def tx_close(tx, cursor): payload = json.loads(tx.payload) storage = models.Storage(tx.chain_id, payload['storage'], None, cursor) storage.active = False storage.save(cursor)
def createStorage(params): ''' :param params: Dictionary of elements to be inserted in Storage model :return: status code and message Status = 200 and message = records created successfully if data will be saved successfully to models Status = 200 and message = record already exist if record with same id already exists Status = 400 and message = error message if there is any error while inserting record ''' try: storageid = int(params['storageid']) stock = params['stock'] skuid = int(params['skuid']) if validateStorage(storageid, skuid): resp = { 'payload': {'message': 'Invalid record'}, 'status_code': 200 } else: newstorage = models.Storage(storageid, stock, skuid) models.db.session.add(newstorage) models.db.session.commit() resp = { 'payload': {'message': 'Record created successfully'}, 'status_code': 200 } except Exception as e: resp = { 'payload': {'message': str(e)}, 'status_code': 400 } return resp
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 = payload.get('extra', '{}') usage.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) host.balance += storage.hosting_fee host.save(cursor) request.delete(cursor)
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_setup(tx, cursor): payload = json.loads(tx.payload) payload['registration_fee'] = int(payload['registration_fee']) payload['hosting_fee'] = int(payload['hosting_fee']) owner = models.Account(tx.chain_id, tx.sender, cursor) storage = models.Storage(tx.chain_id, payload['storage'], owner.address, cursor) storage.url = payload['url'] storage.registration_fee = payload['registration_fee'] storage.hosting_fee = payload['hosting_fee'] storage.active = True storage.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_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) host = models.Account(tx.chain_id, storage.owner, cursor) parcel.custody = payload['custody'] if parcel.custody != None and len(parcel.custody) > 100: parcel.custody = parcel.custody[:100] parcel.proxy_account = payload.get('proxy_account', None) if parcel.proxy_account != None and len(parcel.proxy_account) > 40: parcel.proxy_account = parcel.proxy_account[:40] parcel.extra = payload.get('extra', '{}') parcel.on_sale = True parcel.save(cursor) owner.balance -= storage.registration_fee owner.save(cursor) host.balance += storage.registration_fee host.save(cursor)