Smart Contract Development Guide

Intro

SakePerp.fi is a decentralized perpetual contracts platform based on the Ethereum virtual machine. This doc introduces the interface and call method of the SakePerp contracts. Users are able to interact with the contract directly through languages ​​such as Javascript/ Typescript/ Go and develop their own advanced trading strategy.

Contract Addresses and abi

  • Smart Contract Addresses(BSC - Mainnet)

    url: https://bsc-graphnode-api.sakeperp.fi/subgraphs/name/sakeperp/sakeperp-subgraph
    Request this url by post, and set the data {"query":"{contractLists {isExchange name addr exchangeStateAddr insuranceFundAddr highRiskLPTokenAddr quoteAssetName quoteAssetAddr maxLeverage}}"}
    
    The return value is in json format
    {
      "data": {
        "contractLists": [
          {
            "addr": "0x97dc089519d0b341D401a4fBaC26B02a7DCd7f6b",    // Contract Address
            "exchangeStateAddr": "0xB04Af392cF7c2d09A6dC8a812DEDCe02Ef4D2092",    // state contract address of the corresponding trading pool
            "highRiskLPTokenAddr": "0xa1e38c2710b71cad2e2e6afa25eeae26dd3b34bb", // LPToken contract address of the corresponding trading pool
            "insuranceFundAddr": "0x7395DE44d09E2c691F1bd946F660BbFCf5718275",    // Insurance Fund contract address of the corresponding trading pool 
            "isExchange": true,    // Is it a trading pool address?
            "maxLeverage": 15,    // Maximum leverage supported by the trading pool
            "name": "ETH/BUSD",    // Contract name
            "quoteAssetAddr": "0xe9e7cea3dedca5984780bafc599bd69add087d56", // Contract address of the asset used for a quote 
            "quoteAssetName": "BUSD"    // The name of the asset used for a quote
          },
          {
            "addr": "0xCFADd8b52B0737401d8e0aFDc8Aa9194bd40BFc3",
            "exchangeStateAddr": "0x79366156125D61883d09036d9b57d36d897F9895",
            "highRiskLPTokenAddr": "0xeca8f31532b8b51f2185210f22fbe3c33a786b1b",
            "insuranceFundAddr": "0xCAe9E683864E5892DA9d258cae4C4d029BD10620",
            "isExchange": true,
            "maxLeverage": 15,
            "name": "BTC/BUSD",
            "quoteAssetAddr": "0xe9e7cea3dedca5984780bafc599bd69add087d56",
            "quoteAssetName": "BUSD"
          },
          {
            "addr": "0xeCf9e5c123a1E251E4f9E3B9800EC8ab3E5033Ed",    // Contract address
            "exchangeStateAddr": "",
            "highRiskLPTokenAddr": "",
            "insuranceFundAddr": "",
            "isExchange": false,
            "maxLeverage": 0,
            "name": "SakePerp",    // Contract name
            "quoteAssetAddr": "",
            "quoteAssetName": ""
          },
          ......
        ]
      }
    }
  • abi

Contract Interface and Calls

Users need an account on BSC before the smart contract interation. User can initialise an account with a private key or a mnemonic. Please make sure that there's enough Gas and BUSD in the account.

Users can start making contract calls when one has the BSC account and related smart contract information.

Before the contract call, we need to unify different tokens into the same decimal place when we do mathematical calculations to avoid errors.

In order to avoid such errors, the Sake dev team has developed a set of libraries in the contract to perform these kinds of mathematical calculations. Some contract interface entries will require a javascript object to be inputted. There is only one d attribute. All d values require 18 decimal places. For example, if we want a leverage multiple of 1.5, then we need to input 15000000000000000000000.

Position Operation and Enquiry

SakePerp provides interfaces to open, close, add and remove margins.

  • approve

    Before opening a position, users need to authorise the SakePerp contract to have access to their account funds.

  • openPosition(Open Position)

  • closePosition(Close Position)

  • addMargin(Add Margin)

  • removeMargin(Remove Margin)

SakePerpViewer Contract provides a position enquiry interface.

  • getUnrealizedPnl(Query the user's unrealized profit and loss)

  • getPersonalBalanceWithFundingPayment (Query the user's balance in all trading pools)

  • getPersonalPositionWithFundingPayment (Query the user's position information in the specified trading pool)

  • getMarginRatio (Query the user's margin ratio in the specified trading pool)

MM Liquidity Operation and Enquiry

SakePerpVault provides interfaces for adding, removing, and querying liquidity to MMs.

  • approve

    Before an operation, users need to authorise the SakePerp contract to have access to the account funds.

  • addLiquidity (Add Liquidity)

  • removeLiquidity (Remove Liquidity)

  • getTotalLpUnrealizedPNL (Get the total profit and loss of the trading pool)

  • getLpTokenPrice (Get the LPToken price of the trading pool)

  • getTotalMMLiquidity (Get the total liquidity of the trading pool)

  • getTotalMMAvailableLiquidity (Get the available liquidity of the trading pool)

Trading Pool Status Enquiry

The exchange contract provides an interface for querying status information of the trading pool.

  • getUnderlyingPrice(Return the oracle price, also known as indexPrice)

  • getUnderlyingTwapPrice(Return the time-weighted oracle price over a period of time)

  • getSpotPrice(Return the contract price, also known as markPrice)

  • getTwapPrice(Return the time-weighted contract price over a period of time)

  • getReserve(Return the x and y values of the vAMM curve)

  • getMaxHoldingBaseAsset(Return the maximum position size for a single user, a value of 0 means no limit)

  • initMarginRatio(Return the initial margin ratio, which determines the maximum leverage)

  • maintenanceMarginRatio(Return the maintenance margin ratio below which the position will be liquidated)

  • spreadRatio(Return to the spread)

  • liquidationFeeRatio(Return to the liquidation fee ratio)

  • maxLiquidationFee(Return to the max. liquidation fee)

  • getTotalPositionSize(Return to the net position of the trading pool)

  • getPositionSize(Return the size of long and short positions respectively)

Last updated

Was this helpful?