|
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 |