Skip to content

Commit

Permalink
show failed orders
Browse files Browse the repository at this point in the history
  • Loading branch information
rise1507 committed Apr 15, 2024
1 parent 622a9d9 commit 1749502
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ const renderCurrentMultisigInfo = (): void => {
const formatOrder = (lastOrder: LastOrder): string => {
if (lastOrder.errorMessage) {
if (lastOrder.errorMessage.startsWith('Contract not active')) return ``;
if (lastOrder.errorMessage.startsWith('Failed')) {
return `<div class="multisig_lastOrder" order-id="${lastOrder.order.id}" order-address="${addressToString(lastOrder.order.address)}"><span class="orderListItem_title">Failed Order #${lastOrder.order.id}</span> — Execution error</div>`;
}
return `<div class="multisig_lastOrder" order-id="${lastOrder.order.id}" order-address="${addressToString(lastOrder.order.address)}"><span class="orderListItem_title">Invalid Order #${lastOrder.order.id}</span> — ${lastOrder.errorMessage}</div>`;
} else {
const isExpired = lastOrder.orderInfo ? (new Date()).getTime() > lastOrder.orderInfo.expiresAt.getTime() : false;
Expand Down
35 changes: 34 additions & 1 deletion src/multisig/MultisigChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from "../utils/utils";
import {Address, Cell, Dictionary} from "@ton/core";
import {endParse, Multisig, parseMultisigData} from "./Multisig";
import {MyNetworkProvider, sendToIndex} from "../utils/MyNetworkProvider";
import {MyNetworkProvider, sendToIndex, sendToTonApi} from "../utils/MyNetworkProvider";
import {Op} from "./Constants";
import {Order} from "./Order";
import {checkMultisigOrder, MultisigOrderInfo} from "./MultisigOrderChecker";
Expand Down Expand Up @@ -78,6 +78,7 @@ const parseNewOrderOutMsg = (outMsg: any) => {

export interface LastOrder {
utime: number,
transactionHash: string;
type: 'new' | 'execute' | 'pending' | 'executed';
errorMessage?: string;
order?: {
Expand Down Expand Up @@ -224,6 +225,7 @@ export const checkMultisig = async (

lastOrders.push({
utime: tx.now,
transactionHash: tx.hash,
type: 'execute',
order: {
address: {
Expand All @@ -238,6 +240,7 @@ export const checkMultisig = async (
} catch (e: any) {
lastOrders.push({
utime: tx.now,
transactionHash: tx.hash,
type: 'execute',
errorMessage: e.message
})
Expand Down Expand Up @@ -280,6 +283,7 @@ export const checkMultisig = async (

lastOrders.push({
utime: tx.now,
transactionHash: tx.hash,
type: 'new',
order: {
address: {
Expand All @@ -295,6 +299,7 @@ export const checkMultisig = async (
console.log(e);
lastOrders.push({
utime: tx.now,
transactionHash: tx.hash,
type: 'new',
errorMessage: 'Invalid new order: ' + e.message
})
Expand All @@ -312,6 +317,7 @@ export const checkMultisig = async (
if (!lastOrdersMap[orderId]) {
lastOrdersMap[orderId] = {
utime: lastOrder.utime,
transactionHash: lastOrder.transactionHash,
type: lastOrder.type === 'new' ? 'pending' : 'executed',
order: lastOrder.order
}
Expand All @@ -325,6 +331,33 @@ export const checkMultisig = async (

lastOrders = Object.values(lastOrdersMap);


const findFailTx = (tonApiResult: any): boolean => {
if (tonApiResult.transaction) {
if (tonApiResult.transaction.success === false) {
if (tonApiResult.transaction.in_msg.decoded_op_name !== "excess") {
return true;
}
}
}
if (tonApiResult.children) {
for (let child of tonApiResult.children) {
if (findFailTx(child)) return true;
}
}
return false;
}

for (const lastOrder of lastOrders) {
if (lastOrder.type === 'executed') {
const transactionHashHex = Buffer.from(lastOrder.transactionHash, 'base64').toString('hex');
const result = await sendToTonApi('traces/' + transactionHashHex, {}, isTestnet);
if (findFailTx(result)) {
lastOrder.errorMessage = 'Failed';
}
}
}

for (const lastOrder of lastOrders) {
if (lastOrder.type === 'pending') {
try {
Expand Down
21 changes: 21 additions & 0 deletions src/utils/MyNetworkProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,27 @@ export const sendToIndex = async (method: string, params: any, isTestnet: boolea
return json;
}

export const sendToTonApi = async (method: string, params: any, isTestnet: boolean) => {
const mainnetRpc = 'https://dev.tonapi.io/v2/';
const testnetRpc = 'https://testnet.tonapi.io/v2/';
const rpc = isTestnet ? testnetRpc : mainnetRpc;

const headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer AHIQH4F4Y4XR6UIAAAAOGYUHWOWLUS6ZIPEXSCLAPOMMD6FSNMPUKHCIJHIP52YTU4VKURA'
};

const response = await fetch(rpc + method + '?' + new URLSearchParams(params), {
method: 'GET',
headers: headers,
});
const json = await response.json();
if (json.error) {
throw new Error(json.error);
}
return json;
}

export class MyNetworkProvider implements ContractProvider {
private contractAddress: Address;
private isTestnet: boolean;
Expand Down

0 comments on commit 1749502

Please sign in to comment.