def trades_claim(tradeId): trade = Database.find_one("Trade", {"id": int(tradeId)}) decrypted = Cryptor.decryptInput(trade['nominalAmount'], trade['sk']) price = Cryptor.decryptInput(trade['price'], trade['sk']) # Ill need to include the currency here amountInvested = int(decrypted.split(':')[1]) if trade['state'] != 5: return {'message': 'Trade is in state {}, requires state 5'.format(trade['state'])} # get trade dates nav nav = Database.find_one("NAVTimestamp", {"tokenId": trade["tokenId"]}, order_by='-createdAt') # get todays nav and totalSupply token = Database.find_one('Token', {'id': trade['tokenId']}) totalTokens = int(amountInvested / nav['price']) investorTokens = None if amountInvested > 0: effectiveNAV = (1.0+float(price)/100)*nav['price'] investorTokens = amountInvested / effectiveNAV else: investorTokenBalance = Database.find_one("TokenBalance", {"tokenId": token["id"], "userId": investor["id"]}) if not investorTokenBalance['balance']: investorTokenBalance['balance'] = '0' effectiveNAV = (1-float(price)/100)*nav['price'] investorTokens = min(int(amountInvested / effectiveNAV), -1 * int(investorTokenBalance['balance'])) tokenContract = Web3Helper.getContract("ETT.json", token['address']) totalSupply = Web3Helper.call(tokenContract,'dateTotalSupply',arrow.get(trade['executionDate']).format('YYYY-MM-DD'),) # find number of tokens user allocated numberTokens = 0 if not nav['price'] else math.floor(amountInvested * totalSupply / nav['price']) investorTokens = int(investorTokens * math.pow(10, token['decimals'])) # now ask the contract to distribute the tokens (maybe should be the investor that does this) investor = Database.find_one("User", {"id": trade["investorId"]}) tradeKernelContract = Web3Helper.getContract("TradeKernel.json") tx = Web3Helper.transact( tradeKernelContract, 'distributeTokens', trade['hash'], [ Web3Helper.toChecksumAddress(token['address']), Web3Helper.toChecksumAddress(investor['address']), ], [ investorTokens ] ) print(tx.hex()) Database.update("Trade", {"id": trade["id"]}, {"state": 6, "numberTokens": numberTokens}) return {"message": "Tokens distributed"}
def user_put(userId): request = app.current_request data = request.json_body if 'address' in data: # Find if another user # requestingUser = request.user data['address'] = Web3Helper.toChecksumAddress(data['address']) # user = Database.find_one("User", {'address': data['address']}) # if user and user['id'] != requestingUser['id']: # raise ForbiddenError('user already exists with address {}'.format(data['address'])) user = Database.find_one("User", {'id': int(userId)}) if not user: raise NotFoundError('user not found with id {}'.format(userId)) user = Database.update('User', {'id': user['id']}, data, return_updated=True)[0] if 'address' in data: # Set user athorized as investor tx = Web3Helper.transact(permissions_contract, 'setAuthorized', data['address'], 1) return toObject( user, ['id', 'name', 'address', 'role', 'ik', 'spk', 'signature'])
def tokens_contract_update(): request = app.current_request data = request.json_body Database.update( "Token", {"symbol": data["symbol"]}, { "address": Web3Helper.toChecksumAddress(data["tokenAddress"]), }) token = Database.find_one("Token", {"symbol": data["symbol"]}) owner = Database.find_one( "User", {"address": Web3Helper.toChecksumAddress(data["owner"])}) tokenBalance = Database.find_one("TokenBalance", { "tokenId": token['id'], "userId": owner["id"] }) if not tokenBalance: tokenBalance = Database.find_one("TokenBalance", { "tokenId": token['id'], "userId": owner["id"], "balance": data['initialAmount'] }, insert=True) return toObject(token)
def createToken(data): owner = None ownerId = data["ownerId"] if "ownerId" in data else None if not ownerId: owner = Database.find_one("User", {"address": Web3Helper.account()}) ownerId = owner["id"] else: owner = Database.find_one("User", {"id": ownerId}) if not owner: raise NotFoundError('No user found') token_data = { "fundId": data["fundId"], "decimals": data["decimals"], "symbol": data["symbol"], "cutoffTime": int(data["cutoffTime"]), "fee": int(data["fee"]), "ownerId": ownerId, "name": data["name"], "totalSupply": data["initialAmount"], "incomeCategory": data["incomeCategory"], "minimumOrder": data["minimumOrder"], "currency": data["minimumOrder"], } if "fundId" in data: token_data["fundId"] = data["fundId"] # Check if a token with the same symbol already exists symbolToken = Database.find_one("Token", {"symbol": token_data["symbol"]}) if (symbolToken): raise ForbiddenError("Already a token with this symbol") token = Database.find_one("Token", token_data, insert=True) tokenHoldings = Database.find_one( "TokenHoldings", { "tokenId": token['id'], "executionDate": str(datetime.today().date()) }, insert=True) if 'nav' in data: # Create a NAV timestamp executionDate = arrow.now().format('YYYY-MM-DD') Database.insert( "NAVTimestamp", { "tokenId": token['id'], "price": data['nav'], "executionDate": executionDate }) holdings = [] if 'holdings' in data: for holding in data['holdings']: # get the correct security security = Database.find_one("Security", {"symbol": holding["symbol"]}) if (security): Database.update("Security", {"id": security["id"]}, { "name": holding["name"], "currency": holding["currency"], "country": holding["country"], "sector": holding["sector"], "class": holding["class"] }) else: security = Database.find_one("Security", { "symbol": holding["symbol"], "name": holding["name"], "currency": holding["currency"], "country": holding["country"], "sector": holding["sector"], "class": holding["class"] }, insert=True) price = holding['price'] if 'price' in holding else '0' securityTimestamp = Database.find_one('SecurityTimestamp', { 'securityId': security["id"], 'price': price }, order_by='-createdAt', insert=True) tokenHolding = Database.find_one( "TokenHolding", { "securityId": security["id"], "securityAmount": holding["amount"], "tokenHoldingsId": tokenHoldings["id"] }, insert=True) holdings.append(tokenHolding) holdingsString = createHoldingsString(holdings) try: tx = Web3Helper.transact( token_factory_contract, 'createETT', token_data["name"], token_data["decimals"], token_data["symbol"], int(data["initialAmount"]), holdingsString, token_data["cutoffTime"], token_data["fee"], Web3Helper.toChecksumAddress(owner['address']), ) print(tx.hex()) except Exception as e: print(e) pass