Skip to content

Commit

Permalink
(migration): write crude first test for conversion between v1 and tim…
Browse files Browse the repository at this point in the history
…e circles
  • Loading branch information
benjaminbollen committed Dec 11, 2023
1 parent d7ed2fd commit ee5d847
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 12 deletions.
16 changes: 8 additions & 8 deletions src/migration/Migration.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import "../graph/IGraph.sol";
contract CirclesMigration {
// Constant

uint256 private constant ACCURACY = uint256(10**8);
uint256 private constant ACCURACY = uint256(10 ** 8);

// State variables

IHubV1 public immutable hubV1;

IGraph public immutable graphV2;
// IGraph public immutable graphV2;

uint256 public immutable inflation;
uint256 public immutable divisor;
Expand All @@ -29,12 +29,12 @@ contract CirclesMigration {
// https://aboutcircles.com/t/conversion-from-crc-to-time-circles-and-back/463
// the UI conversion used is found here:
// https://github.com/circlesland/timecircle/blob/master/src/index.ts
constructor(IHubV1 _hubV1, IGraph _graphV2) {
constructor(IHubV1 _hubV1) {
require(address(_hubV1) != address(0), "Hub v1 address can not be zero.");
require(address(_graphV2) != address(0), "Graph v2 address can not be zero.");
// require(address(_graphV2) != address(0), "Graph v2 address can not be zero.");

hubV1 = _hubV1;
graphV2 = _graphV2;
// graphV2 = _graphV2;

// from deployed v1 contract SHOULD return inflation = 107
inflation = hubV1.inflation();
Expand Down Expand Up @@ -107,9 +107,9 @@ contract CirclesMigration {
// r = x * (1 - a) + y * a
// if a = secondsIntoCurrentPeriod / Period = s / P
// => P * r = x * (P - s) + y * s
uint256 rP = factorCurrentPeriod * (period - secondsIntoCurrentPeriod)
+ factorNextPeriod * secondsIntoCurrentPeriod;
uint256 rP =
factorCurrentPeriod * (period - secondsIntoCurrentPeriod) + factorNextPeriod * secondsIntoCurrentPeriod;

// account for the adjustment of the accepted gauge of 24 CRC / day,
// rather than 8 CRC / day, so multiply by 3
// and divide by the inflation rate to convert to temporally discounted units
Expand Down
2 changes: 1 addition & 1 deletion test/graph/Graph.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import "../../src/graph/ICircleNode.sol";
import "../../src/circles/TimeCircle.sol";
import "../../src/circles/GroupCircle.sol";
import "../../src/mint/MintSplitter.sol";
import "./MockHub.sol";
import "../migration/MockHub.sol";
import "./MockInternalGraph.sol";

contract GraphTest is Test {
Expand Down
2 changes: 1 addition & 1 deletion test/graph/GraphPathTransfer.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import "../../src/circles/TimeCircle.sol";
import "../../src/circles/GroupCircle.sol";
import "../../src/mint/MintSplitter.sol";
import "../setup/TimeSetup.sol";
import "./MockHub.sol";
import "../migration/MockHub.sol";

contract GraphPathTransferTest is Test, TimeSetup {
// Constant
Expand Down
53 changes: 53 additions & 0 deletions test/migration/Migration.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.13;

import {Test} from "forge-std/Test.sol";
import {StdCheats} from "forge-std/StdCheats.sol";
import "../../src/migration/Migration.sol";
import "../setup/TimeSetup.sol";
import "./MockHub.sol";

contract GraphTest is Test {
// Constants

uint256 private ACCURACY_ONE = uint256(10 ** 8);
uint256 private ACCURACY_ONE_HUNDREDTH = uint256(10 ** 6);
uint256 private MOMENT_IN_TIME = uint256(1702317618);

// State variables

MockHubV1 public mockHubV1;

CirclesMigration public migration;

function setUp() public {
mockHubV1 = new MockHubV1();

migration = new CirclesMigration(mockHubV1);

vm.warp(MOMENT_IN_TIME);
}

function testConversionMigrationV1ToTimeCircles() public {
// `MOMENT_IN_TIME` is in the third period
assertEq(uint256(3), mockHubV1.periods());

uint256 originalAmountV1 = uint256(7471193061687490000000);
// at the constant `MOMENT_IN_TIME`
uint256 expectedAmountV2 = uint256(1809845 * 10 ** 16);

// this is a crude first test. These tests should be redone more accurately
// possibly even on-chain

// for now require accuracy < 1%
uint256 convertedAmount = migration.convertFromV1ToTimeCircles(originalAmountV1);
uint256 difference = uint256(0);
if (convertedAmount < expectedAmountV2) {
difference = ACCURACY_ONE - (ACCURACY_ONE * convertedAmount) / expectedAmountV2;
} else {
difference = ACCURACY_ONE - (ACCURACY_ONE * expectedAmountV2) / convertedAmount;
}
uint256 relativeDifference = difference / ACCURACY_ONE_HUNDREDTH;
assertEq(0, relativeDifference);
}
}
4 changes: 2 additions & 2 deletions test/graph/MockHub.sol → test/migration/MockHub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ contract MockHubV1 is IHubV1 {
return base;
}
uint256 y = 1;
while(exponent > 1) {
if(exponent % 2 == 0) {
while (exponent > 1) {
if (exponent % 2 == 0) {
base = base * base;
exponent = exponent / 2;
} else {
Expand Down

0 comments on commit ee5d847

Please sign in to comment.