|
// Replace with your Ethereum address |
|
const user_address = "0xYOUR_ADDRESS_HERE"; |
|
const metamask_card_contract = "0x9dd23a4a0845f10d65d293776b792af1131c7b30"; |
|
|
|
const tokens = [ |
|
{ symbol: "USDC", address: "0x176211869ca2b568f2a7d4ee941e073a821ee1ff" }, |
|
{ symbol: "USDT", address: "0xa219439258ca9da29e9cc4ce5596924745e12b93" } |
|
]; |
|
|
|
const RPC_URL = "https://rpc.linea.build"; |
|
|
|
// Pads an address to 32 bytes (64 hex chars) without "0x" |
|
function padAddress(addr) { |
|
const a = addr.toLowerCase().replace("0x", ""); |
|
return "0".repeat(64 - a.length) + a; |
|
} |
|
|
|
// Makes an eth_call via the Linea RPC endpoint |
|
async function ethCall(contract, data) { |
|
let req = new Request(RPC_URL); |
|
req.method = "POST"; |
|
req.headers = { "Content-Type": "application/json" }; |
|
req.body = JSON.stringify({ |
|
jsonrpc: "2.0", |
|
id: 1, |
|
method: "eth_call", |
|
params: [{ |
|
to: contract, |
|
data: data |
|
}, "latest"] |
|
}); |
|
let res = await req.loadJSON(); |
|
if (res.error || !res.result) { |
|
throw new Error("eth_call error: " + JSON.stringify(res.error || res)); |
|
} |
|
return BigInt(res.result); |
|
} |
|
|
|
// For a given token, fetch balance and allowance, then return the lower value |
|
async function getTokenMin(token) { |
|
const balanceData = "0x70a08231" + padAddress(user_address); |
|
const allowanceData = "0xdd62ed3e" + padAddress(user_address) + padAddress(metamask_card_contract); |
|
const [balance, allowance] = await Promise.all([ |
|
ethCall(token.address, balanceData), |
|
ethCall(token.address, allowanceData) |
|
]); |
|
return balance < allowance ? balance : allowance; |
|
} |
|
|
|
(async () => { |
|
|
|
const minValues = await Promise.all(tokens.map(getTokenMin)); |
|
const total = minValues.reduce((sum, value) => sum + value, BigInt(0)); |
|
// Assuming 6 decimals for both tokens |
|
const displayValue = (Number(total) / 1e6).toLocaleString(undefined, { |
|
minimumFractionDigits: 2, |
|
maximumFractionDigits: 2 |
|
}); |
|
|
|
let widget = new ListWidget(); |
|
widget.addText("🦊💳 cap: $" + displayValue); |
|
Script.setWidget(widget); |
|
widget.presentSmall(); |
|
Script.complete(); |
|
|
|
})(); |