Last active
March 30, 2021 21:26
-
-
Save jrhea/11d1b5605f5bd9c30a8a397535ec2fc3 to your computer and use it in GitHub Desktop.
methods for balancer sor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// calc_in_given_out (swap) | |
function _tokenInForExactTokenOut(amount, poolPairData) { | |
let inReserves = poolPairData.balanceIn; | |
let outReserves = poolPairData.balanceOut; | |
let lpShares = poolPairData.lpShares; | |
let inputToken = poolPairData.inputToken | |
let outputToken = poolPairData.outputToken; | |
let baseToken = poolPairData.baseToken; | |
let principalToken = poolPairData.principalToken; | |
let phi = poolPairData.swapFee; | |
let t = poolPairData.time; | |
if(inputToken == baseToken){ | |
outReserves += lpShares; | |
} else if(inputToken == principalToken){ | |
inReserves += lpShares; | |
} | |
k=Math.pow(inReserves,1-t) + Math.pow(outReserves,1-t); | |
withoutFee = Math.pow(k-Math.pow(outReserves-amount,1-t),1/(1-t)) - inReserves; | |
fee = Math.abs(amount-withoutFee)*phi; | |
return withoutFee+fee; | |
} | |
// calc_out_given_in (swap) | |
function _exactTokenInForTokenOut(amount, poolPairData) { | |
let inReserves = poolPairData.balanceIn; | |
let outReserves = poolPairData.balanceOut; | |
let lpShares = poolPairData.lpShares; | |
let inputToken = poolPairData.inputToken | |
let outputToken = poolPairData.outputToken; | |
let baseToken = poolPairData.baseToken; | |
let principalToken = poolPairData.principalToken; | |
let phi = poolPairData.swapFee; | |
let t = poolPairData.time; | |
if(outputToken == baseToken){ | |
inReserves += lpShares; | |
} else if(outputToken == principalToken){ | |
outReserves += lpShares; | |
} | |
k=Math.pow(inReserves,1-t) + Math.pow(outReserves,1-t); | |
withoutFee = outReserves - Math.pow(k-Math.pow(inReserves+amount,1-t),1/(1-t)); | |
fee = Math.abs(amount-withoutFee)*phi; | |
return withoutFee-fee; | |
} | |
console.log("Case: _tokenInForExactTokenOut where inputToken = PT and outputToken = BT") | |
xReserves = 447689.0001456285 | |
yReserves = 967438.5972366218 | |
totalSupply = 265819.67027583614 | |
phi = .05 | |
t = 0.20475000000000002 | |
amount = 2429.409437012756 | |
var poolPairData = {} | |
poolPairData.balanceIn = yReserves | |
poolPairData.balanceOut = xReserves | |
poolPairData.lpShares = totalSupply | |
poolPairData.inputToken = "0x0P" | |
poolPairData.outputToken = "0x0B" | |
poolPairData.baseToken = "0x0B" | |
poolPairData.principalToken = "0x0P" | |
poolPairData.swapFee = phi | |
poolPairData.time = t | |
expectedAmountIn = 3020.089879734272 | |
amountIn = _tokenInForExactTokenOut(amount, poolPairData) | |
console.log("Amount Out: " + amount + " -> Amount In: " + amountIn + " = " + expectedAmountIn) | |
console.log("Case: _tokenInForExactTokenOut where inputToken = BT and outputToken = PT") | |
xReserves = 227297.54091594956 | |
yReserves = 230177.25143358269 | |
totalSupply = 311863.7956289041 | |
phi = .05 | |
t = 0.12425 | |
amount = 1690.739673346158 | |
var poolPairData = {} | |
poolPairData.balanceIn = xReserves | |
poolPairData.balanceOut = yReserves | |
poolPairData.lpShares = totalSupply | |
poolPairData.inputToken = "0x0B" | |
poolPairData.outputToken = "0x0P" | |
poolPairData.baseToken = "0x0B" | |
poolPairData.principalToken = "0x0P" | |
poolPairData.swapFee = phi | |
poolPairData.time = t | |
expectedAmountIn = 1527.2097822578185 | |
amountIn = _tokenInForExactTokenOut(amount, poolPairData) | |
console.log("Amount Out: " + amount + " -> Amount In: " + amountIn + " = " + expectedAmountIn) | |
console.log("Case: _exactTokenInForTokenOut where inputToken = BT and outputToken = PT") | |
xReserves = 674971.6048091053 | |
yReserves = 252853.96590876798 | |
totalSupply = 888791.4151192267 | |
phi = .05 | |
t = 0.127 | |
amount = 2362.8991926827807 | |
var poolPairData = {} | |
poolPairData.balanceIn = xReserves | |
poolPairData.balanceOut = yReserves | |
poolPairData.lpShares = totalSupply | |
poolPairData.inputToken = "0x0B" | |
poolPairData.outputToken = "0x0P" | |
poolPairData.baseToken = "0x0B" | |
poolPairData.principalToken = "0x0P" | |
poolPairData.swapFee = phi | |
poolPairData.time = t | |
expectedAmountOut = 2516.969797522765 | |
amountOut = _exactTokenInForTokenOut(amount, poolPairData) | |
console.log("Amount In: " + amount + " -> Amount Out: " + amountOut + " = " + expectedAmountOut) | |
console.log("Case: _exactTokenInForTokenOut where inputToken = PT and outputToken = BT") | |
xReserves = 251639.14235585282 | |
yReserves = 475364.5726183281 | |
totalSupply = 203024.0527059632 | |
phi = .05 | |
t = 0.06075 | |
amount = 2193.719312762269 | |
var poolPairData = {} | |
poolPairData.balanceIn = yReserves | |
poolPairData.balanceOut = xReserves | |
poolPairData.lpShares = totalSupply | |
poolPairData.intputToken = "0x0P" | |
poolPairData.outputToken = "0x0B" | |
poolPairData.baseToken = "0x0B" | |
poolPairData.principalToken = "0x0P" | |
poolPairData.swapFee = phi | |
poolPairData.time = t | |
expectedAmountOut = 2058.2884734369927 | |
amountOut = _exactTokenInForTokenOut(amount, poolPairData) | |
console.log("Amount In: " + amount + " -> Amount Out: " + amountOut + " = " + expectedAmountOut) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment