diff --git a/example-01-subWallet/README.md b/example-01-subWallet/README.md new file mode 100644 index 0000000..5ae1ccd --- /dev/null +++ b/example-01-subWallet/README.md @@ -0,0 +1,9 @@ +# 监听钱包 + +此例子结合 `onAccountChange` 和 `getSignaturesForAddress` 方法来订阅指定账户的改变并获取指定账户的最新交易。 + +获取到最新交易后,将通过telegram bot发送通知。 + +通过 `npx esrun example-01-subWallet/index.ts`运行。 + +![](../img/example-01-01.png) \ No newline at end of file diff --git a/example-01-subWallet/index.ts b/example-01-subWallet/index.ts new file mode 100644 index 0000000..7a645b8 --- /dev/null +++ b/example-01-subWallet/index.ts @@ -0,0 +1,43 @@ +import { + Connection, + PublicKey +} from '@solana/web3.js'; +import axios from 'axios'; + +const connection = new Connection("https://api.mainnet-beta.solana.com", "confirmed"); +// const connection = new Connection("https://mainnet-ams.chainbuff.com", "confirmed"); +const publicKey = new PublicKey('orcACRJYTFjTeo2pV8TfYRTpmqfoYgbVi9GeANXTCc8'); +const botToken = ''; +const chatId = ''; + +async function sendMessage(message) { + const url = `https://api.telegram.org/bot${botToken}/sendMessage`; + + try { + await axios.post(url, { + chat_id: chatId, + text: message, + parse_mode: 'HTML', + disable_web_page_preview: true, + }); + + } catch (error) { + console.error('Error sending message:', error.message); + } +} + +// async function test() { +// const sig = await connection.getSignaturesForAddress(publicKey, {limit: 1}, 'confirmed'); +// await sendMessage(`新交易!\n\nhttps://solscan.io/tx/${sig[0].signature}`) +// } + +// test(); + +connection.onAccountChange( + publicKey, + async () => { + const sig = await connection.getSignaturesForAddress(publicKey, {limit: 1}, 'confirmed'); + await sendMessage(`新交易!\n\nhttps://solscan.io/tx/${sig[0].signature}`) + }, + 'confirmed' +); \ No newline at end of file diff --git a/img/example-01-01.png b/img/example-01-01.png new file mode 100644 index 0000000..33b585b Binary files /dev/null and b/img/example-01-01.png differ