Nodejs

我可以實時知道乙太幣何時從乙太坊進入我的賬戶嗎?

  • August 6, 2022

我想在我的 Dapp(nodejs) 中創建一個功能,當乙太幣實時進入我的帳戶時通知我。

例如,有沒有辦法使用 websockets 或 API?

誰能給我一些建議?

為此,您有多種選擇。一個簡單的方法是用於web3.eth.subscribe("pendingTransactions",callback)監聽到達記憶體池的傳入交易。然後,您可以使用您感興趣的帳戶/地址進行過濾tx.to === myAccountAddress

為了嘗試這個,我使用了一個 QuickNode ( https://www.quicknode.com/ ) 節點,連接到 Rinkeby 測試網網路,它工作正常:

const Web3 = require("web3");
const url =
 "wss://divine-shy-silence.rinkeby.discover.quiknode.pro/<your quicknode rinkeby api key>/";

const options = {
 timeout: 30000,
 clientConfig: {
   maxReceivedFrameSize: 100000000,
   maxReceivedMessageSize: 100000000,
 },
 reconnect: {
   auto: true,
   delay: 5000,
   maxAttempts: 15,
   onTimeout: false,
 },
};

const web3 = new Web3(new Web3.providers.WebsocketProvider(url, options));
const subscription = web3.eth.subscribe("pendingTransactions", (err, res) => {
 if (err) console.error(err);
});

const account1 = "0x6827b8f6cc60497d9bf5210d602C0EcaFDF7C405"; // Your test address 1
const account2 = "0x66B0b1d2930059407DcC30F1A2305435fc37315E"; // A second test address

const init = function () {
 subscription.on("data", (txHash) => {
   setTimeout(async () => {
     try {
       const tx = await web3.eth.getTransaction(txHash);
       if (tx.to === account1) {
           
         console.log("Receiving some eth for account 1: ", tx);
       } else if (tx.to === account2) {
         console.log("Receiving some eth for account 2: ", tx);
       }
     } catch (err) {
       console.error(err);
     }
   });
 });
};

init();

每次我的一個地址收到一些乙太幣時,都會列印出如下內容:

{
 blockHash: null,
 blockNumber: null,
 from: '0x6827b8f6cc60497d9bf5210d602C0EcaFDF7C405',
 gas: 21000,
 maxPriorityFeePerGas: '1500000000',
 maxFeePerGas: '1500000013',
 hash: '0x63dd06e925447df905bbd7d594354fb9c354874dc924f2e52a7d45d0c647787a',
 input: '0x',
 nonce: 98,
 to: '0x66B0b1d2930059407DcC30F1A2305435fc37315E',
 transactionIndex: null,
 value: '5310000000000000',
 type: 2,
 accessList: [],
 chainId: '0x4',
 v: '0x1',
 r: '0x1ecc9d1f58656771dccad5291a509a66224d5d8055ff2d705a77b7c8cc5c99fd',
 s: '0x39d71a67319c6f279f913b47b93f9e6acd3128d49ce762f33daeb24b95d3bcfe'
}

請注意,交易將在記憶體池中,尚未在區塊中確認,因為我們訂閱了pendingTransactions,但至少你知道這即將到來。之後,您可以嘗試通過其事務雜湊查詢它,以查看它是否已經在一個塊中。blockHash請注意,在上面的 tx 中, or沒有值blockNumber

之後我們可以做的是再次嘗試獲取 tx,web3.eth.getTransaction(txHash):看看它是否已經在一個塊中,確認後,tx 應該是這樣的:

{
 blockHash: '0x6f860b5f62096cceb523c077be35f7517d7752a713992573d789a7319863a617',
 blockNumber: 11156860,
 from: '0x6827b8f6cc60497d9bf5210d602C0EcaFDF7C405',
 gas: 21000,
 gasPrice: '1500000009',
 maxPriorityFeePerGas: '1500000000',
 maxFeePerGas: '1500000013',
 hash: '0x63dd06e925447df905bbd7d594354fb9c354874dc924f2e52a7d45d0c647787a',
 input: '0x',
 nonce: 98,
 to: '0x66B0b1d2930059407DcC30F1A2305435fc37315E',
 transactionIndex: 11,
 value: '5310000000000000',
 type: 2,
 accessList: [],
 chainId: '0x4',
 v: '0x1',
 r: '0x1ecc9d1f58656771dccad5291a509a66224d5d8055ff2d705a77b7c8cc5c99fd',
 s: '0x39d71a67319c6f279f913b47b93f9e6acd3128d49ce762f33daeb24b95d3bcfe'
}

您可以閱讀這篇向您展示如何操作的文章:https ://www.quicknode.com/guides/defi/how-to-access-ethereum-mempool

另一種選擇是執行您自己的測試網/主網乙太坊節點並使用 Geth 客戶端對其進行查詢。但這設置起來比較繁瑣。

引用自:https://ethereum.stackexchange.com/questions/133129