Skip to content

Commit

Permalink
refactor: refactored code to make onchain req
Browse files Browse the repository at this point in the history
  • Loading branch information
dawksh committed Apr 9, 2022
1 parent 5d4d04c commit 1c59500
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 61 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"solidity.defaultCompiler": "localNodeModule"
}
46 changes: 46 additions & 0 deletions contracts/Interfaces/LinkTokenInterface.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;

interface LinkTokenInterface {
function allowance(address owner, address spender)
external
view
returns (uint256 remaining);

function approve(address spender, uint256 value)
external
returns (bool success);

function balanceOf(address owner) external view returns (uint256 balance);

function decimals() external view returns (uint8 decimalPlaces);

function decreaseApproval(address spender, uint256 addedValue)
external
returns (bool success);

function increaseApproval(address spender, uint256 subtractedValue)
external;

function name() external view returns (string memory tokenName);

function symbol() external view returns (string memory tokenSymbol);

function totalSupply() external view returns (uint256 totalTokensIssued);

function transfer(address to, uint256 value)
external
returns (bool success);

function transferAndCall(
address to,
uint256 value,
bytes calldata data
) external returns (bool success);

function transferFrom(
address from,
address to,
uint256 value
) external returns (bool success);
}
83 changes: 83 additions & 0 deletions contracts/MetaHuddle.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

import "@chainlink/contracts/src/v0.8/ChainlinkClient.sol";

contract MetaHuddle is ChainlinkClient {
using Chainlink for Chainlink.Request;

constructor(address _linkToken) {
linkContract = LinkTokenInterface(_linkToken);
setChainlinkToken(_linkToken);
oracle = 0xc57B33452b4F7BB189bB5AfaE9cc4aBa1f7a4FD8;
jobId = "d5270d1c311941d0b08bead21fea7747";
chainlinkFee = 0.1 * 10**18;
}

modifier onlyOwner() {
require(
msg.sender == address(0x28172273CC1E0395F3473EC6eD062B6fdFb15940)
);
_;
}

string private endpoint =
"https://metapass-huddle.herokuapp.com/api/getHuddle";

// Chainlink Variables
uint256 public volume;
address private oracle;
bytes32 private jobId;
uint256 private chainlinkFee;
LinkTokenInterface public linkContract;

function getHuddleLink(address child) public returns (bytes32 link) {
Chainlink.Request memory request = buildChainlinkRequest(
jobId,
address(this),
this.fulfill.selector
);
request.add("get", makeString(toAsciiString(child)));
return sendChainlinkRequestTo(oracle, request, chainlinkFee);
}

function makeString(string memory child)
public
view
returns (string memory)
{
return string(abi.encodePacked(endpoint, "/", child));
}

function fulfill(bytes32 _requestId, uint256 _volume)
public
recordChainlinkFulfillment(_requestId)
{
volume = _volume;
}

function withdrawLink() public onlyOwner {
linkContract.transfer(
msg.sender,
linkContract.balanceOf(address(this))
);
}

function toAsciiString(address x) public pure returns (string memory) {
bytes memory s = new bytes(40);
for (uint256 i = 0; i < 20; i++) {
bytes1 b = bytes1(uint8(uint256(uint160(x)) / (2**(8 * (19 - i)))));
bytes1 hi = bytes1(uint8(b) / 16);
bytes1 lo = bytes1(uint8(b) - 16 * uint8(hi));
s[2 * i] = char(hi);
s[2 * i + 1] = char(lo);
}
return string(abi.encodePacked("0x", s));
}

function char(bytes1 b) public pure returns (bytes1 c) {
if (uint8(b) < 10) return bytes1(uint8(b) + 0x30);
else return bytes1(uint8(b) + 0x57);
}
}
55 changes: 7 additions & 48 deletions contracts/MetapassFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,27 @@ pragma solidity >=0.7.0 <0.9.0;

import "./Metapass.sol";
import "./MetaStorage.sol";
import "./MetaHuddle.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@chainlink/contracts/src/v0.8/ChainlinkClient.sol";
import "@chainlink/contracts/src/v0.8/interfaces/LinkTokenInterface.sol";

contract MetapassFactory is Ownable, ChainlinkClient {
contract MetapassFactory is Ownable {
MetaStorage storageProxy;
using Chainlink for Chainlink.Request;

// Chainlink Variables
uint256 public volume;

address private oracle;
bytes32 private jobId;
uint256 private chainlinkFee;
LinkTokenInterface public linkContract;
MetaHuddle huddleProxy;

uint256 cutNumerator = 0;
uint256 cutDenominator = 100;

event childEvent(address child);

constructor(address _storageProxy, address _linkToken) {
linkContract = LinkTokenInterface(_linkToken);
constructor(address _storageProxy, address metaHuddleAddress) {
storageProxy = MetaStorage(_storageProxy);
setChainlinkToken(_linkToken);
oracle = 0xc57B33452b4F7BB189bB5AfaE9cc4aBa1f7a4FD8;
jobId = "d5270d1c311941d0b08bead21fea7747";
chainlinkFee = 0.1 * 10**18; // (Varies by network and job)
huddleProxy = MetaHuddle(metaHuddleAddress);
}

mapping(address => Metapass[]) public addressToEventMap;

function getHuddleLink(address child) public returns (bytes32 link) {
Chainlink.Request memory request = buildChainlinkRequest(
jobId,
address(this),
this.fulfill.selector
);
request.add(
"get",
string(
abi.encodePacked(
"https://metapass-huddle.herokuapp.com/api/getHuddle",
string(abi.encode(child))
)
)
);
return sendChainlinkRequestTo(oracle, request, chainlinkFee);
}

function fulfill(bytes32 _requestId, uint256 _volume)
public
recordChainlinkFulfillment(_requestId)
{
volume = _volume;
function createHuddleEvent(address child) public returns (bytes32 link) {
return huddleProxy.getHuddleLink(child);
}

function createEvent(
Expand Down Expand Up @@ -99,13 +65,6 @@ contract MetapassFactory is Ownable, ChainlinkClient {
}
}

function withdrawLink() public onlyOwner {
linkContract.transfer(
msg.sender,
linkContract.balanceOf(address(this))
);
}

function getEventChildren() public view returns (Metapass[] memory) {
return addressToEventMap[msg.sender];
}
Expand Down
27 changes: 14 additions & 13 deletions scripts/deploy-factory.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
const hre = require("hardhat");

async function main() {
const MetapassFactory = await hre.ethers.getContractFactory(
"MetapassFactory"
);
const factoryContract = await MetapassFactory.deploy(
"0x6a956a4C72203e111BA5B5d396bc0ad286AeBd9e"
);
const MetapassFactory = await hre.ethers.getContractFactory(
"MetapassFactory"
);
const factoryContract = await MetapassFactory.deploy(
"0x3e4f13d936176A1cD21757d2725e66Af6F1c12C5",
"0x9e248FdC510E96556635dbF354605533e005337f"
);

await factoryContract.deployed();
await factoryContract.deployed();

console.log("Factory contract deployed to:", factoryContract.address);
console.log("Factory contract deployed to:", factoryContract.address);
}

main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
19 changes: 19 additions & 0 deletions scripts/deploy-huddle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const hre = require("hardhat");

async function main() {
const MetaHuddle = await hre.ethers.getContractFactory("MetaHuddle");
const huddleContract = await MetaHuddle.deploy(
"0x326c977e6efc84e512bb9c30f76e30c160ed06fb"
);

await huddleContract.deployed();

console.log("Huddle contract deployed to:", huddleContract.address);
}

main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});

0 comments on commit 1c59500

Please sign in to comment.