Skip to content

Commit

Permalink
createApplication
Browse files Browse the repository at this point in the history
  • Loading branch information
cyri113 committed Jul 19, 2024
1 parent 8f77135 commit 35aeb88
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 0 deletions.
42 changes: 42 additions & 0 deletions contracts/ApplicationManager.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.24;

Check warning

Code scanning / Slither

Incorrect versions of Solidity Warning

Version constraint ^0.8.24 contains known severe issues (https://solidity.readthedocs.io/en/latest/bugs.html)
.
It is used by:
- ^0.8.24
- ^0.8.24
- ^0.8.24

import {IApplicationManager} from "./IApplicationManager.sol";

contract ApplicationManager is IApplicationManager {
mapping(uint => Application) private applications_;
uint private nextApplicationId_;

constructor() {}

function nextApplicationId() external view returns (uint) {
return nextApplicationId_;
}

function createApplication(Application memory _application) external {

Check warning

Code scanning / Slither

Conformance to Solidity naming conventions Warning

applications_[nextApplicationId_] = _application;
emit ApplicationCreated(
nextApplicationId_,
applications_[nextApplicationId_]
);
nextApplicationId_++;
}

function updateApplication(
uint _id,
Application memory _application
) external override {}

function deleteApplication(uint _id) external override {}

function getApplication(
uint _id

Check warning

Code scanning / Slither

Conformance to Solidity naming conventions Warning

Parameter ApplicationManager.getApplication(uint256)._id is not in mixedCase
) external view returns (Application memory) {
return applications_[_id];
}

function getApplications(
uint _start,
uint _limit
) external override returns (Application[] memory application) {}
}
33 changes: 33 additions & 0 deletions contracts/IApplicationManager.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.24;

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

event ApplicationCreated(uint id, Application application);
event ApplicationUpdated(uint id, Application application);
event ApplicationDeleted(uint id, Application application);

function nextApplicationId() external view returns (uint);

function createApplication(Application memory _application) external;

function updateApplication(
uint _id,
Application memory _application
) 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 application);
}
89 changes: 89 additions & 0 deletions test/ApplicationManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { loadFixture } from "@nomicfoundation/hardhat-toolbox-viem/network-helpers";
import { expect } from "chai";
import hre from "hardhat";
import { type Address, getAddress, parseUnits } from "viem";

interface Application {
name: string;
account: Address;
}

describe("ApplicationManager", () => {
// We define a fixture to reuse the same setup in every test.
// We use loadFixture to run this setup once, snapshot that state,
// and reset Hardhat Network to that snapshot in every test.
async function deploy() {
// Contracts are deployed using the first signer/account by default
const [owner, otherAccount, app1Account] =
await hre.viem.getWalletClients();

const contract = await hre.viem.deployContract("ApplicationManager");

const publicClient = await hre.viem.getPublicClient();

return {
contract,
owner,
otherAccount,
app1Account,
publicClient,
};
}

describe("Deployment", () => {
it("Should set the nextxApplicationId to 0", async () => {
const { contract } = await loadFixture(deploy);
expect(await contract.read.nextApplicationId()).to.equal(
parseUnits("0", 0),
);
});
});

describe("createApplication", () => {
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
let contract: any;
let app: Application;

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

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

it("Should accept name (string) and account (address) as inputs", async () => {
expect(await contract.write.createApplication([app])).to.be.string;
});
it("Should increment nextApplicationId", async () => {
const t0 = await contract.read.nextApplicationId();
expect(t0).to.equal(parseUnits("0", 0));
await contract.write.createApplication([app]);
const t1 = await contract.read.nextApplicationId();
expect(t1).to.equal(parseUnits("1", 0));
});
it("Should create a new Application", async () => {
const applicationId = await contract.read.nextApplicationId();
await contract.write.createApplication([app]);
expect(await contract.read.getApplication([applicationId])).to.contain(
app,
);
});
it("Should emit the ApplicationCreated event with the new application's details", async () => {
const applicationId = await contract.read.nextApplicationId();
const txHash = await contract.write.createApplication([app]);

const events = await contract.getEvents.ApplicationCreated();

expect(events.length).to.be.equal(1);

const event = events[0];
expect(event.eventName).to.be.equal("ApplicationCreated");
expect(event.transactionHash).to.be.equal(txHash);
expect(event.args.id).to.be.equal(applicationId);
expect(event.args.application).to.contain(app);
});
});
});

0 comments on commit 35aeb88

Please sign in to comment.