Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get block time of the block where the transaction is located #452

Closed
uetkaje opened this issue Nov 10, 2024 · 5 comments
Closed

Get block time of the block where the transaction is located #452

uetkaje opened this issue Nov 10, 2024 · 5 comments

Comments

@uetkaje
Copy link

uetkaje commented Nov 10, 2024

Hi,

I need to get block time where the transaction is located.

{
        accounts: {},
        slots: {},
        transactions: {
            pumpFun: {
                accountInclude: ["6EF8rrecthR5Dkzon8Nwu78hRvfCKubJ14M5uBEwF6P"],
                accountExclude: [],
                accountRequired: []
            }
        },
        transactionsStatus: {},
        entry: {},
	blocks: {},
        blocksMeta: {
		blockMeta: {}
	},
        commitment: "confirmed",
        accountsDataSlice: [],
        ping: undefined
}

With above subscribe request, the transaction message comes before the block meta message. So I can't get block time using the transaction's slot. Is there any way to achieve this?

Thanks in advance.

Best regards,
Emre

@fanatid
Copy link
Collaborator

fanatid commented Nov 10, 2024

on processed commitment level it's not possible to get block time before tx

@benjiqq
Copy link

benjiqq commented Nov 14, 2024

@uetkaje your example commit level is confirmed. try processed? then map after the after fact. not sure whats the best way I created this issue for general case #455

@fanatid
Copy link
Collaborator

fanatid commented Nov 14, 2024

issue for this: #228

@fanatid fanatid closed this as completed Nov 14, 2024
@uetkaje
Copy link
Author

uetkaje commented Nov 14, 2024

@benjiqq I tried processed commitment. As @fanatid said, transactions come before block meta. There is no way to do this.

Temporary workaround:

const blocks: Record<string, number> = {};

const handleBlockMeta = async (data: SubscribeUpdate): Promise<void> => {
	blocks[data.blockMeta.slot] = parseInt(data.blockMeta.blockTime!.timestamp);
};

const handleTransaction = async (data: SubscribeUpdate): Promise<void> => {
	const transaction = data.transaction?.transaction;
	const message = transaction?.transaction?.message;
	const meta = transaction?.meta;
	if (!transaction || !message || !meta)
        	return;
	if (meta?.err)
		return;
	const slot = parseInt(data.transaction.slot);
	const maxSlot = Math.max(...Object.keys(blocks).map(b => parseInt(b)));
	const timestamp = Math.floor(((blocks[maxSlot.toString()] * 1000) + ((slot - maxSlot) * 400)) / 1000);
};

I will track issue #228

Thanks a lot.

@benjiqq
Copy link

benjiqq commented Nov 15, 2024

@uetkaje thats a good approach, not sure whyat block data comes after tx?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants