Snap Coin Pay
A payment management library to make deposits and withdrawals easy on the Snap Coin Network!
General
You probably also want to add the snap-coin, async-trait, and tokio libraries, since these are quite essential.
Usage
We must have some a connection to the Snap Coin Network, and there are two standardized ways to do this, and to create a ChainInteraction struct, that the deposit processor will use:
- Snap Coin API Client
use ApiChainInteraction;
let chain_interaction = new;
This line connects to a Snap Coin Node over the Snap Coin API (snap_coin::api) protocol on port 3003. This approach is great when you need more then one instance of this program running.
- Snap Coin Embedded Node, when we need maximum performance, and uptime.
use ;
use NodeChainInteraction;
let : = create_full_node;
let chain_interaction = new;
This line connects to a Snap Coin Node over the Snap Coin API protocol, port 3003. This approach is great when you need more then one instance of this program running.
Deposits
Handling (incoming) deposit transactions can be hard, since many factors like re-orgs, expiration, etc. can be daunting to implement, so Snap Coin Pay exposes the deposit_payment_processor module that handles it all for you!
We must also define on custom on deposit confirmation logic.
use DepositPaymentProcessor;
// We define deposit confirmation logic
// We create a instance of the confirmation ConfirmationHandler
let on_confirmation = ConfirmationHandler ;
// We create a deposit processor
let deposit_processor = new;
// We start the deposit processor (20 is the amount of confirmation required for a transaction to be confirmed. Depending on your application 10-20 is fine)
deposit_processor
.start
.await?;
Accepting deposits is easy, we just need to add a deposit address, and the deposit processor will automatically start listening for any deposits to it, and it will provide you with a deposit status.
let deposit_address = new_random.to_public; // Create some deposit address, in a real application you want to store this somewhere safe
println!;
// We tell the deposit processor to listen for events regarding this address. This does not persist between program runs - You need to re-add all deposit addresses every time the program starts
deposit_processor.add_deposit_address.await;
println!;
loop
The DepositProcessor struct has a few extra functions:
processor.stop()Stop the processor, and all it's underlying tasks.processor.remove_deposit_address(deposit_address: Public)Stop listening for deposits to this address.
Withdrawals
Snap Coin Pay also supports atomic withdrawals, meaning you can withdraw funds, without having to worry about forks and expiry. It is very similar to the DepositProcessor, and utilizes similar parameters.
let on_confirmation = WithdrawalConfirmationHandler ;
let withdrawal_processor = new;
// We start the withdrawal processor (20 is the amount of confirmation required for a transaction to be confirmed. Depending on your application 10-20 is fine)
withdrawal_processor.start.await?; // Amount
let wallet_private =
new_from_base36.unwrap;
let receiver =
new_from_base36.unwrap;
println!;
let withdrawal_id = withdrawal_processor // You receive a withdrawal ID (type = UUID), which you can use to get it's status at any time
.submit_withdrawal // 1 is amount of NANO (like sat to BTC, but for SNAP) to withdraw.
.await?;
loop