Last active
May 20, 2020 18:09
-
-
Save leonprou/20bd3ef3521a49b9a8cb969e0e4a74da to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.body.data.level | |
const pool = req.body.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.body.id, | |
data: { winnerAccount } | |
} | |
console.log(response) | |
return res.status(200).send(response) | |
} | |
} | |
console.log('no winner found') | |
// still no winner | |
return res.status(200).send({ | |
jobRunID: req.body.id | |
}) | |
} | |
// test data | |
// poeAdapter({ | |
// body: { | |
// data: { pool: '0x0d65b0802d0713bac6426cf4935d252807f59136000000000000000000000000', level: 1 } | |
// } | |
// }) | |
module.exports = poeAdapter |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment