Skip to content

Instantly share code, notes, and snippets.

@tstachl
Created April 23, 2022 16:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tstachl/6784de15983eaffd86d78259735c5599 to your computer and use it in GitHub Desktop.
Save tstachl/6784de15983eaffd86d78259735c5599 to your computer and use it in GitHub Desktop.
Google App Script to get Anchor Protocol Earn balance for a terra wallet address.
function getAnchorBalance(addr) {
const contract = 'terra1hzh9vpxhsk8253se0vv5jj6etdvxu3nv8z07zu';
const aUstBalance = JSON.parse(JSON.parse(UrlFetchApp.fetch("https://mantle.terra.dev/?cw20--balance=" + contract, {
method: 'post',
headers: { 'Accept-Encoding': 'gzip, deflate, br', 'Content-Type': 'application/json', 'Accept': 'application/json' },
payload: JSON.stringify({
operationName: null,
variables: {},
query: `{\n ${contract}: WasmContractsContractAddressStore(ContractAddress: \"${contract}\", QueryMsg: \"{\\\"balance\\\":{\\\"address\\\":\\\"${addr}\\\"}}\") {\n Height\n Result\n __typename\n }\n}\n`
})
}).getContentText()).data[contract].Result).balance;
const rate = JSON.parse(UrlFetchApp.fetch('https://api.anchorprotocol.com/api/v1/market/ust').getContentText()).exchange_rate;
return aUstBalance * rate / 1000000;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment