Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

application manager #4

Merged
merged 12 commits into from
Jul 20, 2024
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"[typescript]": {
"editor.defaultFormatter": "biomejs.biome"
}
},
"solidity.compileUsingRemoteVersion": "v0.8.26+commit.8a97fa7a"
}
Binary file modified .yarn/install-state.gz
Binary file not shown.
64 changes: 64 additions & 0 deletions contracts/ApplicationManager.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.26;

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

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

constructor() {}

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

function createApplication(Application memory application) external {
require(!addressUsed[application.account], "Address already used for another application");
applications[nextApplicationId] = application;
addressUsed[application.account] = true;
emit ApplicationCreated(nextApplicationId, applications[nextApplicationId]);
nextApplicationId++;
}
Behzad-rabiei marked this conversation as resolved.
Show resolved Hide resolved

function updateApplication(uint id, Application memory application) external {
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]);
}
Behzad-rabiei marked this conversation as resolved.
Show resolved Hide resolved

function deleteApplication(uint id) external {
require(applicationExists(id), "Application does not exist");
addressUsed[applications[id].account] = false;
emit ApplicationDeleted(id, applications[id]);
delete applications[id];
}

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

function getApplications(uint start, uint limit) external view returns (Application[] memory) {
Application[] memory result = new Application[](limit);
uint count = 0;
uint index = start;
while (count < limit && index < nextApplicationId) {
if (applicationExists(index)) {
result[count] = applications[index];
count++;
}
index++;
}

assembly { mstore(result, count) }

return result;
}

Check warning

Code scanning / Slither

Assembly usage Warning

Behzad-rabiei marked this conversation as resolved.
Show resolved Hide resolved
}
20 changes: 20 additions & 0 deletions contracts/IApplicationManager.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.26;

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 getNextApplicationId() 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);
}
34 changes: 0 additions & 34 deletions contracts/Lock.sol

This file was deleted.

2 changes: 1 addition & 1 deletion hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import "@nomicfoundation/hardhat-toolbox-viem";
import "@nomiclabs/hardhat-solhint";

const config: HardhatUserConfig = {
solidity: "0.8.24",
solidity: "0.8.26",
};

export default config;
Loading
Loading