Skip to content

Commit

Permalink
WIP: Charging module
Browse files Browse the repository at this point in the history
  • Loading branch information
LorranSutter committed Jun 25, 2024
1 parent 89194ff commit 308cd02
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 20 deletions.
26 changes: 26 additions & 0 deletions contracts/implementations/Charging/Charging.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.13;

import "../../libraries/ChargingStorage.sol";

import {ADMIN_ROLE} from "../../shared/Roles.sol";

import "@solidstate/contracts/access/access_control/AccessControlInternal.sol";

/**
* @title Charging
* @notice TODO Documentation
*/
contract Charging is AccessControlInternal {
event OperationCostSet(bytes32 operation, uint256 cost);

// TODO Documentation
function setOperationCost(
bytes32 operation,
uint256 cost
) external onlyRole(ADMIN_ROLE) {
ChargingStorage.getStorage().operationCost[operation] = cost;

emit OperationCostSet(operation, cost);
}
}
22 changes: 22 additions & 0 deletions contracts/implementations/Charging/ChargingInternal.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.13;

import "../../libraries/SharedStorage.sol";
import "../../libraries/ChargingStorage.sol";

import "@openzeppelin/contracts/interfaces/IERC20.sol";

/**
* @title ChargingInternal
* @notice TODO Documentation
*/
library ChargingInternal {
// TODO Documentation
function _chargeDcx(address sender, bytes32 operation) internal {
SharedStorage.Storage storage s = SharedStorage.getStorage();

uint256 cost = ChargingStorage.getStorage().operationCost[operation];

IERC20(s.dimoCredit).transferFrom(sender, s.foundation, cost);
}
}
41 changes: 21 additions & 20 deletions contracts/implementations/tableland/DeviceDefinitionTable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ contract DeviceDefinitionTable is AccessControlInternal {
string model,
uint256 year
);
event DeviceDefinitionDeleted(
uint256 indexed tableId,
string id
);
event DeviceDefinitionDeleted(uint256 indexed tableId, string id);

/**
* @notice Creates a new definition table associated with a specific manufacturer
Expand Down Expand Up @@ -294,7 +291,12 @@ contract DeviceDefinitionTable is AccessControlInternal {

emit DeviceDefinitionUpdated(tableId, data.id, data.model, data.year);

_updateDeviceDefinitionData(tablelandTables, tableId, data.id, data.metadata);
_updateDeviceDefinitionData(
tablelandTables,
tableId,
data.id,
data.metadata
);
}

/**
Expand Down Expand Up @@ -487,21 +489,21 @@ contract DeviceDefinitionTable is AccessControlInternal {
string calldata id,
string calldata metadata
) private {

// Set the values to update
string memory setters = string.concat("metadata=", string(abi.encodePacked("'", metadata, "'")));
string memory setters = string.concat(
"metadata=",
string(abi.encodePacked("'", metadata, "'"))
);
// Specify filters for which row to update
string memory filters = string.concat("id=", string(abi.encodePacked("'", id, "'")));
string memory filters = string.concat(
"id=",
string(abi.encodePacked("'", id, "'"))
);

tablelandTables.mutate(
address(this),
tableId,
SQLHelpers.toUpdate(
"",
tableId,
setters,
filters
)
SQLHelpers.toUpdate("", tableId, setters, filters)
);
}

Expand All @@ -518,16 +520,15 @@ contract DeviceDefinitionTable is AccessControlInternal {
string calldata id
) private {
// Specify filters for which row to delete
string memory filters = string.concat("id=", string(abi.encodePacked("'", id, "'")));
string memory filters = string.concat(
"id=",
string(abi.encodePacked("'", id, "'"))
);

tablelandTables.mutate(
address(this),
tableId,
SQLHelpers.toDelete(
"",
tableId,
filters
)
SQLHelpers.toDelete("", tableId, filters)
);
}
}
29 changes: 29 additions & 0 deletions contracts/libraries/ChargingStorage.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.13;

import "../interfaces/IDimo.sol";
import "../interfaces/ILicense.sol";

// TODO Documentation
/**
* @title ChargingStorage
* @notice Storage of the Charging contract
*/
library ChargingStorage {
bytes32 internal constant CHARGING_STORAGE_SLOT =
keccak256("DIMORegistry.charging.storage");

struct Storage {
// TODO Use Enumerable Set ?
mapping(bytes32 => uint256) operationCost;
}

/* solhint-disable no-inline-assembly */
function getStorage() internal pure returns (Storage storage s) {
bytes32 slot = CHARGING_STORAGE_SLOT;
assembly {
s.slot := slot
}
}
/* solhint-enable no-inline-assembly */
}
28 changes: 28 additions & 0 deletions contracts/libraries/SharedStorage.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.13;

// TODO Documentation
/**
* @title SharedStorage
* @notice Storage of shared variables
*/
library SharedStorage {
bytes32 internal constant SHARED_STORAGE_SLOT =
keccak256("DIMORegistry.shared.storage");

struct Storage {
address foundation; // TODO Maybe a better name
address dimoCredit;
address dimoToken;
address manufacturerLicense; // TODO to deprecate AdLicenseValidator
}

/* solhint-disable no-inline-assembly */
function getStorage() internal pure returns (Storage storage s) {
bytes32 slot = SHARED_STORAGE_SLOT;
assembly {
s.slot := slot
}
}
/* solhint-enable no-inline-assembly */
}

0 comments on commit 308cd02

Please sign in to comment.