Skip to content

Commit

Permalink
change token type to string
Browse files Browse the repository at this point in the history
  • Loading branch information
zhoushuren committed Aug 28, 2024
1 parent f1619e7 commit cc2e909
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/core/UnitAlgorithm.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;
import { console2 } from "forge-std/console2.sol";
// import { console2 } from "forge-std/console2.sol";

interface IMedian{
function read() external view returns (uint256);
Expand All @@ -9,6 +9,8 @@ contract UnitAlgorithm {

address public median;

address public gov;

mapping (address => bool) public orcl;

uint256 public lastMonthUnitPrice;
Expand All @@ -19,19 +21,25 @@ contract UnitAlgorithm {
uint256 updateTime;
}

mapping (uint256 => address[]) public tokensPerMonth;
mapping (uint256 => string[]) public tokensPerMonth;

mapping (uint256 => mapping ( string => MarketInfo)) public tokenPerMonthMarketInfo;

mapping (uint256 => mapping ( address => MarketInfo)) public tokenPerMonthMarketInfo;
modifier onlyGov{
require(msg.sender == gov, "only gov!");
_;
}

constructor(address _median){
constructor(address _median, address _gov){
median = _median;
gov = _gov;
}

function setOrcl(address _account, bool _a) public {
function setOrcl(address _account, bool _a) public onlyGov {
orcl[_account] = _a;
}

function updateTokens(uint256 _month, address[] calldata _token) public { // only gov
function updateTokens(uint256 _month, string[] calldata _token) public onlyGov { // only gov
tokensPerMonth[_month] = _token;
}

Expand All @@ -46,8 +54,8 @@ contract UnitAlgorithm {
For example, pre-writing can be done before the last day of the month at 23:59:59.
On the first day of the next month at 00:00:00, the pre-writing can take effect.
*/
function updateMarketInfo(uint256 _month, address _token, uint256[] calldata _lastMCP, uint256[] calldata _lastMMC, uint8[] calldata v, bytes32[] calldata r, bytes32[] calldata s) public {
MarketInfo storage marketInfo = tokenPerMonthMarketInfo[_month][_token];
function updateMarketInfo(uint256 _month, string calldata _token, uint256[] calldata _lastMCP, uint256[] calldata _lastMMC, uint8[] calldata v, bytes32[] calldata r, bytes32[] calldata s) public {
MarketInfo storage marketInfo = tokenPerMonthMarketInfo[_month][_token];

require(_lastMCP.length == _lastMMC.length, "length not eq!");
require(_month >= block.timestamp, "time out!");
Expand All @@ -58,12 +66,11 @@ contract UnitAlgorithm {
marketInfo.lastMonthMarketCap = _lastMMC[i];
marketInfo.updateTime = block.timestamp;
}

lastMonthUnitPrice = IMedian(median).read();
// lastMonthUnitPrice = IMedian(median).read();
}

function totalMarketCap(uint256 _month) public view returns(uint256) {
address[] memory _tokens = tokensPerMonth[_month];
string[] memory _tokens = tokensPerMonth[_month];
uint256 _totalMarketCap = 0;
for(uint i = 0 ; i < _tokens.length; i++) {
MarketInfo memory marketInfo = tokenPerMonthMarketInfo[_month][_tokens[i]];
Expand All @@ -74,7 +81,7 @@ contract UnitAlgorithm {
// test
function calculate(uint256 _month, uint256[] calldata _price) public view returns(uint256) {
uint256 _count = 0;
address[] memory _tokens = tokensPerMonth[_month];
string[] memory _tokens = tokensPerMonth[_month];

for(uint i = 0; i< _tokens.length; i++) {
MarketInfo memory marketInfo = tokenPerMonthMarketInfo[_month][_tokens[i]];
Expand Down
109 changes: 109 additions & 0 deletions test/UnitAlgorithm.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.21;

import { console2 } from "forge-std/console2.sol";
import { BaseSetup } from "./BaseSetup.t.sol";
import { TinuToken } from "../src/core/TinuToken.sol";
import { UnitAlgorithm } from "../src/core/UnitAlgorithm.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "forge-std/console.sol"; // test

contract UnitAlgorithmTest is BaseSetup {

UnitAlgorithm public unitAlgorithm;

// TinuToken token1 = new TinuToken();
// TinuToken token2 = new TinuToken();
// TinuToken token3 = new TinuToken();
/*
[
'avalanche-2', 'binancecoin',
'bitcoin', 'bitcoin-cash',
'cardano', 'chainlink',
'dogecoin', 'ethereum',
'fetch-ai', 'internet-computer',
'leo-token', 'litecoin',
'matic-network', 'near',
'pepe', 'polkadot',
'ripple', 'shiba-inu',
'solana', 'the-open-network',
'tron', 'uniswap'
]
*/
function setUp() public override {
super.setUp();
vm.startPrank(owner);

unitAlgorithm = new UnitAlgorithm(address(0), owner);

string[] memory tokens = new string[](22);
tokens[0] = "avalanche-2";
tokens[1] = "bitcoin";
tokens[2] = "cardano";
tokens[3] = "dogecoin";
tokens[4] = "fetch-ai";
tokens[5] = "leo-token";
tokens[6] = "matic-network";
tokens[7] = "pepe";
tokens[8] = "ripple";
tokens[9] = "solana";
tokens[10] = "tron";
tokens[11] = "binancecoin";
tokens[12] = "bitcoin-cash";
tokens[13] = "chainlink";
tokens[14] = "ethereum";
tokens[15] = "internet-compute";
tokens[16] = "litecoin";
tokens[17] = "near";
tokens[18] = "polkadot";
tokens[19] = "shiba-inu";
tokens[20] = "the-open-network";
tokens[21] = "uniswap";

unitAlgorithm.updateTokens(block.timestamp, tokens);

console2.log("aaaaa:", unitAlgorithm.tokensPerMonth(block.timestamp, 2));

vm.stopPrank();
}

function test_feedInfo() external {

vm.startPrank(owner);

uint256 _month = block.timestamp;

uint256 _lastMCP = 6110437611602430;
uint256 _lastMMC = 1205378364698;

(address alice, uint256 alicePk) = makeAddrAndKey("alice");
unitAlgorithm.setOrcl(alice, true);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(alicePk,keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", keccak256(abi.encodePacked(_lastMCP, _lastMMC)))));

uint256[] memory _lastMCPs = new uint256[](1);
_lastMCPs[0] = _lastMCP;
uint256[] memory _lastMMCs = new uint256[](1);
_lastMMCs[0] = _lastMMC;

uint8[] memory vs = new uint8[](1);
vs[0] = v;
bytes32[] memory rs = new bytes32[](1);
rs[0] = r;
bytes32[] memory ss = new bytes32[](1);
ss[0] = s;

string memory token = "bitcoin";

unitAlgorithm.updateMarketInfo(_month, token, _lastMCPs, _lastMMCs, vs, rs, ss);

(uint256 lastMonthClosePrice, uint256 lastMonthMarketCap, uint256 updateTime) = unitAlgorithm.tokenPerMonthMarketInfo(_month, token);

assertEq(lastMonthClosePrice, _lastMCP);
assertEq(lastMonthMarketCap, _lastMMC);

uint256 cap = unitAlgorithm.totalMarketCap(_month);
console2.log(cap);

vm.stopPrank();
}
}

0 comments on commit cc2e909

Please sign in to comment.