Skip to content

Commit

Permalink
ft_watcher: sol swap layer parser
Browse files Browse the repository at this point in the history
ft_watcher: test: mock tests

ft_watcher: working solana swap layer parser

ft_watcher: fix tests

ft_watcher: fix swap recipients and token balances

ft_watcher: remove unused mock files

ft_watcher: update mock file names

ft_watcher: move getTokenBalance

ft_watcher: clean sol parser

ft_watcher: add batched parseFill

ft_watcher: update names

ft_watcher: minor tweaks

ft_watcher: formatter

ft_watcher: fix tests

ft_watcher: add comment

Signed-off-by: bingyuyap <[email protected]>
  • Loading branch information
bingyuyap committed Aug 26, 2024
1 parent 71a6d43 commit af5e302
Show file tree
Hide file tree
Showing 19 changed files with 8,083 additions and 6 deletions.
47 changes: 47 additions & 0 deletions common/src/solana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
MessageV0,
PublicKeyInitData,
PublicKey,
VersionedTransactionResponse,
} from '@solana/web3.js';
import axios from 'axios';
import { decode } from 'bs58';
Expand Down Expand Up @@ -128,3 +129,49 @@ export function parseWormholeSequenceFromLogs(logs: string[]): number | null {
}
return null;
}

export function blockTimeToDate(blockTime: number) {
return new Date(blockTime * 1000);
}

/**
* Calculates the change in token balance for a specified token within an associated token account
* owned by a given owner. If the tokenBalance is not found, an amount of 0n is to be assumed
*
* @param transaction - The transaction object, which contains metadata and details about pre and post
* transaction states, including token balances.
* @param owner - The public key of the owner of the associated token account whose token balance
* change is to be calculated.
* @param mint - The mint address of the specific token for which the balance change is to be calculated.
*
* @returns The difference between the token balance after the transaction (`postTokenBalance`) and
* the token balance before the transaction (`preTokenBalance`). This value is returned as a `BigInt`
* and represents the change in token balance for the specified owner and token.
*/
export function getTokenBalanceChange(
transaction: VersionedTransactionResponse,
owner: string,
mint: string
) {
const preTokenBalances = transaction.meta?.preTokenBalances || [];
const postTokenBalances = transaction.meta?.postTokenBalances || [];

const preTokenBalance = preTokenBalances.find((tb) => tb.mint === mint && tb.owner === owner) || {
uiTokenAmount: {
amount: 0n,
},
};

const postTokenBalance = postTokenBalances.find(
(tb) => tb.mint === mint && tb.owner === owner
) || {
uiTokenAmount: {
amount: 0n,
},
};

const change =
BigInt(postTokenBalance.uiTokenAmount.amount) - BigInt(preTokenBalance.uiTokenAmount.amount);

return change;
}
2 changes: 1 addition & 1 deletion watcher/jest.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"testRegex": "(/__tests__/.*|/tests/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"],
"setupFiles": ["./setup-jest.ts"],
"testPathIgnorePatterns": ["sdk", "/node_modules/", "/dist/"]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"context": {
"apiVersion": "1.18.15",
"slot": 2860
},
"value": {
"data": {
"parsed": {
"info": {
"isNative": false,
"mint": "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
"owner": "HRTQAZJF7YNogjDUehhVShJxtFdBr8gNWsVJbvL1kvXU",
"state": "initialized",
"tokenAmount": {
"amount": "6900000000",
"decimals": 6,
"uiAmount": 6900,
"uiAmountString": "6900"
}
},
"type": "account"
},
"program": "spl-token",
"space": 165
},
"executable": false,
"lamports": 2039280,
"owner": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
"rentEpoch": 18446744073709552000,
"space": 165
}
}
Loading

0 comments on commit af5e302

Please sign in to comment.