|
1 | 1 | import { Request, Response } from 'express' |
2 | 2 |
|
3 | | -import HttpError from '../lib/HttpError' |
| 3 | +import { decimal } from '../helpers/numbers' |
| 4 | + |
| 5 | +import Asset from '../models/Asset' |
| 6 | +import HttpError from '../models/HttpError' |
4 | 7 | import Organization from '../models/Organization' |
| 8 | +import OrganizationBalance from '../models/OrganizationBalance' |
| 9 | + |
| 10 | +const TARGET = decimal(100e6) // 100M USD |
| 11 | +const REWARD = decimal(100e3) // 100k ANT |
| 12 | +const OPTIONS = decimal(1e6) // 1M |
| 13 | +const CAP_PRICE = decimal(0.1) // 0.1 ANT |
5 | 14 |
|
6 | 15 | export default { |
7 | 16 | async all(request: Request, response: Response) { |
8 | | - const total = await Organization.count() |
9 | | - const value = await Organization.totalAntValue() |
10 | | - response.status(200).send({ total, value }) |
| 17 | + const count = await Organization.count() |
| 18 | + const last = (await Organization.last())?.createdAt || 0 |
| 19 | + const total = await Organization.totalValue() |
| 20 | + const optionPrice = decimal(total).div(TARGET).mul(REWARD.div(OPTIONS)) |
| 21 | + const option = optionPrice > CAP_PRICE ? CAP_PRICE : optionPrice |
| 22 | + response.status(200).send({ count, last, total, option }) |
11 | 23 | }, |
12 | 24 |
|
13 | 25 | async show(request: Request, response: Response) { |
14 | 26 | const organization = await Organization.findByAddress(request.params.address) |
15 | 27 | if (!organization) throw HttpError.NOT_FOUND('Organization not found') |
16 | | - const { createdAt, address, executor, valueUSD, valueANT, ant } = organization |
17 | | - response.status(200).send({ createdAt, address, executor, valueUSD, valueANT, ant }) |
| 28 | + |
| 29 | + const orgBalances = await OrganizationBalance.findByOrganization(organization) |
| 30 | + const balances = Promise.all(orgBalances.map(async ({ assetId, price, amount, value }) => { |
| 31 | + const asset = await Asset.findById(assetId) |
| 32 | + return { asset: { address: asset?.address, symbol: asset?.symbol, decimals: asset?.decimals }, price, amount, value } |
| 33 | + })); |
| 34 | + |
| 35 | + const { createdAt, address, executor, value, syncedAt } = organization |
| 36 | + response.status(200).send({ createdAt, address, executor, value, syncedAt, balances }) |
18 | 37 | } |
19 | 38 | } |
0 commit comments