@divergence-protocol/diver-sdk
v1.4.4
Published
## PositionManager
Downloads
153
Readme
Diver SDK
PositionManager
Multicall
import { PositionManager } from './positionManager'
const data1 = PositionManager.createBattleCallParameters(args1);
const data2 = PositionManager.mintCallParameters(arg2);
const {calldata, decode} = PositionManager.multicall([data1, data2])
Create battle
import {ethers} from 'ethers'
import {PositionManager} from "./PositionManager";
const provider = new ethers.providers.JsonRpcProvider('<YOUR-ENDPOINT-HERE>')
const positionAddress = '0x8ad599c3A0ff1De082011EFDDc58f1908eb6e6D8'
const args = {
oracle: '',
battleKey: {
collateralTokenAddr: '',
underlying: '',
periodType: PeriodType.WEEK,
strikeValue: 0
},
sqrtPriceX96: '',
}
const data = PositionManager.createBattleCallParameters(args);
provider.getSiger().sendTransaction({
from: '',
to: positionAddress,
data: data
}).then(res => console.log(res)).catch(e => console.error(e))
Mint
import {ethers} from 'ethers'
import {PositionManager} from "./PositionManager";
const provider = new ethers.providers.JsonRpcProvider('<YOUR-ENDPOINT-HERE>')
const positionAddress = '0x8ad599c3A0ff1De082011EFDDc58f1908eb6e6D8'
const args = {
battleKey: {
collateralTokenAddr: '',
underlying: '',
periodType: PeriodType.WEEK,
strikeValue: 0
},
tickLower: 0,
tickUpper: 0,
collateralDelta: 0,
amountSpearMin: 0,
amountShieldMin: 0,
recipient: '',
deadline: ''
}
const data = PositionManager.mintCallParameters(args);
provider.getSiger().sendTransaction({
from: '',
to: positionAddress,
data: data
}).then(res => console.log(res)).catch(e => console.error(e))
Burn
import {ethers} from 'ethers'
import {PositionManager} from "./PositionManager";
const provider = new ethers.providers.JsonRpcProvider('<YOUR-ENDPOINT-HERE>')
const positionAddress = '0x8ad599c3A0ff1De082011EFDDc58f1908eb6e6D8'
const battleKey = {
collateralTokenAddr: '',
underlying: '',
periodType: PeriodType.WEEK,
strikeValue: 0
}
const tokenId = 0
const data = PositionManager.burnCallParameters(battleKey, tokenId);
provider.getSiger().sendTransaction({
from: '',
to: positionAddress,
data: data
}).then(res => console.log(res)).catch(e => console.error(e))
Withdraw
import {ethers} from 'ethers'
import {PositionManager} from "./PositionManager";
const provider = new ethers.providers.JsonRpcProvider('<YOUR-ENDPOINT-HERE>')
const positionAddress = '0x8ad599c3A0ff1De082011EFDDc58f1908eb6e6D8'
const battleKey = {
collateralTokenAddr: '',
underlying: '',
periodType: PeriodType.WEEK,
strikeValue: 0
}
const tokenId = 0
const data = PositionManager.withdrawObligationCallParameters(battleKey, tokenId);
provider.getSiger().sendTransaction({
from: '',
to: positionAddress,
data: data
}).then(res => console.log(res)).catch(e => console.error(e))
Trade
import {ethers} from 'ethers'
import {PositionManager} from "./PositionManager";
import {TradeAction} from "./enum";
const provider = new ethers.providers.JsonRpcProvider('<YOUR-ENDPOINT-HERE>')
const positionAddress = '0x8ad599c3A0ff1De082011EFDDc58f1908eb6e6D8'
const args = {
battleKey: {
collateralTokenAddr: '',
underlying: '',
periodType: PeriodType.WEEK,
strikeValue: 0
},
action: TradeAction.BUY_SHIELD,
amountSpecified: 0,
recipient: '',
amountOutMin: 0,
sqrtPriceLimitX96: '',
deadline: ''
}
const data = PositionManager.tradeCallParameters(args);
provider.getSiger().sendTransaction({
from: '',
to: positionAddress,
data: data
}).then(res => console.log(res)).catch(e => console.error(e))
Battle
Claim
import {ethers} from 'ethers'
import {Battler} from "./Battle";
const provider = new ethers.providers.JsonRpcProvider('<YOUR-ENDPOINT-HERE>')
const address = '0x8ad599c3A0ff1De082011EFDDc58f1908eb6e6D8'
const data = Battle.claimCallParameters(10);
provider.getSiger().sendTransaction({
from: '',
to: address,
data: data
}).then(res => console.log(res)).catch(e => console.error(e))
CurrentSqrtPriceX96
import {ethers} from 'ethers'
import {Quoter} from "./Quoter";
import {Battle} from "./Battle";
const provider = new ethers.providers.JsonRpcProvider('<YOUR-ENDPOINT-HERE>')
const battleAddress = '0x8ad599c3A0ff1De082011EFDDc58f1908eb6e6D8'
const data = Battle.currentSqrtPriceX96();
provider.call({
to: battleAddress,
data: data
}).then(res => console.log(res)).catch(e => console.error(e))
Get user balance
import {Battle} from "./Battle";
const ri = 2
const account = ''
const data1 = Battle.spearBalanceOf(ri, account);
const data2 = Battle.shieldBalanceOf(ri, account)
Helper
// 1. get x96 sqrt price, input is shield price for collateral
getSqrtPriceX96FromPrice(0.4);
// 2. get shield price from x96 sqrt price
getPriceFromSqrtPriceX96("1000000")
// 3. get shield price from tick
getPriceFromTick(8)
// 4. get tick from shield price
getTickFromPrice(0.4)
// 5. get tick from sqrt price x96
getTickFromSqrtPriceX96("100000000")
// 6. get sqrt price from tick
getSqrtPriceX96FromTick(8)
// 3. calculate price imapct
const currentSqrtPriceX96 = "1000000000000"
const inputAmount = "1000000"
const outputAmount = "200000000000"
const isBuy = true
const impact = calculatePriceImpact(currentSqrtPriceX96, inputAmount, outputAmount, isBuy)
Quoter
quoteExactInput
import {ethers} from 'ethers'
import {Quoter} from "./Quoter";
const provider = new ethers.providers.JsonRpcProvider('<YOUR-ENDPOINT-HERE>')
const quoterAddress = '0x8ad599c3A0ff1De082011EFDDc58f1908eb6e6D8'
const action = 0 // 0: buy spear, 1: sell spear, 2: buy shield, 3: sell shield
const price = 0.4
const amountSpecified = "100000000"
const battleAddress = ""
const data = Quoter.quoteExactInputCallParameters(action, price, amountSpecified, battleAddress);
provider.call({
to: quoterAddress,
data: data
}).then(res => console.log(res)).catch(e => console.error(e))
Oracle
Get round ts
import { Oracle } from './oracle'
import { PeriodType } from './enum'
const { calldata, decode } = Oracle.getRoundTS(PeriodType.BI_WEEK)
provider.call({
to: oracleAddress,
data: calldata
}).then(res => {
const result = decode(res)
}).catch(e => console.error(e))