Skip to content

Advanced transactions

Igor Grkavac edited this page Sep 28, 2021 · 6 revisions

In addition to regular transactions that are common in almost every blockchain (sending and receiving funds), Safex also introduced advanced transactions for marketplace usage:

Each transaction is checked on the blockchain side like regular transactions. Additionally, each advanced transaction is validated in two more ways:

  • Transaction input(checked in command.cpp) which checks command input from the transaction
  • Transaction output(checked in blockchain.cpp) which checks if all the outputs and inputs are correct and all the logic is satisfied

Account Creation

Safex Account is needed if the user wants to sell an item or create a blockchain-wide price oracle.

Safex account creation command contains 3 fields:

  • Username
  • Public key
  • Data

Safex Account is generated with the usage of Safex wallet. The wallet generates a pair of secret/public keys just like for a regular address. This additional pair of keys (beside spend and view keys) allow the user to restore and manipulate his account from any wallet and address as long as it has his Safex account secret key.

When the user creates a Safex account creation tx, he needs to lock SFT (1000 SFT on Mainnet [file cryptonote_config.h line: 199]) for some period of blocks (22000 blocks on Mainnet [file cryptonote_config.h line: 203]) Locking SFT prevents overpopulation of the accounts on the chain. After the given period, SFT will be unlocked and available for sending/locking again/staking. To prevent denial from node when adding locked output to mixin for other users, Safex daemon is marking outputs that are used for Safex account creation when the lock period is not over as locked [file blockchain.cpp function: get_outs].

Tx inputs Tx outputs
txin_to_script create_account SFT to lock and Safex account information txout_to_script out_safex_account Safex account creation output
txin_token_to_key Additional SFT to lock txout_to_key Change for SFX
txout_token_to_key Locked SFT
txin_to_key SFX for transfer fee txout_token_to_key Change for SFT

The Safex related checks (file command.cpp / function create_account::validate)for this transaction are:

  • Token amount in the command input cannot be 0
    – As command input is spending some SFT, so the input cannot be 0
  • Username can have only lowercase letters, digits, dash(-), and underscore(_)
  • Username must be unique (does not exist in the DB)
  • Public key must be valid
  • Username can be up to 32 characters long
  • Account data can be up to 2048 characters long
    The Safex transaction related check (file blockchain.cpp / function check_safex_tx_command) for this transaction are:
  • Safex account fields in the output must match the ones from the input
  • Check if token output with exact token amount is in the outputs
    – With this check, we can mark it as the locked amount and can deny it if a user tries to use it before the locking period

After this transaction:

  • Safex account token fee is locked
  • Safex account is stored in the LMDB
  • For safety purposes, on the wallet side, Safex Account can be used after 10 blocks are passed

Account Edit

If the user wants to change his Safex Account data (e.g. if he stored a link to his website and he moved to some other webpage) Safex Account data can be edited with edit account transactions.

Safex account edit command contains 2 fields:

  • Username
  • Data
Tx inputs Tx outputs
txin_to_script edit_account Reference to Safex Account and new Safex account information txout_to_script out_safex_account_update Safex account edit output
txin_to_key SFX for transfer fee txout_to_key Change for SFX

The Safex related checks (file command.cpp / function edit_account::validate)for this transaction are:

  • Username must be inside the DB
  • Referenced output must be create_account output with the same username as in command
    – This check is needed to confirm that the transaction is signed with the same keys like the previous one
  • Account data can be up to 2048 characters long

The Safex transaction-related check (file blockchain.cpp / function check_safex_tx_command) for this transaction are:

  • Safex account fields in the output must match the ones from the input
    After this transaction:
  • New Safex account data is stored in the LMDB for a given username

Offer Creation

Safex Offer is an item that the user wants to list on the blockchain. Safex Offer contains:

  • Username
    – The Safex account username that is previously created
  • Title
    – The title of the Safex offer
  • Offer ID
    – Unique ID that wallet automatically generates at the time of creation.
  • Price
    – Price of the item
    – In SFX if there is no price peg specified
    – In price peg currency if specified
  • Minimum Safex price
    – For security to stop malicious price pegs and to intervene in case of a big rate drop, a minimum SFX price is also required
  • Quantity
    – Amount of the items in stock for sale over the network
    – Offer is automatically set to inactive if quantity reaches 0
  • Active
    – If the item listed can be bought or not
    – Can be set at any time by the seller if he wants to delist an item
  • Price peg ID (optional)
    – Set by user if they want to attach their price to some price peg in the blockchain
  • Description
    – Offer data that is limited to 2KB, it is recommended to here store links to show more info about the offer
  • Seller private view key
    – Used to prove that the purchase funds are sent to the right address
  • Seller address
    – Used to prove that the purchase funds are sent to the right address
Tx inputs Tx outputs
txin_to_script create_offer Reference to Safex Account and Safex offer information txout_to_script out_safex_offer Safex offer output
txin_to_key SFX for transfer fee txout_to_key Change for SFX

The Safex related checks (file command.cpp / function create_offer::validate)for this transaction are:

  • Username must be in the DB
  • Check if username is the same as the one from the referenced output
    – This check is needed to confirm that the transaction is signed with the same keys like the previous one
  • Offer Id must be unique (does not exist in the DB)
  • Offer price cannot be less than 0.0001 SFX
    – We want to deny too small prices on the chain
  • Offer price cannot be greater than 1 billion (total Safex supply)
  • If price peg is not used, min SFX price cannot be bigger than the price
    – Min sfx price represents the price limit if the price is using a price peg. If there is no price peg used, min sfx price will be used as a regular price and thus cannot be smaller
  • Offer title can be up to 80 characters long
  • Offer description can be up to 2048 characters long
  • If price peg is attached, it must exist in the DB

The Safex transaction-related check (file blockchain.cpp / function check_safex_tx_command) for this transaction are:

  • Safex offer fields in the output must match the ones from the input

After this transaction:

  • Safex offer is stored in the LMDB
  • For safety purposes, on the wallet side, Safex Offer can be listed and purchased after 10 blocks are passed

Offer Edit

After some time, if the user wants to change his Safex Offer data(for example, if he got more products in stock and wants to update quantity) Safex Offer data can be edited with edit offer transactions.

Tx inputs Tx outputs
txin_to_script edit_offer Reference to Safex Offer and new Safex offer information txout_to_script out_safex_offer_update Safex offer update output
txin_to_key SFX for transfer fee txout_to_key Change for SFX

The Safex related checks (file command.cpp / function create_account::validate)for this transaction are:

  • Username must be in the DB
  • Offer Id must be in the DB
  • Check if offer Id is the same as the one from the referenced output
    – This check is needed to confirm that the transaction is signed with the same keys like the previous one
  • Offer price cannot be less than 0.0001 SFX
  • Offer price cannot be greater than 1 billion (total Safex supply)
  • If price peg is not used, min SFX price and price fields must match
  • Offer title can be up to 80 characters long
  • Offer description can be up to 2048 characters long
  • If price peg is attached, it must exist in the DB

The Safex transaction related check (file blockchain.cpp / function check_safex_tx_command) for this transaction are:

  • Safex offer fields in the output must match the ones from the input

After this transaction:

  • Safex offer is edited in the LMDB

Price Peg Creation

Safex price peg is an entity that provides a rate for the seller who wants to calculate the price in some other currency(USD, BTC, …). Safex price peg contains:

  • Username
    – The Safex account username that is previously created
  • Title
    – Title of the Safex price peg
  • Price peg ID
    – Unique ID created by the wallet at the time of the creation
  • Rate
    – Rate of the currency to SFX
  • Currency
    – The currency from which price peg will be used for conversion
  • Description
    – Price peg data that is limited to 2KB, it is recommended to here store links to show more info about the price peg(from where it catches the rate, …)
Tx inputs Tx outputs
txin_to_script create_price_peg Reference to Safex Account and Safex price peg information txout_to_script out_safex_price_peg Safex price peg output
txin_to_key SFX for transfer fee txout_to_key Change for SFX

The Safex related checks (file command.cpp / function create_price_peg::validate)for this transaction are:

  • Username must be in the DB
  • Check if username is the same as the one from the referenced output
    – This check is needed to confirm that the transaction is signed with the same keys like the previous one
  • Price peg Id must be unique (does not exist in the DB)
  • Price peg title can be up to 60 characters long
  • Price peg currency can be up to 8 characters long
  • Price peg currency name must be all upper letters
  • Price peg rate cannot be 0
  • Price peg description can be up to 2048 characters long

The Safex transaction related check (file blockchain.cpp / function check_safex_tx_command) for this transaction are:

  • Safex price peg fields in the output must match the ones from the input

After this transaction:

  • Safex price peg is stored in the LMDB

Price Peg Edit

As Safex price peg show rate between a currency and SFX, it is not constant, so update of the rate is also enabled with a special tx that changes the price rate immediately.

Tx inputs Tx outputs
txin_to_script update_price_peg Reference to Safex Price peg and new Safex price peg rate txout_to_script out_safex_price_peg_update Safex price peg update output
txin_to_key SFX for transfer fee txout_to_key Change for SFX

The Safex related checks (file command.cpp / function create_account::validate)for this transaction are:

  • Price peg must be in the DB
  • Check if price peg ID is the same as the one from the referenced output
    – This check is needed to confirm that the transaction is signed with the same keys like the previous one
  • Price peg rate cannot be 0

The Safex transaction related check (file blockchain.cpp / function check_safex_tx_command) for this transaction are:

  • Safex price peg fields in the output must match the ones from the input

After this transaction:

  • Safex price peg is edited in the LMDB

Token Stake

As we said earlier, Safex is using proof-of-work mining for generating new SFX which is earned by miners. Safex token staking system is another way to earn SFX that doesn’t issue new cash. Users can stake tokens (locking them for some period of time. While the tokens are staked, they cannot be used for account creation or used in transfers.) anytime. They can unstake (unlock and use) their tokens if a sufficient interval of blocks has passed (8.000 blocks on Mainnet). If purchases on the marketplace were made, then they will also collect SFX revenue share. The amount of revenue share is calculated like this:

  • Each purchase that is done on the blockchain takes a 5% fee from the price of the product
  • For every interval of blocks, a percentage of tokens staked for the user is calculated and that percentage of collected fees is added to the total sum of SFX that will be given to the user who is unstaking tokens
Tx inputs Tx outputs
txin_to_script token_stake SFT to stake txout_to_script out_staked_token Staked SFT
txin_to_script token_stake SFT to stake txout_to_key Change for SFX
txin_to_key SFX for transfer fee

The Safex input related checks (file command.cpp / function token_stake::validate) for this transaction are:

  • Is staked token amount whole
    SFT cannot be divided to decimals
  • Is staked token amount equal to the tokens referenced in the input

The Safex transaction related check (file blockchain.cpp / function check_safex_tx_command) for this transaction are:

  • Check if the minimum amount of tokens is staked ( 25000 SFT )
  • Check if the amount of staked tokens in the output is less or equal to the amount in the input

After this transaction:

  • Tokens are staked on the network
  • LMDB is updated with new staked token sum

Token Unstake

After staking tokens, every purchase is storing 5% fee and after some block intervals, the user can collect dividends and take his tokens back.
When staked, tokens are not collecting dividends during the starting interval when they are staked and during the ending interval when they are unstaked.
Default interval period for Mainnet is 1000 blocks. Interval starts from block height 1 (intervals are 1-1000, 1001-2000).
Minimum intervals for tokens to be staked on Mainnet is 8 (8.000 blocks)

Tx inputs Tx outputs
txin_to_script token_unstake SFT to unstake txout_token_to_key Unstaked SFT
txin_to_key SFX for transfer fee txout_to_key Collected SFX fee
txout_to_key Change for SFX

The Safex input related checks (file command.cpp / function token_unstake::validate) for this transaction are:

  • Is collected network fee matching the network fee that belongs to the user
  • Is stake token output found and if it is the same token amount as it is in the output that the command is referencing
  • Have enough blocks passed from staking

After this transaction:

  • Tokens are unstaked and return to the user
  • User collected dividends
  • LMDB is updated with a new staked token sum

Safex Purchase

Safex purchase is a special transaction that is sending funds to the seller of the product, giving 5% fee to the network, and receiving a feedback token (which can be used to give a rating and a comment for the seller).

Safex purchase contains:

  • Offer Id
    – The Offer Id of the item the user is purchasing
  • Quantity
    – Quantity of the items to purchase
  • Price
    – Total price to pay
  • Shipping
    – True if shipping is required
  • Offer hash
    – As a part of a safeguard for the buyer, when a user selects the item he wants to purchase, a hash of the offer data is created and added to the transaction. If the offer data is changed before the purchase is processed, the transaction will be rejected.
Tx inputs Tx outputs
txin_to_script simple_purchase SFX for purchase and purchase info txout_to_script out_safex_purchase Purchase output sent to seller
txin_to_key SFX for purchase txout_to_script out_network_fee Network fee given to the network
txout_to_script out_safex_feedback_token Feedback token that user can use to give feedback
txin_to_key SFX for transfer fee txout_to_key Change for SFX

The Safex related checks (file command.cpp / function simple_purchase::validate)for this transaction are:

  • Offer ID must be in the DB
  • Offer must be active
  • Quantity for purchase cannot be less than quantity that is left
  • Quantity cannot be 0
  • Total price cannot be less than quantity * item price
  • Dynamically calculated offer hash is matching

The Safex transaction related check (file blockchain.cpp / function check_safex_tx_command) for this transaction are:

  • Safex purchase fields in the output must match the ones from the input
  • Safex feedback fields in the output must match the ones from the input
  • Purchase tx cannot be locked more than current DB height
  • With seller private view key that is given from offer info in the DB, daemon calculates outputs that are sent to the offer address and checks if enough funds is sent
  • Check if network fee outputs are 5% of the purchase

After this transaction:

  • Seller receives info about the purchase
  • Buyer receives feedback token that he can later use
  • LMDB is updated with a new quantity of the item
  • LMDB is updated with new amount of network fee collected

Safex Feedback

To prevent malicious sellers on the network, Safex is providing a rating system where each purchase is giving the buyer a feedback token that he can use to give a score and a comment for the seller.

Safex feedback fields are:

  • Rating
    – Can be between 0 and 3
  • Comment
    – Data that the user wants to share with the community
  • Offer ID
    – ID of the offer to give feedback
Tx inputs Tx outputs
txin_to_script create_feedback Reference to Safex Feedback token and Safex Feedback information txout_to_script out_safex_feedback Safex Feedback output
txin_to_key SFX for transfer fee txout_to_key Change for SFX

The Safex related checks (file command.cpp / function create_feedback::validate)for this transaction are:

  • Offer Id must be in the DB
  • Rating must be between 0 and 3
  • Feedback description can be up to 2048 characters long

The Safex transaction related check (file blockchain.cpp / function check_safex_tx_command) for this transaction are:

  • Safex feedback fields in the output must match the ones from the input

After this transaction:

  • LMDB is updated with a new rating and comment added