Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ pytest -v tests/test_overnight_max_leverage.py
pytest -v tests/test_slippage.py
pytest -v tests/test_target_funding_rate.py

pytest -v tests/test_vault.py




Expand Down
115 changes: 115 additions & 0 deletions examples/vault_view_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
from ostium_python_sdk import OstiumVault
from web3 import Web3
import os
from dotenv import load_dotenv

# Load environment variables
load_dotenv()

# Initialize Web3 and vault
w3 = Web3(Web3.HTTPProvider(os.getenv('ARBITRUM_RPC_URL')))
vault = OstiumVault(
w3=w3,
vault_address=os.getenv('VAULT_ADDRESS'),
usdc_address=os.getenv('USDC_ADDRESS'),
private_key=os.getenv('PRIVATE_KEY'),
verbose=True
)

def print_vault_info():
# Basic token information
asset = vault.get_asset()
print(f"OLP Token Address: {asset}")

name = vault.get_name()
print(f"Token Name: {name}")

symbol = vault.get_symbol()
print(f"Token Symbol: {symbol}")

decimals = vault.get_decimals()
print(f"Token Decimals: {decimals}")

# Balance and supply information
balance = vault.get_balance()
print(f"My OLP Balance: {balance}")

total_assets = vault.get_total_assets()
print(f"Total Assets in Vault: {total_assets} USDC")

available_assets = vault.get_available_assets()
print(f"Available Assets: {available_assets} USDC")

tvl = vault.get_tvl()
print(f"Total Value Locked: {tvl} USDC")

market_cap = vault.get_market_cap()
print(f"Market Cap: {market_cap}")

# PnL related information
acc_pnl = vault.get_acc_pnl_per_token()
print(f"Accumulated PnL per Token: {acc_pnl}")

current_epoch_pnl = vault.get_current_epoch_positive_open_pnl()
print(f"Current Epoch Positive Open PnL: {current_epoch_pnl}")

daily_pnl_delta = vault.get_daily_acc_pnl_delta_per_token()
print(f"Daily Accumulated PnL Delta per Token: {daily_pnl_delta}")

total_closed_pnl = vault.get_total_closed_pnl()
print(f"Total Closed PnL: {total_closed_pnl}")

# Epoch information
current_epoch = vault.get_current_epoch()
print(f"Current Epoch: {current_epoch}")

epoch_start = vault.get_current_epoch_start()
print(f"Current Epoch Start: {epoch_start}")

withdraw_timelock = vault.get_withdraw_epochs_timelock()
print(f"Withdrawal Epochs Timelock: {withdraw_timelock}")

# Supply and limits
max_supply = vault.get_current_max_supply()
print(f"Current Max Supply: {max_supply}")

max_supply_increase = vault.get_max_supply_increase_daily_p()
print(f"Max Supply Increase Daily %: {max_supply_increase}")

# Discount information
max_discount = vault.get_max_discount_p()
print(f"Max Discount %: {max_discount}")

max_discount_threshold = vault.get_max_discount_threshold_p()
print(f"Max Discount Threshold %: {max_discount_threshold}")

total_discounts = vault.get_total_discounts()
print(f"Total Discounts: {total_discounts}")

total_locked_discounts = vault.get_total_locked_discounts()
print(f"Total Locked Discounts: {total_locked_discounts}")

# Other metrics
collateralization = vault.get_collateralization_p()
print(f"Collateralization %: {collateralization}")

total_deposited = vault.get_total_deposited()
print(f"Total Deposited: {total_deposited} USDC")

total_liability = vault.get_total_liability()
print(f"Total Liability: {total_liability}")

total_rewards = vault.get_total_rewards()
print(f"Total Rewards: {total_rewards}")

# Conversion examples
assets = 1000 # 1000 USDC
shares = vault.convert_to_shares(assets)
print(f"\nConversion Examples:")
print(f"{assets} USDC = {shares} shares")

converted_assets = vault.convert_to_assets(shares)
print(f"{shares} shares = {converted_assets} USDC")

if __name__ == "__main__":
print_vault_info()
5 changes: 4 additions & 1 deletion ostium_python_sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
from .subgraph import SubgraphClient
from .config import NetworkConfig
from .faucet import Faucet
from .ostium import Ostium
from .vault import OstiumVault

__all__ = ["OstiumSDK", "SubgraphClient", "NetworkConfig", "Faucet"]
__all__ = ["OstiumSDK", "SubgraphClient",
"NetworkConfig", "Faucet", "Ostium", "OstiumVault"]
6 changes: 4 additions & 2 deletions ostium_python_sdk/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def mainnet(cls) -> 'NetworkConfig':
contracts={
"usdc": "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
"trading": "0x6D0bA1f9996DBD8885827e1b2e8f6593e7702411",
"tradingStorage": "0xcCd5891083A8acD2074690F65d3024E7D13d66E7"
"tradingStorage": "0xcCd5891083A8acD2074690F65d3024E7D13d66E7",
"vault": "0x20D419a8e12C45f88fDA7c5760bb6923Cee27F98"
},
is_testnet=False
)
Expand All @@ -33,7 +34,8 @@ def testnet(cls) -> 'NetworkConfig':
contracts={
"usdc": "0xe73B11Fb1e3eeEe8AF2a23079A4410Fe1B370548",
"trading": "0x2A9B9c988393f46a2537B0ff11E98c2C15a95afe",
"tradingStorage": "0x0b9F5243B29938668c9Cfbd7557A389EC7Ef88b8"
"tradingStorage": "0x0b9F5243B29938668c9Cfbd7557A389EC7Ef88b8",
"vault": "0x2fbf52c8769c5da05afee7853b12775461cD04d2"
},
is_testnet=True
)
7 changes: 2 additions & 5 deletions ostium_python_sdk/faucet.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import time
from datetime import datetime
from ostium_python_sdk.abi.faucet_testnet_abi import faucet_abi # Import the ABI
from .utils import get_account

# Class for testnet usage only - to get testnet USDC tokens

Expand Down Expand Up @@ -50,12 +51,8 @@ def _check_private_key(self):
raise ValueError(
"Private key is required for Faucet operations")

def _get_account(self):
self._check_private_key()
return self.web3.eth.account.from_key(self.private_key)

def request_tokens(self) -> dict:
account = self._get_account()
account = get_account(self.web3, self.private_key)
self.log("Requesting tokens from faucet")
"""
Request testnet USDC tokens from the faucet.
Expand Down
35 changes: 19 additions & 16 deletions ostium_python_sdk/ostium.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from .abi.usdc_abi import usdc_abi
from .abi.trading_abi import trading_abi
from .abi.trading_storage_abi import trading_storage_abi
from .utils import convert_to_scaled_integer, fromErrorCodeToMessage, get_tp_sl_prices, to_base_units
from .utils import convert_to_scaled_integer, fromErrorCodeToMessage, get_tp_sl_prices, to_base_units, approve_usdc, get_account
from eth_account.account import Account


Expand Down Expand Up @@ -75,14 +75,9 @@ def get_slippage_percentage(self):
return self.slippage_percentage

def get_public_address(self):
public_address = self._get_account().address
public_address = get_account(self.web3, self.private_key).address
return public_address

def _get_account(self) -> Account:
self._check_private_key()
"""Get account from stored private key"""
return self.web3.eth.account.from_key(self.private_key)

def get_block_number(self):
return self.web3.eth.get_block('latest')['number']

Expand All @@ -96,10 +91,18 @@ def _check_private_key(self):

def perform_trade(self, trade_params, at_price):
self.log(f"Performing trade with params: {trade_params}")
account = self._get_account()
account = get_account(self.web3, self.private_key)
amount = to_base_units(trade_params['collateral'], decimals=6)
self.__approve(account, amount, self.use_delegation,
trade_params.get('trader_address'))

# Use shared approval function
approve_usdc(
self.web3,
self.usdc_contract,
self.ostium_trading_address,
amount,
self.private_key,
self.verbose
)

try:
self.log(f"Final trade parameters being sent: {trade_params}")
Expand Down Expand Up @@ -255,7 +258,7 @@ def close_trade(self, pair_id, trade_index, close_percentage=100, trader_address
A dictionary containing the transaction receipt and order ID
"""
self.log(f"Closing trade for pair {pair_id}, index {trade_index}")
account = self._get_account()
account = get_account(self.web3, self.private_key)

close_percentage = to_base_units(close_percentage, decimals=2)

Expand Down Expand Up @@ -317,7 +320,7 @@ def close_trade(self, pair_id, trade_index, close_percentage=100, trader_address
def remove_collateral(self, pair_id, trade_index, remove_amount):
self.log(
f"Remove collateral for trade for pair {pair_id}, index {trade_index}: {remove_amount} USDC")
account = self._get_account()
account = get_account(self.web3, self.private_key)

amount = to_base_units(remove_amount, decimals=6)

Expand Down Expand Up @@ -349,7 +352,7 @@ def add_collateral(self, pairID, index, collateral, trader_address=None):
Returns:
The transaction receipt
"""
account = self._get_account()
account = get_account(self.web3, self.private_key)
try:
amount = to_base_units(collateral, decimals=6)
self.__approve(account, amount,
Expand Down Expand Up @@ -412,7 +415,7 @@ def update_tp(self, pair_id, trade_index, tp_price, trader_address=None):
"""
self.log(
f"Updating TP for pair {pair_id}, index {trade_index} to {tp_price}")
account = self._get_account()
account = get_account(self.web3, self.private_key)
try:
tp_value = to_base_units(tp_price, decimals=18)

Expand Down Expand Up @@ -470,7 +473,7 @@ def update_sl(self, pairID, index, sl, trader_address=None):
Returns:
The transaction receipt
"""
account = self._get_account()
account = get_account(self.web3, self.private_key)
try:
sl_value = to_base_units(sl, decimals=18)

Expand Down Expand Up @@ -546,7 +549,7 @@ def __approve(self, account, collateral, use_delegation, trader_address=None):
f"Sufficient allowance for {trader_address} not present. Please approve the trading contract to spend USDC.")

def withdraw(self, amount, receiving_address):
account = self._get_account()
account = get_account(self.web3, self.private_key)

try:
amount_in_base_units = to_base_units(amount, decimals=6)
Expand Down
10 changes: 10 additions & 0 deletions ostium_python_sdk/sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from .price import Price
from web3 import Web3
from .ostium import Ostium
from .vault import OstiumVault
from .config import NetworkConfig
from typing import Union
from .subgraph import SubgraphClient
Expand Down Expand Up @@ -75,6 +76,15 @@ def __init__(self, network: Union[str, NetworkConfig], private_key: str = None,
use_delegation=self.use_delegation
)

# Initialize vault instance
self.vault = OstiumVault(
self.w3,
self.network_config.contracts["vault"],
self.network_config.contracts["usdc"],
private_key=self.private_key,
verbose=self.verbose
)

# Initialize subgraph client
self.subgraph = SubgraphClient(
url=self.network_config.graph_url, verbose=self.verbose)
Expand Down
Loading
Loading