3

So right now I'm calling getSignatures and getting blocktime of the earliest one I get returned. Is there a more efficient way of doing this?

2
  • I could be wrong but unless you stored the creation time in the account data, there is no accurate way of getting the account age from the chain itself.
    – sohrab
    Commented Sep 4, 2022 at 12:19
  • Yea I think youre right thats why Im doing it this way to get a rough estimation. I guess Ill keep doing it this way as I dont need it to be that accurate its for ui purposes only. This is for some nfts so Ive added timestamp field on offchain data.
    – Noe Trejo
    Commented Sep 5, 2022 at 4:25

2 Answers 2

1
const RPC_ENDPOINT = "xxx-xxx";

const connection = new Connection(RPC_ENDPOINT, 'confirmed', {
    commitment: 'confirmed',
    timeout: 10000
});
const signatures = await connection.getSignaturesForAddress(mint);

console.log('signatures', signatures[signatures.length - 1]);

You can get the oldest(first) Tx signature with blockTime.

1

The code example that Bojan provided only gets you the age of the first transaction of the function call getSignaturesForAddress, which could be either the actual first transaction or the 1000th transaction, as that function can only return up to 1000 signatures. However, the function also accepts a parameter called before which you can provide to fetch only signatures that of an older date than the provided before signature. What you can do is to iteratively fetch all transactions and extract the blocktime of the actual last signature. Here is an example code on how to achieve this:

async function getSignatures(address:string) {
    let signatures: any[] = [];
    let connection = new Connection("https://api.mainnet-beta.solana.com");
    let initialSignatures = await connection.getSignaturesForAddress(new PublicKey(address));
    signatures.push(...initialSignatures);
    while (initialSignatures.length == 1000) {
        const options = {
            before: initialSignatures[initialSignatures.length - 1].signature,
        };
        initialSignatures = await connection.getSignaturesForAddress(new PublicKey(address), options);
        signatures.push(...initialSignatures);
    }
    return signatures;
}
async function getWalletAge(address:string) {
    const signatures = await getSignatures(address);
    return signatures[signatures.length - 1].blockTime;
}

Please keep in mind: Wallets such as exchanges and AMMs have a significantly larger number of transactions, which may lead to very inefficient execution time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.