1 个不稳定版本
0.3.1 | 2024年8月5日 |
---|---|
0.3.0 |
|
0.2.0 |
|
#729 in 神奇豆
每月300次下载
45KB
753 行
solana-payments
用Rust编写的轻量级库,用于解析和创建索拉纳支付URL。
使用SPL令牌和查找函数解析URL
#[tokio::main]
async fn main() {
use solana_payments::SolanaPayUrl;
use solana_client::nonblocking::rpc_client::RpcClient;
use spl_token_2022::{state::Mint, extension::StateWithExtensions};
let transfer_1_sol = "solana:mvines9iiHiQTysrwkJjGf2gb9Ex9jXJX8ns3qwf2kN?amount=1&label=Michael&message=Thanks%20for%20all%20the%20fish&memo=OrderId12345";
// parsing requires a lookup function to be passed in that fetches the account
// data of the spl-token token mint account which the deserializes it's data to get
// the number of decimals configured for the Mint
let lookup_fn = |public_key: [u8; 32]| async move {
let client = RpcClient::new("https://api.mainnet-beta.solana.com".into());
let public_key = solana_program::pubkey::Pubkey::new_from_array(public_key);
let account = client.get_account(&public_key).await.unwrap();
let mint = StateWithExtensions::<Mint>::unpack(&account.data).unwrap();
mint.base.decimals
};
let url = "solana:mvines9iiHiQTysrwkJjGf2gb9Ex9jXJX8ns3qwf2kN?amount=0.01&spl-token=EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v";
let url_decoded = SolanaPayUrl::new().parse(&url, lookup_fn).await.unwrap();
dbg!(url_decoded);
}
返回以下输出
SolanaPayUrl {
recipient: PublicKey(mvines9iiHiQTysrwkJjGf2gb9Ex9jXJX8ns3qwf2kN),
amount: Some(
Number {
integral: 0,
fractional: 1,
leading_zeroes: 1,
significant_digits_count: 1,
as_string: "0.01",
total_fractional_count: 2,
},
),
spl_token: Some(
PublicKey(EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v),
),
references: [],
label: None,
message: None,
spl_memo: None,
}
使用查找函数解析原生SOL的URL
use solana_payments::SolanaPayUrl;
let transfer_1_sol = "solana:mvines9iiHiQTysrwkJjGf2gb9Ex9jXJX8ns3qwf2kN?amount=1&label=Michael&message=Thanks%20for%20all%20the%20fish&memo=OrderId12345";
// Return native SOL decimals
let lookup_fn = |_value| async { 9 };
let transfer_1_sol_decoded = smol::block_on(async {
solana_payments::SolanaPayUrl::new()
.parse(transfer_1_sol, lookup_fn)
.await
.unwrap()
});
// Return native SOL decimals from the [solana_payments::Utils](crate::Utils::native_sol)
// helper if you don't want to send a request to an RPC to lookup native SOL decimals
let transfer_1_sol_decoded = smol::block_on(async {
SolanaPayUrl::new()
.parse(transfer_1_sol, solana_payments::Utils::native_sol)
.await
.unwrap()
});
从头开始构建索拉纳支付URL
use solana_payments::SolanaPayUrl;
let url_encoded_str = SolanaPayUrl::default()
.add_recipient("mvines9iiHiQTysrwkJjGf2gb9Ex9jXJX8ns3qwf2kN")
.unwrap()
.add_amount("1")
.unwrap()
.add_spl_token("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v")
.unwrap()
.add_label("Michael")
.unwrap()
.add_message("Thanks for all the fish")
.unwrap()
.add_spl_memo("OrderId12345")
.unwrap()
.add_reference("7owWEdgJRWpKsiDFNU4qT2kgMe2kitPXem5Yy8VdNatx")
.unwrap()
.add_reference_multiple(&[
"7owWEdgJRWpKsiDFNU4qT2kgMe2kitPXem5Yy8VdNatx",
"7owWEdgJRWpKsiDFNU4qT2kgMe2kitPXem5Yy8VdNatx",
"7owWEdgJRWpKsiDFNU4qT2kgMe2kitPXem5Yy8VdNatx",
])
.unwrap()
.to_url();
许可证
此作品根据CC0-1.0 许可证发布于公共领域,或者根据Apache-2.0 许可证授权
依赖项
~5MB
~111K SLoC