-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
83 lines (74 loc) · 2.18 KB
/
helpers.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
//Helper functions and constants for talko webhook
require('dotenv').config();
const {gql} = require('@urql/core')
const fetch = require('node-fetch')
const _ = require('lodash')
const TABLE_NAME = process.env.TABLE_NAME
//get latest item by blockheight from the table
//if not items received apply the starting blockheight from envs
const params = {
TableName: TABLE_NAME,
Key: {"id":"production-blockheight"}
}
const projectSpecificTokens = gql`
query TokenQuery($project: String!, $mintBlock: String!, $contractAddress: String!) {
tokens(orderBy: tokenId, orderDirection: asc, where: {project: $project, mintBlock_gt: $mintBlock, contractAddress: $contractAddress}) {
tokenId
contractAddress
isLikeToken
metadataUri
mintBlock
ownerAddress
isOriginal
isSharedInstance
parentTokenId
project {
id
}
category {
id
}
}
}
`
//retrieve metadata from a given url
async function getMetadataObject(metadataUri) {
try {
const response = await fetch(metadataUri)
return response
} catch(error) {
console.log('Failed to retrieve metadata from metadataurl', error)
}
}
//refactor to support new table structure
function updateParams(latestMintBlock){
return {
TableName: TABLE_NAME,
Key: {"id":"production-blockheight"},
UpdateExpression: 'set blockheight = :v',
ExpressionAttributeValues: {
':v': _.parseInt(latestMintBlock)
}
}
}
//takes in dynamodb client
async function getItem(client) {
try {
const data = await client.get(params).promise()
return data.Item
} catch (err) {
return err
}
}
//refactor to support new table structure
//takes in dynamodb client and latest mintblock
async function updateItem(client, latestMintBlock) {
const updatedParams = updateParams(latestMintBlock)
try {
const data = await client.update(updatedParams).promise()
console.log("Success", data)
} catch(err) {
console.log("Error", err)
}
}
module.exports = {projectSpecificTokens, getMetadataObject, updateParams, getItem, updateItem}