@dydxfoundation/governance
Version:
dYdX governance smart contracts
73 lines (72 loc) • 2.59 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.StoredBalance = void 0;
const ethers_1 = require("ethers");
const config_1 = __importDefault(require("../../src/config"));
// Verbose logging for debugging.
const LOG_BALANCE_UPDATES = config_1.default.STAKING_TESTS_LOG_BALANCE_UPDATES;
class StoredBalance {
constructor(contract, label) {
this.contract = contract;
this.label = label;
this.cachedEpoch = 0;
this.current = ethers_1.BigNumber.from(0);
this.next = ethers_1.BigNumber.from(0);
}
async getCurrent() {
await this.load();
return this.current;
}
async getNext() {
await this.load();
return this.next;
}
async increaseCurrentAndNext(amount) {
await this.load();
this.log(`${this.label}: (${this.current}, ${this.next}) -> (${this.current.add(amount)}, ${this.next.add(amount)}})`);
this.current = this.current.add(amount);
this.next = this.next.add(amount);
}
async decreaseCurrentAndNext(amount) {
await this.load();
this.log(`${this.label}: (${this.current}, ${this.next}) -> (${this.current.sub(amount)}, ${this.next.sub(amount)}})`);
this.current = this.current.sub(amount);
this.next = this.next.sub(amount);
}
async increaseNext(amount) {
await this.load();
this.log(`${this.label}: (${this.current}, ${this.next}) -> (${this.current}, ${this.next.add(amount)}})`);
this.next = this.next.add(amount);
}
async decreaseNext(amount) {
await this.load();
this.log(`${this.label}: (${this.current}, ${this.next}) -> (${this.current}, ${this.next.sub(amount)}})`);
this.next = this.next.sub(amount);
}
forceRollover() {
this.current = this.next;
}
clone() {
const balance = new StoredBalance(this.contract, this.label);
balance.cachedEpoch = this.cachedEpoch;
balance.current = this.current;
balance.next = this.next;
return balance;
}
log(message) {
if (LOG_BALANCE_UPDATES) {
console.log(message);
}
}
async load() {
const epoch = await this.contract.getCurrentEpoch();
if (!epoch.eq(this.cachedEpoch)) {
this.current = this.next;
this.cachedEpoch = epoch.toNumber();
}
}
}
exports.StoredBalance = StoredBalance;