This repository has been archived by the owner on Apr 15, 2024. It is now read-only.
forked from frakt-solana/new-loan-bots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
151 lines (131 loc) · 3.28 KB
/
index.js
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import express from 'express'
import bodyParser from 'body-parser'
import cors from 'cors'
import dotenv from 'dotenv'
dotenv.config()
import {
generateCardFile,
removeCardFile,
generateCardFilePath,
} from './generateCard/index.js'
import { postTweet } from './twitter/index.js'
import { initDiscord, createPostOnDiscordChannel } from './discord/index.js'
import { getNftMetadataByMint } from './helpers.js'
await initDiscord();
const app = express()
app.use(cors())
app.use(bodyParser.json())
const processedLoans = {
value: [],
};
export const SHORT_TERM = 'short-term'
export const LONG_TERM = 'long-term'
const generateAndPostCardFile = async ({
nftMint,
rawLoanToValue,
rawLoanValue,
rawInterest,
rawPeriod,
loansType,
res,
}) => {
if (processedLoans.value.includes(nftMint)) {
console.log(`This loan was already processed in last 10 min`)
return res.send('This loan was already processed in last 10 min')
}
processedLoans.value = [...processedLoans.value, nftMint]
const loanToValueNumber = rawLoanToValue / 100 || 0
const loanToValue = loanToValueNumber.toString()
const loanValueNumber = rawLoanValue / 1e9 || 0
const loanValue = loanValueNumber.toFixed(3)
const interest = (rawInterest / 100 || 0).toString()
const nftPrice = (loanValue / (loanToValue / 100)).toFixed(2)
const period = rawPeriod ? rawPeriod.toString() : '7'
const { nftImageUrl, nftName, nftCollectionName } =
await getNftMetadataByMint(nftMint)
console.log('Loan data: ', {
nftMint,
nftName,
nftImageUrl,
nftCollectionName,
period,
loanToValue,
loanValue,
interest,
nftPrice: nftPrice,
})
if (!nftImageUrl || !nftName) {
console.log(`This nft has broken metadata`)
return res.send('This nft has broken metadata')
}
const cardFilePath = generateCardFilePath(nftMint)
await generateCardFile(nftMint, {
nftName,
nftImageUrl,
period,
loanToValue,
loanValue,
interest,
nftPrice,
loansType,
});
await postTweet({
fullPathToCardImage: cardFilePath,
nftName,
nftCollectionName,
period,
loanToValue,
loanValue,
loansType,
})
await createPostOnDiscordChannel(cardFilePath)
await removeCardFile(nftMint, processedLoans, 10 * 60 * 1000)
}
app.post('/new-loan-price', async (req, res) => {
try {
const {
nftMint,
loanToValue: rawLoanToValue,
loanValue: rawLoanValue,
interest: rawInterest,
} = req.body
await generateAndPostCardFile({
nftMint,
rawLoanToValue,
rawLoanValue,
rawInterest,
loansType: LONG_TERM,
res,
})
} catch (error) {
console.error(error)
res.statusCode = 503
res.send('Oh shit!')
}
})
app.post('/new-loan-time', async (req, res) => {
try {
const {
nftMint,
loanToValue: rawLoanToValue,
loanValue: rawLoanValue,
interest: rawInterest,
period: rawPeriod,
} = req.body
await generateAndPostCardFile({
nftMint,
rawLoanToValue,
rawLoanValue,
rawInterest,
rawPeriod,
loansType: SHORT_TERM,
})
} catch (error) {
console.error(error)
res.statusCode = 503
res.send('Oh shit!')
}
})
app.listen(8080, function () {
console.log(`Server is listening on port ${process.env.PORT || 8080}`)
})