Skip to content

Commit

Permalink
Merge pull request #540 from arconnectio/development
Browse files Browse the repository at this point in the history
ArConnect BETA 1.20.3
  • Loading branch information
nicholaswma authored Nov 14, 2024
2 parents d4b2f3b + 3286376 commit 567d394
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
37 changes: 35 additions & 2 deletions src/lib/transactions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type GQLResultInterface from "ar-gql/dist/faces";
import type { GQLEdgeInterface } from "ar-gql/dist/faces";
import type { RawTransaction } from "~notifications/api";
import type { RawTransaction, Transaction } from "~notifications/api";
import { timeoutPromise, type TokenInfo } from "~tokens/aoTokens/ao";
import { formatAddress } from "~utils/format";
import { ExtensionStorage } from "~utils/storage";
Expand All @@ -9,10 +9,16 @@ import type { Token } from "~tokens/token";
import BigNumber from "bignumber.js";
import browser from "webextension-polyfill";
import { balanceToFractioned, formatFiatBalance } from "~tokens/currency";
import { AF_ERROR_QUERY } from "~notifications/utils";
import { gql } from "~gateways/api";
import { txHistoryGateways } from "~gateways/gateway";
import { retryWithDelay } from "~utils/retry";

let tokens: TokenInfo[] = null;
export let tokenInfoMap = new Map<string, TokenInfo | Token>();

const AGENT_TOKEN_ADDRESS = "8rbAftv7RaPxFjFk5FGUVAVCSjGQB4JHDcb9P9wCVhQ";

export type ExtendedTransaction = RawTransaction & {
cursor: string;
month: number;
Expand Down Expand Up @@ -87,10 +93,37 @@ const processTransaction = (transaction: GQLEdgeInterface, type: string) => ({
date: ""
});

export async function checkTransactionError(
transaction: GQLEdgeInterface | Transaction
): Promise<boolean> {
if (transaction.node.recipient !== AGENT_TOKEN_ADDRESS) {
return false;
}

return retryWithDelay(async (attempt) => {
const data = await gql(
AF_ERROR_QUERY,
{ messageId: transaction.node.id },
txHistoryGateways[attempt % txHistoryGateways.length]
);

if (data?.data === null && (data as any)?.errors?.length > 0) {
throw new Error((data as any)?.errors?.[0]?.message || "GraphQL Error");
}

return data.data.transactions.edges.length > 0;
}, 2).catch(() => false);
}

const processAoTransaction = async (
transaction: GQLEdgeInterface,
type: string
) => {
const hasError = await checkTransactionError(transaction);
if (hasError) {
return null;
}

const tokenData = await timeoutPromise(
fetchTokenByProcessId(transaction.node.recipient),
10000
Expand Down Expand Up @@ -124,7 +157,7 @@ export const processTransactions = async (
if (isAo) {
return Promise.all(
edges.map((transaction) => processAoTransaction(transaction, type))
);
).then((transactions) => transactions.filter(Boolean));
} else {
return edges.map((transaction) => processTransaction(transaction, type));
}
Expand Down
32 changes: 32 additions & 0 deletions src/notifications/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,38 @@ query($address: String!) {
}
`;

export const AF_ERROR_QUERY = `
query($messageId: String!) {
transactions(
first: 10,
tags: [
{name: "Data-Protocol", values: ["ao"]},
{name: "Action", values: ["Transfer-Error"]},
{name: "Message-Id", values: [$messageId]},
]
) {
edges {
cursor
node {
recipient
id
owner {
address
}
block {
timestamp
height
}
tags {
name
value
}
}
}
}
}
`;

export const AO_SENT_QUERY = `
query($address: String!) {
transactions(
Expand Down
7 changes: 7 additions & 0 deletions src/routes/popup/notifications.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
SubscriptionStatus,
type SubscriptionData
} from "~subscriptions/subscription";
import { checkTransactionError } from "~lib/transactions";

export default function Notifications() {
const [notifications, setNotifications] = useState<Transaction[]>([]);
Expand Down Expand Up @@ -86,9 +87,15 @@ export default function Notifications() {
formattedNotifications: Transaction[];
}> => {
const address = await getActiveAddress();

let formattedNotifications = await Promise.all(
notifications.map(async (notification) => {
try {
const hasError = await checkTransactionError(notification);
if (hasError) {
return { formattedMessage: null, notification };
}

let formattedMessage: string = "";
if (notification.transactionType === "PrintArchive") {
formattedMessage = browser.i18n.getMessage("print_archived");
Expand Down

0 comments on commit 567d394

Please sign in to comment.