# Trading via Etherscan

Pika Protocol is a fully decentralized and permission-less exchange, operated not by team, but by unstoppable code. In the rare case when the website server is down, you can always trade directly with the smart contract from block explorers like Etherscan.

## Preparation

If you have previously approved the contract in Pika UI, you can skip this step.

#### **Approve** Pika Contracts for USDC

To approve the contract, you first open the USDC contract on Etherscan: <https://optimistic.etherscan.io/token/0x7f5c764cbc14f9669b88837ca1490cca17c31607#writeContract>

Then clicks `Connect to Web` to connect to your Metamask wallet.

You need to click the Approve function 4 times to approve these 4 addresses:

1. PikaPerpV4 address: 0x9b86B2Be8eDB2958089E522Fe0eB7dD5935975AB
2. PositionManager address: 0xb67c152e69217b5acb85a2e19df13423351b0e27
3. OrderBook address: 0x835a179a9E1A57f15823eFc82bC460Eb2D9d2E7C
4. PositionRouter address: 0xa78Cd820b198A943199deb0506E77d655b5078cC

The `amount` is your intended trade amount. If you want to trade 100 USDC, the input amount should be 10000000000(100\*1e8).&#x20;

#### Enable Trading

You need to open the PikaPerpV4 contract: <https://optimistic.etherscan.io/address/0x9b86b2be8edb2958089e522fe0eb7dd5935975ab#code> and call `setAccountManager` twice to enable the trading for market, limit and stop orders:

1. Call `setAccountManager` function by setting `_manager` as the PositionManager contract address(0xb67c152e69217b5acb85a2e19df13423351b0e27) and `_isActive` is true.
2. Call the `setAccountManager` function by setting `_manager` as the OrderBook contract address(0x835a179a9E1A57f15823eFc82bC460Eb2D9d2E7C) and `_isActive` is true.

To enable placing market orders together with take profit and stop loss orders, you need to send two additional transactions:

1. Open the positionManager contract: <https://optimistic.etherscan.io/address/0xB67c152E69217b5aCB85A2e19dF13423351b0E27> and call `setAccountManager` function, `_manager` is the positionRouter contract: 0xa78Cd820b198A943199deb0506E77d655b5078cC
2. Open the orderbook contract: <https://optimistic.etherscan.io/address/0x835a179a9E1A57f15823eFc82bC460Eb2D9d2E7C> and call `setAccountManager` function, `_manager` is the positionRouter contract: 0xa78Cd820b198A943199deb0506E77d655b5078cC

## Place a Market Order

#### Place an Open Market Order

Open the PositionManager contract at[ ](https://optimistic.etherscan.io/address/0xB67c152E69217b5aCB85A2e19dF13423351b0E27#writeContract)<https://optimistic.etherscan.io/address/0xB67c152E69217b5aCB85A2e19dF13423351b0E27#writeContract> and fill in the parameters for `createOpenPosition` function. The parameters are described below. Then you can click the `Write` button to confirm the transaction.

<table><thead><tr><th width="150">Parameters</th><th>Description</th></tr></thead><tbody><tr><td>createOpenPosition</td><td>The ether to pay for execution fee. The input should be 0.00025.</td></tr><tr><td>account</td><td>Your wallet address</td></tr><tr><td>productId</td><td>The number represents the intended trading pair. Please refer to <a data-mention href="trading-pairs">trading-pairs</a> for the productId of each trading pair.</td></tr><tr><td>margin</td><td>It is the USDC margin for the position. <br>Note the intended margin amount needs to multiply 1e8 for the input. For example: 50 USDC margin's input is 5000000000.</td></tr><tr><td>leverage</td><td>It is the leverage of the position. The value needs to multiply 1e8 for the input (1000000000 represents 10x leverage).</td></tr><tr><td>isLong</td><td>It is the direction of the position.<br>If you want to close a long position, the input is "true".<br>If you want to close a short position, the input is "false"</td></tr><tr><td>acceptablePrice</td><td>It is the lowest acceptable price for open short or close long order, and highest acceptable price for open long or close short order. The order will be cancelled if the current price does not satisfy the acceptable price. Note the intended price needs to multiply 1e8 for the input.</td></tr><tr><td>executionFee</td><td>It is the execution fee paid to keepers to cover the gas fee of executing the order. The input amount should be 25000 (representing 0.00025ETH).</td></tr><tr><td>referralCode</td><td>It is the referral code of the trade. Put <code>0x0000000000000000000000000000000000000000000000000000000000000000</code> before the launch of referral program</td></tr></tbody></table>

#### Check the Position

In around 10 seconds after submitting the open order, you can check your position on Pika from PikaPerpV4 contract's `getPosition` function: <https://optimistic.etherscan.io/address/0x9b86B2Be8eDB2958089E522Fe0eB7dD5935975AB#readContract>. Below is the description of each parameters.

<table><thead><tr><th width="150">Parameters</th><th>Description</th></tr></thead><tbody><tr><td>account</td><td>User wallet address</td></tr><tr><td>productId</td><td>The number represents the trading pair. Please refer to <a data-mention href="trading-pairs">trading-pairs</a> for the productId of each trading pair.</td></tr><tr><td>isLong</td><td>It is the direction for the position.<br>If it is "true", your position is long.<br>If it is "false", your position is short.</td></tr></tbody></table>

The values in the return array represent: `productId`, `leverage`, `entryPrice`, `oraclePriceAtTheEntry` (can be ignored), `margin`, `ownerOfPosition`, `entryTimestamp`, `direction`, and `fundingAtTheEntry` (can be ignored).

For example, the position below is a 2x ETH Long with the margin of 30 USDC, with enty price at 1847.867, opened at the timestamp of 1691810315.

<figure><img src="https://754592214-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQqm3d5en6TYOTzV6C8wz%2Fuploads%2FbHg95bzvr1kRZK4DFstr%2Fimage.png?alt=media&#x26;token=79753924-9556-4b82-ba5d-fd9f21ea2da5" alt=""><figcaption></figcaption></figure>

If your open order is not executed successfully, your active position will show up as 0s.&#x20;

#### Place a Close Market Order

Open the PositionManager contract at <https://optimistic.etherscan.io/address/0xB67c152E69217b5aCB85A2e19dF13423351b0E27#writeContract> and fill in the parameters for `createClosePosition` function. The parameters are the same as those for `createOpenPosition` mentioned above. Then you can click the `Write` button to confirm the transaction.

## Place a Limit/Stop Order

#### Place an Open Limit/Stop Order

Open the OrderBook contract at <https://optimistic.etherscan.io/address/0x835a179a9E1A57f15823eFc82bC460Eb2D9d2E7C#writeContract> and fill in the parameters for `createOpenOrder` function. Fill in the parameters using the description provided below. Once all the parameters are entered, click the `Write` button to confirm the transaction.

<table><thead><tr><th width="150">Parameters</th><th>Description</th></tr></thead><tbody><tr><td>createOpenOrder</td><td>The ether to pay for execution fee. The input should be 0.00025.</td></tr><tr><td>productId</td><td>The number represents the intended trading pair. Please refer to <a data-mention href="trading-pairs">trading-pairs</a> for the productId of each trading pair.</td></tr><tr><td>margin</td><td>It is the USDC margin for the position. <br>Note the intended margin amount needs to multiply 1e8 for the input. For example: 50 USDC margin's input is 5000000000.</td></tr><tr><td>leverage</td><td>It is the leverage of the position. The value needs to multiply 1e8 for the input (1000000000 represents 10x leverage).</td></tr><tr><td>isLong</td><td>It is the direction of the position.<br>If you want to close a long position, the input is "true".<br>If you want to close a short position, the input is "false"</td></tr><tr><td>triggerPrice</td><td>It is the price at which the order will be triggerred. Note the trigger has high success rate but not 100% if the price moves cross the price and then goes back quickly before the keeper is able to trigger it. Note the intended price needs to multiply 1e8 for the input.</td></tr><tr><td>triggerAboveThreshold</td><td>If you want the order to be triggered when the price is above triggerPrice, the input is "true".<br>If you want the order to be triggered when the price is below triggerPrice, the input is "false".</td></tr><tr><td>executionFee</td><td>It is the execution fee paid to keepers to cover the gas fee of executing the order. The input amount should be 25000 (representing 0.00025ETH).</td></tr></tbody></table>

#### Place a Close Limit/Stop Order

Open the OrderBook contract at[ ](https://optimistic.etherscan.io/address/0x835a179a9E1A57f15823eFc82bC460Eb2D9d2E7C#code)<https://optimistic.etherscan.io/address/0x835a179a9E1A57f15823eFc82bC460Eb2D9d2E7C#writeContract> and fill in the parameters for `createCloseOrder` function. These parameters are the same as those for `createOpenOrder` mentioned above, except for the \`size, which refers to the position size you wish to close. The intended size needs to multiply 1e8 for the input. Then you can click the `Write` button to confirm the transaction.

## Place an Open Market Order with Take Profit and Stop Loss Orders

Open the PositionRouter contract at[ ](https://optimistic.etherscan.io/address/0xB67c152E69217b5aCB85A2e19dF13423351b0E27#writeContract)<https://optimistic.etherscan.io/address/0xa78Cd820b198A943199deb0506E77d655b5078cC#writeContract> and fill in the parameters for `createOpenMarketOrderWithCloseTriggerOrders` function. The parameters are described below. After entering all the necessary information, click the `Write` button to confirm the transaction.

<table><thead><tr><th width="150">Parameters</th><th>Description</th></tr></thead><tbody><tr><td>createOpenMarketOrderWithCloseTriggerOrders</td><td>The ether to pay for execution fee. The input should be 0.00075 if you are open a market order with both take profit and stop loss orders. The input is 0.0005 if you are open a market order with either take proft or stop loss order.</td></tr><tr><td>productId</td><td>The number represents the intended trading pair. Please refer to <a data-mention href="trading-pairs">trading-pairs</a> for the productId of each trading pair.</td></tr><tr><td>margin</td><td>It is the USDC margin for the position. <br>Note the intended margin amount needs to multiply 1e8 for the input. For example: 50 USDC margin's input is 5000000000.</td></tr><tr><td>leverage</td><td>It is the leverage of the position. The value needs to multiply 1e8 for the input (1000000000 represents 10x leverage).</td></tr><tr><td>isLong</td><td>It is the direction of the position.<br>If you want to close a long position, the input is "true".<br>If you want to close a short position, the input is "false"</td></tr><tr><td>acceptablePrice</td><td>It is the lowest acceptable price for open short or close long order, and highest acceptable price for open long or close short order. The order will be cancelled if the current price does not satisfy the acceptable price. Note the intended price needs to multiply 1e8 for the input.</td></tr><tr><td>executionFee</td><td>It is the execution fee paid to keepers to cover the gas fee of executing the order. The input amount should be 75000 (representing 0.00075ETH) if you open a market order with both take profit and stop loss orders. The input is 50000 if you are open a market order with either take proft or stop loss order.</td></tr><tr><td>stopLossPrice</td><td>It is the price below which the close long order will get triggered, and above which the close short order will get triggerred.<br>Put 0 if do not want to place stop loss order.</td></tr><tr><td>takeProfitPrice</td><td>It is the price above which the close long order will get triggered, and below which the close short order will get triggerred.<br>Put 0 if do not want to place take profit order.</td></tr><tr><td>referralCode</td><td>Put <code>0x0000000000000000000000000000000000000000</code> before the referral program is launched.</td></tr></tbody></table>

## Place Take Profit and Stop Loss Orders for an Existing Position

Open the PositionRouter contract at[ ](https://optimistic.etherscan.io/address/0xB67c152E69217b5aCB85A2e19dF13423351b0E27#writeContract)<https://optimistic.etherscan.io/address/0xa78Cd820b198A943199deb0506E77d655b5078cC#writeContract> and fill in the parameters for `createCloseTriggerOrders` function. These parameters are the same as those for `createOpenMarketOrderWithCloseTriggerOrders` mentioned above. Then you can click the `Write` button to confirm the transaction.

## Add and Remove Margin

First, open the PikaPerpV4 contract at <https://optimistic.etherscan.io/address/0x9b86B2Be8eDB2958089E522Fe0eB7dD5935975AB#readContract>. Call the `getPositionId` function to retrieve the `positionId` using your address, `productId`, and position direction.

Next, navigate to <https://optimistic.etherscan.io/address/0x9b86B2Be8eDB2958089E522Fe0eB7dD5935975AB#writeContract>. Call the `modifyMargin` function, inputting the `positionId` from the previous step, the margin amount you wish to add or remove, and specify `true` for adding margin or `false` for removing margin in the `shouldIncrease` field.
