Skip to content

Commit

Permalink
feat: added redirect for processes with mirror
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholaswma committed Jun 17, 2024
1 parent c812cee commit 3ab4fdf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
28 changes: 16 additions & 12 deletions src/Token.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import { AoInstance, TokenInfo, Id, Owner, getTagValue, Message, Balances, isAddress } from "./utils";
import { connect, createDataItemSigner } from "@permaweb/aoconnect";
import Quantity from "./Quantity";
Expand All @@ -23,17 +22,19 @@ export default async function Token(
const Name = getTagValue("Name", msg.Tags);
const Denomination = getTagValue("Denomination", msg.Tags);
const Logo = getTagValue("Logo", msg.Tags);
const Mirror = getTagValue("Balance-Mirror", msg.Tags);

if (!Ticker && !Name) continue;

// if the message was found, return the token details
// if the message was found, return the token details
return new TokenInstance(
id,
{
Name,
Ticker,
Denomination: BigInt(Denomination || 0),
Logo
Logo,
Mirror,
},
signer,
ao
Expand Down Expand Up @@ -99,16 +100,19 @@ export class TokenInstance {
*/
async getBalance(address: string) {
// query ao
const process = this.#info.Mirror ? this.#info.Mirror : this.#id;
const res = await this.#ao.dryrun({
Id,
Owner: address,
process: this.#id,
process,
tags: [{ name: "Action", value: "Balance" }]
});

// find result message
for (const msg of res.Messages as Message[]) {
const balance = getTagValue("Balance", msg.Tags);
const balance = this.#info.Mirror
? msg.Data
: getTagValue("Balance", msg.Tags);

// return balance if found
if (balance) {
Expand All @@ -118,7 +122,7 @@ export class TokenInstance {
);
}
}

// default return
return new Quantity(0, this.#info.Denomination);
}
Expand All @@ -136,20 +140,20 @@ export class TokenInstance {
tags: [{ name: "Action", value: "Balances" }]
});
const bals: Balances = {};

// find result message
for (const msg of res.Messages as Message[]) {
for (const msg of res.Messages as Message[]) {
// return balance if found
if (msg.Target !== Owner|| !msg.Data) continue;

try {
const raw = JSON.parse(msg.Data);

for (const addr in raw) {
bals[addr] = new Quantity(
BigInt(raw[addr]),
BigInt(raw[addr]),
this.#info.Denomination
);
);
}
} catch {}
}
Expand Down
3 changes: 2 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type Tag } from "arweave/web/lib/transaction";
import { type connect } from "@permaweb/aoconnect";
import Quantity from "./Quantity"
import Quantity from "./Quantity";

/**
* Dummy ID
Expand All @@ -20,6 +20,7 @@ export interface TokenInfo {
Ticker?: string;
Denomination: bigint;
Logo?: string;
Mirror?: string;
}

/**
Expand Down

0 comments on commit 3ab4fdf

Please sign in to comment.