Skip to content

Commit

Permalink
Merge pull request #35 from MHaris-Ferrum/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
AbdulAhadArain authored Aug 29, 2024
2 parents e2f80b7 + 05bed64 commit 9a5c535
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ app.use(errorConverter);
// handle error
app.use(errorHandlers);

// app.use(crons);
app.use(crons);

export default app;
14 changes: 14 additions & 0 deletions src/interfaces/QuantumPortalAccount.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,22 @@ export interface QuantumPortalContractAccount {
contractId: string;
}

interface Implementations {
address: string;
name: string;
}

export interface QuantumPortalAccount {
address: string;
isContract: boolean;
isVerified: boolean;
token: object;
hash: string;
creator: string;
name: string;
// metadata: object;
implementationAddress: string;
implementationName: string;
implementations: [Implementations];
contract: { [k: string]: QuantumPortalContractAccount };
}
14 changes: 14 additions & 0 deletions src/models/quantumPortalAccount.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ import { QuantumPortalAccount } from '../interfaces';
const quantumPortalAccountSchema = new Schema<Document & QuantumPortalAccount>({
address: String,
isContract: Boolean,
isVerified: Boolean,
name: String,
// metadata: Object,
hash: String,
creator: String,
token: Object,
implementationAddress: String,
implementationName: String,
implementations: [
{
address: String,
name: String,
},
],
contract: Object,
});

Expand Down
18 changes: 16 additions & 2 deletions src/services/contract.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import { accountService } from '../services';
import CryptoJS from 'crypto-js';
import encHex from 'crypto-js/enc-hex';
import SHA256 from 'crypto-js/sha256';
import axios from 'axios';
import config from '../config/config';

type WordArray = CryptoJS.lib.WordArray;

export const registerContract = async (
networks: string,
networks: [string],
contractAddress: string,
contract: QuantumPortalContractObject,
): Promise<QuantumPortalContractObject | QuantumPortalAccount> => {
Expand All @@ -33,11 +35,23 @@ export const registerContract = async (
let accountObj = await QuantumPortalAccountModel.findOne({
address: contractAddress,
});
const address = await axios.get(
`${config.explorerUrl}/api/v2/addresses/${contractAddress}`,
);
console.log('address', address.data);
const account: QuantumPortalAccount = !!accountObj
? (accountObj.toJSON() as any)
: ({
name: address?.data?.name,
address: contractAddress,
isContract: true,
creator: address?.data?.creator_address_hash,
hash: address?.data?.creation_tx_hash,
token: address?.data?.token,
isContract: address?.data?.is_contract,
isVerified: address?.data?.is_verified,
implementationName: address?.data?.implementation_name,
implementationAddress: address?.data?.implementation_address,
implementations: address?.data?.implementations,
contract: {},
} as QuantumPortalAccount);
for (const net of networks) {
Expand Down
117 changes: 112 additions & 5 deletions src/services/transaction.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export const getTxs = async (
limit: number,
address?: string,
): Promise<any> => {
const query: any = {};
const query: any = {
method: 'finalize',
};
if (address) {
query.$or = [{ from: address }, { to: address }];
}
Expand All @@ -38,22 +40,127 @@ export const getTxs = async (
countPromise,
docsPromise,
]);
const matchingRecords = [];
if (results.length > 0) {
// Prepare an array to hold all matching mineRemoteBlock transactions

// Step 2: Loop through each finalize transaction and find matching mineRemoteBlock transactions
for (const finalizeTx of results) {
const blockNonce = finalizeTx.decodedInput.parameters.find(
(param: any) => param.name === 'blockNonce',
).value;
const remoteChainId = finalizeTx.decodedInput.parameters.find(
(param: any) => param.name === 'remoteChainId',
).value;

// Find matching mineRemoteBlock transactions
const matchingMineRemoteBlockTx =
await QuantumPortalTransactionModel.findOne({
method: 'mineRemoteBlock',
$and: [
{
'decodedInput.parameters': {
$elemMatch: {
name: 'blockNonce',
value: blockNonce,
},
},
},
{
'decodedInput.parameters': {
$elemMatch: {
name: 'remoteChainId',
value: remoteChainId,
},
},
},
],
});

if (matchingMineRemoteBlockTx) {
const transactions =
matchingMineRemoteBlockTx.decodedInput.parameters.find(
(param: any) => param.name === 'transactions',
).value;
matchingRecords.push({
...finalizeTx.toObject(),
remoteContract: transactions[0][1],
sourceMsgSender: transactions[0][2],
sourceBeneficiary: transactions[0][3],
});
}
}
}

const totalPages = Math.ceil(totalResults / limit);
const result = {
results,
results: matchingRecords.length ? matchingRecords : results,
page,
limit,
totalPages,
totalResults,
totalResults: matchingRecords.length
? matchingRecords.length
: results.length,
};
return result;
};

export const getTransaction = async (txId: string): Promise<any> => {
const tx = await QuantumPortalTransactionModel.findOne({
let remoteContract: any = '';
let sourceMsgSender: any = '';
let sourceBeneficiary: any = '';

const tx: any = await QuantumPortalTransactionModel.findOne({
hash: txId,
method: 'finalize',
});
return tx;
if (tx) {
const blockNonce = tx.decodedInput.parameters.find(
(param: any) => param.name === 'blockNonce',
).value;
const remoteChainId = tx.decodedInput.parameters.find(
(param: any) => param.name === 'remoteChainId',
).value;
const matchingMineRemoteBlockTx =
await QuantumPortalTransactionModel.findOne({
method: 'mineRemoteBlock',
$and: [
{
'decodedInput.parameters': {
$elemMatch: {
name: 'blockNonce',
value: blockNonce,
},
},
},
{
'decodedInput.parameters': {
$elemMatch: {
name: 'remoteChainId',
value: remoteChainId,
},
},
},
],
});

if (matchingMineRemoteBlockTx) {
const transactions =
matchingMineRemoteBlockTx.decodedInput.parameters.find(
(param: any) => param.name === 'transactions',
).value;
remoteContract = transactions[0][1];
sourceMsgSender = transactions[0][2];
sourceBeneficiary = transactions[0][3];
// console.log(tx);
}
}
return {
...tx.toObject(),
remoteContract,
sourceMsgSender,
sourceBeneficiary,
};
};

export const getAllTransactions = async (
Expand Down

0 comments on commit 9a5c535

Please sign in to comment.