forked from dsgriffin/nft-sales-x-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
84 lines (71 loc) · 2.04 KB
/
utils.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
// external
const axios = require('axios');
const retry = require('async-retry');
const _ = require('lodash');
const { ethers } = require('ethers');
// local
const { currencies } = require('./currencies.js');
function _reducer(previous, current) {
const currency = currencies[current.token.toLowerCase()];
if (currency !== undefined) {
const result =
previous +
Number(ethers.utils.formatUnits(current.amount, currency.decimals));
return result;
} else {
return previous;
}
}
function getSeaportSalePrice(decodedLogData) {
const offer = decodedLogData.offer;
const consideration = decodedLogData.consideration;
const offerSideNfts = offer.some(
(item) =>
item.token.toLowerCase() === process.env.CONTRACT_ADDRESS.toLowerCase()
);
// if nfts are on the offer side, then consideration is the total price, otherwise the offer is the total price
if (offerSideNfts) {
const totalConsiderationAmount = consideration.reduce(_reducer, 0);
return totalConsiderationAmount;
} else {
const totalOfferAmount = offer.reduce(_reducer, 0);
return totalOfferAmount;
}
}
async function getTokenData(tokenId) {
try {
const assetName = await retry(
async (bail) => {
// retrieve metadata for asset from opensea
const response = await axios.get(
`https://api.opensea.io/api/v1/asset/${process.env.CONTRACT_ADDRESS}/${tokenId}`,
{
headers: {
'X-API-KEY': process.env.X_API_KEY,
},
}
);
const data = response.data;
// just the asset name for now, but retrieve whatever you need
return {
assetName: _.get(data, 'name'),
};
},
{
retries: 5,
}
);
return assetName;
} catch (error) {
if (error.response) {
console.log(error.response.data);
console.log(error.response.status);
} else {
console.error(error.message);
}
}
}
module.exports = {
getSeaportSalePrice: getSeaportSalePrice,
getTokenData: getTokenData,
};