版本
0.2.4 | 2022 年 1 月 |
---|---|
0.2.3 | 2021 年 12 月 30 日 |
0.1.8 | 2021 年 12 月 22 日 |
0.1.4 | 2021 年 11 月 30 日 |
0.1.0 |
|
12 在 #synchronized
193 每月下载
40KB
898 行
Solana 阴影
Solana 阴影 crate 为离线处理添加了 solana 链上账户的阴影。它实时同步所有账户及其与程序相关的数据,并允许离线机器人对这些账户的更改做出反应。
使用
在 Cargo.toml
中添加此库。
[dependencies]
solana-shadow = "*"
查看 examples/
目录以获取使用示例。
镜像程序 ID 及其所有拥有的账户
// this is the prog id that owns all pyth oracles on mainnet
let prog = "FsJ3A3u2vn5cTVofAjvy6y5kwABJAqYWpe4975bi2epH".parse()?;
let network = Network::Mainnet;
let local = BlockchainShadow::new_for_program(&prog, network).await?;
loop {
local.for_each_account(|pubkey, account| {
println!(" - [{}]: {:?}", pubkey, account);
});
sleep(Duration::from_secs(3)).await;
}
local.worker().await?;
镜像少量随机账户
// https://pyth.network/developers/accounts/
let ethusd = "JBu1AL4obBcCMqKBBxhpWCNUt136ijcuMZLFvTP7iWdB".parse()?;
let btcusd = "GVXRSBjFk6e6J3NbVPXohDJetcTjaeeuykUpbQF8UoMU".parse()?;
let local = BlockchainShadow::new_for_accounts(&vec![ethusd, btcusd], Network::Mainnet).await?;
loop {
let ethacc = shadow.get_account(ðusd).unwrap();
let ethprice = cast::<Price>(ðacc.data).agg.price;
let btcacc = shadow.get_account(&btcusd).unwrap();
let btcprice = cast::<Price>(&btcacc.data).agg.price;
println!("ETH/USD: {}", ethprice);
println!("BTC/USD: {}", btcprice);
sleep(Duration::from_secs(3)).await;
}
local.worker().await?;
监听账户的更改
// https://pyth.network/developers/accounts/
let ethusd = "JBu1AL4obBcCMqKBBxhpWCNUt136ijcuMZLFvTP7iWdB".parse()?;
let btcusd = "GVXRSBjFk6e6J3NbVPXohDJetcTjaeeuykUpbQF8UoMU".parse()?;
let solusd = "H6ARHf6YXhGYeQfUzQNGk6rDNnLBQKrenN712K4AQJEG".parse()?;
// create an offline shadow of the on-chain data.
// whenever the data change on-chain those changes
// will be reflected immediately in this type.
let shadow = BlockchainShadow::new_for_accounts(
&vec![ethusd, btcusd, solusd],
Network::Mainnet,
)
.await?;
tokio::spawn(async move {
// start printing updates only after 5 seconds
tokio::time::sleep(Duration::from_secs(5)).await;
// now everytime an account changes, its pubkey will be
// broadcasted to all receivers that are waiting on updates.
while let Ok((pubkey, account)) = updates_channel.recv().await {
let price = cast::<Price>(&account.data).agg.price;
println!("account updated: {}: {}", &pubkey, price);
}
});
shadow.worker().await?;
依赖
76MB
~1.5M SLoC