Last active
January 21, 2022 11:09
-
-
Save ttt50966/321ef302a14a60af102f5247f529cccf to your computer and use it in GitHub Desktop.
This file contains 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
//Author: Kuàn-ka | |
//This is a "LikeCoin" script for Scriptable | |
//Please dowonload the app below first | |
//https://apps.apple.com/tw/app/scriptable/id1405459188 | |
let address = args.widgetParameter; | |
if (address == null) {address = "cosmos1eu6qpj8xu820mjsvq37f7gv4jn8xj8egalpkcp"} | |
let units = 1000000000 //預設單位為 nanolike 所以乘以10^9 | |
let digits = 3 //顯示小數點後幾位 | |
//以下api適用所有cosmos hub的RPC,只需要將https://mainnet-node.like.co/ 這部分換成其他鏈的RPC就可以做出不同widget | |
url = "https://mainnet-node.like.co/cosmos/bank/v1beta1/balances/" + address | |
let r = new Request(url) | |
let balances_json = await r.loadJSON() | |
balances = Number(balances_json["balances"][0]["amount"])/units | |
let url2 = "https://mainnet-node.like.co/cosmos/distribution/v1beta1/delegators/"+address+"/rewards" | |
let r2 = new Request(url2) | |
let rewards_json = await r2.loadJSON() | |
let rewards = Number(rewards_json["total"][0]["amount"])/units | |
let url3 = "https://mainnet-node.like.co/cosmos/staking/v1beta1/delegations/" + address | |
let r3 = new Request(url3) | |
let staking_json = await r3.loadJSON() | |
let NumStake = Number(staking_json["pagination"]["total"]) | |
let shares = 0.0 | |
for (let i = 0; i < NumStake; i++) { | |
shares+= Number(staking_json["delegation_responses"][i]["delegation"]["shares"])/units | |
} | |
total = balances + rewards + shares | |
const width=120 | |
const h=7 | |
const w = new ListWidget() | |
REFRESH_INTERVAL = 1 //mins | |
w.backgroundColor=new Color("#222222") | |
var refreshDate = Date.now() + 1000*60*REFRESH_INTERVAL | |
w.refreshAfterDate = new Date(refreshDate) | |
function getwidget(total, haveGone, str) { | |
const titlew = w.addText(str) | |
titlew.textColor = new Color("#808080") | |
titlew.font = Font.boldSystemFont(13) | |
w.addSpacer(4) | |
const imgw = w.addImage(creatProgress(total,haveGone)) | |
imgw.imageSize=new Size(width, h) | |
w.addSpacer(12) | |
} | |
function creatProgress(total,count){ | |
let colorCodeCounter = 'dc3f73' | |
if (count/total >= 0.1) { colorCodeCounter = '65c64c'} | |
if (count/total >= 0.6) { colorCodeCounter = 'dfce60'} | |
if (count/total >= 0.9) { colorCodeCounter = 'b74d34'} | |
const context =new DrawContext() | |
context.size=new Size(width, h) | |
context.opaque=false | |
context.respectScreenScale=true | |
context.setFillColor(new Color("#48484b")) | |
const path = new Path() | |
path.addRoundedRect(new Rect(0, 0, width, h), 3, 2) | |
context.addPath(path) | |
context.fillPath() | |
context.setFillColor(new Color("#"+colorCodeCounter)) | |
const path1 = new Path() | |
path1.addRoundedRect(new Rect(0, 0, width*count/total, h), 3, 2) | |
context.addPath(path1) | |
context.fillPath() | |
return context.getImage() | |
} | |
w.addSpacer(8) | |
let provider = w.addText("LikeCoin") //widget 標題 | |
provider.font = Font.mediumSystemFont(24) | |
provider.textColor = new Color("#808080") | |
w.addSpacer(10) | |
getwidget(total, balances, "餘額 " + balances.toFixed(digits).toString()) | |
getwidget(total, shares, "委託 " + shares.toFixed(digits).toString()) | |
getwidget(total, rewards, "獎勵 " + rewards.toFixed(digits).toString()) | |
Script.setWidget(w) | |
Script.complete() | |
w.presentSmall() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment