Coder Social home page Coder Social logo

lesteryehgood / perpetual-protocol Goto Github PK

View Code? Open in Web Editor NEW

This project forked from perpetual-protocol/perpetual-protocol

0.0 1.0 0.0 2.18 MB

Perpetual Protocol Solidity smart contracts

License: GNU General Public License v3.0

JavaScript 0.12% TypeScript 64.55% Handlebars 0.03% Solidity 35.30%

perpetual-protocol's Introduction

Perpetual Protocol

Perpetual Protocol is a decentralized perpetual contract protocol for every asset, made possible by a Virtual Automated Market Maker (henceforth referred to as a “vAMM” or “vAMMs”).

For those who aren’t familiar with perpetual contracts: A perpetual contract is a derivative similar to a futures contract but without an expiry date. For conventional futures contracts such as WTI, the contract’s price will gradually converge with the underlying asset’s spot market price as the expiry date approaches. Perpetual contracts are futures contracts that automatically roll over on a given time period, e.g. 1 hour, 8 hours, etc. In order to keep the perpetual contract in line with the underlying index, one side of the market pays the other a funding rate. The funding rate effectively implies a cost of capital and the steepness of the futures curve. You can learn more about how funding works by reading our documentation.

Key features of the Perpetual Protocol include:
  • 10x Leverage On-Chain Perpetual Contacts Traders can trade with up to 10x leverage long or short, with transparent fees and 24/7 guaranteed liquidity.
  • Go Long or Short on Any Asset Every asset can be supported on Perpetual Protocol. Whether it's gold, fiat, BTC, BCH, ETH, ERC-20s, XRP, EOS, LTC, ZEC, XMR, and more - Perpetual Protocol can support it all. All that Perpetual Protocol requires is a price feed for the underlying asset from an oracle.
  • Lower Slippage than Other AMMs Traders on constant product (x*y=k) market makers like Uniswap suffer higher slippage than traders on centralized exchanges (CEXs) because k is capped by the liquidity provided. Perpetual Protocol’s vAMM can set K algorithmically to provide lower slippage to traders.

Please refer to the Perpetual Protocol documentation for more details.

Welcome to contribute to this project. Please join our Discord to discuss.

Local development and testing

Requirements

You should have Node 12 installed. Use nvm to install it.

Get started

Clone this repository, install NodeJS dependencies, and build the source code:

git clone [email protected]:perpetual-protocol/perp-contract.git
npm i
npm run build

Local development

Deploy in the local enviroment, deployed contract addresses listed in build/system-local.json.

npm run dev

To learn more use cases, please refer to the development guide.

Testing

To run all the test cases,

npm run test

If you only want to run partial test cases, add .only and pass the title of test cases or test sections. For example,

describe.only("Amm Unit Test", () => { ... }

or

it.only("admin open amm", async () => { ... }

Test cases can be found in the ./tests folder.

Mainnet fork testing

This is for testing the deployment or upgradability of contracts.

Two params are required:

  • stage can be staging or production
  • fileName is the targeted testing file in the publish/migrations directory
npm run simulate <stage> <fileName>

Deploy contracts on rinkeby without versioning

Deploy contracts on rinkeby without versioning requires two environment variables: WEB3_RINKEBY_ENDPOINT & RINKEBY_MNEMONIC.

Once these two variables are set, the deployment script is needed. For example, we want to deploy the "PerpRewardVesting" contract, and the deploy script should be similar to this code snippet:

import { ethers, upgrades } from "hardhat"
import { ContractName } from "../publish/ContractName"

async function main() {
    const perpAddress = "0xaFfB148304D38947193785D194972a7d0d9b7F68"
    const options = {
        unsafeAllowCustomTypes: true,
    }
    const Vesting = await ethers.getContractFactory(ContractFullyQualifiedName.PerpRewardVesting)
    const instance = await upgrades.deployProxy(Vesting, [perpAddress], options)
    await instance.deployed()
}

main()

execute the below commands:

./node_modules/.bin/hardhat --network rinkeby run scripts/deploy-reward-vesting.ts

And the contract should be deployed on rinkeby, and the file ".openzeppelin/rinkeby.json" is modified with new contract addresses, but the changes can be ignored for testing purposes.

Checking Chainlink aggregator

When we create a new AMM, we always need to execute addAggregator() to add a chainlink aggregator. Still, the arguments are all hex format numbers, and pretty easy to type the wrong parameters. There is a hardhat task "check:chainlink" to show all major information. We can use this information to check on gnosis multisign.

First step: go to https://docs.chain.link/docs/ethereum-addresses to find which price feed we need for the new AMM. e.g. if you want to check SNX/USD on homestead, please execute:

$ ./node_modules/.bin/hardhat check:chainlink  --network homestead --address 0xDC3EA94CD0AC27d9A86C180091e7f78C683d3699

it will print basic information for SNX/USD:

When we create a new AMM, we always need to execute `addAggregator()` to add a chainlink aggregator. Still, the arguments are all hex format numbers, and pretty easy to type wrong parameters. There is a hardhat task "check:chainlink" to show all major information. We can use this information to check on gnosis multisign.
First step: go to https://docs.chain.link/docs/ethereum-addresses to find which price feed we need for the new AMM. e.g. if you want to check SNX/USD on homestead, please execute:
$ ./node_modules/.bin/hardhat check:chainlink  --network homestead --address 0xDC3EA94CD0AC27d9A86C180091e7f78C683d3699
it will print basic information for SNX/USD:
pair: SNX / USD
base symbol: SNX
quote symbol: USD
latest price: 20.69401159
price feed key: 0x534e580000000000000000000000000000000000000000000000000000000000
functionData: 0x3f0e084f534e580000000000000000000000000000000000000000000000000000000000000000000000000000000000dc3ea94cd0ac27d9a86c180091e7f78c683d3699

When you send a transaction on gnosis app, we can use functionData to check if the transaction data is correct.

License

GPL3.0 or later

perpetual-protocol's People

Contributors

wraecca avatar kimiwu123 avatar tienshaoku avatar detoo avatar yurenju avatar vinta avatar paco0x avatar phchan9 avatar 42bchen avatar lkblkb avatar tailingchen avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.