Try   HackMD

Bufficorn Trait Swapping Architecture

Database Schema

Swap Schema

field type
id MongoId
swapInitiator Text
swapInitiatorBufficorn Number
swapInitiatedTimestamp Date
swapInitiateSporkTx Text
swapExecutor Text
swapExecutorBufficorn Number
swapExecutedTimestamp Date
swapExecutedSporkTx Text
traitsToSwap [Enum]
stakeStatus Enum

Escrow Contract

Made an escrow contract gist for ideation. Bufficorn Trait Swapper Escrow

Go Grazing:

  1. list bufficorns the wallet owns (using alchemy api)
  2. interact with grazing contract & go grazing

Gone Home:

  1. list bufficorns the wallet has put for grazing (using subgraph)

validations before going home:
- check if min grazing period over

  1. interact with grazing contract & gone home

Trait Swapping:

As a swap initiator:

  1. choose my bufficorn that's grazing to be trait swapped
  2. enter token id of the other bufficorn to be trait swapped
  3. choose traits to be swapped

validations before initiating swap:
- check if the other bufficorn is grazing (using grazing contract)
- check if the initiator bufficorn is locked in any other swap (using escrow contract)
- check if the executor bufficorn is locked in any other swap (using escrow contract)
- check if initiator wallet has enough spork token balance

  1. create a swap record (using database)
  2. with the swap id from previous step, deposit spork token (interact with the initiateTraitSwap() in escrow contract)
  3. provide the swap id to the other party (swap executor) to execute.

As a swap executor:

  1. Enter the swap id received from swap initiator.

validations before executing the swap:
- check if the swap id is valid & the stake status(using database)
- check if the swap window over (using database)
- check if the executor bufficorn is locked in any other swap (using escrow contract)
- check if executor wallet has enough spork token balance

  1. with the swap id, deposit spork token (interact with the executeTraitSwap() in escrow contract)
  2. on successful tx, update the corresponding swap info in the database using the swap id.

Withdraw stake
If the executor fails to finish the swap, the initiator can use the withdrawSporkStake() function in escrow contract to get back his stake.

  1. Enter swap id to withdraw stake.

validations before withdrawing the stake:
- check if the swap id is valid & the stake status(using database)
- check if the swap window over (using database)
- check if the requesting address is a swap initiator for the corresponding swap
- check if the escrow contract has enough spork balance to send back the stake

  1. return spork token & on successful tx, update the corresponding stake info in the database using the swap id.