Skip to content

Commit

Permalink
upd: reworked runes portfolio to also show 24h change as well as the …
Browse files Browse the repository at this point in the history
…total value sum
  • Loading branch information
JoeyKhd committed Jan 4, 2025
1 parent 10107e5 commit 65142c6
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 35 deletions.
28 changes: 23 additions & 5 deletions packages/plugin-ordinals/src/actions/runes/portfolio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import { WalletProvider, walletProvider } from "../../providers/wallet";
import API from "../../utils/api";
import { z } from "zod";
import { balanceTemplate } from "../../templates";
import { handleError } from "../../utils";
import { dollarFormatter, handleError } from "../../utils";
import { formatUnits } from "viem";
import BigNumber from "bignumber.js";

export const addressSchema = z.object({
address: z.string(),
Expand Down Expand Up @@ -62,13 +64,29 @@ export default {
? addresses.taprootAddress
: derivedAddress;

const ordiscan = new API(runtime.getSetting("ORDISCAN_API_KEY"));
const portfolio = await ordiscan.getRunesPortfolio(taprootAddress);
const api = new API();

const balances = portfolio?.results;
const balances = await api.getRunesPortfolio(taprootAddress);
let balanceText = "";
for (const balance of balances) {
const formattedBalance = formatUnits(
BigInt(balance.amount),
balance.divisibility
);

const totalValue = new BigNumber(balance.currentPrice)
.multipliedBy(new BigNumber(formattedBalance))
.toNumber();

const pastDayChange =
balance.priceChangePercentage24h.toFixed(2);
const pastDayPositive = Number(pastDayChange) > 0;

balanceText += `${balance.symbol} ${balance?.runeName}: ${formattedBalance} [Value: ${dollarFormatter.format(totalValue)}] (${pastDayPositive ? "+" : ""}${pastDayChange}%)\n`;
}

callback({
text: `Runes portfolio for address ${taprootAddress}:\n${balances?.map((item, idx) => `${idx + 1}: ${item?.rune?.spaced_name} - ${item.balance}\n`)}`,
text: `Runes portfolio for address ${taprootAddress}:\n\n${balanceText}`,
});

return true;
Expand Down
32 changes: 6 additions & 26 deletions packages/plugin-ordinals/src/actions/runes/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ import {
Range,
Etching,
} from "runelib";
import { getRequiredRuneUtxos, handleError } from "../../utils";
import {
estimateTransactionSize,
getRequiredRuneUtxos,
handleError,
} from "../../utils";

export const transferSchema = z.object({
rune: z.string(),
Expand Down Expand Up @@ -208,30 +212,6 @@ export default {
psbt.addOutputAddress(addresses.taprootAddress, BigInt(546));
}

const estimateTransactionSize = (
taprootInputCount: number,
p2shP2wpkhInputCount: number,
outputCounts: {
p2wpkh: number;
taproot: number;
opReturn: number;
}
) => {
const baseSize = 10;
const taprootInputSize = 57 * taprootInputCount;
const p2shP2wpkhInputSize = 91 * p2shP2wpkhInputCount;
const outputSize =
31 * outputCounts.p2wpkh +
43 * outputCounts.taproot +
43 * outputCounts.opReturn;
return (
baseSize +
taprootInputSize +
p2shP2wpkhInputSize +
outputSize
);
};

const estimatedSize = estimateTransactionSize(
toUseRuneUtxos.utxos.length, // The rune utxo's that we are using
1, // One BTC UTXO
Expand All @@ -241,7 +221,7 @@ export default {
opReturn: 1, // Mintstone
}
);

const feerates = await wallet.getFeeRates();

if (!feerates?.fastestFee) {
Expand Down
11 changes: 11 additions & 0 deletions packages/plugin-ordinals/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,14 @@ export interface IAccount {
schnorrPublicKey?: any;
publicKey?: any;
}

export interface IBalance {
amount: number;
divisibility: number;
symbol: string;
runeName: string;
inscriptionId: string;
id: `${string}:${string}`;
priceChangePercentage24h: number;
currentPrice: number;
}
9 changes: 5 additions & 4 deletions packages/plugin-ordinals/src/utils/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { elizaLogger } from "@elizaos/core";
import { IBalance } from "../types";

const ORDISCAN_BASE_URL = "https://api.ordiscan.com/v1";
const HIRO_BASE_URL = "https://api.hiro.so";
Expand Down Expand Up @@ -32,9 +33,9 @@ class API {
this.ordiscanApiKey = ordiscanApiKey;
}

async getRunesPortfolio(address: string) {
return fetcher(
`${HIRO_BASE_URL}/runes/v1/addresses/${address}/balances?offset=0&limit=20`
async getRunesPortfolio(address: string): Promise<IBalance[]> {
return await fetcher(
`https://api-3.xverse.app/v2/address/${address}/rune-balance?includeUnconfirmed=true`
);
}

Expand All @@ -48,7 +49,7 @@ class API {
);
}

async getRareSats(address: string){
async getRareSats(address: string) {
// return fetcher(
// `https://api-3.xverse.app/v1/market/address/${address}/rune/${runeName}/utxos`
// );
Expand Down
29 changes: 29 additions & 0 deletions packages/plugin-ordinals/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,32 @@ export const getRequiredRuneUtxos = (
insufficientFunds: accumulated < amountNeeded,
};
};

export const estimateTransactionSize = (
taprootInputCount: number,
p2shP2wpkhInputCount: number,
outputCounts: {
p2wpkh: number;
taproot: number;
opReturn: number;
}
) => {
const baseSize = 10;
const taprootInputSize = 57 * taprootInputCount;
const p2shP2wpkhInputSize = 91 * p2shP2wpkhInputCount;
const outputSize =
31 * outputCounts.p2wpkh +
43 * outputCounts.taproot +
43 * outputCounts.opReturn;
return (
baseSize +
taprootInputSize +
p2shP2wpkhInputSize +
outputSize
);
};

export const dollarFormatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
});

0 comments on commit 65142c6

Please sign in to comment.