Skip to content

Instantly share code, notes, and snippets.

@jpn0424
Last active May 8, 2019 05:35
Show Gist options
  • Select an option

  • Save jpn0424/f8d03216c7a02a641cea3ebf653eb731 to your computer and use it in GitHub Desktop.

Select an option

Save jpn0424/f8d03216c7a02a641cea3ebf653eb731 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.4.24+commit.e67f0147.js&optimize=true&gist=
pragma solidity ^0.4.24;
contract ManagePermission {
address private _admin;
constructor() public {
_admin = msg.sender;
}
function isAdmin() internal view returns(bool) {
return msg.sender == _admin;
}
modifier onlyAdmin() {
require(isAdmin());
_;
}
}
/* Template */
contract Template is ManagePermission {
/* structure */
struct contractList {
uint256 index; //可以被儲存的位置,初始是0
mapping(uint256 => address) contractAddress;
}
/* storage */
mapping(address => contractList) publisher;
/* status */
bool contracEff;
/* Contract List */
/* rule */
/* Base */
/* address list -> Here can search List of contracts created by the user */
function searchAddressList(address _publisher, uint256 _index) view internal returns(address) {
require(_index != 0);
if(publisher[_publisher].index >= _index) _index--; // search the latest
else _index = publisher[_publisher].index-1;
return publisher[_publisher].contractAddress[_index];
}
bytes4 aaa = 0x01020304;
/* return arg by decode the input string for publisher that use to create a new contract */
function decodeBytesToArgs() view internal returns(bytes){
bytes memory arg1 = new bytes(1);
arg1[0] = 0xff;
return arg1;
}
//function getArg() view external returns(uint256 a, address b, string c) {
// return decodeStringToArgs();
//}
/* Operate */
/* user */
function createContract() public {
bytes memory a;
bytes memory b;
string memory c;
/* get arg */ //怎麼做? // 試看看型態轉換 // 改傳bytes
(a) = decodeBytesToArgs(); // 沒加var不行,但var被棄用了,可能需要修改
uint256[1] memory result;
for (uint i=0; i<8 && i<a.length; ++i) {
result[i] = uint256(a[i]);
}
/* crate & store in List */
publisher[msg.sender].contractAddress[publisher[msg.sender].index] = new Contract(msg.sender, result[0]);
/* point to the next position to be store */
publisher[msg.sender].index++;
}
function searchOwnAddressList(uint256 _index) view external returns(address) {
return searchAddressList(msg.sender, _index);
}
/* admin */
function searchuserAddressList(address _publisher, uint256 _index) onlyAdmin view external returns(address) {
return searchAddressList(_publisher, _index);
}
/* Oracle */
/* service */
}
/* Main content */
contract Contract {
address public creater; //use to debug
uint256 public a; //use to debug
address public b; //use to debug
string public c; //use to debug
constructor(address _creater, uint256 _arg1) public {
creater = _creater;
a = _arg1;
}
}
pragma solidity ^0.4.24;
pragma experimental ABIEncoderV2;
// 提供權限管理
// 腳色:管理者、合約建立者
contract ManagePermission {
address private admin;
address private serverService;
constructor() public {
admin = msg.sender;
}
function isAdmin() internal view returns(bool) {
return msg.sender == admin;
}
modifier onlyAdmin() {
require(isAdmin());
_;
}
/* 設定後端服務帳戶 */
function setServerServiceAccount(address serverServiceAccount) external onlyAdmin {
serverService = serverServiceAccount;
}
function isServer() internal view returns(bool) {
return msg.sender == serverService;
}
modifier onlyService() {
require(isServer());
_;
}
}
// 基礎庫,數據儲存、數據結構、事件
contract FbBase is ManagePermission {
/* Data Structure */
// 後端服務會監控合約,當發生建立合約的事件時,會記錄到這裡
// 使用者發佈的交易細節,由管理者與與後端服務填寫
// 合約資訊儲存在Receipt,所以細節用transHash代替
struct contractDetail {
bytes32 transHash; // 交易號
address contractAddress; // 合約地址
bool effectiveness; // 有效性
}
// 使用者的事件
struct userEvent {
uint256 eventIndex; // 事件流水號
uint256 templateIndex; // 樣本流水號
address creator; // 這個事件的發送者
uint32 actStatus; // 事件狀態, "1" 代表建立合約
uint256 contractDetailIndex; // contract index, it's can use this to search contract detail, else set null.
}
struct userEventList {
uint256 eventNumber; // 事件總數
mapping (uint256 => userEvent) userEvents;
mapping (uint256 => contractDetail) contractStatus;
}
/* Storage */
mapping (address => userEventList) users;
/* Operate */
function vieweventIndex() external view returns(uint256){
return users[msg.sender].eventNumber;
}
function viewEvent(address _address, uint256 _index) internal view returns(userEvent) {
return users[_address].userEvents[_index];
}
function viewcontractDetail(address _address, uint256 _eventIndex) internal view returns(contractDetail) {
return users[_address].contractStatus[_eventIndex];
}
// 增加事件
function addEvent(userEventList storage _user, uint256 _templateIndex) internal {
_user.eventNumber++;
_user.userEvents[users[msg.sender].eventNumber].eventIndex = _user.eventNumber;
_user.userEvents[users[msg.sender].eventNumber].templateIndex = _templateIndex;
_user.userEvents[users[msg.sender].eventNumber].creator = msg.sender;
}
}
contract TemplateInterface {
function createContract() external;
}
// 這裡提供合約樣板功能
contract ContractTemplate is FbBase {
struct templateDetail {
string author;
string contractInfo;
address _templateAddress;
bool effectiveness;
}
// 紀錄樣板資訊
// (uint256)id => templateDetail 透過id找Template
mapping (uint256 => templateDetail) templateInfo;
// 管理員設定樣板位址
function setTemplateAddress(address _address, uint256 _id) external onlyAdmin() {
templateInfo[_id]._templateAddress = _address;
templateInfo[_id].effectiveness = true;
}
TemplateInterface templatecontract;
// 獲取樣板操作指針
//@_id:樣板號
function _getTemplatePointer(uint256 _id) internal {
templatecontract = TemplateInterface(templateInfo[_id]._templateAddress);
}
// 建立樣板,使用者建立合約時使用
//@_id:樣板號
function createTemplate(uint256 _id) internal {
_getTemplatePointer(_id);
templatecontract.createContract();
}
}
contract FbUserActManage is ContractTemplate {
event userCreateContractEvent(uint256 _eventIndex, uint256 _contractDetailIndex);
function createNewActivity(uint256 _templateIndex) external {
createTemplate(_templateIndex);
addEvent(users[msg.sender], _templateIndex);
users[msg.sender].userEvents[users[msg.sender].eventNumber].actStatus = 1;
uint256 _pcount = users[msg.sender].userEvents[users[msg.sender].eventNumber - 1].contractDetailIndex;
users[msg.sender].userEvents[users[msg.sender].eventNumber].contractDetailIndex = _pcount + 1;
// 紀錄eventIndex 和 contractDetailIndex
emit userCreateContractEvent(users[msg.sender].userEvents[users[msg.sender].eventNumber].eventIndex, users[msg.sender].userEvents[users[msg.sender].eventNumber].contractDetailIndex);
}
function viewUserEvent(uint256 _index) external view returns(userEvent) {
return viewEvent(msg.sender, _index);
}
// User透過Contract號查詢自己發佈的合約資訊
function viewUserContractDetail(uint256 _index) external view returns(contractDetail) {
return viewcontractDetail(msg.sender, _index);
}
}
contract FbAdminActManage is FbUserActManage {
/* Admin */
// 設定Template的有效性
function setTemplateEff(uint256 _templateIndex, bool _eff) external onlyAdmin {
templateInfo[_templateIndex].effectiveness = _eff;
}
/* Service */
// 可能需要設定一個後端服務專用的權限
// 後端服務偵測事件後放入資訊到ContractDetail
// 後端根據transation receipt 判斷設定位置。(Address From, eventIndex)
/*
address _address; // 發起者
bytes32 transHash; // 交易號
address contractAddress; // 外部輸入
bool effectiveness; // 有效性
*/
function setContractDetail(address _address, uint256 _eventindex, bytes32 _transHash, address _contractAddress) external onlyService {
uint256 pcontractDetailIndex = users[_address].userEvents[_eventindex].contractDetailIndex;
users[_address].contractStatus[pcontractDetailIndex].transHash = _transHash;
users[_address].contractStatus[pcontractDetailIndex].contractAddress = _contractAddress;
}
}
pragma solidity ^0.4.18;
contract Fbethorder {
event ActivityInfor(string location, string RegisterDay, string Abstract);
uint256 constant public FEE = 0.5 ether;
uint256 constant public ReFEE = 0.2 ether;
mapping (uint256 => address) public id2Addr;
mapping (address => uint256) public userId;
mapping (address => bool) public useratt;
address public fborder;
address public FbWallet;
uint256 public nowtime;
uint256 public userAmount;
uint256 public maxAttendees;
uint256 public startTime;
uint256 public endTime;
bool public classstart;
function Fbethorder(address _FbWallet, uint256 _maxAttendees, uint256 _startTime, string _location, string _RegisterDay, string _Abstract) public {
FbWallet = _FbWallet;
maxAttendees = _maxAttendees;
userAmount = 0;
startTime = _startTime;
classstart = false;
fborder = msg.sender;
emit ActivityInfor( _location, _RegisterDay, _Abstract );
}
function getnow() public {
nowtime = now;
}
function () payable external {
getTicket(msg.sender);
}
function getTicket (address _attendee) payable public {
require(now >= startTime && msg.value >= FEE && userId[_attendee] == 0);
userAmount ++;
require(userAmount <= maxAttendees);
userId[_attendee] = userAmount;
id2Addr[userAmount] = _attendee;
}
/* 發起人設定結束時間,時間使用UTC+8 */
function EndTime(uint256 _endTime) public {
require(FbWallet == msg.sender);
endTime = _endTime;
classstart = true;
}
function Useratt( ) public {
/* 確定開課,確任使用者是有報名成功的出席者 */
require(classstart == true && userId[msg.sender] != 0 && useratt[msg.sender] == false);
useratt[msg.sender] = true;
}
/* 課程結束,準時退回押金,延遲罰款 */
function withdraw () public {
require(FbWallet == msg.sender);
for(uint i = 1; i <= userAmount; i++){
if(useratt[id2Addr[i]] == false) continue;
/* 這邊時間要減去 UTC+8 */
if(now <= endTime + 2 minutes){
id2Addr[i].transfer(ReFEE);
}else {
id2Addr[i].transfer(ReFEE + 0.1 ether);
}
}
FbWallet.transfer(this.balance);
}
}
pragma solidity ^0.4.24;
contract TemplateTest {
event contractAddress(address _contract);
address public viewaddress; // use to debug
function createContract() public {
address a = new Contract("hellow");
viewaddress = a;
emit contractAddress(a);
}
}
/* Main content */
contract Contract {
string public info;
constructor (string _test) public {
info = _test;
}
}
pragma solidity ^0.4.24;
contract Test {
struct Answer
{
bytes32 text;
uint voteCount; // number of accumulated votes
// add more non-key fields as needed
}
struct Question
{
bytes32 text;
bytes32[] answerList; // list of answer keys so we can look them up
mapping(bytes32 => Answer) answerStructs; // random access by question key and answer key
// add more non-key fields as needed
}
mapping(bytes32 => Question) questionStructs; // random access by question key
bytes32[] questionList; // list of question keys so we can enumerate them
function newQuestion(bytes32 questionKey, bytes32 text)
// onlyOwner
returns(bool success)
{
// not checking for duplicates
questionStructs[questionKey].text = text;
questionList.push(questionKey);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment