Skip to content

Commit

Permalink
fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
akihokurino committed Jan 6, 2023
1 parent f7c7957 commit 0fdf719
Show file tree
Hide file tree
Showing 18 changed files with 1,042 additions and 151 deletions.
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ ROOT := $(realpath $(dir $(lastword $(MAKEFILE_LIST))))
NAME := "RustToken Sample"
DESCRIPTION := "RustToken Sample Generate"
IMAGE_FILENAME := "sample.png"
IMAGE_URL := "https://placehold.jp/3d4070/ffffff/500x500.png?text=Reveal"
AMOUNT := "1"
NETWORK := "Ethereum"
SCHEMA := "ERC721"
Expand All @@ -30,6 +31,13 @@ send-eth: build
--to-address $(TO_ADDRESS) \
--network $(NETWORK)

make-metadata: build
./target/debug/rust-ethereum \
--command make-metadata \
--name $(NAME) \
--description $(DESCRIPTION) \
--image-url $(IMAGE_URL)

mint: build
./target/debug/rust-ethereum \
--command mint \
Expand Down Expand Up @@ -103,4 +111,5 @@ extract-abi:
cat ethereum/artifacts/contracts/RustToken1155.sol/RustToken1155.json | jq '.abi' > src/ethereum/ethers_rs/rust-token1155.abi.json
cat ethereum/artifacts/contracts/RustToken721.sol/RustToken721.json | jq -r '.bytecode' > src/ethereum/ethers_rs/rust-token721.bin
cat ethereum/artifacts/contracts/RustToken1155.sol/RustToken1155.json | jq -r '.bytecode' > src/ethereum/ethers_rs/rust-token1155.bin
cat ethereum/artifacts/contracts/SampleOracle.sol/SampleOracle.json | jq '.abi' > src/ethereum/ethers_rs/sample-oracle.abi.json
cat ethereum/artifacts/contracts/RevealToken721.sol/RevealToken721.json | jq '.abi' > src/ethereum/ethers_rs/reveal-token721.abi.json
cat ethereum/artifacts/contracts/RevealToken721.sol/RevealToken721.json | jq '.bytecode' > src/ethereum/ethers_rs/reveal-token721.bin
1 change: 1 addition & 0 deletions chainlink/node/Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
run:
cd ../time-adapter && npm install
docker-compose up
10 changes: 5 additions & 5 deletions chainlink/time-job.toml → chainlink/time-adapter-job.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
contractAddress = "0x45585c78a16c62b510E6336fD8B95C61e88039B0"
name = "Get Time"
contractAddress = "0x45585c78a16c62b510E6336fD8B95C61e88039B0" # Oracle Address
name = "TimeAdapter"
observationSource = """
decode_log [
type="ethabidecodelog"
Expand All @@ -25,7 +25,7 @@ observationSource = """
encode_data [
type="ethabiencode"
abi="(bytes32 requestId, string now, int256 timestamp)"
abi="(bytes32 requestId, string now, uint256 timestamp)"
data="{\\"requestId\\": $(decode_log.requestId), \\"now\\": $(data_now), \\"timestamp\\": $(data_timestamp)}"
]
Expand All @@ -43,6 +43,6 @@ observationSource = """
]
encode_data -> encode_tx -> submit_tx
"""
schemaVersion = 1
"""
schemaVersion = 1
type = "directrequest"
2 changes: 1 addition & 1 deletion chainlink/time-adapter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ app.post("/", (req: express.Request, res: express.Response) => {
data: {
jobRunID: req.body.id ?? 0,
now: now.toISOString(),
timestamp: now.getTime(),
timestamp: Math.floor(now.getTime() / 1000),
},
})
);
Expand Down
5 changes: 0 additions & 5 deletions ethereum/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,15 @@ node:

deploy-hardhat:
npx hardhat run scripts/deploy-tokens.ts --network hardhat
npx hardhat run scripts/deploy-sample-oracle.ts --network hardhat
npx hardhat run scripts/deploy-oracle.ts --network hardhat

deploy-geth:
npx hardhat run scripts/deploy-tokens.ts --network geth
npx hardhat run scripts/deploy-sample-oracle.ts --network geth
npx hardhat run scripts/deploy-oracle.ts --network geth

deploy-tokens-goerli:
npx hardhat run scripts/deploy-tokens.ts --network goerli

deploy-sample-oracle-goerli:
npx hardhat run scripts/deploy-sample-oracle.ts --network goerli

deploy-oracle-goerli:
npx hardhat run scripts/deploy-oracle.ts --network goerli

Expand Down
7 changes: 5 additions & 2 deletions ethereum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
- RustToken1155
`0xF239EeE3a78eC18ABBB78E9b5E46758019EE5d81`

- SampleOracle
`0xc9800a41B4443FDD0896264bB8E0d148bc6032E8`
- RevealToken721
`0xDD791c156D0086321Fb9AFb642BE2479926C8Aae`

- Oracle
`0x45585c78a16c62b510E6336fD8B95C61e88039B0`

## Deployed Address(Mumbai)

Expand Down
174 changes: 174 additions & 0 deletions ethereum/contracts/RevealToken721.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@chainlink/contracts/src/v0.8/ChainlinkClient.sol";
import "@quant-finance/solidity-datetime/contracts/DateTime.sol";

// https://forum.openzeppelin.com/t/how-to-make-a-contract-inheriting-chalinkclient-uups-upgradable/20670
// UpgradableとChainlinkClientは現状共存できないっぽい
contract RevealToken721 is ERC721Enumerable, Ownable, ChainlinkClient {
using Chainlink for Chainlink.Request;

mapping(uint256 => string) private _token2hash;
uint256 private _localTokenId;
string private _baseUrl;
struct TimeAdapterResponse {
string now;
uint256 timestamp;
}
TimeAdapterResponse public timeAdapterResponse;
uint256 public chainlinkFee;
address public oracleAddress;
bytes32 public timeAdapterJobId;

/**
* Network: Goerli
* Chainlink Address: 0x326C977E6efc84E512bB9C30f76E30c160eD06FB
* Oracle Address: 0x45585c78a16c62b510E6336fD8B95C61e88039B0
* TimeAdapter JobId: 371ddf3b-2f03-4ee2-bfea-97ebe6398165(セットするときはハイフンなし)
*/
constructor(
string memory name,
string memory symbol,
address chainlinkAddress,
address oracle,
string memory jobId
) ERC721(name, symbol) {
_localTokenId = 0;
_baseUrl = "https://akiho-playground.infura-ipfs.io/ipfs/";

setChainlinkToken(chainlinkAddress);
chainlinkFee = 1 * 10**18;
oracleAddress = oracle;
timeAdapterJobId = stringToBytes32(jobId);
}

function stringToBytes32(string memory source)
public
pure
returns (bytes32 result)
{
bytes memory tempEmptyStringTest = bytes(source);
if (tempEmptyStringTest.length == 0) {
return 0x0;
}

assembly {
result := mload(add(source, 32))
}
}

function getChainlinkTokenString() public view returns (address) {
return chainlinkTokenAddress();
}

function setChainlinkTokenString(address _address) public onlyOwner {
setChainlinkToken(_address);
}

function setChainlinkFee(uint256 fee) public onlyOwner {
chainlinkFee = fee;
}

function setOracleAddress(address oracle) public onlyOwner {
oracleAddress = oracle;
}

function setTimeAdapterJobId(string memory jobId) public onlyOwner {
timeAdapterJobId = stringToBytes32(jobId);
}

function mint(string memory contentHash) public virtual onlyOwner {
_localTokenId += 1;
_token2hash[_localTokenId] = contentHash;
_mint(_msgSender(), _localTokenId);
}

function setBaseURI(string memory base) public virtual onlyOwner {
_baseUrl = base;
}

function tokenURI(uint256 tokenId)
public
view
virtual
override
returns (string memory)
{
if (timeAdapterResponse.timestamp > 0) {
uint256 hour = DateTime.getHour(timeAdapterResponse.timestamp);
if (hour >= 3) {
string memory contentHash = _token2hash[tokenId];
return string(abi.encodePacked(_baseUrl, contentHash));
}
}

return
string(
abi.encodePacked(
_baseUrl,
"QmbJswgamjqeuLoH8eFEaGSCehLNqE4C4E4PRUo5ymLzgg"
)
);
}

function updateTime() public onlyOwner returns (bytes32 requestId) {
Chainlink.Request memory req = buildChainlinkRequest(
timeAdapterJobId,
address(this),
this.fulfillUpdateTime.selector
);
req.add("params", "sample time adapter");
requestId = sendChainlinkRequestTo(oracleAddress, req, chainlinkFee);
}

function fulfillUpdateTime(
bytes32 requestId,
string memory _now,
uint256 timestamp
) public recordChainlinkFulfillment(requestId) {
timeAdapterResponse = TimeAdapterResponse({
now: _now,
timestamp: timestamp
});
}

function cancelRequest(
bytes32 requestId,
bytes4 callbackFunctionId,
uint256 expiration
) public onlyOwner {
cancelChainlinkRequest(
requestId,
chainlinkFee,
callbackFunctionId,
expiration
);
}

function withdrawLink() public onlyOwner {
LinkTokenInterface link = LinkTokenInterface(chainlinkTokenAddress());
require(
link.transfer(msg.sender, link.balanceOf(address(this))),
"Unable to transfer"
);
}

function getCurrentHour() public view returns (int256) {
if (timeAdapterResponse.timestamp > 0) {
uint256 hour = DateTime.getHour(timeAdapterResponse.timestamp);
return int256(hour);
}

return -1;
}

function setTimestampForDebug(uint256 timestamp) public onlyOwner {
timeAdapterResponse = TimeAdapterResponse({
now: "",
timestamp: timestamp
});
}
}
108 changes: 0 additions & 108 deletions ethereum/contracts/SampleOracle.sol

This file was deleted.

Loading

0 comments on commit 0fdf719

Please sign in to comment.