Skip to content

Instantly share code, notes, and snippets.

@shd101wyy

shd101wyy/gua.sol

Last active Apr 18, 2018
Embed
What would you like to do?
pragma solidity ^0.4.0;
// pragma experimental ABIEncoderV2;
// https://medium.com/daox/three-methods-to-transfer-funds-in-ethereum-by-means-of-solidity-5719944ed6e9
contract Gua {
address owner;
address public previousContractAddress;
/**
* 0x0 => earnings
* 0x1 => likes
* 0x2 => dislikes
* 0x3 => reports
*
* transactionHash => field => value
*/
mapping (bytes32 => mapping(uint => uint)) public state; // transactionHash
mapping (address => string) public metaDataJSONStringMap;
/**
* uint[2] here
* [0] => block.number
* [1] => timestamp
*/
mapping (address => uint[2]) public currentArticleTransactionMap;
mapping (bytes32 => uint[2]) public currentCommentTransactionMap;
mapping (bytes32 => uint[2]) public currentTagTransactionByTimeMap;
mapping (bytes32 => uint[2]) public currentTagTransactionByTrendMap;
function Decent(address previousContractAddr) public {
owner = msg.sender;
previousContractAddress = previousContractAddr;
}
function updateState(bytes32 transactionHash, uint field, uint value) external {
state[transactionHash][field] = state[transactionHash][field] + value;
}
function multipleState(bytes32 transactionHash, uint[] fieldsAndValues) external {
for (uint i = 0; i < fieldsAndValues.length; i += 2) {
state[transactionHash][fieldsAndValues[i]] = state[transactionHash][fieldsAndValues[i]] + fieldsAndValues[i+1];
}
}
function getState(bytes32 transactionHash, uint field) external constant returns (uint) {
return state[transactionHash][field];
}
function setMetaDataJSONStringMap(address addr, string value) external {
metaDataJSONStringMap[addr] = value;
}
function getMetaDataJSONStringValue(address addr) external constant returns (string) {
return metaDataJSONStringMap[addr];
}
// Post Articles
event PostArticleEvent(uint version, uint timestamp, string message, bytes32 previousArticleTransactionHash, uint[2] previousArticleTransaction);
event PostTagByTimeEvent(bytes32 tag, bytes32 previousTagTransactionHash, uint[2] previousTagTransaction);
event PostTagByTrendEvent(bytes32 tag, bytes32 previousTagTransactionHash, uint[2] previousTagTransaction);
function postArticle(uint version, uint timestamp, string message, bytes32 previousArticleTransactionHash, bytes32[] tags, bytes32[] previousTagTransactionByTimeHashes, bytes32[] previousTagTransactionByTrendHashes) external {
emit PostArticleEvent(version, timestamp, message, previousArticleTransactionHash, currentArticleTransactionMap[msg.sender]);
uint blockNumber = block.number;
currentArticleTransactionMap[msg.sender][0] = blockNumber;
currentArticleTransactionMap[msg.sender][1] = timestamp;
for (uint i = 0; i < tags.length; i++) {
emit PostTagByTimeEvent(tags[i], previousTagTransactionByTimeHashes[i], currentTagTransactionByTimeMap[tags[i]]);
currentTagTransactionByTimeMap[tags[i]][0] = blockNumber;
currentTagTransactionByTimeMap[tags[1]][1] = timestamp;
emit PostTagByTrendEvent(tags[i], previousTagTransactionByTrendHashes[i], currentTagTransactionByTrendMap[tags[i]]);
currentTagTransactionByTrendMap[tags[i]][0] = blockNumber;
currentTagTransactionByTimeMap[tags[i]][1] = timestamp;
}
}
function getCurrentArticleTransaction(address authorAddress) external constant returns (uint[2]) {
return currentArticleTransactionMap[authorAddress];
}
function getCurrentTagTransactionByTime(bytes32 tag) external constant returns (uint[2]) {
return currentTagTransactionByTimeMap[tag];
}
function getCurrentTagTransactionByTrend(bytes32 tag) external constant returns (uint[2]) {
return currentTagTransactionByTrendMap[tag];
}
function getCurrentCommentTransaction(bytes32 parentTransactionHash) external constant returns (uint[2]) {
return currentCommentTransactionMap[parentTransactionHash];
}
// Post Comments
event PostCommentEvent(uint version, uint timestamp, bytes32 parentTransactionHash, string message, bytes32 previousCommentTransactionHash);
function postComment(uint version, uint timestamp, bytes32 parentTransactionHash, uint parentTransactionBlockNumber, string message, bytes32 previousCommentTransactionHash, bytes32[] tags, bytes32[] previousTagTransactionByTrendHashes) external {
emit PostCommentEvent(version, timestamp, parentTransactionHash, message, previousCommentTransactionHash);
currentCommentTransactionMap[parentTransactionHash][0] = block.number;
currentCommentTransactionMap[parentTransactionHash][1] = timestamp;
for (uint i = 0; i < tags.length; i++) {
emit PostTagByTrendEvent(tags[i], previousTagTransactionByTrendHashes[i], currentTagTransactionByTrendMap[tags[i]]);
currentTagTransactionByTrendMap[tags[i]][0] = parentTransactionBlockNumber;
currentTagTransactionByTrendMap[tags[i]][1] = timestamp;
}
}
// Send ether
function sendEther(bytes32 transactionHash, address postAutherAddress, uint amount1, address appAuthorAddress, uint amount2) payable external {
state[transactionHash][0x0] = state[transactionHash][0x0] + amount1;
if (amount1 > 0) {
postAutherAddress.transfer(amount1);
}
if (amount2 > 0) {
appAuthorAddress.transfer(amount2);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment