Skip to content

Commit

Permalink
Applications now include id
Browse files Browse the repository at this point in the history
  • Loading branch information
cyri113 committed Aug 21, 2024
1 parent 372c2ab commit 49232d1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
31 changes: 19 additions & 12 deletions contracts/ApplicationManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,39 @@ contract ApplicationManager is
}

function createApplication(
Application memory newApplication
ApplicationDto memory dto
) external nonReentrant restricted {
require(
!addressUsed[newApplication.account],
!addressUsed[dto.account],
"Address already used for another application"
);
applications[nextApplicationId] = newApplication;
addressUsed[newApplication.account] = true;
emit ApplicationCreated(
nextApplicationId,
applications[nextApplicationId]
);
uint id = nextApplicationId;
nextApplicationId++;
Application memory newApplication = Application({
id: id,
name: dto.name,
account: dto.account
});
applications[id] = newApplication;
addressUsed[newApplication.account] = true;
emit ApplicationCreated(id, applications[id]);
}

Check notice

Code scanning / Slither

Reentrancy vulnerabilities Low


function updateApplication(
uint id,
Application memory updatedApplication
ApplicationDto memory dto
) external nonReentrant restricted {
require(applicationExists(id), "Application does not exist");
require(
!addressUsed[updatedApplication.account] ||
applications[id].account == updatedApplication.account,
!addressUsed[dto.account] ||
applications[id].account == dto.account,
"Account used by another application"
);
applications[id] = updatedApplication;
applications[id] = Application({
id: id,
name: dto.name,
account: dto.account
});
emit ApplicationUpdated(id, applications[id]);
}

Expand Down
20 changes: 17 additions & 3 deletions contracts/IApplicationManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
pragma solidity 0.8.26;

interface IApplicationManager {
struct ApplicationDto {
string name;
address account;
}

struct Application {
uint id;
string name;
address account;
}
Expand All @@ -12,9 +18,17 @@ interface IApplicationManager {
event ApplicationDeleted(uint id, Application application);

function getNextApplicationId() external view returns (uint);
function createApplication(Application memory application) external;
function updateApplication(uint id, Application memory application) external;

function createApplication(ApplicationDto memory dto) external;

function updateApplication(uint id, ApplicationDto memory dto) external;

function deleteApplication(uint id) external;

function getApplication(uint id) external view returns (Application memory);
function getApplications(uint start, uint limit) external returns (Application[] memory);

function getApplications(
uint start,
uint limit
) external returns (Application[] memory);
}
6 changes: 4 additions & 2 deletions test/ApplicationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { generatePrivateKey, privateKeyToAccount } from "viem/accounts";

interface Application {
id?: bigint;
name: string;
account: Address;
}
Expand Down Expand Up @@ -139,7 +140,7 @@ describe("ApplicationManager", () => {
});
expect(
await contract.read.getApplication([applicationId]),
).to.contain(app);
).to.contain({ ...app, id: applicationId });
});
it("Should emit the ApplicationCreated event with the new application's details", async () => {
const applicationId = await contract.read.getNextApplicationId();
Expand Down Expand Up @@ -191,6 +192,7 @@ describe("ApplicationManager", () => {
expect(
await contract.read.getApplication([applicationId]),
).to.contain({
id: applicationId,
account: app.account,
name: newName,
});
Expand Down Expand Up @@ -345,7 +347,7 @@ describe("ApplicationManager", () => {
account: generateRandomAddress(),
};
await contract.write.createApplication([app]);
apps.push(app);
apps.push({ ...app, id: parseUnits(`${i}`, 0) });
}
});

Expand Down

0 comments on commit 49232d1

Please sign in to comment.