-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a5d022d
commit 579abdb
Showing
3 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`运行。 | ||
|
||
 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.