Skip to content

Commit

Permalink
first commit - add all IOTABOT smart contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
huhn511 committed Feb 5, 2022
0 parents commit 6fe514d
Show file tree
Hide file tree
Showing 68 changed files with 58,412 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ETHERSCAN_API_KEY=ABC123ABC123ABC123ABC123ABC123ABC1
ROPSTEN_URL=https://eth-ropsten.alchemyapi.io/v2/<YOUR ALCHEMY KEY>
PRIVATE_KEY=0xabc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc1
4 changes: 4 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
artifacts
cache
coverage
24 changes: 24 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module.exports = {
env: {
browser: false,
es2021: true,
mocha: true,
node: true,
},
plugins: ["@typescript-eslint"],
extends: [
"standard",
"plugin:prettier/recommended",
"plugin:node/recommended",
],
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaVersion: 12,
},
rules: {
"node/no-unsupported-features/es-syntax": [
"error",
{ ignores: ["modules"] },
],
},
};
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
node_modules
.env
coverage
coverage.json
typechain

#Hardhat files
cache
artifacts
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
hardhat.config.ts
scripts
test
5 changes: 5 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
artifacts
cache
coverage*
gasReporterOutput.json
7 changes: 7 additions & 0 deletions .solhint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "solhint:recommended",
"rules": {
"compiler-version": ["error", "^0.8.0"],
"func-visibility": ["warn", { "ignoreConstructors": true }]
}
}
1 change: 1 addition & 0 deletions .solhintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Advanced Sample Hardhat Project

This project demonstrates an advanced Hardhat use case, integrating other tools commonly used alongside Hardhat in the ecosystem.

The project comes with a sample contract, a test for that contract, a sample script that deploys that contract, and an example of a task implementation, which simply lists the available accounts. It also comes with a variety of other tools, preconfigured to work with the project code.

Try running some of the following tasks:

```shell
npx hardhat accounts
npx hardhat compile
npx hardhat clean
npx hardhat test
npx hardhat node
npx hardhat help
REPORT_GAS=true npx hardhat test
npx hardhat coverage
npx hardhat run scripts/deploy.ts
TS_NODE_FILES=true npx ts-node scripts/deploy.ts
npx eslint '**/*.{js,ts}'
npx eslint '**/*.{js,ts}' --fix
npx prettier '**/*.{json,sol,md}' --check
npx prettier '**/*.{json,sol,md}' --write
npx solhint 'contracts/**/*.sol'
npx solhint 'contracts/**/*.sol' --fix
```

# Etherscan verification

To try out Etherscan verification, you first need to deploy a contract to an Ethereum network that's supported by Etherscan, such as Ropsten.

In this project, copy the .env.example file to a file named .env, and then edit it to fill in the details. Enter your Etherscan API key, your Ropsten node URL (eg from Alchemy), and the private key of the account which will send the deployment transaction. With a valid .env file in place, first deploy your contract:

```shell
hardhat run --network iscp scripts/sample-script.ts
```

Then, copy the deployment address and paste it in to replace `DEPLOYED_CONTRACT_ADDRESS` in this command:

```shell
npx hardhat verify --network ropsten DEPLOYED_CONTRACT_ADDRESS "Hello, Hardhat!"
```

# Performance optimizations

For faster runs of your tests and scripts, consider skipping ts-node's type checking by setting the environment variable `TS_NODE_TRANSPILE_ONLY` to `1` in hardhat's environment. For more details see [the documentation](https://hardhat.org/guides/typescript.html#performance-optimizations).
4 changes: 4 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export const nftmarketaddress = "0xF25eC7d0342baa587A33e402d007412E1AcC8907"
export const nftaddress = "0x97CCF6c3167a23fA147Ed96659E87498cf860983"

28 changes: 28 additions & 0 deletions contracts/AddressUtils.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.4;

/**
* Utility library of inline functions on addresses
*/
library AddressUtils {

/**
* Returns whether the target address is a contract
* @dev This function will return false if invoked during the constructor of a contract,
* as the code is not actually created until after the constructor finishes.
* @param addr address to check
* @return whether the target address is a contract
*/
function isContract(address addr) internal view returns (bool) {
uint256 size;
// XXX Currently there is no better way to check if there is a contract in an address
// than to check the size of the code at that address.
// See https://ethereum.stackexchange.com/a/14016/36603
// for more details about how this works.
// TODO Check this again before the Serenity release, because all addresses will be
// contracts then.
assembly { size := extcodesize(addr) } // solium-disable-line security/no-inline-assembly
return size > 0;
}

}
172 changes: 172 additions & 0 deletions contracts/Auction.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.4;

// import "./AddressUtils.sol";

library AddressUtils {

/**
* Returns whether the target address is a contract
* @dev This function will return false if invoked during the constructor of a contract,
* as the code is not actually created until after the constructor finishes.
* @param addr address to check
* @return whether the target address is a contract
*/
function isContract(address addr) internal view returns (bool) {
uint256 size;
// XXX Currently there is no better way to check if there is a contract in an address
// than to check the size of the code at that address.
// See https://ethereum.stackexchange.com/a/14016/36603
// for more details about how this works.
// TODO Check this again before the Serenity release, because all addresses will be
// contracts then.
assembly { size := extcodesize(addr) } // solium-disable-line security/no-inline-assembly
return size > 0;
}

}

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";

import "@openzeppelin/contracts/utils/math/SafeMath.sol";

contract Auction {
using SafeMath for uint256;
using AddressUtils for address;

event AuctionCreated(uint256 _index, address _creator, address _asset, address _token);
event AuctionBid(uint256 _index, address _bidder, uint256 amount);
event Claim(uint256 auctionIndex, address claimer);

enum Status { pending, active, finished }
struct Auction {
address assetAddress;
uint256 assetId;
address tokenAddress;

address creator;

uint256 startTime;
uint256 duration;
uint256 currentBidAmount;
address currentBidOwner;
uint256 bidCount;
}
Auction[] private auctions;

function createAuction(address _assetAddress,
uint256 _assetId,
address _tokenAddress,
uint256 _startPrice,
uint256 _startTime,
uint256 _duration) public returns (uint256) {

require(_assetAddress.isContract());
ERC721 asset = ERC721(_assetAddress);
require(asset.ownerOf(_assetId) == msg.sender);
require(asset.getApproved(_assetId) == address(this));

require(_tokenAddress.isContract());

if (_startTime == 0) { _startTime = block.timestamp; }

Auction memory auction = Auction({
creator: msg.sender,
assetAddress: _assetAddress,
assetId: _assetId,
tokenAddress: _tokenAddress,
startTime: _startTime,
duration: _duration,
currentBidAmount: _startPrice,
currentBidOwner: address(0),
bidCount: 0
});
auctions.push(auction);
uint256 index = auctions.length - 1;

emit AuctionCreated(index, auction.creator, auction.assetAddress, auction.tokenAddress);

return index;
}

function bid(uint256 auctionIndex, uint256 amount) public returns (bool) {
Auction storage auction = auctions[auctionIndex];
require(auction.creator != address(0));
require(isActive(auctionIndex));

if (amount > auction.currentBidAmount) {
// we got a better bid. Return tokens to the previous best bidder
// and register the sender as `currentBidOwner`
ERC20 token = ERC20(auction.tokenAddress);
require(token.transferFrom(msg.sender, address(this), amount));
if (auction.currentBidAmount != 0) {
// return funds to the previuos bidder
token.transfer(
auction.currentBidOwner,
auction.currentBidAmount
);
}
// register new bidder
auction.currentBidAmount = amount;
auction.currentBidOwner = msg.sender;
auction.bidCount = auction.bidCount.add(1);

emit AuctionBid(auctionIndex, msg.sender, amount);
return true;
}
return false;
}

function getTotalAuctions() public view returns (uint256) { return auctions.length; }

function isActive(uint256 index) public view returns (bool) { return getStatus(index) == Status.active; }

function isFinished(uint256 index) public view returns (bool) { return getStatus(index) == Status.finished; }

function getStatus(uint256 index) public view returns (Status) {
Auction storage auction = auctions[index];
if (block.timestamp < auction.startTime) {
return Status.pending;
} else if (block.timestamp < auction.startTime.add(auction.duration)) {
return Status.active;
} else {
return Status.finished;
}
}

function getCurrentBidOwner(uint256 auctionIndex) public view returns (address) { return auctions[auctionIndex].currentBidOwner; }

function getCurrentBidAmount(uint256 auctionIndex) public view returns (uint256) { return auctions[auctionIndex].currentBidAmount; }

function getBidCount(uint256 auctionIndex) public view returns (uint256) { return auctions[auctionIndex].bidCount; }

function getWinner(uint256 auctionIndex) public view returns (address) {
require(isFinished(auctionIndex));
return auctions[auctionIndex].currentBidOwner;
}

function claimTokens(uint256 auctionIndex) public {
require(isFinished(auctionIndex));
Auction storage auction = auctions[auctionIndex];

require(auction.creator == msg.sender);
ERC20 token = ERC20(auction.tokenAddress);
require(token.transfer(auction.creator, auction.currentBidAmount));

emit Claim(auctionIndex, auction.creator);
}

function claimAsset(uint256 auctionIndex) public {
require(isFinished(auctionIndex));
Auction storage auction = auctions[auctionIndex];

address winner = getWinner(auctionIndex);
require(winner == msg.sender);

ERC721 asset = ERC721(auction.assetAddress);
asset.transferFrom(auction.creator, winner, auction.assetId);

emit Claim(auctionIndex, winner);
}
}
Loading

0 comments on commit 6fe514d

Please sign in to comment.