Skip to content

Instantly share code, notes, and snippets.

@kmjones1979
Created November 25, 2024 15:20
Show Gist options
  • Save kmjones1979/36d4c9192ecf35f3f6cb443af937e866 to your computer and use it in GitHub Desktop.
Save kmjones1979/36d4c9192ecf35f3f6cb443af937e866 to your computer and use it in GitHub Desktop.
Javascript for Chainlink Function
// This example show how to check if an account is enlisted in the mission
const account = args[0].toLowerCase() // make sure the account is in lowercase
const query_url = args[1] //https://api.studio.thegraph.com/query/37762/mymission/version/latest
// Uncomment to use Graph API Key (for published missions)
//const graphKey = secrets.graphKey
const graphRequest = Functions.makeHttpRequest({
url: `${query_url}`,
method: "POST",
headers: {
"Content-Type": "application/json",
},
data: {
query: /* GraphQL */ `
{
enlisteds(
first: 1
orderBy: blockTimestamp
orderDirection: desc
where: { account: "${account}" }
) {
id
account
blockNumber
blockTimestamp
transactionHash
}
}
`,
},
})
const [graphResponse] = await Promise.all([graphRequest])
let id = []
if (!graphResponse.error) {
for (let i = 0; i < 1; i++) {
id.push(graphResponse.data.data.enlisteds[i].id.toLowerCase()) // make sure the id is in lowercase
console.log("account: ", account)
console.log("id[0]... ", id[0])
}
} else {
console.log("graphReponse Error, ", graphResponse)
}
if (id[0] === account) {
return Functions.encodeUint256(1)
console.log("account is enlisted")
} else {
return Functions.encodeUint256(0)
console.log("account is not enlisted")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment