From 1d68ccd93637977df0fd2c4792e615a6fd658c01 Mon Sep 17 00:00:00 2001 From: X Date: Wed, 20 Jul 2022 17:12:24 +0800 Subject: [PATCH] add library --- contracts/core/Pool.sol | 34 ++++++++++++++++++++++++++ contracts/core/Position.sol | 38 +++++++++++++++++++++++++++++ test/core/Pool.js | 48 +++++++++++++++++++++++++++++++++++++ 3 files changed, 120 insertions(+) create mode 100644 contracts/core/Pool.sol create mode 100644 contracts/core/Position.sol create mode 100644 test/core/Pool.js diff --git a/contracts/core/Pool.sol b/contracts/core/Pool.sol new file mode 100644 index 00000000..0e33bea5 --- /dev/null +++ b/contracts/core/Pool.sol @@ -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); + } +} diff --git a/contracts/core/Position.sol b/contracts/core/Position.sol new file mode 100644 index 00000000..3788b9cb --- /dev/null +++ b/contracts/core/Position.sol @@ -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; + } +} diff --git a/test/core/Pool.js b/test/core/Pool.js new file mode 100644 index 00000000..00ec495f --- /dev/null +++ b/test/core/Pool.js @@ -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() + ) + }) +})