How to Trade Directly with the Smart Contract

  • Updated

As you may have seen with recent events, user interfaces are susceptible to hacks and are not yet decentralized enough. One way to avoid interacting with fraudulent UIs is to bookmark all of the contract addresses you frequently interact with. If the front end has been exploited, then as you go to interact with it, a different address that’s not in your bookmarks will be displayed, raising the alarm that something’s not right and preventing you from incurring losses.

Another method to avoid being deceived by a fraudulent front end is to trade directly with the smart contracts via Etherscan. In this guide, we’ll show you how to trade directly with the Perpetual Protocol smart contracts, so you can still open or close trades in the extreme event that our UI is compromised. 

To deposit, withdraw and open or close positions on Perp v2, there are two smart contracts you’ll need to interact with:

  1. the Vault contract, for deposits and withdrawals: 0xad7b4c162707e0b2b5f6fddbd3f8538a5fba0d60
  2. And the Clearinghouse contract, for executing trades: 0x82ac2ce43e33683c58be4cdc40975e73aa50f459

Let’s see how you can interact directly with the smart contract using Etherscan to make trades. 

How to Deposit

To get started, you’ll have to make a deposit to the vault contract address. 

Head over to the following page on optimistic.etherscan: https://optimistic.etherscan.io/address/0xad7b4c162707e0b2b5f6fddbd3f8538a5fba0d60#writeProxyContract

Click on “Connect to Web3” to connect to the site. 

Screenshot_2022-08-16_at_18.13.37.png

Select wallet type (MetaMask or WalletConnect):

Screenshot_2022-08-17_at_01.34.15.png

Once connected, the red circle will turn green: 

Screenshot_2022-08-16_at_18.14.46.png

Unfurl “1. Deposit”, then enter the token address of the collateral you want to deposit (USDC, ETH, WETH or FRAX) and the deposit amount. 

Screenshot_2022-08-17_at_01.37.40.png

  • USDC token address: 0x7F5c764cBc14f9669B88837ca1490cCa17c31607
  • ETH token address: 0xDeadDeAddeAddEAddeadDEaDDEAdDeaDDeAD0000
  • WETH token address: 0x4200000000000000000000000000000000000006
  • FRAX token address: 0x2E3D870790dC77A83DD1d18184Acc7439A53f475

Example: Deposit $100 USDC

If you want to deposit $100 worth of USDC, then you’d input 100 * 10^18 = 100000000000000000000 into the amount field and enter 0x7F5c764cBc14f9669B88837ca1490cCa17c31607 in the token address field, as shown below:

[“0x7F5c764cBc14f9669B88837ca1490cCa17c3160”]

[100000000000000000000]

Screenshot_2022-08-17_at_01.59.04.png

Click on “Write” and confirm the transaction in your wallet. 

Example: Deposit 0.10 ETH

If you want to deposit 0.10 ETH, then you’d input 0.1 * 10^18 = 100000000000000000 into the amount field and enter 0xDeadDeAddeAddEAddeadDEaDDEAdDeaDDeAD0000 in the token address field instead.

How to Open a Position

To open a position, you’ll need to interact with the Clearinghouse contract, which can be done here: https://optimistic.etherscan.io/address/0x82ac2ce43e33683c58be4cdc40975e73aa50f459#writeProxyContract

To open a position, head down to “8. openPosition'' and click on the arrow to reveal the field.

80f989450c097099ff49691a3cae4497.gif

As covered in our Integration Guide and Code Samples, you have to input various parameters to open a position, each of which are explained below. 

struct OpenPositionParams { 
address baseToken;
bool isBaseToQuote;
bool isExactInput;
uint256 amount;
uint256 oppositeAmountBound;
uint256 deadline;
uint160 sqrtPriceLimitX96;
bytes32 referralCode; }

Screenshot_2022-08-17_at_01.48.03.png

Once you’re on the pool information page, you can then click on the base token (highlighted below) to get the relevant v-token address. For example, if you want to trade vAAVE (the base token of the AAVE-USD market), then the base token address is: 0x34235c8489b06482a99bb7fcab6d7c467b92d248. 

  • The isBaseToQuote parameter is used to specify if you want to go long or short the asset for the v-token pool address you have specified. 
    • To short the base asset, enter ‘true
    • To long the base asset, enter ‘false’ 
    • isExactInput can be entered as true or false. 
    • The uint256 amount parameter specifies the size of your position. For example, if you want to trade $50 worth of AAVE, then you should input 50 * 10^18 // 18 decimals = 50000000000000000000.
    • uint256 oppositeAmountBound is a restriction on how many tokens to receive or pay, depending on isBaseToQuote and isExactInput. 
    • uint256 deadline sets a deadline for executing the transaction. You can use a Unix time converter to enter this parameter, by converting a custom date 15 minutes ahead of the current time to Unix timestamp:

Screenshot_2022-08-17_at_01.49.39.png

  • uint160 sqrtPriceLimitX96 is a restriction on the ending price after the swap, where sqrtPriceLimitX96 is defined here. For no restriction, you can enter 0. 
  • referralCode is used to add your referral code (or use 0x0000000000000000000000000000000000000000000000000000000000000000 if you do not have a code to apply). 

Putting it all together, here’s an input for shorting $50 worth of AAVE:

["0x34235c8489b06482a99bb7fcab6d7c467b92d248",true,false,"50000000000000000000","0","1660666527","0","0x0000000000000000000000000000000000000000000000000000000000000000"]

Screenshot_2022-08-17_at_01.50.50.png

To open the position, click on the “Write” button and confirm the transaction in your wallet.

Congratulations, you have now opened a position by interacting directly with the smart contract!

How to Close a Position

Closing a position is similar to opening one.

Go to “4. closePosition”:

Screenshot_2022-08-16_at_20.16.51.png

Specify the address baseToken, sqrtPriceLimitX96, oppositeAmountBound, deadline, and referral code

So for closing the $50 short position in the AAVE market, input the following:

["0x34235c8489b06482a99bb7fcab6d7c467b92d248",”0”,"0",”1660678522”,"0x0000000000000000000000000000000000000000000000000000000000000000"]

Screenshot_2022-08-17_at_01.54.23.png

Then click “Write” and confirm the transaction in your wallet. 

How to Withdraw

To withdraw funds, head to “16. Withdraw” from the vault contract.

Similar to depositing, you just specify the token address of the asset you want to withdraw and the amount. 

Screenshot_2022-08-17_at_01.55.48.png

Example: Withdraw $100 USDC

If you want to withdraw $100 worth of USDC, then you’d input 100 * 10^18 = 100000000000000000000 into the amount field and enter 0x7F5c764cBc14f9669B88837ca1490cCa17c31607 in the token address field, as shown below:

[“0x7F5c764cBc14f9669B88837ca1490cCa17c3160”]

[100000000000000000000]

Screenshot_2022-08-17_at_01.57.57.png

Click on “Write” and confirm the transaction in your wallet. 

Example: Withdraw 0.10 ETH

If you want to withdraw 0.10 ETH, then you’d input 0.1 * 10^18 = 100000000000000000 into the amount field and enter 0xDeadDeAddeAddEAddeadDEaDDEAdDeaDDeAD0000 in the token address field instead.