Skip to main content

Find Whale Transaction

Updated on
Jun 29, 2024

Overview

Blockchain adoption has reached a point where millions of transactions occur daily. While most of these transactions are relatively small, wealthy cryptocurrency participants (whales) are also making significant moves.

In this function example, we will get transactions with native asset transfers and return the ones that exceed a given threshold..

What We Will Do

For this example, we will extract the native asset transfers above a given threshold. We will demonstrate this in two ways: 



  1. A standalone Function which can be called via API, passing the transaction size as a parameter.
  2. A transformation that can be applied to a QuickNode Stream, feeding the output to the destination of your choice, block by block.

Sample Function

The following Function code extracts Ethereum whale transactions in JavaScript Node.js v20 runtime using the block dataset:

function main(params) {
// Extract block data from the params object
const block = params.data[0];

// Extract metadata
const network = params.metadata.network;
const blockNo = hexToInt(block.number);

// Configure native asset details. On Ethereum, this is eth, Polygon it is matic, etc.
const asset = {
units: "eth",
decimals: 18
};

// Apply the minimum tx size to filter on, via the value passed in through user_data
const minTxSize_dec = params.user_data.minTxSize;

// Convert the tx size to the number stored onchain, with decimal places added
const minTxSize_val = decToVal(minTxSize_dec, asset);

// Extract matching transactions
const transactions = block.transactions;
const txArray_raw = transactions.filter(obj => hexToInt(obj.value) >= minTxSize_val);
const count = txArray_raw.length;

// Decode transactions into a cleaner format
const txArray_clean = [];
txArray_raw.forEach((tx, txIndex) => {
const value_clean = valToDec(hexToInt(tx.value), asset);
txArray_clean.push({
hash: tx.hash,
to: tx.to,
from: tx.from,
value: value_clean,
units: asset.units
});
});

// While debugging, it is useful to include raw tx array, only return the necessary data in prod
const returnObj = {
block: blockNo,
network: network,
result: `${count} transaction(s) w/ value > ${minTxSize_dec} ${asset.units}`,
transactions_clean: txArray_clean,
transactions_raw: txArray_raw
};

return returnObj;
}

function hexToInt(hex) {
let decodedVal = hex.substring(2);
decodedVal = parseInt(decodedVal, 16);

return decodedVal;
}

function hexToStr(hex) {
let decodedStr = hex.substring(2);
decodedStr = Buffer(decodedStr, 'hex').toString();

return decodedStr;
}

function valToDec(val, asset) {
const dec = val / Math.pow(10, asset.decimals);
return dec;
}

function decToVal(dec, asset) {
const val = dec * Math.pow(10, asset.decimals);
return val;
}

Request

We will invoke the function with the following cURL command. Be sure to replace the YOUR_API_KEY with your own QuickNode API key and the POST URL with the URL of your Function.

curl -X POST "https://api.quicknode.com/functions/rest/v1/namespaces/0f6812dd-a17f-4cbc-9ab4-7a529eb33940/functions/whale-transactions/call" \
-H "accept: application/json" \
-H "Content-Type: application/json" \
-H "x-api-key: YOUR_API_KEY" \
-d '{
"network": "ethereum-mainnet",
"dataset": "block",
"block": "latest",
"user_data": {
"minTxSize": 5
}
}'

Response

Resulting in the following response:

{
"response": {
"activationId": "aac26c1e2f1a4cea826c1e2f1a5ceacc",
"annotations": [
{
"key": "path",
"value": "e6639b18-a87c-470f-830e-18d66e62becd/whale-transactions"
},
{
"key": "waitTime",
"value": 26
},
{
"key": "kind",
"value": "nodejs:20"
},
{
"key": "timeout",
"value": false
},
{
"key": "limits",
"value": {
"concurrency": 1,
"logs": 10,
"memory": 256,
"timeout": 60000
}
}
],
"duration": 5,
"end": 1719534186910,
"logs": [],
"name": "whale-transactions",
"namespace": "e6639b18-a87c-470f-830e-18d66e62becd",
"publish": false,
"response": {
"result": {
"block": 20186576,
"network": "ethereum-mainnet",
"result": "1 transaction(s) w/ value > 5 eth",
"transactions_clean": [
{
"from": "0xdb3d858dcc295be2309e69539b2de07b41d658f7",
"hash": "0xa10eb45ff44c35369ecfa3a8ed3cf96391d7bf59427dbd6fa5abdf487e9a8167",
"to": "0x88cb830539ed194cc98e55ef5036ff35c13d1ed1",
"units": "eth",
"value": 20.45
}
],
"transactions_raw": [
{
"blockHash": "0x1c8c3fe8e32304339b9ec38ea168f1439d84f28d24b21a1eaddb24ae0bc4acdb",
"blockNumber": "0x13405d0",
"chainId": "0x1",
"from": "0xdb3d858dcc295be2309e69539b2de07b41d658f7",
"gas": "0x5208",
"gasPrice": "0x12a05f200",
"hash": "0xa10eb45ff44c35369ecfa3a8ed3cf96391d7bf59427dbd6fa5abdf487e9a8167",
"input": "0x",
"nonce": "0x101a6",
"r": "0x58ebbc58c30a31f888c7524074ff19af802f809cd8aa0941fbceca7ec31cc31a",
"s": "0x588512f3e49c3b306d2ca5d4a7c277aa57b165364f5c8bc13981b034bb82b373",
"to": "0x88cb830539ed194cc98e55ef5036ff35c13d1ed1",
"transactionIndex": "0x77",
"type": "0x0",
"v": "0x26",
"value": "0x11bccfea6b8bd0000"
}
]
},
"size": 944,
"status": "success",
"success": true
},
"start": 1719534186905,
"subject": "934ecff2-da9b-4b8e-8e74-ac81b0ebf3d4",
"version": "0.0.18"
}
}

Learn more about QuickNode Functions.

Sample Stream Transformation

The following code can be used in a QuickNode Stream to filter and transform the output:

Try it yourself by deploying the filtered Stream to your account.

function main(data) {
// Extract block data from the params object
const block = data.streamData;

// Extract metadata
const blockNo = hexToInt(block.number);

// Configure native asset details. On Ethereum, this is eth, Polygon it is matic, etc.
const asset = {
units: "eth",
decimals: 18
};

// Apply the minimum tx size to filter on
const minTxSize_dec = 5;

// Convert the tx size to the number stored onchain, with decimal places added
const minTxSize_val = decToVal(minTxSize_dec, asset);

// Extract matching transactions
const transactions = block.transactions;
const txArray_raw = transactions.filter(obj => hexToInt(obj.value) >= minTxSize_val);
const count = txArray_raw.length;

// Decode transactions into a cleaner format
const txArray_clean = [];
txArray_raw.forEach((tx, txIndex) => {
const value_clean = valToDec(hexToInt(tx.value), asset);
txArray_clean.push({
hash: tx.hash,
to: tx.to,
from: tx.from,
value: value_clean,
units: asset.units
});
});

// While debugging, it is useful to include raw tx array, only return the necessary data in prod
const returnObj = {
block: blockNo,
result: `${count} transaction(s) w/ value > ${minTxSize_dec} ${asset.units}`,
transactions_clean: txArray_clean,
transactions_raw: txArray_raw
};

return returnObj;
}

function hexToInt(hex) {
let decodedVal = hex.substring(2);
decodedVal = parseInt(decodedVal, 16);

return decodedVal;
}

function hexToStr(hex) {
let decodedStr = hex.substring(2);
decodedStr = Buffer(decodedStr, 'hex').toString();

return decodedStr;
}

function valToDec(val, asset) {
const dec = val / Math.pow(10, asset.decimals);
return dec;
}

function decToVal(dec, asset) {
const val = dec * Math.pow(10, asset.decimals);
return val;
}

Output

Resulting in the following output:

{
"block": 20187607,
"result": "1 transaction(s) w/ value > 5 eth",
"transactions_clean": [
{
"from": "0xbf0181f23e53a1bb6deec9c8c01b6db590da1b40",
"hash": "0xf8c2abfc4534eecec832367fa264dc97dccec1ae8d0424625c67950e230237a6",
"to": "0x7a250d5630b4cf539739df2c5dacb4c659f2488d",
"units": "eth",
"value": 10
}
],
"transactions_raw": [
{
"accessList": [],
"blockHash": "0x472b139d2c6222e289928a2ce6874850a156a5d34d4c0a64d14ece6e1667d2df",
"blockNumber": "0x13409d7",
"chainId": "0x1",
"from": "0xbf0181f23e53a1bb6deec9c8c01b6db590da1b40",
"gas": "0x32af7e",
"gasPrice": "0x11580ce6d",
"hash": "0xf8c2abfc4534eecec832367fa264dc97dccec1ae8d0424625c67950e230237a6",
"input": "0xf305d71900000000000000000000000076d713ef778acb0a8fa99574fb15a1c2786c49f50000000000000000000000000000000000000000000000008ac7230489e800000000000000000000000000000000000000000000000000008ac7230489e800000000000000000000000000000000000000000000000000008ac7230489e80000000000000000000000000000bf0181f23e53a1bb6deec9c8c01b6db590da1b4000000000000000000000000000000000000000000000000000000000667e3527",
"maxFeePerGas": "0x168fd314d",
"maxPriorityFeePerGas": "0x38f27c79",
"nonce": "0x3d",
"r": "0x734aa57549a899bd78811f28c580f18ac9fe6014907e017eee51e8dfe30977",
"s": "0x351ef09040f88aed41286a2bf9ad9d60d956bbaa03a13ae6c132470977bded1e",
"to": "0x7a250d5630b4cf539739df2c5dacb4c659f2488d",
"transactionIndex": "0x5d",
"type": "0x2",
"v": "0x0",
"value": "0x8ac7230489e80000",
"yParity": "0x0"
}
]
}

Learn more about modifying Stream output.

We ❤️ Feedback!

If you have any feedback or questions about this documentation, let us know. We'd love to hear from you!

Share this doc