Skip to content

Commit 65ae7d3

Browse files
committed
feat (WIP): get transactions from wallet
1 parent d31a1ab commit 65ae7d3

File tree

3 files changed

+104
-1
lines changed

3 files changed

+104
-1
lines changed

src/services/listeners.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { initProfileTokens } from "../utils/utils";
1717
import { checkCurrentRate } from "../services/passportPurchase";
1818
import {
1919
getPassportsInfoForProfile,
20+
getReceivedAmounts,
2021
getSpClubInfo,
2122
getVpnTimeUsed,
2223
storeSystemData,
@@ -51,6 +52,10 @@ const listenProfileVer = async (
5152
await getProfileAssets(profiles[0], profiles[1]);
5253
await getVpnTimeUsed();
5354
await getSpClubInfo(profiles[0], currentPageInvitees);
55+
// const receivedTransactions = await getReceivedAmounts(
56+
// profiles[1].keyID
57+
// );
58+
// console.log(receivedTransactions);
5459
}
5560

5661
if (block % 2 === 0) {

src/services/wallets.ts

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ import {
1111
conetDepinProvider,
1212
conetProvider,
1313
localDatabaseName,
14+
rewardWalletAddress,
15+
solanaRpc,
1416
} from "../utils/constants";
1517
import contracts from "../utils/contracts";
1618
import { CoNET_Data, setCoNET_Data } from "../utils/globals";
1719
import * as Bip39 from "bip39";
18-
20+
import { Connection, PublicKey } from "@solana/web3.js";
1921
import { Keypair } from "@solana/web3.js";
2022
import Bs58 from "bs58";
2123
import { scanSolanaSol, scanSolanaSp } from "./listeners";
@@ -944,6 +946,99 @@ const getSpClubMemberId = async (profile: profile) => {
944946
return profile.spClub.memberId;
945947
};
946948

949+
async function getReceivedAmounts(walletAddress: string) {
950+
const connection = new Connection(solanaRpc, "confirmed");
951+
952+
try {
953+
const walletPubKey = new PublicKey(walletAddress);
954+
const senderPubKey = new PublicKey(rewardWalletAddress);
955+
956+
// Step 1: Get transaction signatures
957+
const signatures = await connection.getSignaturesForAddress(walletPubKey, {
958+
limit: 20,
959+
});
960+
961+
if (signatures.length === 0) {
962+
console.log("No transactions found.");
963+
return [];
964+
}
965+
966+
// For only one transaction it works. Here's an example.
967+
// For multiple transactions it fails because the server doesn't support it.
968+
// const transaction = await connection.getTransaction(
969+
// signatures[0].signature,
970+
// {
971+
// commitment: "confirmed",
972+
// maxSupportedTransactionVersion: 0, // Ensures we handle versioned transactions
973+
// }
974+
// );
975+
// console.log(transaction);
976+
977+
// Step 2: Fetch all transactions in **one batch request**
978+
const transactions = await connection.getTransactions(
979+
signatures.map((sig) => sig.signature),
980+
{ commitment: "confirmed" }
981+
);
982+
// Step 3: Filter transactions where sender matches
983+
const receivedTransactions = transactions
984+
.filter((tx: any) => tx && tx.meta)
985+
.map((tx) => {
986+
if (!tx || !tx.meta) return null;
987+
988+
// Get account balances before and after the transaction
989+
const preBalances = tx.meta.preBalances;
990+
const postBalances = tx.meta.postBalances;
991+
992+
// Get account keys (supports both legacy & versioned transactions)
993+
const accountKeys =
994+
tx.transaction.message.getAccountKeys().staticAccountKeys;
995+
996+
// Find the index of the recipient (walletAddress) in the account keys
997+
const recipientIndex = accountKeys.findIndex(
998+
(key: any) => key.toBase58() === walletPubKey.toBase58()
999+
);
1000+
1001+
if (recipientIndex === -1) return null; // Wallet not found in the transaction
1002+
1003+
// Calculate SOL received (in lamports, convert to SOL)
1004+
const solReceived =
1005+
(postBalances[recipientIndex] - preBalances[recipientIndex]) / 1e9;
1006+
1007+
// Extract token transfers (SPL tokens)
1008+
const tokenTransfers = tx.meta.postTokenBalances?.map(
1009+
(tokenBalance: any) => {
1010+
const preToken = tx?.meta?.preTokenBalances?.find(
1011+
(pre) =>
1012+
pre.mint === tokenBalance.mint && pre.owner === walletAddress
1013+
);
1014+
1015+
const receivedAmount =
1016+
(tokenBalance.uiTokenAmount.uiAmount || 0) -
1017+
(preToken?.uiTokenAmount.uiAmount || 0);
1018+
1019+
return {
1020+
mint: tokenBalance.mint,
1021+
receivedAmount,
1022+
};
1023+
}
1024+
);
1025+
1026+
return {
1027+
signature: tx.transaction.signatures[0],
1028+
solReceived,
1029+
tokenTransfers,
1030+
};
1031+
});
1032+
1033+
console.log(receivedTransactions.filter((tx: any) => tx !== null));
1034+
1035+
return receivedTransactions.filter((tx: any) => tx !== null);
1036+
} catch (error) {
1037+
console.error("Error fetching transaction history:", error);
1038+
return [];
1039+
}
1040+
}
1041+
9471042
export {
9481043
createOrGetWallet,
9491044
createGPGKey,
@@ -961,4 +1056,5 @@ export {
9611056
getSpClubInfo,
9621057
getSpClubMemberId,
9631058
getRefereesPage,
1059+
getReceivedAmounts,
9641060
};

src/utils/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const _ethRpc = [
1313
];
1414
const solanaRpc = "https://solana-rpc.conet.network";
1515
const ethRpc = () => _ethRpc[Math.round(Math.random() * (_ethRpc.length - 1))];
16+
const rewardWalletAddress = "GUq7PhyAUZko2mPhv3CupmdJKQ61LH8VyrdsRL25q7zg";
1617

1718
const conetProvider = new ethers.JsonRpcProvider(conetRpc);
1819
const ethProvider = new ethers.JsonRpcProvider(ethRpc());
@@ -25,6 +26,7 @@ export {
2526
apiv4_endpoint,
2627
conetRpc,
2728
solanaRpc,
29+
rewardWalletAddress,
2830
conetProvider,
2931
ethProvider,
3032
conetDepinProvider,

0 commit comments

Comments
 (0)