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.
Reliability
Deposits
Deposits are scanned atomically - even if the deposit processor is restarted or stopped for any amount of time, it will re-sync to the chain head and confirm all deposit addresses that were previously defined. Make sure you store all deposit addresses and re-add them on restart.
Withdrawals
Withdrawals are also handled atomically. Each withdrawal is assigned a stable WithdrawalId (UUID) that persists across resubmissions. If a transaction expires from the mempool or is dropped, it is automatically resubmitted. The processor resumes correctly after a restart, scanning from where it left off.
Usage
We must have some connection to the Snap Coin Network. There are two standardized ways to create a ChainInteraction struct:
Snap Coin API Client
Great when you need more than one instance of this program running.
use ApiChainInteraction;
let chain_interaction = new;
Snap Coin Embedded Node
Best for maximum performance and uptime.
use ;
use NodeChainInteraction;
let : =
create_full_node;
let chain_interaction = new;
Deposits
Handling incoming deposit transactions can be tricky - re-orgs, expiration, and confirmations are all handled for you by the DepositPaymentProcessor.
Define your confirmation logic:
use ;
Create and start the processor:
let on_confirmation = ConfirmationHandler ;
// Pass an optional path for the scanned-height save file (defaults to ./snap-pay-deposit-scanned-height)
let deposit_processor = create;
// 10 is the number of confirmations required. 10–20 is typical depending on your application.
deposit_processor.start.await?;
Add deposit addresses and poll for status:
// Create a deposit address - in a real app, store this somewhere persistent
let deposit_address = new_random.to_public;
println!;
// Tell the processor to watch this address.
// Note: this does NOT persist across restarts - re-add all addresses on every startup.
deposit_processor.add_deposit_address.await;
println!;
loop
Additional DepositPaymentProcessor methods:
processor.stop()- Stop the processor and all underlying tasks.processor.remove_deposit_address(deposit_address: Public)- Stop watching an address.
Withdrawals
Snap Coin Pay supports fully atomic withdrawals. Transactions that expire from the mempool are automatically resubmitted. Each withdrawal has a stable WithdrawalId you can use to query status at any time. The WithdrawalPaymentProcessor has persistent state of all the ongoing withdrawals, so you do not need to store these manually. The processor is atomic, meaning it can be stopped mid withdrawals, and it will still handle everything correctly. The withdrawal processor also automatically logs all withdrawals to the withdrawal log, with the sender's private key, for emergency recovery and monitoring.
Define your confirmation logic:
use ;
Create and start the processor:
let on_confirmation = WithdrawalConfirmationHandler ;
// Pass optional paths for the DB, scanned-height save files and the withdrawal log
let withdrawal_processor = create;
// 10 is the number of confirmations required. 10–20 is typical.
withdrawal_processor.start.await?;
Submit a withdrawal and poll for status:
let wallet_private =
new_from_base36.unwrap;
let receiver =
new_from_base36.unwrap;
println!;
// submit_withdrawal returns a WithdrawalId (UUID) you can use to track status
let withdrawal_id = withdrawal_processor
.submit_withdrawal // amount is in NANO (smallest unit)
.await?;
loop
Additional WithdrawalPaymentProcessor methods:
processor.stop()- Stop the processor and all underlying tasks.processor.untrack_withdrawal(withdrawal_id: WithdrawalId)- Remove a withdrawal from tracking (e.g. after confirmed and fully processed).processor.get_all_withdrawals()- Returns aHashMap<WithdrawalId, WithdrawalStatus>of all tracked withdrawals.
Withdrawal statuses
Pending { transaction }- Submitted and waiting in the mempool.Confirming { transaction }- Included in a block, accumulating confirmations.Expired- Dropped from the mempool; being automatically resubmitted.Confirmed { transaction }- Finalized with sufficient confirmations.