// Rubic Crypto Price Widget v1.2.0
// Copyright (c) 2021 Sergey Andreev <andreev@mywish.io>
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
const params = args.widgetParameter ? args.widgetParameter.split(",") : [];
const isDarkTheme = params?.[0] === 'dark';
const padding = 2;
const widget = new ListWidget();
if (isDarkTheme) {
widget.backgroundColor = new Color('#1C1C1E');;
}
widget.setPadding(padding, padding, padding, padding);
widget.url = 'https://www.coingecko.com/en/coins/rubic';
const headerStack = widget.addStack();
headerStack.setPadding(0, 0, 15, 0);
const headerText = headerStack.addText("Crypto price");
headerText.font = Font.mediumSystemFont(16);
if (isDarkTheme) {
headerText.textColor = new Color('#FFFFFF');
}
async function buildWidget() {
const rubicImage = await loadImage('https://assets.rubic.exchange/assets/ethereum/0x3330bfb7332ca23cd071631837dc289b09c33333/logo.png');
const algbImage = await loadImage('https://assets.rubic.exchange/assets/polygon/0x0169ec1f8f639b32eec6d923e24c2a2ff45b9dd6/logo.png');
const ethereumImage = await loadImage('https://assets.rubic.exchange/assets/ethereum/0x0000000000000000000000000000000000000000/logo.png');
const rubicPriceInfo = await getTokenPriceInfo('rubic');
const algbPriceInfo = await getTokenPriceInfo('algebra');
const ethereumPriceInfo = await getTokenPriceInfo('ethereum');
const roundedRubicPrice = Math.round(rubicPriceInfo.price * 1000) / 1000;
const roundedAlgbPrice = Math.round(algbPriceInfo.price * 1000) / 1000;
const roundedEthereumPrice = Math.round(ethereumPriceInfo.price);
addCrypto(rubicImage, 'RBC ', `$${roundedRubicPrice}`, rubicPriceInfo.grow);
addCrypto(algbImage, 'ALGB', `$${roundedAlgbPrice}`, algbPriceInfo.grow);
addCrypto(ethereumImage, 'ETH ', `$${roundedEthereumPrice}`, ethereumPriceInfo.grow);
}
function addCrypto(image, symbol, price, grow) {
const rowStack = widget.addStack();
rowStack.setPadding(0, 0, 14, 0);
rowStack.layoutHorizontally();
const imageStack = rowStack.addStack();
const symbolStack = rowStack.addStack();
const priceStack = rowStack.addStack();
imageStack.setPadding(0, 0, 0, 10);
priceStack.setPadding(0, 8, 0, 0);
const imageNode = imageStack.addImage(image);
imageNode.imageSize = new Size(20, 20);
imageNode.leftAlignImage();
const symbolText = symbolStack.addText(symbol);
symbolText.font = Font.mediumSystemFont(16);
const priceText = priceStack.addText(price);
priceText.font = Font.mediumSystemFont(16);
if (isDarkTheme) {
symbolText.textColor = new Color('#FFFFFF');
}
if (grow) {
priceText.textColor = new Color('#4AA956');
} else {
priceText.textColor = new Color('#D22E2E');
}
}
async function getTokenPriceInfo(tokenId) {
const url = `https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=${tokenId}`;
const req = new Request(url)
const apiResult = await req.loadJSON()
return { price: apiResult[0].current_price, grow: apiResult[0].price_change_24h > 0 };
}
async function loadImage(imgUrl) {
const req = new Request(imgUrl)
return await req.loadImage()
}
await buildWidget();
Script.setWidget(widget);
Script.complete();
widget.presentSmall();
Launch the application in the upper right corner, click on "+". Insert everything copied there.
Click on the "Run Script" button at the bottom.
Go to the IOS device main screen and open the mode for adding applications and widgets with a long tap.
Click " + " on the top left and select the Scriptable widget.
In the modal window, click "Add widget".
Optional: You can choose the dark theme in the parameter menu.
Click on the added widget, click on the "Script" option and select the script added on point 3 by name (most likely "Untitled Script 1").