@@ -11,11 +11,13 @@ import {
1111 conetDepinProvider ,
1212 conetProvider ,
1313 localDatabaseName ,
14+ rewardWalletAddress ,
15+ solanaRpc ,
1416} from "../utils/constants" ;
1517import contracts from "../utils/contracts" ;
1618import { CoNET_Data , setCoNET_Data } from "../utils/globals" ;
1719import * as Bip39 from "bip39" ;
18-
20+ import { Connection , PublicKey } from "@solana/web3.js" ;
1921import { Keypair } from "@solana/web3.js" ;
2022import Bs58 from "bs58" ;
2123import { 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+
9471042export {
9481043 createOrGetWallet ,
9491044 createGPGKey ,
@@ -961,4 +1056,5 @@ export {
9611056 getSpClubInfo ,
9621057 getSpClubMemberId ,
9631058 getRefereesPage ,
1059+ getReceivedAmounts ,
9641060} ;
0 commit comments