/
ArchiveController.ts
83 lines (65 loc) · 2.27 KB
/
ArchiveController.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import * as Pino from 'pino'
import { PoetNode } from '../daos/PoetNodeDao'
import { FileTooBig, PoeAddressNotVerified, PoeBalanceInsufficient } from '../errors/errors'
import { Erc20 } from '../helpers/Erc20'
import { Network } from '../interfaces/Network'
import { Account } from '../models/Account'
export interface ArchiveController {
readonly postArchive: (
account: Account,
archive: ReadableStream,
network: Network,
fileSize: number,
) => Promise<any>
}
interface Arguments {
readonly dependencies: Dependencies
readonly configuration: Configuration
}
interface Dependencies {
readonly logger: Pino.Logger
readonly mainnetNode: PoetNode
readonly testnetNode: PoetNode
}
interface Configuration {
readonly ethereumUrl: string
readonly poeContractDecimals: number
readonly poeContractAddress: string
readonly poeBalanceMinimum: number
readonly maximumFileSizeInBytes: number
}
export const ArchiveController = ({
dependencies: {
logger,
mainnetNode,
testnetNode,
},
configuration: {
ethereumUrl,
poeContractDecimals,
poeContractAddress,
poeBalanceMinimum,
maximumFileSizeInBytes,
},
}: Arguments): ArchiveController => {
logger.info({ poeContractAddress, poeBalanceMinimum }, 'ArchiveController Instantiated')
const poeContract = Erc20({ ethereumUrl, contractAddress: poeContractAddress })
const networkToNode = (network: Network) => network === Network.LIVE ? mainnetNode : testnetNode
const postArchive = async (account: Account, archive: ReadableStream, network: Network, fileSize: number) => {
if (fileSize > maximumFileSizeInBytes)
throw new FileTooBig(fileSize, maximumFileSizeInBytes)
const node = networkToNode(network)
const { id: userId, email, poeAddress, poeAddressVerified } = account
logger.debug({ userId, email, poeAddress, poeAddressVerified, network })
if (!poeAddressVerified)
throw new PoeAddressNotVerified()
const poeBalance = await poeContract.getBalance(poeAddress)
const poeBalanceWithDecimals = poeBalance / Math.pow(10, poeContractDecimals)
if (poeBalanceWithDecimals < poeBalanceMinimum)
throw new PoeBalanceInsufficient(poeBalanceMinimum, poeBalanceWithDecimals)
return node.postArchive(archive)
}
return {
postArchive,
}
}