Stub of solidity contracts
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 31s
Details
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 31s
Details
This commit is contained in:
parent
acfe81e5e0
commit
abc03096ba
|
@ -0,0 +1,14 @@
|
|||
node_modules
|
||||
.env
|
||||
|
||||
# Hardhat files
|
||||
/cache
|
||||
/artifacts
|
||||
|
||||
# TypeChain files
|
||||
/typechain
|
||||
/typechain-types
|
||||
|
||||
# solidity-coverage files
|
||||
/coverage
|
||||
/coverage.json
|
|
@ -0,0 +1,6 @@
|
|||
// SPDX-License-Identifier: Unlicense
|
||||
pragma solidity ^0.8.24;
|
||||
|
||||
import "./ReputationHolder.sol";
|
||||
|
||||
contract Availability is ReputationHolder {}
|
|
@ -0,0 +1,38 @@
|
|||
// SPDX-License-Identifier: Unlicense
|
||||
pragma solidity ^0.8.24;
|
||||
|
||||
import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";
|
||||
import "./ReputationHolder.sol";
|
||||
|
||||
/// This contract must manage validation pools and reputation,
|
||||
/// because otherwise there's no way to enforce appropriate permissions on
|
||||
/// transfer of value between reputation NFTs.
|
||||
contract DAO is ERC721("Reputation", "REP"), ReputationHolder {
|
||||
mapping(uint256 tokenId => uint256) _tokenValues;
|
||||
uint256 _nextTokenId;
|
||||
|
||||
/// Inspect the value of a given reputation NFT
|
||||
function valueOf(uint256 tokenId) public view returns (uint256 value) {
|
||||
value = _tokenValues[tokenId];
|
||||
}
|
||||
|
||||
/// Internal function to mint a new reputation NFT
|
||||
function mint() internal returns (uint256 tokenId) {
|
||||
// Generate a new (sequential) ID for the token
|
||||
tokenId = _nextTokenId++;
|
||||
// Mint the token, initially to be owned by the current contract.
|
||||
_mint(address(this), tokenId);
|
||||
}
|
||||
|
||||
/// Internal function to transfer value from one reputation token to another
|
||||
function transferValueFrom(
|
||||
uint256 fromTokenId,
|
||||
uint256 toTokenId,
|
||||
uint256 amount
|
||||
) internal {
|
||||
require(amount >= 0);
|
||||
require(valueOf(fromTokenId) >= amount);
|
||||
_tokenValues[fromTokenId] -= amount;
|
||||
_tokenValues[toTokenId] += amount;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
// SPDX-License-Identifier: Unlicense
|
||||
pragma solidity ^0.8.24;
|
||||
|
||||
// Uncomment this line to use console.log
|
||||
// import "hardhat/console.sol";
|
||||
|
||||
contract Lock {
|
||||
uint public unlockTime;
|
||||
address payable public owner;
|
||||
|
||||
event Withdrawal(uint amount, uint when);
|
||||
|
||||
constructor(uint _unlockTime) payable {
|
||||
require(
|
||||
block.timestamp < _unlockTime,
|
||||
"Unlock time should be in the future"
|
||||
);
|
||||
|
||||
unlockTime = _unlockTime;
|
||||
owner = payable(msg.sender);
|
||||
}
|
||||
|
||||
function withdraw() public {
|
||||
// Uncomment this line, and the import of "hardhat/console.sol", to print a log in your terminal
|
||||
// console.log("Unlock time is %o and block timestamp is %o", unlockTime, block.timestamp);
|
||||
|
||||
require(block.timestamp >= unlockTime, "You can't withdraw yet");
|
||||
require(msg.sender == owner, "You aren't the owner");
|
||||
|
||||
emit Withdrawal(address(this).balance, block.timestamp);
|
||||
|
||||
owner.transfer(address(this).balance);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
// SPDX-License-Identifier: Unlicense
|
||||
pragma solidity ^0.8.24;
|
||||
|
||||
import {ERC721Holder} from "@openzeppelin/contracts/token/ERC721/utils/ERC721Holder.sol";
|
||||
|
||||
abstract contract ReputationHolder is ERC721Holder {}
|
|
@ -0,0 +1,110 @@
|
|||
// SPDX-License-Identifier: Unlicense
|
||||
pragma solidity ^0.8.24;
|
||||
|
||||
import {IERC721Receiver} from "@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol";
|
||||
import "./DAO.sol";
|
||||
import "./ReputationHolder.sol";
|
||||
|
||||
struct AvailabilityStake {
|
||||
address worker;
|
||||
uint256 tokenId;
|
||||
uint256 amount;
|
||||
bool assigned;
|
||||
}
|
||||
|
||||
enum WorkStatus {
|
||||
Requested,
|
||||
EvidenceSubmitted,
|
||||
ApprovalSubmitted,
|
||||
Complete
|
||||
}
|
||||
|
||||
struct WorkRequest {
|
||||
address customer;
|
||||
uint256 fee;
|
||||
WorkStatus status;
|
||||
uint stakeIndex;
|
||||
bool approval;
|
||||
}
|
||||
|
||||
contract Work1 is IERC721Receiver {
|
||||
DAO dao;
|
||||
uint public immutable price;
|
||||
mapping(uint => AvailabilityStake) stakes;
|
||||
uint stakeCount;
|
||||
mapping(uint => WorkRequest) requests;
|
||||
uint requestCount;
|
||||
|
||||
event WorkAssigned(address worker, uint requestIndex);
|
||||
|
||||
constructor(DAO dao_, uint price_) {
|
||||
dao = dao_;
|
||||
price = price_;
|
||||
}
|
||||
|
||||
/// Accept availability stakes as reputation token transfer
|
||||
function onERC721Received(
|
||||
address,
|
||||
address from,
|
||||
uint256 tokenId,
|
||||
bytes calldata
|
||||
) external override returns (bytes4) {
|
||||
AvailabilityStake storage stake = stakes[stakeCount++];
|
||||
stake.worker = from;
|
||||
stake.tokenId = tokenId;
|
||||
stake.amount = dao.valueOf(tokenId);
|
||||
return IERC721Receiver.onERC721Received.selector;
|
||||
}
|
||||
|
||||
/// Select a worker randomly from among the available workers, weighted by amount staked
|
||||
function randomWeightedSelection() internal view returns (uint) {
|
||||
uint totalStakes;
|
||||
for (uint i = 0; i < stakeCount; i++) {
|
||||
if (stakes[i].assigned) continue;
|
||||
totalStakes += stakes[i].amount;
|
||||
}
|
||||
uint select = block.prevrandao % totalStakes;
|
||||
uint acc;
|
||||
for (uint i = 0; i < stakeCount; i++) {
|
||||
if (stakes[i].assigned) continue;
|
||||
acc += stakes[i].amount;
|
||||
if (acc >= select) return i;
|
||||
}
|
||||
revert("Failed to select worker");
|
||||
}
|
||||
|
||||
/// Assign a random available worker
|
||||
function assignWork(uint requestIndex) internal returns (uint stakeIndex) {
|
||||
stakeIndex = randomWeightedSelection();
|
||||
AvailabilityStake storage stake = stakes[stakeIndex];
|
||||
stake.assigned = true;
|
||||
emit WorkAssigned(stake.worker, requestIndex);
|
||||
}
|
||||
|
||||
/// Accept work request with fee
|
||||
function requestWork() external payable {
|
||||
require(msg.value >= price, "Insufficient fee");
|
||||
uint requestIndex = requestCount++;
|
||||
WorkRequest storage request = requests[requestIndex];
|
||||
request.customer = msg.sender;
|
||||
request.stakeIndex = assignWork(requestIndex);
|
||||
}
|
||||
|
||||
/// Accept work evidence from worker
|
||||
function submitWorkEvidence(uint requestIndex) external {
|
||||
WorkRequest storage request = requests[requestIndex];
|
||||
require(request.status == WorkStatus.Requested);
|
||||
AvailabilityStake storage stake = stakes[request.stakeIndex];
|
||||
require(stake.worker == msg.sender);
|
||||
request.status = WorkStatus.EvidenceSubmitted;
|
||||
}
|
||||
|
||||
/// Accept work approval/disapproval from customer
|
||||
function submitWorkApproval(uint requestIndex, bool approval) external {
|
||||
WorkRequest storage request = requests[requestIndex];
|
||||
require(request.status == WorkStatus.EvidenceSubmitted);
|
||||
request.status = WorkStatus.ApprovalSubmitted;
|
||||
request.approval = approval;
|
||||
// TODO: Initiate validation pool
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
require("@nomicfoundation/hardhat-toolbox");
|
||||
|
||||
/** @type import('hardhat/config').HardhatUserConfig */
|
||||
module.exports = {
|
||||
solidity: "0.8.24",
|
||||
};
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,20 @@
|
|||
{
|
||||
"name": "dgf-prototype",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"@nomicfoundation/hardhat-toolbox": "^4.0.0",
|
||||
"hardhat": "^2.20.1",
|
||||
"prettier": "^3.2.5",
|
||||
"prettier-plugin-solidity": "^1.3.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"@openzeppelin/contracts": "^5.0.2"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
// We require the Hardhat Runtime Environment explicitly here. This is optional
|
||||
// but useful for running the script in a standalone fashion through `node <script>`.
|
||||
//
|
||||
// You can also run a script with `npx hardhat run <script>`. If you do that, Hardhat
|
||||
// will compile your contracts, add the Hardhat Runtime Environment's members to the
|
||||
// global scope, and execute the script.
|
||||
const hre = require("hardhat");
|
||||
|
||||
async function main() {
|
||||
const currentTimestampInSeconds = Math.round(Date.now() / 1000);
|
||||
const unlockTime = currentTimestampInSeconds + 60;
|
||||
|
||||
const lockedAmount = hre.ethers.parseEther("0.001");
|
||||
|
||||
const lock = await hre.ethers.deployContract("Lock", [unlockTime], {
|
||||
value: lockedAmount,
|
||||
});
|
||||
|
||||
await lock.waitForDeployment();
|
||||
|
||||
console.log(
|
||||
`Lock with ${ethers.formatEther(
|
||||
lockedAmount
|
||||
)}ETH and unlock timestamp ${unlockTime} deployed to ${lock.target}`
|
||||
);
|
||||
}
|
||||
|
||||
// We recommend this pattern to be able to use async/await everywhere
|
||||
// and properly handle errors.
|
||||
main().catch((error) => {
|
||||
console.error(error);
|
||||
process.exitCode = 1;
|
||||
});
|
|
@ -0,0 +1,126 @@
|
|||
const {
|
||||
time,
|
||||
loadFixture,
|
||||
} = require("@nomicfoundation/hardhat-toolbox/network-helpers");
|
||||
const { anyValue } = require("@nomicfoundation/hardhat-chai-matchers/withArgs");
|
||||
const { expect } = require("chai");
|
||||
|
||||
describe("Lock", function () {
|
||||
// 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 deployOneYearLockFixture() {
|
||||
const ONE_YEAR_IN_SECS = 365 * 24 * 60 * 60;
|
||||
const ONE_GWEI = 1_000_000_000;
|
||||
|
||||
const lockedAmount = ONE_GWEI;
|
||||
const unlockTime = (await time.latest()) + ONE_YEAR_IN_SECS;
|
||||
|
||||
// Contracts are deployed using the first signer/account by default
|
||||
const [owner, otherAccount] = await ethers.getSigners();
|
||||
|
||||
const Lock = await ethers.getContractFactory("Lock");
|
||||
const lock = await Lock.deploy(unlockTime, { value: lockedAmount });
|
||||
|
||||
return { lock, unlockTime, lockedAmount, owner, otherAccount };
|
||||
}
|
||||
|
||||
describe("Deployment", function () {
|
||||
it("Should set the right unlockTime", async function () {
|
||||
const { lock, unlockTime } = await loadFixture(deployOneYearLockFixture);
|
||||
|
||||
expect(await lock.unlockTime()).to.equal(unlockTime);
|
||||
});
|
||||
|
||||
it("Should set the right owner", async function () {
|
||||
const { lock, owner } = await loadFixture(deployOneYearLockFixture);
|
||||
|
||||
expect(await lock.owner()).to.equal(owner.address);
|
||||
});
|
||||
|
||||
it("Should receive and store the funds to lock", async function () {
|
||||
const { lock, lockedAmount } = await loadFixture(
|
||||
deployOneYearLockFixture
|
||||
);
|
||||
|
||||
expect(await ethers.provider.getBalance(lock.target)).to.equal(
|
||||
lockedAmount
|
||||
);
|
||||
});
|
||||
|
||||
it("Should fail if the unlockTime is not in the future", async function () {
|
||||
// We don't use the fixture here because we want a different deployment
|
||||
const latestTime = await time.latest();
|
||||
const Lock = await ethers.getContractFactory("Lock");
|
||||
await expect(Lock.deploy(latestTime, { value: 1 })).to.be.revertedWith(
|
||||
"Unlock time should be in the future"
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Withdrawals", function () {
|
||||
describe("Validations", function () {
|
||||
it("Should revert with the right error if called too soon", async function () {
|
||||
const { lock } = await loadFixture(deployOneYearLockFixture);
|
||||
|
||||
await expect(lock.withdraw()).to.be.revertedWith(
|
||||
"You can't withdraw yet"
|
||||
);
|
||||
});
|
||||
|
||||
it("Should revert with the right error if called from another account", async function () {
|
||||
const { lock, unlockTime, otherAccount } = await loadFixture(
|
||||
deployOneYearLockFixture
|
||||
);
|
||||
|
||||
// We can increase the time in Hardhat Network
|
||||
await time.increaseTo(unlockTime);
|
||||
|
||||
// We use lock.connect() to send a transaction from another account
|
||||
await expect(lock.connect(otherAccount).withdraw()).to.be.revertedWith(
|
||||
"You aren't the owner"
|
||||
);
|
||||
});
|
||||
|
||||
it("Shouldn't fail if the unlockTime has arrived and the owner calls it", async function () {
|
||||
const { lock, unlockTime } = await loadFixture(
|
||||
deployOneYearLockFixture
|
||||
);
|
||||
|
||||
// Transactions are sent using the first signer by default
|
||||
await time.increaseTo(unlockTime);
|
||||
|
||||
await expect(lock.withdraw()).not.to.be.reverted;
|
||||
});
|
||||
});
|
||||
|
||||
describe("Events", function () {
|
||||
it("Should emit an event on withdrawals", async function () {
|
||||
const { lock, unlockTime, lockedAmount } = await loadFixture(
|
||||
deployOneYearLockFixture
|
||||
);
|
||||
|
||||
await time.increaseTo(unlockTime);
|
||||
|
||||
await expect(lock.withdraw())
|
||||
.to.emit(lock, "Withdrawal")
|
||||
.withArgs(lockedAmount, anyValue); // We accept any value as `when` arg
|
||||
});
|
||||
});
|
||||
|
||||
describe("Transfers", function () {
|
||||
it("Should transfer the funds to the owner", async function () {
|
||||
const { lock, unlockTime, lockedAmount, owner } = await loadFixture(
|
||||
deployOneYearLockFixture
|
||||
);
|
||||
|
||||
await time.increaseTo(unlockTime);
|
||||
|
||||
await expect(lock.withdraw()).to.changeEtherBalances(
|
||||
[owner, lock],
|
||||
[lockedAmount, -lockedAmount]
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue