Code Monkey home page Code Monkey logo

Comments (6)

Morteza3123 avatar Morteza3123 commented on August 11, 2024

//first the contract check actual vBayc positions balance of users
int256 allLongvBaycBalance = getAllLongvBaycBalance();
int256 allShortBaycBalance = getAllShortvBaycBalance();
//check if we dont have one side(long or short balance = 0) this funding action will not run
if (allLongvBaycBalance > 0 && allShortBaycBalance < 0) {

uint256 fundingFee = (uint256(allLongvBaycBalance) * (indexPrice - oraclePrice)) / 24;

uint256 fundingFee = (positive(allShortBaycBalance) * (oraclePrice - oraclePrice)) / 24;

I added the condition that if one side doesn't exist, the funding function will not run. Also, the funding fee will be according to the real trader's assetSize. For example, if index price is more than oracle price, we calculate the funding fee according to the all active short asset size and change the price to give this reward to long users(by decrease the vusd pool and decreasing the vBayc pool (by fixed k)

from nex-dex-contracts.

Waltermelon-lang avatar Waltermelon-lang commented on August 11, 2024

I not sure about the following:
If I open a long worth $10K and after me a short opens for $1K,
so now the pool price is allowed to move according to the funding rate.
Lets say the pool moves up 10% in price, so now the long is at a $11K, and the short is down to $900, so 100$ loss versus $1K profit.
This is not possible since the losses needs to cancel out the profits.
If we say, well then the short goes to zero, then they cancel out, but that would mean the short is suddenly 10x leveraged because the long position size was bigger.

Therefore, I think it is not possible not move the pool price (unless long and short are exactly the same size).
Another idea therefore might be: charge users the fundFee so they are incentivised to move the pool.
However, whenever they want to exit or get liquididated they are also charged their part of the fundFee,
so that if we have 10K users, they are all billed for 1/10.000 (1/activeUsers.length) gas fee per fundrate round .

from nex-dex-contracts.

Morteza3123 avatar Morteza3123 commented on August 11, 2024

In current code, I always calculate the fund fee by loser's position size.

By your example. Long worth 10K and short worth 1K.
If funding rate is +10%(indexPrice > oraclePrice), So funding fee will be calculte according to the worth of short positions (fundingFee = 10%*1K= 100);

If funding rate is -10%(indexPrice < oraclePrice), So funding fee will be calculte according to the worth of long positions (fundingFee = 10%*10K= 1000);

So always, funding fee will be according to the loser asset size.
If we have 1000K long and only 1K short and funding rate is positive, losers are long users and the funding fee is based on long user's asset size. If funding rate is negative, losers are short users and funding fee is based on short user's asset size. So funding fee is always based on the loser assetSize(1000K-10%*1000K or 1K - 10%1K)

from nex-dex-contracts.

Waltermelon-lang avatar Waltermelon-lang commented on August 11, 2024

"In current code, I always calculate the fund fee by loser's position size."
Good to know and I do not want to calculate the funding fee like this, it has to be relative to people their position size and just as an incentive without actually moving the pool price.
You can't touch the pool price (unless you can convince me otherwise), cause it can create net positive/negative profit and this is a zero sum game.

from nex-dex-contracts.

Morteza3123 avatar Morteza3123 commented on August 11, 2024

function setFundingRate() public onlyOwner {
uint256 indexPrice = vBaycPoolSize / vUsdPoolSize;
uint256 oraclePrice = nftOracle.showPrice(latestRequestId);
//first the contract check actual vBayc positions balance of users
int256 allLongvBaycBalance = getAllLongvBaycBalance();
int256 allShortBaycBalance = getAllShortvBaycBalance();
//check if we dont have one side(long or short balance = 0) this funding action will not run
if (allLongvBaycBalance > 0 && allShortBaycBalance < 0) {
if (indexPrice > oraclePrice) {
uint256 fundingFee = (uint256(allLongvBaycBalance) * (indexPrice - oraclePrice)) / 24;
for (uint256 i = 0; i < activeUsers.length; i++) {
if (uservBaycBalance[activeUsers[i]] > 0) {
uint256 userFundingFee = (fundingFee * uint256(uservBaycBalance[activeUsers[i]])) /
uint256(allLongvBaycBalance);
collateral[usdc][activeUsers[i]] -= userFundingFee;
} else if (uservBaycBalance[activeUsers[i]] < 0) {
uint256 userFundingFee = (fundingFee * uint256(uservBaycBalance[activeUsers[i]])) /
positive(allShortBaycBalance);
collateral[usdc][activeUsers[i]] += userFundingFee;
}
}
} else if (indexPrice < oraclePrice) {
uint256 fundingFee = (positive(allShortBaycBalance) * (oraclePrice - oraclePrice)) / 24;
for (uint256 i = 0; i < activeUsers.length; i++) {
if (uservBaycBalance[activeUsers[i]] > 0) {
uint256 userFundingFee = (fundingFee * uint256(uservBaycBalance[activeUsers[i]])) /
uint256(allLongvBaycBalance);
collateral[usdc][activeUsers[i]] += userFundingFee;
} else if (uservBaycBalance[activeUsers[i]] < 0) {
uint256 userFundingFee = (fundingFee * uint256(uservBaycBalance[activeUsers[i]])) /
positive(allShortBaycBalance);
collateral[usdc][activeUsers[i]] += userFundingFee;
}
}
}
}
}

I edited the function, so each user will have the funding fee effect in the collateral.

from nex-dex-contracts.

Waltermelon-lang avatar Waltermelon-lang commented on August 11, 2024

💯

from nex-dex-contracts.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.