Skip to content

Commit

Permalink
update example-01-subWallet
Browse files Browse the repository at this point in the history
  • Loading branch information
XingqiWang committed Nov 16, 2024
1 parent a5d022d commit 579abdb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
9 changes: 9 additions & 0 deletions example-01-subWallet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# 监听钱包

此例子结合 `onAccountChange``getSignaturesForAddress` 方法来订阅指定账户的改变并获取指定账户的最新交易。

获取到最新交易后,将通过telegram bot发送通知。

通过 `npx esrun example-01-subWallet/index.ts`运行。

![](../img/example-01-01.png)
43 changes: 43 additions & 0 deletions example-01-subWallet/index.ts
Original file line number Diff line number Diff line change
@@ -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'
);
Binary file added img/example-01-01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 579abdb

Please sign in to comment.