예제 #1
0
  def funds_post():
    request = app.current_request
    data = request.json_body
    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')

    fund_data = {
      "name": data["name"],
      "ownerId": ownerId
    }

    fund = Database.find_one('Fund', fund_data, insert=True)

    for token in data["tokens"]:
      token_data = {
        "name": data["name"],
        "fundId": fund["id"],
        "decimals": token["decimals"],
        "symbol": token["symbol"],
        "cutoffTime": int(token["cutoffTime"]),
        "fee": int(token["fee"]),
        "ownerId": ownerId,
        "currency": token["currency"],
        "initialAmount": token["initialAmount"],
        "incomeCategory": token["incomeCategory"],
        "minimumOrder": token["minimumOrder"]
      }
      if "nav" in token: token_data["nav"] = token['nav']
      if "holdings" in token: token_data["holdings"] = token["holdings"]
      token = createToken(token_data)

    return toObject(fund)
예제 #2
0
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