Atomic Swap on Bitcoin SV

Atomic swap is the trustless exchange of different assets without counterparty risk. We show a general method to achieve atomic swap of bitcoins and tokens on the same underlying BSV blockchain.

Token Sale Contract

TokenSale Contract

As in the previous articles, Line 15 & 17 ensure the preimage is from the current transaction. Line 23 reads previous contract balance, i.e., amount of bitcoins in the output spent by this input. Line 25 calculates new contract balance. Line 28 allocates tokens to the buyer. The rest validates new bitcoin and token balances are in the new output. Here is some sample code using this contract.


  • swap bitcoins for layer 2 tokens; layer 1 tokens for layer 2 tokens; one layer 1/2 token for another layer 1/2 token
  • add another public function for the issuer(s) to redeem the funds in the contract
  • vary token price based on purchase time and setup token release time, by obtaining time using techniques in contract CheckLockTimeVerify
  • merge token records if same buyer purchases multiple times


  • It is non-interactive. It requires no back and forth communication or cooperation between involved parties. Thus many issues caused by one party becoming unresponsive are completely avoided.
  • It does not require timelocks and thus does not lockup funds.

A swap is contained in a single transaction calling the contract. Either it is accepted, bitcoins and tokens are exchanged instantly; or it is rejected and the exchange does not happen at all. It is truly atomic since there is no intermediate state for the transaction.

Update (05/27/2020)

Token table after 5 sales

Disclaimer: this article focuses on technical aspects of atomic swaps and does not cover legal aspects.

[1] For simplicity, same buyer (i.e., public key) is used. It is trivial to change to multiple public keys, each representing a buyer.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store

sCrypt ( is a company with a mission to provide integrated on-chain smart contracting solutions on Bitcoin Satoshi Vision