Skip to content
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
5 changes: 3 additions & 2 deletions contracts/modules/capital/Ramm.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ contract Ramm is IRamm, MasterAwareV2 {
SPOT_PRICE_B = spotPriceB;
}

function loadState() internal view returns (State memory) {
return State(slot0.nxmReserveA,
function loadState() public view returns (State memory) {
return State(
slot0.nxmReserveA,
slot0.nxmReserveB,
slot1.ethReserve,
slot1.budget,
Expand Down
1 change: 1 addition & 0 deletions test/unit/Ramm/getBookValue.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');

const { setup } = require('./setup');

const { parseEther } = ethers.utils;
Expand Down
1 change: 1 addition & 0 deletions test/unit/Ramm/getReserves.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');

const { setup, SPOT_PRICE_A, SPOT_PRICE_B } = require('./setup');

const { parseEther } = ethers.utils;
Expand Down
1 change: 1 addition & 0 deletions test/unit/Ramm/getSpotPrices.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');

const { getState, setup } = require('./setup');
const { setNextBlockTime, mineNextBlock } = require('../../utils/evm');

Expand Down
25 changes: 25 additions & 0 deletions test/unit/Ramm/loadState.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');

const { setup } = require('./setup');

describe('loadState', function () {
it('should correctly return the current state', async function () {
const fixture = await loadFixture(setup);
const { ramm } = fixture.contracts;

const state = await ramm.loadState();

// Expected state
const { nxmReserveA, nxmReserveB } = await ramm.slot0();
const { ethReserve, budget, updatedAt } = await ramm.slot1();
const ratchetSpeed = await ramm.ratchetSpeed();

expect(state.nxmA).to.be.equal(nxmReserveA);
expect(state.nxmB).to.be.equal(nxmReserveB);
expect(state.eth).to.equal(ethReserve);
expect(state.budget).to.equal(budget);
expect(state.ratchetSpeed).to.equal(ratchetSpeed);
expect(state.timestamp).to.equal(updatedAt);
});
});
24 changes: 24 additions & 0 deletions test/unit/Ramm/removeBudget.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');

const { setup } = require('./setup');
const { setNextBlockBaseFee } = require('../../utils/evm');

describe('removeBudget', function () {
it('should set the budget to 0', async function () {
const fixture = await loadFixture(setup);
const { ramm } = fixture.contracts;
const [governance] = fixture.accounts.governanceContracts;

await setNextBlockBaseFee(0);
const governanceSigner = await ethers.provider.getSigner(governance.address);

const before = await ramm.slot1();
await ramm.connect(governanceSigner).removeBudget(); // onlyGovernance
const after = await ramm.slot1();

expect(before.budget).to.be.not.equal(0);
expect(after.budget).to.be.equal(0);
});
});
40 changes: 40 additions & 0 deletions test/unit/Ramm/storeState.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const { getState, setup } = require('./setup');
const { setNextBlockTime } = require('../../utils/evm');

const { parseEther } = ethers.utils;

describe('storeState', function () {
it('should store state correctly', async function () {
const fixture = await loadFixture(setup);
const { ramm, pool, tokenController } = fixture.contracts;
const [member] = fixture.accounts.members;

const ethIn = parseEther('1');
const minTokensOut = parseEther('28');
const { timestamp } = await ethers.provider.getBlock('latest');
const nextBlockTimestamp = timestamp + 6 * 60 * 60;

const initialState = await getState(ramm);
const capital = await pool.getPoolValueInEth();
const supply = await tokenController.totalSupply();
const before = await ramm._getReserves(initialState, capital, supply, nextBlockTimestamp);

// buy NXM
await setNextBlockTime(nextBlockTimestamp);
const tx = await ramm.connect(member).swap(0, minTokensOut, { value: ethIn });
await tx.wait();
const after = await ramm.loadState();

const k = before.eth.mul(before.nxmA);
const newEth = before.eth.add(ethIn);

// check storeState correctly stored new values
expect(after.nxmA).to.be.equal(k.div(newEth));
expect(after.nxmB).to.be.equal(before.nxmB.mul(newEth).div(before.eth));
expect(after.eth).to.be.equal(newEth);
expect(after.timestamp).to.be.equal(nextBlockTimestamp);
});
});
17 changes: 10 additions & 7 deletions test/unit/Ramm/swap.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');

const { getState, setup } = require('./setup');
const { setNextBlockTime } = require('../../utils/evm');
const { setNextBlockBaseFee, setNextBlockTime } = require('../../utils/evm');

const { parseEther } = ethers.utils;

Expand Down Expand Up @@ -73,9 +74,10 @@ describe('swap', function () {
const nxmBalanceBefore = await nxm.balanceOf(member.address);
const ethBalanceBefore = await ethers.provider.getBalance(member.address);

await setNextBlockBaseFee(0);
await setNextBlockTime(nextBlockTimestamp);
const tx = await ramm.connect(member).swap(nxmIn, parseEther('0.015')); // initial sportPriceB 0.0152
const { gasUsed, effectiveGasPrice } = await tx.wait();
const tx = await ramm.connect(member).swap(nxmIn, parseEther('0.015'), { maxPriorityFeePerGas: 0 }); // 0.0152 spotB
await tx.wait();

// after state
const totalSupplyAfter = await tokenController.totalSupply();
Expand All @@ -92,7 +94,7 @@ describe('swap', function () {

expect(totalSupplyAfter).to.be.equal(totalSupplyBefore.sub(nxmIn));
expect(nxmBalanceAfter).to.be.equal(nxmBalanceBefore.sub(nxmIn));
expect(ethBalanceAfter).to.be.equal(ethBalanceBefore.add(ethOut).sub(gasUsed.mul(effectiveGasPrice)));
expect(ethBalanceAfter).to.be.equal(ethBalanceBefore.add(ethOut));

expect(stateAfter.nxmA).to.be.equal(newNxmA);
expect(stateAfter.nxmB).to.be.equal(newNxmB);
Expand Down Expand Up @@ -120,9 +122,10 @@ describe('swap', function () {
const nxmBalanceBefore = await nxm.balanceOf(member.address);
const ethBalanceBefore = await ethers.provider.getBalance(member.address);

await setNextBlockBaseFee(0);
await setNextBlockTime(nextBlockTimestamp);
const tx = await ramm.connect(member).swap(0, parseEther('31'), { value: ethIn });
const { gasUsed, effectiveGasPrice } = await tx.wait();
const tx = await ramm.connect(member).swap(0, parseEther('31'), { value: ethIn, maxPriorityFeePerGas: 0 });
await tx.wait();

// after state
const totalSupplyAfter = await tokenController.totalSupply();
Expand All @@ -138,7 +141,7 @@ describe('swap', function () {
const nxmOut = state.nxmA.sub(newNxmA);

expect(totalSupplyAfter).to.be.equal(totalSupplyBefore.add(nxmOut));
expect(ethBalanceAfter).to.be.equal(ethBalanceBefore.sub(ethIn).sub(gasUsed.mul(effectiveGasPrice)));
expect(ethBalanceAfter).to.be.equal(ethBalanceBefore.sub(ethIn));
expect(nxmBalanceAfter).to.be.equal(nxmBalanceBefore.add(nxmOut));

expect(stateAfter.nxmA).to.be.equal(newNxmA);
Expand Down