Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CoinMachine token accounting bug #1065

Merged
merged 3 commits into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions contracts/extensions/CoinMachine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ contract CoinMachine is ColonyExtension, BasicMetaTransaction {

/// @notice Returns the version of the extension
function version() public override pure returns (uint256) {
return 5;
return 6;
}

/// @notice Configures the extension
Expand Down Expand Up @@ -267,8 +267,6 @@ contract CoinMachine is ColonyExtension, BasicMetaTransaction {
// We need to update the price if the active period is not the current one.
if (activePeriod < currentPeriod) {
emaIntake = wmul((WAD - alpha), emaIntake) + wmul(alpha, activeIntake); // wmul(wad, int) => int
activeIntake = 0;
activeSold = 0;

// Handle any additional missed periods
uint256 periodGap = currentPeriod - activePeriod - 1;
Expand All @@ -282,6 +280,9 @@ contract CoinMachine is ColonyExtension, BasicMetaTransaction {
activePrice = wdiv(emaIntake, targetPerPeriod);
}

activeIntake = 0;
activeSold = 0;

emit PeriodUpdated(initialActivePeriod, currentPeriod);
}

Expand Down
2 changes: 1 addition & 1 deletion test-smoke/colony-storage-consistent.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ contract("Contract Storage", (accounts) => {
console.log("miningCycleStateHash:", miningCycleAccount.stateRoot.toString("hex"));
console.log("tokenLockingStateHash:", tokenLockingAccount.stateRoot.toString("hex"));

expect(colonyNetworkAccount.stateRoot.toString("hex")).to.equal("329bb37f05a543fad925845a53366700392f905814799f205929eaaad20e320a");
expect(colonyNetworkAccount.stateRoot.toString("hex")).to.equal("3ae9f034f263a6fc4934d575114cfd729408600547d52414b5371cf106508cb0");
expect(colonyAccount.stateRoot.toString("hex")).to.equal("e77447c223f5bdfee71413e843a505316693368b74c1930ec27d3c1c944450fb");
expect(metaColonyAccount.stateRoot.toString("hex")).to.equal("58f1833f0b94c47c028c91ededb70d6697624ecf98bc2cc7930bf55f40d2d931");
expect(miningCycleAccount.stateRoot.toString("hex")).to.equal("1f3909ac9098d953ec1d197e6d7924384e96209770f445466ea2f0c0c39f4834");
Expand Down
35 changes: 32 additions & 3 deletions test/extensions/coin-machine.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ contract("Coin Machine", (accounts) => {
await forwardTime(periodLength.toNumber(), this);
await coinMachine.updatePeriod();

const emaIntake = WAD.muln(100).mul(WAD.sub(alphaAsWad)).add(maxPerPeriod.mul(alphaAsWad));
const emaIntake = WAD.muln(100).mul(WAD.sub(alphaAsWad)).add(new BN(0).mul(alphaAsWad));
const expectedPrice = emaIntake.div(WAD.muln(100));
currentPrice = await coinMachine.getCurrentPrice();
expect(currentPrice).to.eq.BN(expectedPrice);
Expand Down Expand Up @@ -609,12 +609,40 @@ contract("Coin Machine", (accounts) => {
await forwardTime(periodLength.toNumber(), this);
await coinMachine.updatePeriod();

const emaIntake = WAD.muln(100).mul(WAD.sub(alphaAsWad)).add(maxPerPeriod.mul(alphaAsWad));
const emaIntake = WAD.muln(100).mul(WAD.sub(alphaAsWad)).add(new BN(0).mul(alphaAsWad));
const expectedPrice = emaIntake.div(WAD.muln(100));
currentPrice = await coinMachine.getCurrentPrice();
expect(currentPrice).to.eq.BN(expectedPrice);
});

it("can correctly account for sold tokens between periods when adding a balance", async () => {
await colony.uninstallExtension(COIN_MACHINE, { from: USER0 });
await colony.installExtension(COIN_MACHINE, version, { from: USER0 });
const coinMachineAddress = await colonyNetwork.getExtensionInstallation(COIN_MACHINE, colony.address);
coinMachine = await CoinMachine.at(coinMachineAddress);

await purchaseToken.mint(USER0, WAD.muln(10), { from: USER0 });
await purchaseToken.approve(coinMachine.address, WAD.muln(10), { from: USER0 });

// Iniitalise with a zero balance, then add tokens
await coinMachine.initialise(token.address, purchaseToken.address, 60 * 60, 10, WAD, WAD, WAD, WAD, ADDRESS_ZERO);
await token.mint(coinMachine.address, WAD.muln(200));

const periodLength = await coinMachine.getPeriodLength();

let activeSold;

await coinMachine.buyTokens(WAD, { from: USER0 });
activeSold = await coinMachine.getActiveSold();
expect(activeSold).to.eq.BN(WAD);

await forwardTime(periodLength.toNumber(), this);

await coinMachine.buyTokens(WAD, { from: USER0 });
activeSold = await coinMachine.getActiveSold();
expect(activeSold).to.eq.BN(WAD);
});

it("it monotonically adjusts prices according to demand", async () => {
const periodLength = await coinMachine.getPeriodLength();
const maxPerPeriod = await coinMachine.getMaxPerPeriod();
Expand All @@ -624,8 +652,9 @@ contract("Coin Machine", (accounts) => {
await purchaseToken.mint(USER0, maxPerPeriod.muln(10000), { from: USER0 });
await purchaseToken.approve(coinMachine.address, maxPerPeriod.muln(10000), { from: USER0 });

const numIter = process.env.CIRCLE_ENV === "true" ? 100 : 5;
let previousPrice = maxPerPeriod.muln(10000); // A very large number.
for (let i = 0; i < 100; i += 1) {
for (let i = 0; i < numIter; i += 1) {
// There used to be a check for a 'steady state' price here, but
// that only worked by chance.
currentPrice = await coinMachine.getCurrentPrice();
Expand Down