π»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
Go to polygonscan.com and search for the storage contract address.
Click on βContractβ which has the green tick next to it.
Click on βRead Contractβ β now you can see everything that can be read from the contract.
Scroll down to β59. tradingβ, where youβll find the trading contract address.
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
Now that you have the trading contract address, paste it into the search bar at the top.
Click βContractβ with the green tick next to it.
Click βWrite Contractβ.
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.
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:
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.
The pairIndex of the trade. e.g. BTC = 0
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.
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.
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
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.
If the trade is buy Trade e.g. To go long, buy = true, or for short buy = false.
How much leverage you want. e.g. for 10x leverage = 10.
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
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.
Click 'Write' and submit transaction.
Finding your Open Trades
Go to the Storage contract β go to Contract, βRead Contractβ.
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β¦
This will return the number of trades you have on that pair, so you know how many you need to close!
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.
Go to the Storage contract β go to Contract, βRead Contractβ
You can confirm how many limit orders you have open on a pair with β36. openLimitOrdersCountβ β put in your address and the pairIndex.
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.
If it returns a list of integers you have an open limit order β if reverts you do not.
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.
Go to the Storage Contract as described above and find the Trading Contract => Contract => Write Contract => Connect Web3.
Scroll down to either:
"13. updateSl" to update your stop loss.
"14. updateTp" to update your take profit.
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.
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.
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.
Scroll down to β2. closeTradeMarketβ
Input your pairIndex and the index of your trades. These terms are explained here.
Click 'Write' and submit transaction.
Cancelling a Limit / Stop Limit Order
Refer to Finding your Open Limit Orders.
Now go to the Trading Contract -> Write Contract -> Connect to Web3
Scroll to β1. cancelOpenLimitOrderβ. and input the pairIndex and your index that you just confirmed you have an open limit order in.
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.
Refer to Finding your Open Limit Orders to find your relevant limit orders.
Go to the trading contract as described above.
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.
Click 'Write' and submit transaction.
Last updated