Skip to content

Commit

Permalink
feat: complete the updateApplication function
Browse files Browse the repository at this point in the history
  • Loading branch information
Behzad-rabiei committed Jul 19, 2024
1 parent c9f11f6 commit adffcb6
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 11 deletions.
29 changes: 23 additions & 6 deletions contracts/ApplicationManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,44 @@ import {IApplicationManager} from "./IApplicationManager.sol";

contract ApplicationManager is IApplicationManager {
mapping(uint => Application) private applications;
mapping(address => bool) private addressUsed;
uint private nextApplicationId;

constructor() {}

function getNextApplicationId() external view returns (uint) {
function applicationExists(uint id) internal view returns (bool) {
return applications[id].account != address(0);
}
function getNextApplicationId() external view override returns (uint) {
return nextApplicationId;
}

function createApplication(Application memory application) external {
function createApplication(Application memory application) external override {
require(!addressUsed[application.account], "Address already used for another application");
applications[nextApplicationId] = application;
addressUsed[application.account] = true;
emit ApplicationCreated(nextApplicationId, applications[nextApplicationId]);
nextApplicationId++;
}

function updateApplication(uint id, Application memory application) external override {}
function updateApplication(uint id, Application memory application) external override {
require(applicationExists(id), "Application does not exist");
require(!addressUsed[application.account] || applications[id].account == application.account,
"Address already used for another application");
applications[id] = application;
emit ApplicationUpdated(id, applications[id]);
}

function deleteApplication(uint id) external override {}
function deleteApplication(uint id) external override {
}

function getApplication(uint id) external view returns (Application memory) {
function getApplication(uint id) external view override returns (Application memory) {
require(applicationExists(id), "Application does not exist");
return applications[id];
}

function getApplications(uint start, uint limit) external override returns (Application[] memory application) {}
function getApplications(uint start, uint limit) external view override returns (Application[] memory) {
}


}
73 changes: 68 additions & 5 deletions test/ApplicationManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { loadFixture } from "@nomicfoundation/hardhat-toolbox-viem/network-helpers";
import { expect } from "chai";
import hre from "hardhat";
import { type Address, getAddress, parseUnits } from "viem";
import { type Address, getAddress, parseUnits, } from "viem";
import { generatePrivateKey,privateKeyToAccount } from 'viem/accounts'

const privateKey = generatePrivateKey()


interface Application {
name: string;
Expand Down Expand Up @@ -43,18 +47,17 @@ describe("ApplicationManager", () => {
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
let contract: any;
let app: Application;

let account= privateKeyToAccount(generatePrivateKey()).address
beforeEach(async () => {
const fixture = await loadFixture(deploy);
contract = fixture.contract;

app = {
name: "app",
account: getAddress(fixture.app1Account.account.address),
account,
};
});

it("Should accept name (string) and account (address) as inputs", async () => {
it("Should accept Application as inputs", async () => {
expect(await contract.write.createApplication([app])).to.be.string;
});
it("Should increment nextApplicationId", async () => {
Expand Down Expand Up @@ -85,5 +88,65 @@ describe("ApplicationManager", () => {
expect(event.args.id).to.be.equal(applicationId);
expect(event.args.application).to.contain(app);
});

it("Should revert if the account address is already used", async () => {
await contract.write.createApplication([app]);
await expect(contract.write.createApplication([app])).to.be.rejectedWith(
"Address already used for another application"
);
});
});

describe("updateApplication", () => {
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
let contract: any;
let app: Application;
let applicationId: string;
let account= privateKeyToAccount(generatePrivateKey()).address

beforeEach(async () => {
const fixture = await loadFixture(deploy);
contract = fixture.contract;

app = {
name: "app",
account,
};

applicationId = await contract.read.getNextApplicationId();
await contract.write.createApplication([app]);
});

it("Should accept Application as inputs", async () => {
expect(await contract.write.updateApplication([applicationId, app])).to.be
.string;
});
it("Should update the Application struct in the applications mapping with the provided applicationId to have the application data", async () => {
await contract.write.updateApplication([
applicationId,
{ account: app.account, name: "new app name" },
]);
expect(await contract.read.getApplication([applicationId])).to.contain({
account: app.account,
name: "new app name",
});
});
it("Should revert if the account address is already used", async () => {
const t1 = await contract.read.getNextApplicationId();
expect(t1).to.equal(parseUnits("1", 0));
await contract.write.createApplication([{
account: privateKeyToAccount(generatePrivateKey()).address,
name: "new app"
}]);

await expect(contract.write.updateApplication([t1, app])).to.be.rejectedWith(
"Address already used for another application"
);
});
it("Should revert if the application does not exist", async () => {
await expect(contract.write.updateApplication(["1", app])).to.be.rejectedWith(
"Application does not exist"
);
});
});
});

0 comments on commit adffcb6

Please sign in to comment.