Last active
May 26, 2021 18:29
-
-
Save florindumitru/71551d5440d698122a9b942a7345d21f to your computer and use it in GitHub Desktop.
Pula Widget iOS
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
| //Widget PULA | |
| const widget = new ListWidget() | |
| const indexData = await fetchFngValue() | |
| await createWidget() | |
| // used for debugging if script runs inside the app | |
| if (!config.runsInWidget) { | |
| await widget.presentSmall() | |
| } | |
| widget.setPadding(10, 10, 10, 10) | |
| widget.url = 'https://pula.network' | |
| Script.setWidget(widget) | |
| Script.complete() | |
| // build the content of the widget | |
| async function createWidget() { | |
| let line1, line2, line3 | |
| let icon = widget.addStack() | |
| const coin = await getImage() | |
| const coinImg = icon.addImage(coin) | |
| coinImg.imageSize = new Size(30, 30) | |
| // coinImg.setPadding(10,0,0,0) | |
| icon.layoutHorizontally() | |
| icon.addSpacer(8) | |
| let iconRow = icon.addStack() | |
| iconRow.layoutVertically() | |
| icon.centerAlignContent(); | |
| let iconText = iconRow.addStack() | |
| line1 = iconText.addText("PULA") | |
| line1.font = Font.mediumRoundedSystemFont(25) | |
| line1.leftAlignText() | |
| let line1nxt = iconRow.addText("pula.network") | |
| line1nxt.font = Font.thinSystemFont(13) | |
| widget.addSpacer(10) | |
| let row = widget.addStack() | |
| row.layoutHorizontally() | |
| let fngText = row.addText("$") | |
| fngText.font = Font.mediumRoundedSystemFont(18) | |
| let fngValue = row.addText(indexData.toString().slice(0, -13)) | |
| // fngValue.textColor = new Color('#ffffff') | |
| fngValue.font = Font.boldSystemFont(30) | |
| fngValue.centerAlignText(); | |
| widget.addSpacer(0) | |
| } | |
| // Create the datetime object when update will happen | |
| function toDateTime(secs) { | |
| const t = new Date(); | |
| t.setSeconds(+t.getSeconds() + +secs); | |
| console.log(t) | |
| return t; | |
| } | |
| // fetches the fng value | |
| // async function fetchFngValue() { | |
| // let url = 'https://api.dex.guru/v1/tokens/0xf07897c018b6dd66d37da2f342a2e30e2dc82ed7-bsc' | |
| // const req = new Request(url) | |
| // const apiResult = await req.loadJSON() | |
| // let indexValue = apiResult.priceUSD; | |
| // return indexValue | |
| // } | |
| async function fetchFngValue() { | |
| let url = 'https://api.1inch.exchange/v3.0/56/quote?fromTokenAddress=0xf07897c018b6dd66d37da2f342a2e30e2dc82ed7&toTokenAddress=0xe9e7cea3dedca5984780bafc599bd69add087d56&amount=100000000000000000'; | |
| const req = new Request(url) | |
| const apiResult = await req.loadJSON() | |
| // let indexValue = apiResult.priceUSD; | |
| var toTokenAmount = parseInt(apiResult.toTokenAmount); | |
| var fromTokenAmount = parseInt(apiResult.fromTokenAmount); | |
| var price = toTokenAmount / fromTokenAmount; | |
| return price | |
| } | |
| // get images from local filestore or download them once | |
| async function getImage() { | |
| let fm = FileManager.local() | |
| let dir = fm.documentsDirectory() | |
| let path = fm.joinPath(dir, "logo") | |
| let iconImage = await loadImage('https://pula.network/assets/logom2.png') | |
| fm.writeImage(path, iconImage) | |
| return iconImage | |
| } | |
| // helper function to download an image from a given url | |
| async function loadImage(imgUrl) { | |
| const req = new Request(imgUrl) | |
| return await req.loadImage() | |
| } | |
| // end of script |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment