Skip to content

Commit

Permalink
Merge pull request #33 from stc-community/user-contract
Browse files Browse the repository at this point in the history
feat(contract): Add user authorized
  • Loading branch information
Jchicode authored Aug 1, 2023
2 parents 09097b6 + c9b6982 commit 1efcfcb
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 11 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,7 @@ dist
.DS_Store

.idea

contract/build/artifacts

contract/build/cache
16 changes: 16 additions & 0 deletions contract/contracts/Config.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.7.6;
import "./interface/IAccessControl.sol";

abstract contract Config {

address public constant ACCESS_CONTROL = 0xb173A5644a5B743A152dB86A32B7cc1Cf3FBa7Ba;

modifier onlyBeGrant(address account) {
if (msg.sender != account){
require(IAccessControl(ACCESS_CONTROL).hasRole(account, msg.sender), "Not authorized by the account owner");
}
_;
}

}
12 changes: 12 additions & 0 deletions contract/contracts/interface/IAccessControl.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.7.6;

interface IAccessControl {
function hasRole(string memory publicKeyHash, address account) external view returns (bool);

function grantRole(string memory publicKeyHash, address account, uint256 expireTime) external;

function revokeRole(string memory publicKeyHash, address account) external;

function renounceRole(string memory publicKeyHash) external;
}
37 changes: 35 additions & 2 deletions contract/contracts/oracle/Deploy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity ^0.7.6;

import "@chainlink/contracts/src/v0.7/ChainlinkClient.sol";
import "@chainlink/contracts/src/v0.7/ConfirmedOwner.sol";
import "../Config.sol";


/**
Expand All @@ -15,11 +16,16 @@ contract ContainerDeploy is ChainlinkClient, ConfirmedOwner {

uint256 private constant ORACLE_PAYMENT = (1 * LINK_DIVISIBILITY) / 10; // 0.1 * 10**18
string public currentDeployStatus;
string public currentDeleteStatus;

event RequestContainerDeployFulfilled(
bytes32 indexed requestId,
string deployStatus
);
event RequestDeleteDeployFulfilled(
bytes32 indexed requestId,
string deleteStatus
);

/**
* Constructor FVM link token
Expand All @@ -38,7 +44,7 @@ contract ContainerDeploy is ChainlinkClient, ConfirmedOwner {
string memory _jobId,
string memory _deploy_code,
string memory _request_url,
string memory _userId
string memory _public_key
) public {
Chainlink.Request memory req = buildChainlinkRequest(
stringToBytes32(_jobId),
Expand All @@ -47,7 +53,7 @@ contract ContainerDeploy is ChainlinkClient, ConfirmedOwner {
);
req.add("post",_request_url);
req.add("yaml", _deploy_code);
req.add("userid", _userId);
req.add("userid", _public_key);
address from = msg.sender;
req.add("sender",addressToString(from));
sendChainlinkRequestTo(_oracle, req, ORACLE_PAYMENT);
Expand All @@ -61,6 +67,33 @@ contract ContainerDeploy is ChainlinkClient, ConfirmedOwner {
currentDeployStatus = _deploy_status;
}

/**
* Request container cloud api to delete.
*/
function requestDeleteDeploy(
address _oracle,
string memory _jobId,
string memory _request_url,
string memory _public_key
) public {
Chainlink.Request memory req = buildChainlinkRequest(
stringToBytes32(_jobId),
address(this),
this.fulfillDeleteStatus.selector
);
req.add("delete",_request_url);
req.add("userid", _public_key);
sendChainlinkRequestTo(_oracle, req, ORACLE_PAYMENT);
}

function fulfillDeleteStatus(
bytes32 _requestId,
string calldata _deploy_status
) public recordChainlinkFulfillment(_requestId) {
emit RequestDeleteDeployFulfilled(_requestId, _deploy_status);
currentDeleteStatus = _deploy_status;
}

function getChainlinkToken() public view returns (address) {
return chainlinkTokenAddress();
}
Expand Down
17 changes: 10 additions & 7 deletions contract/contracts/oracle/Pod.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ pragma solidity ^0.7.6;

import "@chainlink/contracts/src/v0.7/ChainlinkClient.sol";
import "@chainlink/contracts/src/v0.7/ConfirmedOwner.sol";
import "../Config.sol";


/**
* THIS IS AN EXAMPLE CONTRACT THAT USES UN-AUDITED CODE.
* DO NOT USE THIS CODE IN PRODUCTION.
*/

contract ContainerPod is ChainlinkClient, ConfirmedOwner {
contract ContainerPod is ChainlinkClient, ConfirmedOwner, Config {
using Chainlink for Chainlink.Request;

uint256 private constant ORACLE_PAYMENT = (1 * LINK_DIVISIBILITY) / 10; // 0.1 * 10**18
string public currentPodStatus;
string public currentDeployStatus;
string public currentDeleteStatus;

event RequestContainerPodFulfilled(
Expand Down Expand Up @@ -43,7 +44,7 @@ contract ContainerPod is ChainlinkClient, ConfirmedOwner {
string memory _jobId,
string memory _pod_code,
string memory _request_url,
string memory _userId
string memory _public_key
) public {
Chainlink.Request memory req = buildChainlinkRequest(
stringToBytes32(_jobId),
Expand All @@ -52,7 +53,7 @@ contract ContainerPod is ChainlinkClient, ConfirmedOwner {
);
req.add("post",_request_url);
req.add("yaml", _pod_code);
req.add("userid", _userId);
req.add("userid", _public_key);
address from = msg.sender;
req.add("sender",addressToString(from));
sendChainlinkRequestTo(_oracle, req, ORACLE_PAYMENT);
Expand All @@ -63,23 +64,25 @@ contract ContainerPod is ChainlinkClient, ConfirmedOwner {
string calldata _pod_status
) public recordChainlinkFulfillment(_requestId) {
emit RequestContainerPodFulfilled(_requestId, _pod_status);
currentPodStatus = _pod_status;
currentDeployStatus = _pod_status;
}

/**
* Request msp container cloud api to delete.
* Request container cloud api to delete.
*/
function requestDeletePod(
address _oracle,
string memory _jobId,
string memory _request_url
string memory _request_url,
string memory _public_key
) public {
Chainlink.Request memory req = buildChainlinkRequest(
stringToBytes32(_jobId),
address(this),
this.fulfillDeleteStatus.selector
);
req.add("delete",_request_url);
req.add("userid", _public_key);
sendChainlinkRequestTo(_oracle, req, ORACLE_PAYMENT);
}

Expand Down
4 changes: 2 additions & 2 deletions contract/hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ module.exports = {
},
opgoerli: {
url: process.env.OPGOERLI_RPC_URL,
accounts: [PRIVATE_KEY],
accounts: PRIVATE_KEY !== undefined ? [PRIVATE_KEY] : [],
chainId: 420
}
},
defaultNetwork: "sepolia",
defaultNetwork: "opgoerli",
gasReporter: {
enabled: REPORT_GAS,
currency: "USD",
Expand Down

0 comments on commit 1efcfcb

Please sign in to comment.