Skip to content

Commit

Permalink
Optimize cost of adding a blogpost
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalZalecki committed Jun 12, 2018
1 parent 3d35002 commit ee6563a
Show file tree
Hide file tree
Showing 7 changed files with 958 additions and 1,684 deletions.
1 change: 0 additions & 1 deletion app/blog/components/PostPage.jsx
Expand Up @@ -93,7 +93,6 @@ export class PostPage extends React.Component {
<div>
<article>
<h1><Link to={`/u/${author}/${hash}`}>{post.title}</Link></h1>
Date: {format(new Date(post.timestamp * 1000), "HH:mm YYYY-MM-dd")}{" | "}
Author: <Link to={`/u/${author}`}>{author}</Link>
<div dangerouslySetInnerHTML={{ __html: marked(post.content) }}></div>
</article>
Expand Down
2 changes: 1 addition & 1 deletion app/blog/contracts/BlogContract.js
@@ -1,7 +1,7 @@
import { getWeb3 } from "../../common/web3";
import BlogABI from "./abi/Blog.json";

const BLOG_ADDRESS = "0xf713ca4e1161013afeda584cbe60a937e274647e";
const BLOG_ADDRESS = "0x197d9950c48a00ddeddbd2cddf57e03898597999";
const { web3, web3Events } = getWeb3();

export const Blog = new web3.eth.Contract(BlogABI, BLOG_ADDRESS);
Expand Down
137 changes: 134 additions & 3 deletions app/blog/contracts/abi/Blog.json
@@ -1,4 +1,13 @@
[
{
"constant": false,
"inputs": [],
"name": "renounceOwnership",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [
Expand All @@ -18,6 +27,20 @@
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "owner",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
Expand All @@ -41,6 +64,20 @@
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_newOwner",
"type": "address"
}
],
"name": "transferOwnership",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [
Expand All @@ -61,9 +98,18 @@
"type": "function"
},
{
"inputs": [],
"payable": true,
"stateMutability": "payable",
"inputs": [
{
"name": "_gasPrice",
"type": "uint256"
},
{
"name": "_gasLimit",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "constructor"
},
{
Expand Down Expand Up @@ -115,6 +161,91 @@
"name": "PostSubmitted",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "previousOwner",
"type": "address"
}
],
"name": "OwnershipRenounced",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "previousOwner",
"type": "address"
},
{
"indexed": true,
"name": "newOwner",
"type": "address"
}
],
"name": "OwnershipTransferred",
"type": "event"
},
{
"constant": true,
"inputs": [
{
"name": "_source",
"type": "string"
}
],
"name": "getPrice",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_gasPrice",
"type": "uint256"
}
],
"name": "setCustomOraclizeGasPrice",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_gasLimit",
"type": "uint256"
}
],
"name": "setCustomOraclizeGasLimit",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [],
"name": "withdraw",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
Expand Down
34 changes: 28 additions & 6 deletions contracts/Blog.sol
@@ -1,10 +1,11 @@
pragma solidity 0.4.24;

import "openzeppelin-solidity/contracts/ownership/Ownable.sol";
import "./lib/usingOraclize.sol";
import "./lib/strings.sol";


contract Blog is usingOraclize {
contract Blog is usingOraclize, Ownable {
using strings for *;

mapping(address => string[]) public hashesByAuthor;
Expand All @@ -14,22 +15,43 @@ contract Blog is usingOraclize {
event PostAdded(address indexed author, string hash, uint timestamp, string title);
event PostSubmitted(address indexed author, string hash, bytes32 queryId);

constructor() public payable {
uint private gasLimit;

constructor(uint _gasPrice, uint _gasLimit) public {
setCustomOraclizeGasPrice(_gasPrice);
setCustomOraclizeGasLimit(_gasLimit);
}

function getPrice(string _source) public view returns (uint) {
return oraclize_getPrice(_source);
}

function setCustomOraclizeGasPrice(uint _gasPrice) public onlyOwner {
oraclize_setCustomGasPrice(_gasPrice);
}

function setCustomOraclizeGasLimit(uint _gasLimit) public onlyOwner {
gasLimit = _gasLimit;
}

function withdraw() public onlyOwner {
owner.transfer(address(this).balance);
}

function __callback(bytes32 _queryId, string _title) public {
require(msg.sender == oraclize_cbAddress());
require(bytes(hashByQueryId[_queryId]).length != 0);
string memory hash = hashByQueryId[_queryId];
address author = authorByHash[keccak256(hash)];
address author = authorByHash[keccak256(bytes(hash))];
hashesByAuthor[author].push(hash);
emit PostAdded(author, hash, now, _title);
}

function addPost(string _hash) public payable returns (bool) {
require(authorByHash[keccak256(_hash)] == address(0), "This post already exists");
bytes32 queryId = oraclize_query("IPFS", "json(".toSlice().concat(_hash.toSlice()).toSlice().concat(").title".toSlice()));
authorByHash[keccak256(_hash)] = msg.sender;
require(authorByHash[keccak256(bytes(_hash))] == address(0), "This post already exists");
require(msg.value >= oraclize_getPrice("IPFS"), "The fee is too low");
bytes32 queryId = oraclize_query("IPFS", "json(".toSlice().concat(_hash.toSlice()).toSlice().concat(").title".toSlice()), gasLimit);
authorByHash[keccak256(bytes(_hash))] = msg.sender;
hashByQueryId[queryId] = _hash;
emit PostSubmitted(msg.sender, _hash, queryId);
return true;
Expand Down
4 changes: 3 additions & 1 deletion migrations/2_blog.js
@@ -1,5 +1,7 @@
var Blog = artifacts.require("./Blog.sol");

const GWEI = 10**9;

module.exports = function(deployer) {
deployer.deploy(Blog);
deployer.deploy(Blog, 4 * GWEI, 200000);
};

0 comments on commit ee6563a

Please sign in to comment.