Created
April 23, 2022 16:51
-
-
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.
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
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