mehditohidi / meht

1 stars 0 forks source link

warpcast stats #2

Open mehditohidi opened 7 months ago

mehditohidi commented 7 months ago

// Created by: ng // Support: @iamng_eth

const urlPath = 'https://iamng.wtf/scripts/fc-degen/points' const icon = 'user' const color = 'purple'

const version = 7

await update()

const params = args.widgetParameter

const textColor = new Color('#cdd5e0') const lableColor = new Color('#cdd5e0') const green = new Color('#b1e457') const purple = new Color('#c951e7')

const w = new ListWidget() w.backgroundColor = new Color('#161a30')

if (!params) { const errorTxt = w.addText(Please provide your address) errorTxt.textColor = Color.red() errorTxt.font = Font.boldRoundedSystemFont(12); } else { const pointsData = await loadPoints(params) const allowanceData = await loadAllowance(params)

if (!pointsData && !allowanceData) {
    const errorTxt = w.addText(`Can't load your points`)
    errorTxt.textColor = Color.red()
    errorTxt.font = Font.boldRoundedSystemFont(12);
} else {
    if (pointsData && pointsData.length > 0) {
        const points = pointsData[0]["points"]

        const pLTxt = w.addText('Points (s3)')
        pLTxt.textColor = lableColor
        pLTxt.font = Font.boldRoundedSystemFont(12);

        const pTxt = w.addText(`${points}`)
        pTxt.textColor = green
        pTxt.font = Font.boldRoundedSystemFont(24);
    } else {
        const pLTxt = w.addText(`No s3 points yet`)
        pLTxt.textColor = lableColor
        pLTxt.font = Font.boldRoundedSystemFont(12);
    }

    w.addSpacer(8)

    if (allowanceData && allowanceData.length > 0) {
        const allowance = allowanceData[0]["tip_allowance"]
        const remainingAllowance = allowanceData[0]["remaining_allowance"]

        const rALTxt = w.addText('Tips Left')
        rALTxt.textColor = lableColor
        rALTxt.font = Font.boldRoundedSystemFont(12);

        w.addSpacer(2)

        const rALW = w.addStack()
        rALW.layoutHorizontally()
        rALW.bottomAlignContent()

        const rATxt = rALW.addText(`${remainingAllowance}`)
        rATxt.textColor = green
        rATxt.font = Font.boldRoundedSystemFont(16);

        rALW.addSpacer(2)

        const aW = rALW.addStack()
        aW.layoutVertically()

        const aTxt = aW.addText(`/${allowance}`)
        aTxt.textColor = green
        aTxt.font = Font.boldRoundedSystemFont(12);
        aTxt.textOpacity = 0.6

        aW.addSpacer(1)
    } else {
        const pLTxt = w.addText(`Can't load tips`)
        pLTxt.textColor = lableColor
        pLTxt.font = Font.boldRoundedSystemFont(12);
    }

    w.addSpacer(null)

    const nextUpdate = timeUntilReset()

    const nextUpdateTimeW = w.addStack()
    nextUpdateTimeW.layoutHorizontally()
    nextUpdateTimeW.centerAlignContent()

    const uLTxt = nextUpdateTimeW.addText('Update in')
    uLTxt.textColor = textColor
    uLTxt.font = Font.boldRoundedSystemFont(12);

    nextUpdateTimeW.addSpacer(2)

    const uTxt = nextUpdateTimeW.addText(`${nextUpdate}`)
    uTxt.textColor = purple
    uTxt.font = Font.boldRoundedSystemFont(12);
}

}

w.url = https://www.degen.tips/

Script.setWidget(w) Script.complete() w.presentSmall()

async function loadImage(imgUrl) { const req = new Request(imgUrl) return await req.loadImage() }

async function loadPoints(params) { const url = https://www.degen.tips/api/airdrop2/season3/points?address=${params} const req = new Request(url) req.method = 'GET' req.headers = { 'Content-Type': 'application/json' } const res = await req.loadJSON()

return res

}

async function loadAllowance(params) { const url = https://www.degen.tips/api/airdrop2/tip-allowance?address=${params} const req = new Request(url) req.method = 'GET' req.headers = { 'Content-Type': 'application/json' } const res = await req.loadJSON()

return res

}

async function update() { let req = new Request(${urlPath}/versions.json); let versions = await req.loadJSON(); if (versions.latestVersion > version) { fileManager = FileManager.iCloud() documentsDirectory = fileManager.documentsDirectory()

    let req = new Request(`${urlPath}/script.js`);
    let code = await req.loadString();

    let codeToStore = Data.fromString(`// Variables used by Scriptable.\n// These must be at the very top of the file. Do not edit.\n// icon-color: ${color}; icon-glyph: ${icon};\n// Created by: ng\n// Support: @iamng_eth\n\nconst urlPath = '${urlPath}'\nconst icon = '${icon}'\nconst color = '${color}'\n\n${code}`);
    let selfFilePath = fileManager.joinPath(documentsDirectory, Script.name() + '.js');
    fileManager.write(selfFilePath, codeToStore);
    let callback = new CallbackURL("scriptable:///run");
    callback.addParameter("scriptName", Script.name());
    callback.open();
}

}

function timeUntilReset() { // Get the current date and time in UTC const now = new Date(); const nowUtc = Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());

// Create a Date object for today's reset event at 8:10 AM UTC
const todayResetUtc = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), 8, 10, 0));

// Check if the reset time has already passed for today
let nextResetUtc;
if (nowUtc > todayResetUtc) {
    // If the reset time has passed, set the next reset to tomorrow at 8:10 AM UTC
    nextResetUtc = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate() + 1, 8, 10, 0));
} else {
    // If the reset time has not passed, set the next reset to today at 8:10 AM UTC
    nextResetUtc = todayResetUtc;
}

// Calculate the time remaining until the next reset in milliseconds
const timeUntilResetMs = nextResetUtc - nowUtc;

// Convert milliseconds to hours, minutes, and seconds
const hours = Math.floor(timeUntilResetMs / (1000 * 60 * 60));
const minutes = Math.floor((timeUntilResetMs % (1000 * 60 * 60)) / (1000 * 60));

// Return the time remaining as a string
return `${hours}h ${minutes}m`;

}