Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Library Example #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
34 changes: 34 additions & 0 deletions contracts/core/Pool.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-License-Identifier: MIT

pragma solidity 0.6.12;
pragma experimental ABIEncoderV2;

import './Position.sol';

contract Pool {
using Position for mapping(bytes32 => Position.Info);
using Position for Position.Info;

mapping (bytes32 => Position.Info) public positions;

function getPosition(
address account,
address collateralToken,
uint256 marketId,
bool isLong
) external view returns (Position.Info memory) {
return positions.get(account, collateralToken, marketId, isLong);
}

function increasePosition(
address account,
address collateralToken,
uint256 marketId,
bool isLong,
uint256 sizeDelta,
uint256 collateralDelta
) external {
Position.Info storage position = positions.get(account, collateralToken, marketId, isLong);
position.increase(sizeDelta, collateralDelta);
}
}
38 changes: 38 additions & 0 deletions contracts/core/Position.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: MIT

pragma solidity 0.6.12;

library Position {
struct Info {
uint256 size;
uint256 collateralAmount;
uint256 averagePrice;
uint256 entryFundingRate;
uint256 reserveAmount;
uint256 updatedAt;
}

function get(
mapping(bytes32 => Info) storage self,
address account,
address collateralToken,
uint256 marketId,
bool isLong
) internal view returns (Position.Info storage) {
return self[keccak256(abi.encodePacked(account, collateralToken, marketId, isLong))];
}

function increase(
Info storage self,
uint256 sizeDelta,
uint256 collateralDelta
) internal {
Info memory _self = self;
self.size = _self.size + sizeDelta;
self.collateralAmount = _self.collateralAmount + collateralDelta;
self.averagePrice = _self.averagePrice + 1;
self.entryFundingRate = _self.entryFundingRate + 2;
self.reserveAmount = _self.reserveAmount + 3;
self.updatedAt = block.timestamp;
}
}
48 changes: 48 additions & 0 deletions test/core/Pool.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const { expect, use } = require("chai")
const { solidity } = require("ethereum-waffle")
const { deployContract } = require("../shared/fixtures")
const { expandDecimals, getBlockTime, increaseTime, mineBlock, reportGasUsed } = require("../shared/utilities")
const { toChainlinkPrice } = require("../shared/chainlink")
const { toUsd, toNormalizedPrice } = require("../shared/units")
const { initVault, getBnbConfig, getBtcConfig, getDaiConfig, validateVaultBalance } = require("./Vault/helpers")

use(solidity)

const USD_PRECISION = expandDecimals(1, 30)

describe("Pool", function () {
const provider = waffle.provider
const [wallet, user0, user1, user2, user3] = provider.getWallets()
let eth
let pool

beforeEach(async () => {
eth = await deployContract("Token", [])
pool = await deployContract("Pool", [])
})

it("increasePosition", async () => {
await pool.increasePosition(user0.address, eth.address, 17, true, 1000, 200)
let position = await pool.getPosition(user0.address, eth.address, 17, true)
console.log("position",
position.size.toString(),
position.collateralAmount.toString(),
position.averagePrice.toString(),
position.entryFundingRate.toString(),
position.reserveAmount.toString(),
position.updatedAt.toString()
)

await pool.increasePosition(user0.address, eth.address, 17, true, 500, 100)

position = await pool.getPosition(user0.address, eth.address, 17, true)
console.log("position",
position.size.toString(),
position.collateralAmount.toString(),
position.averagePrice.toString(),
position.entryFundingRate.toString(),
position.reserveAmount.toString(),
position.updatedAt.toString()
)
})
})