Skip to content

Commit

Permalink
Merge pull request #313 from m0-foundation/feature/WEB3-1046-reads-cl…
Browse files Browse the repository at this point in the history
…ockStartingTimestamp-clockPeriod

Feature/web3 1046 reads clock starting timestamp clock period
  • Loading branch information
ernaneluis authored Jul 17, 2024
2 parents 847a947 + 1596da7 commit d8d4185
Show file tree
Hide file tree
Showing 21 changed files with 101 additions and 80 deletions.
17 changes: 11 additions & 6 deletions app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,18 @@ async function onSetup(rpc: string) {
},
});
const registrarContracts = await api.registrar.getValues();
ttg.setContracts(registrarContracts);
const registrarValues = await api.registrar.getValues();
ttg.setRegistrarValues(registrarValues);
api.epoch.setEpoch(
registrarValues.clockStartingTimestamp,
registrarValues.clockPeriod,
);
api.setGovernors({
standardGovernor: registrarContracts.standardGovernor,
zeroGovernor: registrarContracts.zeroGovernor,
emergencyGovernor: registrarContracts.emergencyGovernor,
standardGovernor: registrarValues.standardGovernor,
zeroGovernor: registrarValues.zeroGovernor,
emergencyGovernor: registrarValues.emergencyGovernor,
});
apiStore.setClient(api);
Expand Down Expand Up @@ -99,7 +104,7 @@ onMounted(async () => {
await votes.fetchAllVotes().catch((e) => trackError(e, "fetchAllVotes"));
await ttg
.fetchEpoch(ttg.getValues.clock)
.fetchEpoch(ttg.values.clock!)
.catch((e) => trackError(e, "fetchEpoch"));
watchForExecutedResetProposal();
Expand Down
32 changes: 9 additions & 23 deletions hardhat/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,12 @@ const getTimestampOfEpochEnd = (epoch) => {
async function moveToVotingEpoch() {
const currentTimestamp = await hhHelpers.time.latest();
const currentEpoch = getEpochFromTimestamp(currentTimestamp);
console.log("current block ", await hhHelpers.time.latestBlock());
console.log({ currentTimestamp, currentEpoch });
if (currentEpoch % 2 === 0) {
console.log("current epoch is transfer, must change to voting");
console.log("updating...");
await hhHelpers.time.increaseTo(getTimestampOfEpochEnd(currentEpoch) + 1); // move to voting epoch
const newTimestamp = await hhHelpers.time.latest();
const newEpoch = getEpochFromTimestamp(newTimestamp);
console.log({
newTimestamp,
newEpoch,
epoch: newEpoch % 2 === 0 ? "transfer" : "voting",
});
return { epoch: newEpoch, timestamp: newTimestamp };
} else {
console.log("epoch is voting");
return { epoch: currentEpoch, timestamp: currentTimestamp };
}
// to avoid error from hardhat where Timestamp is lower than the current timestamp then move straight to the next voting epoch
const newTimestamp = currentEpoch % 2 === 0 ? getTimestampOfEpochStart(currentEpoch+1) : getTimestampOfEpochStart(currentEpoch + 2);
console.log({ currentEpoch, currentTimestamp });
await hhHelpers.time.increaseTo(newTimestamp);
const newEpoch = getEpochFromTimestamp(await hhHelpers.time.latest());
console.log({ newEpoch, newTimestamp });
}

/** Sets up the hardhat environment for use with cypress. */
Expand All @@ -111,8 +99,7 @@ export default async function setup(): Promise<
{ hardhat: { mining: hardhatConfig.mining } },
]);

const epoch = await moveToVotingEpoch();
console.log({ ...epoch });
await moveToVotingEpoch();
}

hre.tasks[TASK_NODE_GET_PROVIDER].setAction(() => {
Expand Down Expand Up @@ -167,8 +154,7 @@ export default async function setup(): Promise<

let [server] = await Promise.all([run, listen]);

const epoch = await moveToVotingEpoch();
console.log({ ...epoch });
await moveToVotingEpoch();

return {
url: "http://" + server.address + ":" + PORT,
Expand All @@ -185,7 +171,7 @@ export default async function setup(): Promise<
const currentTimestamp = await hhHelpers.time.latest();
const currentEpoch = getEpochFromTimestamp(currentTimestamp);
const newTimestamp = getTimestampOfEpochStart(currentEpoch + epoch);
console.log({ epoch, currentTimestamp, newTimestamp });
console.log({ epoch, currentEpoch, currentTimestamp, newTimestamp });
await hhHelpers.time.setNextBlockTimestamp(newTimestamp);
await hre.network.provider.send("hardhat_mine", [
"0x" + blocks.toString(16),
Expand Down
2 changes: 1 addition & 1 deletion lib/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class Api {

this.registrar = new Registrar(this.context);

this.epoch = new Epoch(client);
this.epoch = Epoch.instance;
}

setRpc(rpcUrl: string) {
Expand Down
50 changes: 29 additions & 21 deletions lib/api/modules/epoch/epoch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { PublicClient } from "viem";
import { EpochTypes, MEpoch } from "./epoch.types";

/*
Expand All @@ -11,17 +10,28 @@ see files:
*/

export class Epoch {
client: PublicClient;
static #instance: Epoch;

static _STARTING_TIMESTAMP = 1_713_099_600;
static _EPOCH_PERIOD_SECONDS = 1_296_000; //15 days in seconds
clockStartingTimestamp!: number;
clockPeriod!: number;

constructor(client: PublicClient) {
this.client = client;
private constructor() {}

public static get instance(): Epoch {
if (!Epoch.#instance) {
Epoch.#instance = new Epoch();
}

return Epoch.#instance;
}

setEpoch(clockStartingTimestamp: number, clockPeriod: number) {
this.clockStartingTimestamp = Number(clockStartingTimestamp);
this.clockPeriod = Number(clockPeriod);
}

async getCurrentBlockTimestamp(): Promise<bigint> {
return await this.client.getBlock().then((block) => block.timestamp);
getType(epoch: number) {
return epoch % 2 === 0 ? EpochTypes.TRANSFER : EpochTypes.VOTING;
}

getEpochState(currentEpoch: number): MEpoch {
Expand All @@ -31,17 +41,18 @@ export class Epoch {
const currentEpochEndAsTimestamp =
this.getTimestampOfEpochEnd(currentEpoch);

const getType = (epoch: number) =>
epoch % 2 === 0 ? EpochTypes.TRANSFER : EpochTypes.VOTING;

console.log({
currentEpoch,
currentEpochStart: currentEpochStartAsTimestamp,
currentEpochEnd: currentEpochEndAsTimestamp,
type: getType(currentEpoch),
type: this.getType(currentEpoch),
});

return {
values: {
clockPeriod: this.clockPeriod,
clockStartingTimestamp: this.clockStartingTimestamp,
},
current: {
asNumber: currentEpoch,
asTimestamp: currentEpochStartAsTimestamp,
Expand All @@ -51,7 +62,7 @@ export class Epoch {
start: {
timestamp: currentEpochStartAsTimestamp,
},
type: getType(currentEpoch),
type: this.getType(currentEpoch),
},
next: {
asNumber: currentEpoch + 1,
Expand All @@ -62,23 +73,20 @@ export class Epoch {
start: {
timestamp: currentEpochStartAsTimestamp,
},
type: getType(currentEpoch + 1),
type: this.getType(currentEpoch + 1),
},
};
}

static getEpochFromTimestamp(timestamp: number) {
getEpochFromTimestamp(timestamp: number) {
return (
Math.floor(
(timestamp - Epoch._STARTING_TIMESTAMP) / Epoch._EPOCH_PERIOD_SECONDS,
) + 1
Math.floor((timestamp - this.clockStartingTimestamp) / this.clockPeriod) +
1
);
}

getTimestampOfEpochStart(epoch: number) {
return (
(epoch - 1) * Epoch._EPOCH_PERIOD_SECONDS + Epoch._STARTING_TIMESTAMP
);
return (epoch - 1) * this.clockPeriod + this.clockStartingTimestamp;
}

getTimestampOfEpochEnd(epoch: number) {
Expand Down
4 changes: 4 additions & 0 deletions lib/api/modules/epoch/epoch.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export enum EpochTypes {
export type EpochType = keyof typeof EpochTypes;

export interface MEpoch {
values: {
clockPeriod: number;
clockStartingTimestamp: number;
};
current: {
asNumber: number;
asTimestamp: number;
Expand Down
2 changes: 1 addition & 1 deletion lib/api/modules/governor/modules/proposal/proposals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ export class Proposals extends GovernorModule {
blockNumber: BigInt(proposal.blockNumber!),
});

const epoch = Epoch.getEpochFromTimestamp(Number(block.timestamp));
const epoch = Epoch.instance.getEpochFromTimestamp(Number(block.timestamp));

return {
...proposal,
Expand Down
3 changes: 3 additions & 0 deletions lib/api/modules/registrar/registrar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export class Registrar extends ApiModule {
"powerToken",
"zeroToken",
"vault",
"clock",
"clockPeriod",
"clockStartingTimestamp",
]);
}
}
4 changes: 4 additions & 0 deletions lib/api/modules/registrar/registrar.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export interface MRegistrarValues {
powerToken: string;
zeroToken: string;
vault: string;

clock: number;
clockPeriod: number;
clockStartingTimestamp: number;
}

export interface MRegistrarStore extends MRegistrarValues {
Expand Down
2 changes: 1 addition & 1 deletion modules/m-core/bytecode/DistributionVault.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion modules/m-core/bytecode/EmergencyGovernor.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion modules/m-core/bytecode/EmergencyGovernorDeployer.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion modules/m-core/bytecode/PowerBootstrapToken.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ "bytecode": "0x60a060405234801561000f575f80fd5b5060405161043a38038061043a83398101604081905261002e916101df565b8151815180821461006057604051631f4bb7c160e31b8152600481018390526024810182905260440160405180910390fd5b5f805b838110156100d85784818151811061007d5761007d6102a8565b60200260200101515f80888481518110610099576100996102a8565b60200260200101516001600160a01b03166001600160a01b031681526020019081526020015f20819055826100ce91906102bc565b9150600101610063565b506001600160f01b03811061010057604051630f1da21760e41b815260040160405180910390fd5b608052506102e192505050565b634e487b7160e01b5f52604160045260245ffd5b604051601f8201601f191681016001600160401b03811182821017156101495761014961010d565b604052919050565b5f6001600160401b038211156101695761016961010d565b5060051b60200190565b5f82601f830112610182575f80fd5b8151602061019761019283610151565b610121565b8083825260208201915060208460051b8701019350868411156101b8575f80fd5b602086015b848110156101d457805183529183019183016101bd565b509695505050505050565b5f80604083850312156101f0575f80fd5b82516001600160401b0380821115610206575f80fd5b818501915085601f830112610219575f80fd5b8151602061022961019283610151565b82815260059290921b84018101918181019089841115610247575f80fd5b948201945b838610156102795785516001600160a01b038116811461026a575f80fd5b8252948201949082019061024c565b91880151919650909350505080821115610291575f80fd5b5061029e85828601610173565b9150509250929050565b634e487b7160e01b5f52603260045260245ffd5b808201808211156102db57634e487b7160e01b5f52601160045260245ffd5b92915050565b6080516101426102f85f395f609101526101425ff3fe608060405234801561000f575f80fd5b5060043610610034575f3560e01c8063406f84de146100385780635c7e5d5f14610080575b5f80fd5b61006e6100463660046100b3565b5073ffffffffffffffffffffffffffffffffffffffff165f9081526020819052604090205490565b60405190815260200160405180910390f35b61006e61008e3660046100f5565b507f000000000000000000000000000000000000000000000000000000000000000090565b5f80604083850312156100c4575f80fd5b823573ffffffffffffffffffffffffffffffffffffffff811681146100e7575f80fd5b946020939093013593505050565b5f60208284031215610105575f80fd5b503591905056fea26469706673582212207e8ea8530925c46887596dd5aa3537101f6d7860bb5dda1455cdea00c1bb6b0164736f6c63430008170033" }
{ "bytecode": "0x60a060405234801561000f575f80fd5b5060405161044f38038061044f83398101604081905261002e916101f4565b8151815180821461006057604051631f4bb7c160e31b8152600481018390526024810182905260440160405180910390fd5b5f805b838110156100ed575f85828151811061007e5761007e6102bd565b60200260200101519050805f8089858151811061009d5761009d6102bd565b60200260200101516001600160a01b03166001600160a01b031681526020019081526020015f205f8282546100d291906102d1565b909155506100e2905081846102d1565b925050600101610063565b506001600160f01b03811061011557604051630f1da21760e41b815260040160405180910390fd5b608052506102f692505050565b634e487b7160e01b5f52604160045260245ffd5b604051601f8201601f191681016001600160401b038111828210171561015e5761015e610122565b604052919050565b5f6001600160401b0382111561017e5761017e610122565b5060051b60200190565b5f82601f830112610197575f80fd5b815160206101ac6101a783610166565b610136565b8083825260208201915060208460051b8701019350868411156101cd575f80fd5b602086015b848110156101e957805183529183019183016101d2565b509695505050505050565b5f8060408385031215610205575f80fd5b82516001600160401b038082111561021b575f80fd5b818501915085601f83011261022e575f80fd5b8151602061023e6101a783610166565b82815260059290921b8401810191818101908984111561025c575f80fd5b948201945b8386101561028e5785516001600160a01b038116811461027f575f80fd5b82529482019490820190610261565b918801519196509093505050808211156102a6575f80fd5b506102b385828601610188565b9150509250929050565b634e487b7160e01b5f52603260045260245ffd5b808201808211156102f057634e487b7160e01b5f52601160045260245ffd5b92915050565b60805161014261030d5f395f609101526101425ff3fe608060405234801561000f575f80fd5b5060043610610034575f3560e01c8063406f84de146100385780635c7e5d5f14610080575b5f80fd5b61006e6100463660046100b3565b5073ffffffffffffffffffffffffffffffffffffffff165f9081526020819052604090205490565b60405190815260200160405180910390f35b61006e61008e3660046100f5565b507f000000000000000000000000000000000000000000000000000000000000000090565b5f80604083850312156100c4575f80fd5b823573ffffffffffffffffffffffffffffffffffffffff811681146100e7575f80fd5b946020939093013593505050565b5f60208284031215610105575f80fd5b503591905056fea2646970667358221220e6033888422bdd88cc40074d1a858dcd1604e13e11119fac99b62ada75c8a2c564736f6c63430008170033" }
2 changes: 1 addition & 1 deletion modules/m-core/bytecode/PowerToken.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion modules/m-core/bytecode/PowerTokenDeployer.json

Large diffs are not rendered by default.

Loading

0 comments on commit d8d4185

Please sign in to comment.