πŸ’»Direct Contract Interaction

Here you can find information on how to interact with our contracts directly

Introduction

As gTrade is decentralised on Polygon you can use all the functions directly – even if the website was entirely down! This guide can be used to interact directly with the smart contracts which run all the trading of gTrade on the Polygon chain. It’s easy!

You only need three things:

  • Your own wallet address

  • https://polygonscan.com (or another polygon block explorer)

  • The storage contract address where all the important addresses are kept – including the address of the trading contract, as well as your trades themselves.

STORAGE CONTRACT: 0xaee4d11a16b2bc65edd6416fb626eb404a6d65bd Always confirm you have the correct address for the storage contract.

For security you should cross reference it with the list of contracts here.

Before we open and close trades, it is important to know the different kinds of orders that exist. For more information, please check the Opening / Closing trades page.

It is also worth understanding the properties your trades have:

pairIndex: This corresponds to a trading pair, e.g. BTC = 0, ETH = 1, LINK = 2 etc. A full list of trading pairs can be found here.

index: As you can open 3 trades on each pair, there are 3 β€˜slots’ your trade can sit in. These are index 0, 1, or 2. Your first trade will open in index 0, the second in index 1, and third in slot 3. If you close index 0, but leave open index 1, it will remain in index 1.

So you can imagine, a grid of your trades would look like this if you had two trades open on ETH, and one on BTC:

You also need to know what wei is. Wei is the smallest unit of Ether, or in the case of Polygon, the smallest unit of MATIC. It is the 18th decimal. E.g. 1 MATIC = 1000000000000000000 MATIC wei.

It is also useful to know that some values are referenced as "PRECISION", which means to the 10th decimal place. For instance in the case of asset prices: BTC $46,420.35 = 464203500000000

How to interact with the contract directly

Find the trading contract

  1. Go to polygonscan.com and search for the storage contract address.

  2. Click on β€˜Contract’ which has the green tick next to it.

  3. Click on β€˜Read Contract’ – now you can see everything that can be read from the contract.

  4. Scroll down to β€˜59. trading’, where you’ll find the trading contract address.

  5. You can verify you have the right address by going to β€˜22. isTradingContract’ and paste the address, click query and the Response should be β€˜bool: true

Opening a Trade – Market and Limit Orders

  1. Now that you have the trading contract address, paste it into the search bar at the top.

  2. Click β€˜Contract’ with the green tick next to it.

  3. Click β€˜Write Contract’.

  4. Right underneath β€˜Write Contract’ you will see a red dot with β€˜Connect to Web3’. Click this and connect your wallet (e.g. metamask). You should always double check the site you are interacting with.

  5. Scroll down to β€˜6. openTrade’, you will now need to fill in the following properties:

    • A) Tuple: A tuple is an array of values e.g. [ β€œ0x123…”, 2, 1..]. It can contain strings, integers and Boolean values. The structure for this tuple is:

      1. Your address (e.g. address in metamask). E.g. 0x1ad314… It is worth noting however the contract will fill this in for you, you can use your address for simplicity, or sixty four 0’s.

      2. The pairIndex of the trade. e.g. BTC = 0

      3. The index of the trade. e.g. 0 when opening your first trade, for first β€˜slot’. Please note that it will automatically choose the first available slot in any case.

      4. The initial position of the token: This will be 0 always on opening. If you look at an already open trade it will show you the value of GNS used to open.

      5. The position Size in Dai in wei e.g. If you want to open a trade for 10 DAI, it will be 10 followed by 18 β€˜0’s. e.g. 10000000000000000000

      6. The open Price:

        • For Market orders: The current price of the pair, but with a precision of 10 decimal places as Ethereum and Polygon smart contracts only use integers. e.g. For BTC $46,420.35 = 464203500000000.

        • For Limit / Stop Limit orders: The price you want to open the trade at.

      7. If the trade is buy Trade e.g. To go long, buy = true, or for short buy = false.

      8. How much leverage you want. e.g. for 10x leverage = 10.

      9. Your take Profit. This is the price you would want to close the trade at if in profit. e.g. for this sample trade, if we want to close at 15% profit:

        Open price + (0.01 * Open price * (Take Profit Percentage / Leverage As Percentage)) = TP Price $46,420.35 + (0.01*46420.35*(15/10) = $47,116.65 , so 471166500000000

      10. Your stop Loss. This is the price you want to close the trade at if at a loss. E.g. for this sample trade, if we want to close at 25% loss: Open price - (0.01 * Open price * (Stop Loss Percentage / Leverage Percentage)) = SL Price $46,420.35 - (0.01*46420.35*(25/10) = $45,259.84 , so 45259840000000

    • B) The type of order. This refers to a market order, or different kinds of limit order. If we want to immediately open a market trade at the current price of the pair we just set, we put 0 as it refers to a market open. Types are:

      • 0: Market Order

      • 1: Limit

      • 2: Stop Limit

    • C) The spread Reduction Id. This is the type of NFT you have. Bronze = 1, Silver = 2, Gold = 3, Platinum = 4, Diamond = 5.

    • D) The slippage percentage you want, to 10 decimal places. E.g. for 0.3% slippage: 3000000000

    • E) The referral is the address of your referrer/best friend =). You will have a decreased fee, and they make money – win win. E.g 0x668BE09C64f62035A659Bf235647A58f760F46a5.

  6. Click 'Write' and submit transaction.

Finding your Open Trades

  1. Go to the Storage contract – go to Contract, β€˜Read Contract’.

  2. Scroll down to ’38. openTradesCount’ and put your address as the first value and your pairIndex (refer to the list if you are unsure, it is linked above) as the second value.

    • If you can’t remember what you even opened on, you can start at 0,1,2…

  3. This will return the number of trades you have on that pair, so you know how many you need to close!

  4. If you need to know the specifics of each trade, e.g. if you want to find a previous open price, then go up to ’37. openTrades’ and put in your address, the pairIndex and your index and press query. Try indexes 0, 1, 2 and you should find as many open trades as the value returned in the previous step.

Finding your Limit / Stop Limit Orders

Remember, these are orders which have not yet been opened to the market. If a trade has been opened to the market, you need to see above - Finding your Open Trades.

  1. Go to the Storage contract – go to Contract, β€˜Read Contract’

  2. You can confirm how many limit orders you have open on a pair with ’36. openLimitOrdersCount’ – put in your address and the pairIndex.

  3. You can find your index of the limit orders by going to ’12. getOpenLimitOrders’ and put in your address, the pairIndex, and try indexes 0, 1 and 2.

  4. If it returns a list of integers you have an open limit order – if reverts you do not.

  5. The returned numbers are

    • a) Address you are querying

    • b) pairIndex

    • c) index

    • d) Position Size

    • e) Spread Reduction ID

    • f) Buy trade (long trades are buy = true).

    • g) Leverage

    • h) The Take Profit you set.

    • i) The Stop Loss you set.

      • The TP/SL values are in DAI to 10 decimal places. So if you put in a 10% take profit on the front end, this was calculated to an actual price: e.g. $52,872.91 = 528729100000000.

Updating an Open Trade - Change Stop Loss / Take Profit

In order to update these you will have to already have a trade open. See below if you want to update a limit order.

  1. Go to the Storage Contract as described above and find the Trading Contract => Contract => Write Contract => Connect Web3.

  2. Scroll down to either:

    • "13. updateSl" to update your stop loss.

    • "14. updateTp" to update your take profit.

  3. Input the pairIndex and your index and the new Stop Loss/new Take Profit. This is the new value which you would like to close the trade at – NOT the percentage you would wish to close at. If you are unsure what your open price was to calculate this, refer to β€œFinding your Open Trades”

    • These values are again integers and to 10 decimal places.

    • So if you want to set the value to $5029.78 it would be 50297800000000.

  4. Click 'Write' and submit transaction.

Note: To remove your take profit or stop loss, you can simply set the value to 0.

Closing Open Trades

All you need is to know the pair you may have opened on and have your wallet address.

  1. Go to the Trading contract . Find it as in the opening a trade guide by checking it in the storage contract. Then go to Contract, Write Contract, and Connect to Web3.

  2. Scroll down to β€˜2. closeTradeMarket’

  3. Input your pairIndex and the index of your trades. These terms are explained here.

  4. Click 'Write' and submit transaction.

Cancelling a Limit / Stop Limit Order

  1. Now go to the Trading Contract -> Write Contract -> Connect to Web3

  2. Scroll to β€˜1. cancelOpenLimitOrder’. and input the pairIndex and your index that you just confirmed you have an open limit order in.

  3. Click ''Write" and submit transaction.

Updating a Limit Order that is awaiting execution

Before your limit order opens to the market, it exists as an β€˜openLimitOrder’ meaning it is awaiting execution by an NFT bot when it is at the right price.

  1. Refer to Finding your Open Limit Orders to find your relevant limit orders.

  2. Go to the trading contract as described above.

  3. Go to ’12. updateOpenLimitOrder’ and input the new values that you want – if you are are unsure of each of these then refer to β€˜Opening a Trade’ which describes each of these values and how to calculate them.

  4. Click 'Write' and submit transaction.

Last updated