Skip to content

Commit

Permalink
make assetLimit a state variable
Browse files Browse the repository at this point in the history
  • Loading branch information
rbajollari committed Feb 2, 2024
1 parent 54093b7 commit 7af1ac8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
18 changes: 13 additions & 5 deletions contracts/Ojo.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ contract Ojo is IOjo, AxelarExecutable, Upgradable {

uint256 public resolveWindow;

uint16 public assetLimit;

mapping(bytes32 => OjoTypes.PriceData) public priceData;

error AlreadyInitialized();
Expand All @@ -32,7 +34,7 @@ contract Ojo is IOjo, AxelarExecutable, Upgradable {
bytes4 commandSelector,
bytes calldata commandParams
) external payable {
require(assetNames.length <= 5, "Cannot relay more than 5 assets at a time");
require(assetNames.length <= assetLimit, "Number of assets requested is over limit");

bytes memory payloadWithVersion = abi.encodePacked(
bytes4(uint32(0)), // version number
Expand All @@ -58,8 +60,8 @@ contract Ojo is IOjo, AxelarExecutable, Upgradable {
string memory symbol,
uint256 amount
) external payable {
require(assetNames.length <= 5, "Cannot relay more than 5 assets at a time");
require(assetNames.length <= assetLimit, "Number of assets requested is over limit");

address tokenAddress = gateway.tokenAddresses(symbol);
IERC20(tokenAddress).transferFrom(msg.sender, address(this), amount);
IERC20(tokenAddress).approve(address(gateway), amount);
Expand All @@ -83,16 +85,18 @@ contract Ojo is IOjo, AxelarExecutable, Upgradable {
}

function _setup(bytes calldata data) internal override {
(string memory ojoChain_, string memory ojoAddress_, uint256 resolveWindow_) = abi.decode(
(string memory ojoChain_, string memory ojoAddress_, uint256 resolveWindow_, uint16 assetLimit_) = abi.decode(
data,
(string, string, uint256)
(string, string, uint256, uint16)
);
if (bytes(ojoChain).length != 0) revert AlreadyInitialized();
if (bytes(ojoAddress).length != 0) revert AlreadyInitialized();
if (resolveWindow != 0) revert AlreadyInitialized();
if (assetLimit != 0) revert AlreadyInitialized();
ojoChain = ojoChain_;
ojoAddress = ojoAddress_;
resolveWindow = resolveWindow_;
assetLimit = assetLimit_;
}

function _execute(
Expand Down Expand Up @@ -203,6 +207,10 @@ contract Ojo is IOjo, AxelarExecutable, Upgradable {
resolveWindow = resolveWindow_;
}

function updateAssetLimit(uint8 assetLimit_) external onlyOwner {
assetLimit = assetLimit_;
}

function contractId() external pure returns (bytes32) {
return keccak256('ojo-v1');
}
Expand Down
15 changes: 11 additions & 4 deletions test/Ojo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ describe("Deploy OjoContract", function() {
const ojoChain = "ojoChain";
const ojoAddress = "ojoAddress";
const resolveWindow = 100;
const initParams = ethers.AbiCoder.defaultAbiCoder().encode(["string", "string", "uint256"],[ojoChain, ojoAddress, resolveWindow]);
const assetLimit = 5;
const initParams = ethers.AbiCoder.defaultAbiCoder().encode(["string", "string", "uint256", "uint16"],[ojoChain, ojoAddress, resolveWindow, assetLimit]);

const OjoProxy = await ethers.getContractFactory("OjoProxy");
const ojoProxy = await OjoProxy.deploy();
Expand Down Expand Up @@ -42,7 +43,10 @@ describe("Deploy OjoContract", function() {
await ojo.connect(deployer).updateResolveWindow(150);
expect(await ojo.resolveWindow()).eq(150);

// other account cannot update ojoChain, ojoAddress, and resolveWindow
await ojo.connect(deployer).updateAssetLimit(10);
expect(await ojo.assetLimit()).eq(10);

// other account cannot update ojoChain, ojoAddress, resolveWindow, and assetLimit
await expect(ojo.connect(otherAccount).updateOjoChain("ojoChain3")).to.be.revertedWithCustomError(ojo, "NotOwner");
expect(await ojo.ojoChain()).eq("ojoChain2");

Expand All @@ -51,6 +55,9 @@ describe("Deploy OjoContract", function() {

await expect(ojo.connect(otherAccount).updateResolveWindow(200)).to.be.revertedWithCustomError(ojo, "NotOwner");
expect(await ojo.resolveWindow()).eq(150);

await expect(ojo.connect(otherAccount).updateAssetLimit(20)).to.be.revertedWithCustomError(ojo, "NotOwner");
expect(await ojo.assetLimit()).eq(10);
})

it("reverts when trying to relay more than 5 assets at once", async function() {
Expand All @@ -65,7 +72,7 @@ describe("Deploy OjoContract", function() {
'0x00000000',
'0x',
{value: ethers.parseEther("0")}
)).to.be.revertedWith("Cannot relay more than 5 assets at a time");
)).to.be.revertedWith("Number of assets requested is over limit");

await expect(ojo.connect(deployer).callContractMethodWithOjoPriceDataAndToken(
assetNames,
Expand All @@ -75,6 +82,6 @@ describe("Deploy OjoContract", function() {
'',
0,
{value: ethers.parseEther("0")}
)).to.be.revertedWith("Cannot relay more than 5 assets at a time");
)).to.be.revertedWith("Number of assets requested is over limit");
})
})

0 comments on commit 7af1ac8

Please sign in to comment.