Skip to content

Commit

Permalink
debug
Browse files Browse the repository at this point in the history
  • Loading branch information
trajan0x committed Apr 5, 2022
1 parent 496db84 commit 96dec40
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
10 changes: 10 additions & 0 deletions contracts/bridge/RateLimiter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "@openzeppelin/contracts-4.3.1-upgradeable/proxy/utils/Initializable.sol"
import "@openzeppelin/contracts-4.3.1-upgradeable/access/AccessControlUpgradeable.sol";
import "@openzeppelin/contracts-4.3.1-upgradeable/security/ReentrancyGuardUpgradeable.sol";
import "./interfaces/IRateLimiter.sol";
import "hardhat/console.sol";

// @title RateLimiter
// @dev a bridge asset rate limiter based on https://github.com/gnosis/safe-modules/blob/master/allowances/contracts/AlowanceModule.sol
Expand Down Expand Up @@ -107,6 +108,7 @@ contract RateLimiter is
returns (Allowance memory allowance)
{
allowance = allowances[token];

// solium-disable-next-line security/no-block-members
uint32 currentMin = uint32(block.timestamp / 60);
// Check if we should reset the time. We do this on load to minimize storage read/ writes
Expand Down Expand Up @@ -135,7 +137,10 @@ contract RateLimiter is
onlyRole(BRIDGE_ROLE)
returns (bool)
{
console.log("what");
console.log(token);
Allowance memory allowance = getAllowance(token);
console.log(allowance.initialized);

// Update state
// @dev reverts if amount > (2^96 - 1)
Expand All @@ -149,11 +154,16 @@ contract RateLimiter is

// do not proceed. Store the transaction for later
if (newSpent >= allowance.amount) {
console.log(newSpent);
console.log(amount);
console.log(allowance.amount);
console.log("returning false");
return false;
}

allowance.spent = newSpent;
updateAllowance(token, allowance);
console.log("returning true");

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ let config: HardhatUserConfig = {
target: "ethers-v5",
},
dodoc: {
runOnCompile: true,
runOnCompile: false,
debugMode: false,
// pre solidity 5 breaks docgen
exclude: ["MultisigWallet", "WETH9"]
Expand Down
31 changes: 29 additions & 2 deletions test/bridge/RateLimiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { deployments, ethers } from "hardhat"
import { BigNumber, BigNumberish, Signer } from "ethers"
import Wallet from "ethereumjs-wallet"

import { RateLimiter } from "../../build/typechain/RateLimiter"
import { CHAIN_ID } from "../../utils/network"
import { Address } from "hardhat-deploy/dist/types"
import { faker } from "@faker-js/faker"
Expand All @@ -13,6 +12,7 @@ import {
BridgeConfigV3,
GenericERC20,
RateLimiterTest,
RateLimiter
} from "../../build/typechain"
import epochSeconds from "@stdlib/time-now"

Expand Down Expand Up @@ -100,7 +100,7 @@ describe("Rate Limiter", () => {
expect(lastResetMin).to.be.eq(lastReset)
})

it("should update allowance", async () => {
it.only("should update allowance", async () => {
const allowance = 100 * Math.pow(10, 6) // allowance of $100
const lastReset = Math.floor(epochSeconds() / hour)

Expand Down Expand Up @@ -150,5 +150,32 @@ describe("Rate Limiter", () => {
expect(resetTimeMin).to.be.eq(60)
expect(lastResetMin).to.be.eq(lastReset)
})

it("should reset allowance", async () => {
const allowance = 1000 * Math.pow(10, 6) // allowance of $100
const lastReset = Math.floor(epochSeconds() / hour)

// reset every hour after current epoch time to an allowance of $100
expect(rateLimiter.setAllowance(USDC.address, allowance, hour, lastReset))
.to.be.not.reverted

await expect(
rateLimiterTest.storeCheckAndUpdateAllowance(USDC.address, 1),
).to.be.not.reverted

// make sure method returned false
console.log(await rateLimiterTest.getLastUpdateValue())
expect(await rateLimiterTest.getLastUpdateValue()).to.be.true

await expect(rateLimiter.resetAllowance(USDC.address)).to.be.not.reverted

let [amount, spent, resetTimeMin, lastResetMin] =
await rateLimiter.getTokenAllowance(USDC.address)

expect(allowance).to.be.eq(amount)
expect(spent).to.be.eq(0)
expect(resetTimeMin).to.be.eq(60)
expect(lastResetMin).to.be.eq(lastReset)
})
})
})

0 comments on commit 96dec40

Please sign in to comment.