API
Contracts

Contracts

Docs for the ELFi Protocol contracts.

ELFi Protocol utilizes EIP-2535 Diamonds, see below a list of ELFi's diamond address:

ChainDiamond address
Arbitrum0x153c613D572c050104086c7113d00B76Fbaa5d55
# Python demo: Get contract object

AccountFacet = w3.eth.contract(address= diamond_addr, abi=json.loads('AccountFacet.json'))
AccountFacet.getAccountInfo('0x000000000')

ABIs

All abis is here: ABIs (opens in a new tab)

Precision

USD value are multiplied by (10 ** 18)
Prices are multiplied by (10 ** 8)
Token amounts are multiplied by (10 ** token.decimals)
Rates are multiplied by (10 ** 5)

MarketFacet

All Symbol

To get all symbol information

function getAllSymbols() external view returns (SymbolInfo[] memory)

Response

struct SymbolInfo {
    bytes32 code;   
    Symbol.Status status;
    address stakeToken; // pool address
    address indexToken; // index token
    address baseToken;  // base token for long position
    SymbolConfig config;
}

Get a single symbol

To get a single symbol information

function getSymbol(bytes32 code) external view returns (SymbolInfo memory params)

Response

struct SymbolInfo {
    bytes32 code;
    Symbol.Status status;
    address stakeToken;
    address indexToken;
    address baseToken;
    SymbolConfig config;
}

AccountFacet

Only for CROSSED

Deposit

Deposit tokens to your portfolio account. First request rest api '/tokens' to get supported tokens

function deposit(address token, int256 amount) external payable

Parameters

NameTypeMandatoryDescription
tokenstringyesAddress of token that will be deposited
amountlongyesThe amount of token

Demos

# python demo (Deposit 10 USDC):

USDC = '0xaf88d065e77c8cC2239327C5EDb3A432268e5831'
amount = 10 * 10 ** 6
USDCContract.functions.approve(diamond_addr, amount).build_transaction({
            'from': account.address
        })
AccountFacet.deposit(USDC, amount)

Creating a Withdrawal

Deposit tokens to your portfolio account.

function createWithdrawRequest(
        address token,
        int256 amount,
        int256 executionFee,
        bool isWithdrawMax
    ) external payable

Parameters

NameTypeMandatoryDescription
tokenstringyesAddress of token that will be withdrawn
amountlongyesMultiplied by (10 ** token.decimals), set to 0 if isWithdrawMax is true
executionFeelongyesThe amount of native token that is included for the execution fee, currently not charged, set to 0
isWithdrawMaxboolyesWhether to withdraw all amount of the token

Demos

#python demo (Withdraw 10 USDC):

USDC = '0xaf88d065e77c8cC2239327C5EDb3A432268e5831'
amount = 10 * 10 ** 6
AccountFacet.createWithdrawRequest(USDC, amount, 0, false)
#python demo (Withdraw all WBTC):
WBTC = '0x2f2a2543B76A4166549F7aaB2e75Bef0aefC5B0f'
amount = 0
AccountFacet.createWithdrawRequest(WBTC, amount, 0, true)

Account info

To get account information

function getAccountInfo(address account) external view returns (AccountInfo memory)

Response

struct AccountInfo {
    address owner;
    TokenBalance[] tokenBalances;
    address[] tokens;
    address[] trialFundsTokens;
    uint256[] trialFundsAmounts;
    bytes32[] positions;
    uint256 portfolioNetValue;
    uint256 totalUsedValue;
    int256 availableValue;
    uint256 orderHoldInUsd;
    int256 crossMMR;
    int256 crossNetValue;
    uint256 totalMM;
}

OrderFacet

New Order

Send in a new increase/decrease position order. To create a increase isolate position order, you needs to first approval orderMargin to Diamond

function createOrderRequest(PlaceOrderParams calldata params) external payable;

Parameters PlaceOrderParams

NameTypeMandatoryDescription
symbolstringyesBTCUSD
isCrossMarginboolyesTrue for CROSSED else ISOLATED
isNativeTokenboolyesTrue for native token(Arbitrum is ETH), others false
orderSideintyes1: LONG, 2: SHORT
posSideintyes1: INCREASE, 2: DECREASE
orderTypeintyes1: MARKET, 2: LIMIT, 3: STOP
stopTypeintyes0: NOT_STOP_ORDER, 1: STOP_LOSS, 2: TAKE_PROFIT, 3: POSITION_STOP_LOSS, 4:POSITION_TAKE_PROFIT
marginTokenstringyesAddress of margin token. baseToken for long, USDC for short
qtylongyesThe position size to decrease, multiplied by 10 ** 18
orderMarginlongyesThe amount of margin to increase position. token amount for ISOLATED, multiplied by 10 ** token.decimals. usd amount for CROSSED, multiplied by 10 ** 18
leveragelongyesFrom 1 * 10 ** 5 to 20 * 10 ** 5
triggerPricelongyesThe trigger price for limit / stop-loss / take-profit orders, the order will be attempted to be executed if price reaches the trigger price
acceptablePricelongyesThe acceptable price at which the order can be executed. For market orders, the order will be cancelled if it cannot be executed at the acceptable price. For limit / stop-loss / take-profit orders, the order will not be executed if the trigger price is reached but the acceptable price cannot be fulfilled
executionFeelongyesThe amount of native token that is included for the execution fee, currently not charged, set to 0
placeTimelongyes

Demos

#python demo (Create a increase long order, CROSSED)

WBTC = '0x2f2a2543B76A4166549F7aaB2e75Bef0aefC5B0f'
orderMargin = 100 * 10 ** 18  // 100 USD, usd value for CROSSED
leverage = 5 * 10 ** 5 // 5x
OrderFacet.createOrderRequest({
    symbol: 'BTCUSD',
    isCrossMargin: true,
    isNativeToken: false,
    orderSide: 1,
    posSide: 1,
    orderType: 1,
    stopType: 0,
    marginToken: WBTC,
    qty: 0,
    orderMargin: orderMargin,
    leverage: leverage,
    triggerPrice: 0,
    acceptablePrice: 0,
    executionFee: 0,
    placeTime: 0,
})
#python demo (Create a increase short order, CROSSED)

USDC = '0xaf88d065e77c8cC2239327C5EDb3A432268e5831'
orderMargin = 100 * 10 ** 18  // 100 USD, usd value for CROSSED
leverage = 5 * 10 ** 5 // 5x
OrderFacet.createOrderRequest({
    symbol: 'BTCUSD',
    isCrossMargin: true,
    isNativeToken: false,
    orderSide: 2,
    posSide: 1,
    orderType: 1,
    stopType: 0,
    marginToken: USDC, // short need to use USDC
    qty: 0,
    orderMargin: orderMargin,
    leverage: leverage,
    triggerPrice: 0,
    acceptablePrice: 0,
    executionFee: 0,
    placeTime: 0,
})
#python demo (Create a increase long order, ISOLATED)

WBTC = '0x2f2a2543B76A4166549F7aaB2e75Bef0aefC5B0f'
orderMargin = 1 * 10 ** 8  // 1 WBTC, token amount for ISOLATED
leverage = 5 * 10 ** 5 // 5x
// ISOLATED: first to execute approve
WBTCContract.functions.approve(diamond_addr, orderMargin).build_transaction({
            'from': account.address
        })
OrderFacet.createOrderRequest({
    symbol: 'BTCUSD',
    isCrossMargin: false,
    isNativeToken: false,
    orderSide: 1,
    posSide: 1,
    orderType: 1,
    stopType: 0,
    marginToken: WBTC,
    qty: 0,
    orderMargin: orderMargin,
    leverage: leverage,
    triggerPrice: 0,
    acceptablePrice: 0,
    executionFee: 0,
    placeTime: 0,
})
#python demo (Create a increase short order, ISOLATED)

USDC = '0xaf88d065e77c8cC2239327C5EDb3A432268e5831'
orderMargin = 1000 * 10 ** 6  // 1000 USDC, token amount for ISOLATED
leverage = 5 * 10 ** 5 // 5x
// ISOLATED: first to execute approve
USDCContract.functions.approve(diamond_addr, orderMargin).build_transaction({
            'from': account.address
        })
OrderFacet.createOrderRequest({
    symbol: 'BTCUSD',
    isCrossMargin: false,
    isNativeToken: false,
    orderSide: 2,
    posSide: 1,
    orderType: 1,
    stopType: 0,
    marginToken: USDC, // short need to use USDC
    qty: 0,
    orderMargin: orderMargin,
    leverage: leverage,
    triggerPrice: 0,
    acceptablePrice: 0,
    executionFee: 0,
    placeTime: 0,
})
#python demo (Create a decrease long position order, CROSSED)

WBTC = '0x2f2a2543B76A4166549F7aaB2e75Bef0aefC5B0f'
qty = 1000 * 10 ** 18  // 1000 USD
leverage = 5 * 10 ** 5 // 5x
OrderFacet.createOrderRequest({
    symbol: 'BTCUSD',
    isCrossMargin: true,
    isNativeToken: false,
    orderSide: 2,   // short order to decrease long position
    posSide: 2,
    orderType: 1,
    stopType: 0,
    marginToken: WBTC,
    qty: qty,
    orderMargin: 0,
    leverage: leverage,
    triggerPrice: 0,
    acceptablePrice: 0,
    executionFee: 0,
    placeTime: 0,
})
#python demo (Create a decrease short position order, CROSSED)

USDC = '0xaf88d065e77c8cC2239327C5EDb3A432268e5831'
qty = 1000 * 10 ** 18  // 1000 USD
leverage = 5 * 10 ** 5 // 5x
OrderFacet.createOrderRequest({
    symbol: 'BTCUSD',
    isCrossMargin: true,
    isNativeToken: false,
    orderSide: 1,   // long order to decrease short position
    posSide: 2,
    orderType: 1,
    stopType: 0,
    marginToken: USDC,
    qty: qty,
    orderMargin: 0,
    leverage: leverage,
    triggerPrice: 0,
    acceptablePrice: 0,
    executionFee: 0,
    placeTime: 0,
})
#python demo (Create a decrease long position order, ISOLATED)

WBTC = '0x2f2a2543B76A4166549F7aaB2e75Bef0aefC5B0f'
qty = 1000 * 10 ** 18  // 1000 USD
leverage = 5 * 10 ** 5 // 5x
OrderFacet.createOrderRequest({
    symbol: 'BTCUSD',
    isCrossMargin: false,
    isNativeToken: false,
    orderSide: 2,   // short order to decrease long position
    posSide: 2,
    orderType: 1,
    stopType: 0,
    marginToken: WBTC,
    qty: qty,
    orderMargin: 0,
    leverage: leverage,
    triggerPrice: 0,
    acceptablePrice: 0,
    executionFee: 0,
    placeTime: 0,
})
#python demo (Create a decrease short position order, ISOLATED)
USDC = '0xaf88d065e77c8cC2239327C5EDb3A432268e5831'
qty = 1000 * 10 ** 18  // 1000 USD
leverage = 5 * 10 ** 5 // 5x
OrderFacet.createOrderRequest({
    symbol: 'BTCUSD',
    isCrossMargin: false,
    isNativeToken: false,
    orderSide: 1,   // long order to decrease short position
    posSide: 2,
    orderType: 1,
    stopType: 0,
    marginToken: USDC,
    qty: qty,
    orderMargin: 0,
    leverage: leverage,
    triggerPrice: 0,
    acceptablePrice: 0,
    executionFee: 0,
    placeTime: 0,
})

Cancel an order

Cancel an existing order.

function cancelOrder(int256 orderId) external;

Parameters

PlaceOrderParams

NameTypeMandatoryDescription
orderIdstringyes

All orders

To get all orders of account

function getAccountOrders(address account) external view returns (AccountOrder[] memory);

Response

struct AccountOrder {
    uint256 orderId;
    Order.OrderInfo orderInfo;
}

struct OrderInfo {
    address account;
    bytes32 symbol;
    Side orderSide;
    PositionSide posSide;
    Type orderType;
    StopType stopType;
    bool isCrossMargin;
    bool isExecutionFeeFromTradeVault;
    address marginToken;
    uint256 qty;
    uint256 leverage; 
    uint256 orderMargin;
    uint256 triggerPrice; 
    uint256 acceptablePrice; 
    uint256 placeTime;
    uint256 executionFee;
    uint256 lastBlock;
}

PositionFacet

Get a single position

To get a single position of account

function getSinglePosition(
        address account,
        bytes32 symbol,
        address marginToken,
        bool isCrossMargin
    ) external pure returns (Position.Props memory)

Response

struct Position.Props {
    bytes32 key;
    bytes32 symbol;
    bool isLong;
    bool isCrossMargin;
    address account;
    address marginToken;
    address indexToken;
    uint256 qty;
    uint256 entryPrice;
    uint256 entryMarginTokenPrice;
    uint256 leverage;
    uint256 initialMargin;
    uint256 initialMarginInUsd;
    uint256 initialMarginInUsdFromBalance;
    uint256 holdPoolAmount;
    PositionFee positionFee;
    int256 realizedPnl;
    uint256 lastUpdateTime;
}

All positions

To get all positions of account

function getAllPositions(address account) external view returns (PositionInfo[] memory)

Response

struct PositionInfo {
    Position.Props position;
    uint256 liquidationPrice;
    uint256 currentTimestamp;
}