DEV Community

lmxdawn
lmxdawn

Posted on

Implementation of cryptocurrency (blockchain) deposit and withdrawal services

sequence

This article mainly describes the implementation principles of the three services of cryptocurrency deposit/withdrawal/collection required by the exchange

Prepare

To achieve these functions, we must first figure out what we need to accomplish

  1. Get the latest block
  2. Get transaction records inside the block
  3. Get the completion status of the transaction by the transaction hash
  4. Get the balance of an address
  5. Create a wallet
  6. Sign and send naked transactions
  7. Define the interface as follows
type Worker interface {
 getNowBlockNum() (uint64, error)
 getTransaction(uint64) ([]types.Transaction, uint64, error)
 getTransactionReceipt(*types.Transaction) error
 getBalance(address string) (*big.Int, error)
 createWallet() (*types.Wallet, error)
 sendTransaction(string, string, *big.Int) (string, error)
}
Enter fullscreen mode Exit fullscreen mode

accomplish

After creating a wallet, save the address and private key

recharge

Through an infinite loop service, the transaction data of the latest block is continuously obtained, and the transaction data is verified one by one whether it is completed
. Here, it is judged whether the receiving address (to) of the transaction data belongs to the wallet address created by this service. The wallet address created by the service is judged to be recharged successfully .

withdraw

The user initiates a withdrawal operation, and the balance here is the balance in the logical service. When the user initiates a withdrawal, the private key for the withdrawal configured by the wallet service is used to package and sign the bare transaction. (The withdrawal private key is transferred to the withdrawal address entered by the user), here the hash of the submitted bare transaction is recorded to the wallet service
to continuously obtain the transaction data of the latest block through an infinite loop service, and the transaction data are all one by one. Whether the verification is completed
, here it is judged whether the hash of the transaction data exists in the wallet service, and if so, it will be processed (the transaction hash needs to be done in the logic service for idempotency)

collection

The wallet created by the regular circulation wallet service is transferred to the collection address configured by the wallet service. Here, it is necessary to pay attention to the limit of the collection quantity, and transfer only when the fixed quantity is met (reduce the handling fee)

a simple example

github address: golang implements cryptocurrency deposit/withdrawal/collection services

Top comments (0)