Skip to content

Instantly share code, notes, and snippets.

@thodges-gh
Forked from leonprou/poeAdapter.js
Last active May 20, 2020 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thodges-gh/f95b30a97092051757c4509708fec28d to your computer and use it in GitHub Desktop.
Save thodges-gh/f95b30a97092051757c4509708fec28d to your computer and use it in GitHub Desktop.

Instructions

  • Run the app.js file:
node app.js
  • Call the adapter with curl:
curl -X POST -H 'content-type: application/json' -d '{"id":"abc123","data":{"pool":"0x0d65b0802d0713bac6426cf4935d252807f59136000000000000000000000000","level":1}}' http://localhost:8080
  • Observe results:
{"jobRunID":"abc123","data":{"winnerAccount":"0x000000000000000000000000302f7c3f304a5611658753bfc259e53600a1df71"}}
const { poeAdapter } = require('./index')
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const port = process.env.EA_PORT || 8080
app.use(bodyParser.json())
app.post('/', (req, res) => {
console.log('POST Data: ', req.body)
poeAdapter(req.body, (status, result) => {
console.log('Result: ', result)
res.status(status).json(result)
})
})
app.listen(port, () => console.log(`Listening on port ${port}!`))
const { request } = require('graphql-request')
const axios = require('axios')
const getAccountCharacters = async accountName => {
var options = {
method: 'POST',
url: 'https://www.pathofexile.com/character-window/get-characters',
headers: {
authority: 'www.pathofexile.com',
accept: 'application/json, text/javascript, */*; q=0.01',
dnt: '1',
'x-requested-with': 'XMLHttpRequest',
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
'sec-fetch-mode': 'cors',
'sec-fetch-dest': 'empty'
},
data: `accountName=${accountName}`
}
try {
const response = await axios(options)
return response.data
} catch (error) {
return error.toJSON()
}
}
const getAccountCharacter = async (accountName, genieToken) => {
const characters = await getAccountCharacters(accountName)
// console.log(characters)
for (const character of characters) {
// console.log(character)
if (character.name.endsWith(genieToken)) {
return character
}
}
}
// Path of Exile adapter
const poeAdapter = async (req, res) => {
const level = req.data.level
const pool = req.data.pool
const poolAddress = pool.substring(0, 42)
// getting list of users that joined the pool
const query = `query geDeposits($funding: String!) {
deposits(funding: $funding) {
sender
amount
userId
}
}`
const variables = {
funding: poolAddress
}
const { deposits } = await request(
'https://api.thegraph.com/subgraphs/name/genie-platform/genie-graph-v2',
query,
variables
)
console.log({ deposits })
// for each joined user
for (const deposit of deposits) {
// const senderBytes = '0x' + deposit.sender.slice(2).padStart(64, '0')
// console.log('0x' + deposit.sender.slice(2).padStart(64, '0'))
const [accountName, genieToken] = deposit.userId.split('#')
// get the character from poe API
const character = await getAccountCharacter(accountName, genieToken)
// console.log({ character })
// the goal level reached -> we got a winner
if (character && character.level >= level) {
console.log({ character })
const winnerAccount = '0x' + deposit.sender.slice(2).padStart(64, '0')
const response = {
jobRunID: req.id,
data: { winnerAccount }
}
console.log(response)
return res(200, response)
}
}
console.log('no winner found')
// still no winner
return res(200, {
jobRunID: req.id
})
}
// test data
// poeAdapter({
// body: {
// data: { pool: '0x0d65b0802d0713bac6426cf4935d252807f59136000000000000000000000000', level: 1 }
// }
// })
exports.poeAdapter = poeAdapter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment