Created
March 9, 2022 15:13
-
-
Save Danijel-Enoch/82e015ddb85f238889c5a8abf8aba1ee 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.8.7+commit.e28d00a7.js&optimize=false&runs=200&gist=
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
REMIX EXAMPLE PROJECT | |
Remix example project is present when Remix loads for the very first time or there are no files existing in the File Explorer. | |
It contains 3 directories: | |
1. 'contracts': Holds three contracts with different complexity level, denoted with number prefix in file name. | |
2. 'scripts': Holds two scripts to deploy a contract. It is explained below. | |
3. 'tests': Contains one test file for 'Ballot' contract with unit tests in Solidity. | |
SCRIPTS | |
The 'scripts' folder contains example async/await scripts for deploying the 'Storage' contract. | |
For the deployment of any other contract, 'contractName' and 'constructorArgs' should be updated (along with other code if required). | |
Scripts have full access to the web3.js and ethers.js libraries. | |
To run a script, right click on file name in the file explorer and click 'Run'. Remember, Solidity file must already be compiled. | |
Output from script will appear in remix terminal. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity >=0.7.0 <0.9.0; | |
/** | |
* @title Storage | |
* @dev Store & retrieve value in a variable | |
*/ | |
contract Storage { | |
uint256 number; | |
/** | |
* @dev Store value in variable | |
* @param num value to store | |
*/ | |
function store(uint256 num) public { | |
number = num; | |
} | |
/** | |
* @dev Return value | |
* @return value of 'number' | |
*/ | |
function retrieve() public view returns (uint256){ | |
return number; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity >=0.7.0 <0.9.0; | |
/** | |
* @title Owner | |
* @dev Set & change owner | |
*/ | |
contract Owner { | |
address private owner; | |
// event for EVM logging | |
event OwnerSet(address indexed oldOwner, address indexed newOwner); | |
// modifier to check if caller is owner | |
modifier isOwner() { | |
// If the first argument of 'require' evaluates to 'false', execution terminates and all | |
// changes to the state and to Ether balances are reverted. | |
// This used to consume all gas in old EVM versions, but not anymore. | |
// It is often a good idea to use 'require' to check if functions are called correctly. | |
// As a second argument, you can also provide an explanation about what went wrong. | |
require(msg.sender == owner, "Caller is not owner"); | |
_; | |
} | |
/** | |
* @dev Set contract deployer as owner | |
*/ | |
constructor() { | |
owner = msg.sender; // 'msg.sender' is sender of current call, contract deployer for a constructor | |
emit OwnerSet(address(0), owner); | |
} | |
/** | |
* @dev Change owner | |
* @param newOwner address of new owner | |
*/ | |
function changeOwner(address newOwner) public isOwner { | |
emit OwnerSet(owner, newOwner); | |
owner = newOwner; | |
} | |
/** | |
* @dev Return owner address | |
* @return address of owner | |
*/ | |
function getOwner() external view returns (address) { | |
return owner; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity >=0.7.0 <0.9.0; | |
/** | |
* @title Ballot | |
* @dev Implements voting process along with vote delegation | |
*/ | |
contract Ballot { | |
struct Voter { | |
uint weight; // weight is accumulated by delegation | |
bool voted; // if true, that person already voted | |
address delegate; // person delegated to | |
uint vote; // index of the voted proposal | |
} | |
struct Proposal { | |
// If you can limit the length to a certain number of bytes, | |
// always use one of bytes1 to bytes32 because they are much cheaper | |
bytes32 name; // short name (up to 32 bytes) | |
uint voteCount; // number of accumulated votes | |
} | |
address public chairperson; | |
mapping(address => Voter) public voters; | |
Proposal[] public proposals; | |
/** | |
* @dev Create a new ballot to choose one of 'proposalNames'. | |
* @param proposalNames names of proposals | |
*/ | |
constructor(bytes32[] memory proposalNames) { | |
chairperson = msg.sender; | |
voters[chairperson].weight = 1; | |
for (uint i = 0; i < proposalNames.length; i++) { | |
// 'Proposal({...})' creates a temporary | |
// Proposal object and 'proposals.push(...)' | |
// appends it to the end of 'proposals'. | |
proposals.push(Proposal({ | |
name: proposalNames[i], | |
voteCount: 0 | |
})); | |
} | |
} | |
/** | |
* @dev Give 'voter' the right to vote on this ballot. May only be called by 'chairperson'. | |
* @param voter address of voter | |
*/ | |
function giveRightToVote(address voter) public { | |
require( | |
msg.sender == chairperson, | |
"Only chairperson can give right to vote." | |
); | |
require( | |
!voters[voter].voted, | |
"The voter already voted." | |
); | |
require(voters[voter].weight == 0); | |
voters[voter].weight = 1; | |
} | |
/** | |
* @dev Delegate your vote to the voter 'to'. | |
* @param to address to which vote is delegated | |
*/ | |
function delegate(address to) public { | |
Voter storage sender = voters[msg.sender]; | |
require(!sender.voted, "You already voted."); | |
require(to != msg.sender, "Self-delegation is disallowed."); | |
while (voters[to].delegate != address(0)) { | |
to = voters[to].delegate; | |
// We found a loop in the delegation, not allowed. | |
require(to != msg.sender, "Found loop in delegation."); | |
} | |
sender.voted = true; | |
sender.delegate = to; | |
Voter storage delegate_ = voters[to]; | |
if (delegate_.voted) { | |
// If the delegate already voted, | |
// directly add to the number of votes | |
proposals[delegate_.vote].voteCount += sender.weight; | |
} else { | |
// If the delegate did not vote yet, | |
// add to her weight. | |
delegate_.weight += sender.weight; | |
} | |
} | |
/** | |
* @dev Give your vote (including votes delegated to you) to proposal 'proposals[proposal].name'. | |
* @param proposal index of proposal in the proposals array | |
*/ | |
function vote(uint proposal) public { | |
Voter storage sender = voters[msg.sender]; | |
require(sender.weight != 0, "Has no right to vote"); | |
require(!sender.voted, "Already voted."); | |
sender.voted = true; | |
sender.vote = proposal; | |
// If 'proposal' is out of the range of the array, | |
// this will throw automatically and revert all | |
// changes. | |
proposals[proposal].voteCount += sender.weight; | |
} | |
/** | |
* @dev Computes the winning proposal taking all previous votes into account. | |
* @return winningProposal_ index of winning proposal in the proposals array | |
*/ | |
function winningProposal() public view | |
returns (uint winningProposal_) | |
{ | |
uint winningVoteCount = 0; | |
for (uint p = 0; p < proposals.length; p++) { | |
if (proposals[p].voteCount > winningVoteCount) { | |
winningVoteCount = proposals[p].voteCount; | |
winningProposal_ = p; | |
} | |
} | |
} | |
/** | |
* @dev Calls winningProposal() function to get the index of the winner contained in the proposals array and then | |
* @return winnerName_ the name of the winner | |
*/ | |
function winnerName() public view | |
returns (bytes32 winnerName_) | |
{ | |
winnerName_ = proposals[winningProposal()].name; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"deploy": { | |
"VM:-": { | |
"linkReferences": {}, | |
"autoDeployLib": true | |
}, | |
"main:1": { | |
"linkReferences": {}, | |
"autoDeployLib": true | |
}, | |
"ropsten:3": { | |
"linkReferences": {}, | |
"autoDeployLib": true | |
}, | |
"rinkeby:4": { | |
"linkReferences": {}, | |
"autoDeployLib": true | |
}, | |
"kovan:42": { | |
"linkReferences": {}, | |
"autoDeployLib": true | |
}, | |
"görli:5": { | |
"linkReferences": {}, | |
"autoDeployLib": true | |
}, | |
"Custom": { | |
"linkReferences": {}, | |
"autoDeployLib": true | |
} | |
}, | |
"data": { | |
"bytecode": { | |
"functionDebugData": {}, | |
"generatedSources": [], | |
"linkReferences": {}, | |
"object": "60566050600b82828239805160001a6073146043577f4e487b7100000000000000000000000000000000000000000000000000000000600052600060045260246000fd5b30600052607381538281f3fe73000000000000000000000000000000000000000030146080604052600080fdfea26469706673582212202ec68763da53e83508b90093e16c4b76597da5e1ee394b29eb3bd03081d0e43564736f6c63430008070033", | |
"opcodes": "PUSH1 0x56 PUSH1 0x50 PUSH1 0xB DUP3 DUP3 DUP3 CODECOPY DUP1 MLOAD PUSH1 0x0 BYTE PUSH1 0x73 EQ PUSH1 0x43 JUMPI PUSH32 0x4E487B7100000000000000000000000000000000000000000000000000000000 PUSH1 0x0 MSTORE PUSH1 0x0 PUSH1 0x4 MSTORE PUSH1 0x24 PUSH1 0x0 REVERT JUMPDEST ADDRESS PUSH1 0x0 MSTORE PUSH1 0x73 DUP2 MSTORE8 DUP3 DUP2 RETURN INVALID PUSH20 0x0 ADDRESS EQ PUSH1 0x80 PUSH1 0x40 MSTORE PUSH1 0x0 DUP1 REVERT INVALID LOG2 PUSH5 0x6970667358 0x22 SLT KECCAK256 0x2E 0xC6 DUP8 PUSH4 0xDA53E835 ADDMOD 0xB9 STOP SWAP4 0xE1 PUSH13 0x4B76597DA5E1EE394B29EB3BD0 ADDRESS DUP2 0xD0 0xE4 CALLDATALOAD PUSH5 0x736F6C6343 STOP ADDMOD SMOD STOP CALLER ", | |
"sourceMap": "275:1931:0:-:0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;" | |
}, | |
"deployedBytecode": { | |
"functionDebugData": {}, | |
"generatedSources": [], | |
"immutableReferences": {}, | |
"linkReferences": {}, | |
"object": "73000000000000000000000000000000000000000030146080604052600080fdfea26469706673582212202ec68763da53e83508b90093e16c4b76597da5e1ee394b29eb3bd03081d0e43564736f6c63430008070033", | |
"opcodes": "PUSH20 0x0 ADDRESS EQ PUSH1 0x80 PUSH1 0x40 MSTORE PUSH1 0x0 DUP1 REVERT INVALID LOG2 PUSH5 0x6970667358 0x22 SLT KECCAK256 0x2E 0xC6 DUP8 PUSH4 0xDA53E835 ADDMOD 0xB9 STOP SWAP4 0xE1 PUSH13 0x4B76597DA5E1EE394B29EB3BD0 ADDRESS DUP2 0xD0 0xE4 CALLDATALOAD PUSH5 0x736F6C6343 STOP ADDMOD SMOD STOP CALLER ", | |
"sourceMap": "275:1931:0:-:0;;;;;;;;" | |
}, | |
"gasEstimates": { | |
"creation": { | |
"codeDepositCost": "17200", | |
"executionCost": "97", | |
"totalCost": "17297" | |
}, | |
"internal": { | |
"encode(bytes memory)": "infinite" | |
} | |
}, | |
"methodIdentifiers": {} | |
}, | |
"abi": [] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"compiler": { | |
"version": "0.8.7+commit.e28d00a7" | |
}, | |
"language": "Solidity", | |
"output": { | |
"abi": [], | |
"devdoc": { | |
"author": "Brecht Devos <[email protected]>", | |
"kind": "dev", | |
"methods": {}, | |
"title": "Base64", | |
"version": 1 | |
}, | |
"userdoc": { | |
"kind": "user", | |
"methods": {}, | |
"notice": "[MIT License]Provides a function for encoding some bytes in base64", | |
"version": 1 | |
} | |
}, | |
"settings": { | |
"compilationTarget": { | |
"contracts/libraries/Base64.sol": "Base64" | |
}, | |
"evmVersion": "london", | |
"libraries": {}, | |
"metadata": { | |
"bytecodeHash": "ipfs" | |
}, | |
"optimizer": { | |
"enabled": false, | |
"runs": 200 | |
}, | |
"remappings": [] | |
}, | |
"sources": { | |
"contracts/libraries/Base64.sol": { | |
"keccak256": "0x2a678b6003264920f6f82ec8b20ee2ad01213fe7998e592f9c182f717960cfc2", | |
"license": "MIT", | |
"urls": [ | |
"bzz-raw://7d60f2d86c61d24a0321e17a65cbc98548d871b594cbed4a75ef10e2b89120ca", | |
"dweb:/ipfs/Qmb2Nrs13u5fWCdFduyTMcGDGsZNyyuP7BDkN6FNaeLz77" | |
] | |
} | |
}, | |
"version": 1 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"deploy": { | |
"VM:-": { | |
"linkReferences": {}, | |
"autoDeployLib": true | |
}, | |
"main:1": { | |
"linkReferences": {}, | |
"autoDeployLib": true | |
}, | |
"ropsten:3": { | |
"linkReferences": {}, | |
"autoDeployLib": true | |
}, | |
"rinkeby:4": { | |
"linkReferences": {}, | |
"autoDeployLib": true | |
}, | |
"kovan:42": { | |
"linkReferences": {}, | |
"autoDeployLib": true | |
}, | |
"görli:5": { | |
"linkReferences": {}, | |
"autoDeployLib": true | |
}, | |
"Custom": { | |
"linkReferences": {}, | |
"autoDeployLib": true | |
} | |
}, | |
"data": { | |
"bytecode": { | |
"functionDebugData": {}, | |
"generatedSources": [], | |
"linkReferences": {}, | |
"object": "60566050600b82828239805160001a6073146043577f4e487b7100000000000000000000000000000000000000000000000000000000600052600060045260246000fd5b30600052607381538281f3fe73000000000000000000000000000000000000000030146080604052600080fdfea2646970667358221220fa98df99b2b8f4507cee7795d3931c8c12ce76de5f1fbd6acbdd8bf668136f2164736f6c63430008070033", | |
"opcodes": "PUSH1 0x56 PUSH1 0x50 PUSH1 0xB DUP3 DUP3 DUP3 CODECOPY DUP1 MLOAD PUSH1 0x0 BYTE PUSH1 0x73 EQ PUSH1 0x43 JUMPI PUSH32 0x4E487B7100000000000000000000000000000000000000000000000000000000 PUSH1 0x0 MSTORE PUSH1 0x0 PUSH1 0x4 MSTORE PUSH1 0x24 PUSH1 0x0 REVERT JUMPDEST ADDRESS PUSH1 0x0 MSTORE PUSH1 0x73 DUP2 MSTORE8 DUP3 DUP2 RETURN INVALID PUSH20 0x0 ADDRESS EQ PUSH1 0x80 PUSH1 0x40 MSTORE PUSH1 0x0 DUP1 REVERT INVALID LOG2 PUSH5 0x6970667358 0x22 SLT KECCAK256 STATICCALL SWAP9 0xDF SWAP10 0xB2 0xB8 DELEGATECALL POP PUSH29 0xEE7795D3931C8C12CE76DE5F1FBD6ACBDD8BF668136F2164736F6C6343 STOP ADDMOD SMOD STOP CALLER ", | |
"sourceMap": "167:806:0:-:0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;" | |
}, | |
"deployedBytecode": { | |
"functionDebugData": {}, | |
"generatedSources": [], | |
"immutableReferences": {}, | |
"linkReferences": {}, | |
"object": "73000000000000000000000000000000000000000030146080604052600080fdfea2646970667358221220fa98df99b2b8f4507cee7795d3931c8c12ce76de5f1fbd6acbdd8bf668136f2164736f6c63430008070033", | |
"opcodes": "PUSH20 0x0 ADDRESS EQ PUSH1 0x80 PUSH1 0x40 MSTORE PUSH1 0x0 DUP1 REVERT INVALID LOG2 PUSH5 0x6970667358 0x22 SLT KECCAK256 STATICCALL SWAP9 0xDF SWAP10 0xB2 0xB8 DELEGATECALL POP PUSH29 0xEE7795D3931C8C12CE76DE5F1FBD6ACBDD8BF668136F2164736F6C6343 STOP ADDMOD SMOD STOP CALLER ", | |
"sourceMap": "167:806:0:-:0;;;;;;;;" | |
}, | |
"gasEstimates": { | |
"creation": { | |
"codeDepositCost": "17200", | |
"executionCost": "97", | |
"totalCost": "17297" | |
}, | |
"internal": { | |
"strlen(string memory)": "infinite" | |
} | |
}, | |
"methodIdentifiers": {} | |
}, | |
"abi": [] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"compiler": { | |
"version": "0.8.7+commit.e28d00a7" | |
}, | |
"language": "Solidity", | |
"output": { | |
"abi": [], | |
"devdoc": { | |
"kind": "dev", | |
"methods": {}, | |
"version": 1 | |
}, | |
"userdoc": { | |
"kind": "user", | |
"methods": {}, | |
"version": 1 | |
} | |
}, | |
"settings": { | |
"compilationTarget": { | |
"contracts/libraries/StringUtils.sol": "StringUtils" | |
}, | |
"evmVersion": "london", | |
"libraries": {}, | |
"metadata": { | |
"bytecodeHash": "ipfs" | |
}, | |
"optimizer": { | |
"enabled": false, | |
"runs": 200 | |
}, | |
"remappings": [] | |
}, | |
"sources": { | |
"contracts/libraries/StringUtils.sol": { | |
"keccak256": "0xc28e401a39e36fa4e8dc60178ecc645efeb553517fc65d41758682be0c802b8e", | |
"license": "MIT", | |
"urls": [ | |
"bzz-raw://4eff80b722e3c6d60e1d84283b809b7f9094032f0f9ad29c89cbc81fc3471440", | |
"dweb:/ipfs/QmSzCNyrfdJKfjcydzbE7uhukuEvGD2PqPpuqLoMyCVUEk" | |
] | |
} | |
}, | |
"version": 1 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
*Submitted for verification at Etherscan.io on 2021-09-05 | |
*/ | |
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
/// [MIT License] | |
/// @title Base64 | |
/// @notice Provides a function for encoding some bytes in base64 | |
/// @author Brecht Devos <[email protected]> | |
library Base64 { | |
bytes internal constant TABLE = | |
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | |
/// @notice Encodes some bytes to the base64 representation | |
function encode(bytes memory data) internal pure returns (string memory) { | |
uint256 len = data.length; | |
if (len == 0) return ""; | |
// multiply by 4/3 rounded up | |
uint256 encodedLen = 4 * ((len + 2) / 3); | |
// Add some extra buffer at the end | |
bytes memory result = new bytes(encodedLen + 32); | |
bytes memory table = TABLE; | |
assembly { | |
let tablePtr := add(table, 1) | |
let resultPtr := add(result, 32) | |
for { | |
let i := 0 | |
} lt(i, len) { | |
} { | |
i := add(i, 3) | |
let input := and(mload(add(data, i)), 0xffffff) | |
let out := mload(add(tablePtr, and(shr(18, input), 0x3F))) | |
out := shl(8, out) | |
out := add( | |
out, | |
and(mload(add(tablePtr, and(shr(12, input), 0x3F))), 0xFF) | |
) | |
out := shl(8, out) | |
out := add( | |
out, | |
and(mload(add(tablePtr, and(shr(6, input), 0x3F))), 0xFF) | |
) | |
out := shl(8, out) | |
out := add( | |
out, | |
and(mload(add(tablePtr, and(input, 0x3F))), 0xFF) | |
) | |
out := shl(224, out) | |
mstore(resultPtr, out) | |
resultPtr := add(resultPtr, 4) | |
} | |
switch mod(len, 3) | |
case 1 { | |
mstore(sub(resultPtr, 2), shl(240, 0x3d3d)) | |
} | |
case 2 { | |
mstore(sub(resultPtr, 1), shl(248, 0x3d)) | |
} | |
mstore(result, encodedLen) | |
} | |
return string(result); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
// Source: | |
// https://github.com/ensdomains/ens-contracts/blob/master/contracts/ethregistrar/StringUtils.sol | |
pragma solidity >=0.8.4; | |
library StringUtils { | |
/** | |
* @dev Returns the length of a given string | |
* | |
* @param s The string to measure the length of | |
* @return The length of the input string | |
*/ | |
function strlen(string memory s) internal pure returns (uint) { | |
uint len; | |
uint i = 0; | |
uint bytelength = bytes(s).length; | |
for(len = 0; i < bytelength; len++) { | |
bytes1 b = bytes(s)[i]; | |
if(b < 0x80) { | |
i += 1; | |
} else if (b < 0xE0) { | |
i += 2; | |
} else if (b < 0xF0) { | |
i += 3; | |
} else if (b < 0xF8) { | |
i += 4; | |
} else if (b < 0xFC) { | |
i += 5; | |
} else { | |
i += 6; | |
} | |
} | |
return len; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: UNLICENSED | |
pragma solidity ^0.8.10; | |
import "hardhat/console.sol"; | |
import "@openzeppelin/contracts/utils/Counters.sol"; | |
interface InterfaceTicket { | |
function updateTicketDetails(uint _tId, string calldata _planName, uint _planId, uint _subscriptionCost, uint _subscriptionStart, uint _subscriptionEnd) external; | |
} | |
contract Plan{ | |
using Counters for Counters.Counter; | |
Counters.Counter private _tokenIds; | |
address Owner; | |
struct planDetails { | |
uint planId; | |
string planName; | |
uint planCost; | |
uint planStart; | |
uint planDuration; | |
uint planEnd; | |
bool planStarted; | |
uint planSubscribers; | |
} | |
// currency input is in wei (1 eth = 10^18 wei) | |
// planCost -> in wei | |
// time input is in days | |
// planStart -> After how many days from now you want this plan to activate for users to buy | |
// planEnd -> After how many days after planStart you want this plan to deactivate disabling users to buy | |
// planDuration -> Duration of the plan | |
// planStarted -> did the plan start?? | |
// Mapping of tokenId with planDetails struct | |
mapping(uint256 => planDetails) public plans; | |
// Mapping of address with bool for planControllers | |
mapping(address => bool) public planControllers; | |
address public ticketContractAddr; | |
constructor () { | |
Owner = msg.sender; | |
planControllers[Owner] = true; | |
console.log("Plan.sol contract is deployed"); | |
} | |
modifier onlyOwner { | |
require(msg.sender == Owner, "Only Owner can call this function"); | |
_; | |
} | |
function addPlanControllers(address _user) public onlyOwner { | |
planControllers[_user] = true; | |
} | |
function removePlanControllers(address _user) public onlyOwner { | |
planControllers[_user] = false; | |
} | |
modifier onlyPlanController { | |
require(planControllers[msg.sender], "Only PlanController can call this function"); | |
_; | |
} | |
// cannot be edited later because plan starts immediately after plan is created | |
function createPlan(string calldata _planName, uint _planCost, uint _planDuration) public onlyPlanController { | |
require(bytes(_planName).length != 0, "planName cannot be empty"); | |
require(_planDuration != 0, "planDuration cannot be 0 days"); | |
uint256 newTokenId = _tokenIds.current() + 1; | |
planDetails storage newPlan = plans[newTokenId]; | |
newPlan.planId = newTokenId; | |
newPlan.planName = _planName; | |
newPlan.planCost = _planCost; | |
newPlan.planStart = block.timestamp; | |
newPlan.planDuration = _planDuration * 1 days; | |
newPlan.planStarted = true; | |
_tokenIds.increment(); | |
} | |
// can be edited later as long as plan dint start | |
function createSpecialPlan(string calldata _planName, uint _planCost, uint _planStart, uint _planDuration, uint _planEnd) public onlyPlanController { | |
require(bytes(_planName).length != 0, "planName cannot be empty"); | |
require(_planDuration != 0, "planDuration cannot be 0 days"); | |
require(_planEnd != 0, "planEnd cannot be 0 days"); | |
uint256 newTokenId = _tokenIds.current() + 1; | |
planDetails storage newPlan = plans[newTokenId]; | |
newPlan.planId = newTokenId; | |
newPlan.planName = _planName; | |
newPlan.planCost = _planCost; | |
newPlan.planStart = block.timestamp + _planStart * 1 days; | |
newPlan.planDuration = _planDuration * 1 days; | |
newPlan.planEnd = newPlan.planStart + _planEnd * 1 days; | |
newPlan.planStarted = true; | |
_tokenIds.increment(); | |
} | |
function deletePlan(uint _planId) public onlyPlanController { | |
planDetails storage Plan = plans[_planId]; | |
require(Plan.planStarted == true, "Cannot delete a deleted Plan"); | |
Plan.planStarted = false; | |
} | |
// only SpecialPlans can be editted | |
function editPlan(uint _planId, string calldata _planName, uint _planCost, uint _planStart, uint _planDuration, uint _planEnd) public onlyPlanController { | |
planDetails storage Plan = plans[_planId]; | |
require(block.timestamp < Plan.planStart, "Plan cannot be edited because plan has already started"); | |
require(Plan.planStarted == true, "Cannot Edit a Deleted Plan"); | |
require(_planEnd !=0, "_planEnd cannot be 0"); | |
Plan.planName = _planName; | |
Plan.planCost = _planCost; | |
Plan.planDuration = _planDuration * 1 days; | |
Plan.planStart = block.timestamp + _planStart * 1 days; | |
Plan.planEnd = Plan.planStart + _planEnd * 1 days; | |
} | |
function getPlanDetails(uint _planId) public view returns (planDetails memory) { | |
return plans[_planId]; | |
} | |
function ticketContract(address _ticketContractAddr) public { | |
ticketContractAddr = _ticketContractAddr; | |
} | |
function buyPlan(uint _planId, uint _tId) external payable{ | |
planDetails storage Plan = plans[_planId]; | |
require(msg.value == Plan.planCost, "money sent != planCost"); | |
Plan.planSubscribers += 1; | |
string memory _planName = Plan.planName; | |
uint _subscriptionCost = Plan.planCost; | |
uint _subscriptionStart = block.timestamp; | |
uint _subscriptionEnd = _subscriptionStart + Plan.planDuration; | |
InterfaceTicket(ticketContractAddr).updateTicketDetails(_tId, _planName, _planId, _subscriptionCost, _subscriptionStart, _subscriptionEnd); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: UNLICENSED | |
pragma solidity ^0.8.10; | |
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol"; | |
import "@openzeppelin/contracts/token/ERC721/IERC721.sol"; | |
import "@openzeppelin/contracts/utils/Counters.sol"; | |
import { StringUtils } from "./libraries/StringUtils.sol"; | |
import { Base64 } from "./libraries/Base64.sol"; | |
contract Ticket is ERC721URIStorage { | |
using Counters for Counters.Counter; | |
Counters.Counter private _tokenIds; | |
struct ticketDetails { | |
uint ticketNum; | |
address subscriber; | |
string planName; | |
uint planId; | |
uint subscriptionCost; | |
uint subscriptionStart; | |
uint subscriptionEnd; | |
uint32 transferCount; | |
} | |
// Mapping of tokenId with ticketDetails struct | |
mapping(uint256 => ticketDetails) public tickets; | |
// Mapping of address to bool to check if this address has ticket or not | |
mapping(address => bool) public hasTicket; | |
// We'll be storing our NFT images on chain as SVGs | |
string svgPartOne = '<svg xmlns="http://www.w3.org/2000/svg" width="500" height="300"><path fill="url(#a)" d="M0 0h500v300H0z"/><defs><filter id="b" color-interpolation-filters="sRGB" filterUnits="userSpaceOnUse" height="300" width="500"><feDropShadow dx="0" dy="1" stdDeviation="2" flood-opacity=".225" width="0%" height="0%"/></filter></defs><svg x="10" y="5"><path filter="url(#b)" fill="#fff" d="M72.863 42.949a4.382 4.382 0 0 0-4.394 0l-10.081 6.032-6.85 3.934-10.081 6.032a4.382 4.382 0 0 1-4.394 0l-8.013-4.721a4.52 4.52 0 0 1-1.589-1.616 4.54 4.54 0 0 1-.608-2.187v-9.31a4.27 4.27 0 0 1 .572-2.208 4.25 4.25 0 0 1 1.625-1.595l7.884-4.59a4.382 4.382 0 0 1 4.394 0l7.884 4.59a4.52 4.52 0 0 1 1.589 1.616 4.54 4.54 0 0 1 .608 2.187v6.032l6.85-4.065v-6.032a4.27 4.27 0 0 0-.572-2.208 4.25 4.25 0 0 0-1.625-1.595L41.456 24.59a4.382 4.382 0 0 0-4.394 0l-14.864 8.655a4.25 4.25 0 0 0-1.625 1.595 4.273 4.273 0 0 0-.572 2.208v17.441a4.27 4.27 0 0 0 .572 2.208 4.25 4.25 0 0 0 1.625 1.595l14.864 8.655a4.382 4.382 0 0 0 4.394 0l10.081-5.901 6.85-4.065 10.081-5.901a4.382 4.382 0 0 1 4.394 0l7.884 4.59a4.52 4.52 0 0 1 1.589 1.616 4.54 4.54 0 0 1 .608 2.187v9.311a4.27 4.27 0 0 1-.572 2.208 4.25 4.25 0 0 1-1.625 1.595l-7.884 4.721a4.382 4.382 0 0 1-4.394 0l-7.884-4.59a4.52 4.52 0 0 1-1.589-1.616 4.53 4.53 0 0 1-.608-2.187v-6.032l-6.85 4.065v6.032a4.27 4.27 0 0 0 .572 2.208 4.25 4.25 0 0 0 1.625 1.595l14.864 8.655a4.382 4.382 0 0 0 4.394 0l14.864-8.655a4.545 4.545 0 0 0 2.198-3.803V55.538a4.27 4.27 0 0 0-.572-2.208 4.25 4.25 0 0 0-1.625-1.595l-14.993-8.786z"/></svg><defs><linearGradient id="a" x1="0" y1="0" x2="100%" y2="0%" gradientUnits="userSpaceOnUse" gradientTransform="rotate(55)"><stop offset=".1" stop-color="#8A2387" stop-opacity=".98"/><stop offset=".8" stop-color="#E94057" stop-opacity=".98"/><stop offset="1" stop-color="#F27121" stop-opacity=".98"/></linearGradient></defs><svg x="385" y="190" width="90" height="90"><path filter="url(#b)" fill="#fff" d="M53.58 5.449a30.484 30.484 0 0 0-7.916 1.044c-8.648 2.32-20.517 9.648-29.35 18.704C7.481 34.253 1.834 44.89 4.19 53.696c2.413 9.015 11.38 17.54 22.072 23.065 10.692 5.526 23.024 7.987 31.481 5.719C66.2 80.211 74.681 72.205 80.27 62.336c5.589-9.868 8.246-21.498 5.806-30.613C81.847 15.914 68.183 5.445 53.581 5.449zm-2.439 5.652c4.202.062 7.556 1.45 9.069 4.142 3.026 5.384-2.461 13.959-12.261 19.155-9.799 5.195-20.199 5.038-23.225-.346s2.467-13.96 12.266-19.155c4.9-2.598 9.949-3.858 14.15-3.796zm30.932 21.561c4.564 17.053-9.89 41.975-25.856 46.258-9.708 2.605-23.929-1.361-34.612-8.701 24.023 10.918 56.274 2.1 60.469-37.557zm-65.066 3.505a6.634 6.634 0 0 1 6.636 6.636 6.638 6.638 0 1 1-13.277 0 6.638 6.638 0 0 1 6.641-6.636z"/></svg><text x="315" y="60" font-size="40" fill="#fff" filter="url(#b)" font-family="Plus Jakarta Sans,DejaVu Sans,Noto Color Emoji,Apple Color Emoji,sans-serif" font-weight="bold">subTree</text><text x="183" y="93" font-size="23" fill="#fff" filter="url(#b)" font-family="Plus Jakarta Sans,DejaVu Sans,Noto Color Emoji,Apple Color Emoji,sans-serif" font-weight="bold">Subscriptions Made Easy</text><text x="25" y="230" font-size="38" fill="#fff" filter="url(#b)" font-family="Plus Jakarta Sans,DejaVu Sans,Noto Color Emoji,Apple Color Emoji,sans-serif" font-weight="bold">DinoLabs</text><text x="25" y="270" font-size="28" fill="#fff" filter="url(#b)" font-family="Plus Jakarta Sans,DejaVu Sans,Noto Color Emoji,Apple Color Emoji,sans-serif" font-weight="bold">#'; | |
string svgPartTwo = '</text></svg>'; | |
constructor() payable ERC721("Tickets", "TICK") { | |
console.log("Ticket.sol deployed"); | |
} | |
modifier onlyTicketOwner(uint _tId) { | |
ticketDetails storage readTicketDetails = tickets[_tId]; | |
require(readTicketDetails.subscriber == msg.sender, "only owner of the ticket can call this function"); | |
_; | |
} | |
function register() public { | |
require(hasTicket[msg.sender] == false, "User already has Ticket"); | |
uint256 newTokenId = _tokenIds.current() + 1; | |
ticketDetails storage newToken = tickets[newTokenId]; | |
newToken.ticketNum = newTokenId; | |
newToken.subscriber = msg.sender; | |
newToken.planName = "No Active Plan"; | |
string memory displayTokenNum = formatedNum(newToken.ticketNum); | |
string memory finalSvg = string(abi.encodePacked(svgPartOne, displayTokenNum, svgPartTwo)); | |
string memory json = Base64.encode( | |
bytes( | |
string( | |
abi.encodePacked( | |
'{"name": DinoLabs", "tickedID": "', | |
displayTokenNum, | |
'", "description": "SubTree - Subscription Made Easy", "image": "data:image/svg+xml;base64,', | |
Base64.encode(bytes(finalSvg)), | |
'"}' | |
) | |
) | |
) | |
); | |
string memory finalTokenUri = string( abi.encodePacked("data:application/json;base64,", json)); | |
console.log("\n--------------------------------------------------------"); | |
console.log("finalTokenURI = ",finalTokenUri); | |
console.log("--------------------------------------------------------\n"); | |
_safeMint(msg.sender, newTokenId); | |
_setTokenURI(newTokenId, finalTokenUri); | |
hasTicket[msg.sender] = true; | |
console.log("An ticket w/ ID #%s has been minted to %s", displayTokenNum, msg.sender); | |
_tokenIds.increment(); | |
} | |
function formatedNum(uint name) internal pure returns(string memory) { | |
string memory tId = uint2str(name); | |
uint256 length = StringUtils.strlen(tId); | |
string memory mTikId; | |
if (length > 3) { | |
mTikId = tId; | |
} else if (length > 2) { | |
mTikId = string(abi.encodePacked("0", tId)); | |
} else if (length > 1) { | |
mTikId = string(abi.encodePacked("00", tId)); | |
} else if (length > 0) { | |
mTikId = string(abi.encodePacked("000", tId)); | |
} | |
return string(mTikId); | |
} | |
function uint2str(uint _i) internal pure returns (string memory) { | |
if (_i == 0) { | |
return "0"; | |
} | |
uint j = _i; | |
uint len; | |
while (j != 0) { | |
len++; | |
j /= 10; | |
} | |
bytes memory bstr = new bytes(len); | |
uint k = len; | |
while (_i != 0) { | |
k = k-1; | |
uint8 temp = (48 + uint8(_i - _i / 10 * 10)); | |
bytes1 b1 = bytes1(temp); | |
bstr[k] = b1; | |
_i /= 10; | |
} | |
return string(bstr); | |
} | |
function transferTicket(uint _tId, address transferTo) public onlyTicketOwner(_tId) { | |
ticketDetails storage readTicketDetails = tickets[_tId]; | |
require(readTicketDetails.transferCount < 2, "Max Transfers claimed for this token"); | |
safeTransferFrom(msg.sender, transferTo, _tId); | |
readTicketDetails.transferCount += 1; | |
readTicketDetails.subscriber = transferTo; | |
hasTicket[msg.sender] = false; | |
hasTicket[transferTo] = true; | |
} | |
function updateTicketDetails(uint _tId, string calldata _planName, uint _planId, uint _subscriptionCost, uint _subscriptionStart, uint _subscriptionEnd) external { | |
ticketDetails storage readTicketDetails = tickets[_tId]; | |
require(readTicketDetails.subscriptionEnd < block.timestamp , "ticket already subscribed to a plan"); | |
readTicketDetails.planName = _planName; | |
readTicketDetails.planId = _planId; | |
readTicketDetails.subscriptionCost = _subscriptionCost; | |
readTicketDetails.subscriptionStart = _subscriptionStart; | |
readTicketDetails.subscriptionEnd = _subscriptionEnd; | |
} | |
function getTicketDetails(uint _tId) public view returns (ticketDetails memory) { | |
return tickets[_tId]; | |
} | |
function getSubscriber(uint _tId) public view returns (address) { | |
return tickets[_tId].subscriber; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Right click on the script name and hit "Run" to execute | |
(async () => { | |
try { | |
console.log('Running deployWithEthers script...') | |
const contractName = 'Storage' // Change this for other contract | |
const constructorArgs = [] // Put constructor args (if any) here for your contract | |
// Note that the script needs the ABI which is generated from the compilation artifact. | |
// Make sure contract is compiled and artifacts are generated | |
const artifactsPath = `browser/contracts/artifacts/${contractName}.json` // Change this for different path | |
const metadata = JSON.parse(await remix.call('fileManager', 'getFile', artifactsPath)) | |
// 'web3Provider' is a remix global variable object | |
const signer = (new ethers.providers.Web3Provider(web3Provider)).getSigner() | |
let factory = new ethers.ContractFactory(metadata.abi, metadata.data.bytecode.object, signer); | |
let contract = await factory.deploy(...constructorArgs); | |
console.log('Contract Address: ', contract.address); | |
// The contract is NOT deployed yet; we must wait until it is mined | |
await contract.deployed() | |
console.log('Deployment successful.') | |
} catch (e) { | |
console.log(e.message) | |
} | |
})() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Right click on the script name and hit "Run" to execute | |
(async () => { | |
try { | |
console.log('Running deployWithWeb3 script...') | |
const contractName = 'Storage' // Change this for other contract | |
const constructorArgs = [] // Put constructor args (if any) here for your contract | |
// Note that the script needs the ABI which is generated from the compilation artifact. | |
// Make sure contract is compiled and artifacts are generated | |
const artifactsPath = `browser/contracts/artifacts/${contractName}.json` // Change this for different path | |
const metadata = JSON.parse(await remix.call('fileManager', 'getFile', artifactsPath)) | |
const accounts = await web3.eth.getAccounts() | |
let contract = new web3.eth.Contract(metadata.abi) | |
contract = contract.deploy({ | |
data: metadata.data.bytecode.object, | |
arguments: constructorArgs | |
}) | |
const newContractInstance = await contract.send({ | |
from: accounts[0], | |
gas: 1500000, | |
gasPrice: '30000000000' | |
}) | |
console.log('Contract deployed at address: ', newContractInstance.options.address) | |
} catch (e) { | |
console.log(e.message) | |
} | |
})() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity >=0.7.0 <0.9.0; | |
import "remix_tests.sol"; // this import is automatically injected by Remix. | |
import "../contracts/3_Ballot.sol"; | |
contract BallotTest { | |
bytes32[] proposalNames; | |
Ballot ballotToTest; | |
function beforeAll () public { | |
proposalNames.push(bytes32("candidate1")); | |
ballotToTest = new Ballot(proposalNames); | |
} | |
function checkWinningProposal () public { | |
ballotToTest.vote(0); | |
Assert.equal(ballotToTest.winningProposal(), uint(0), "proposal at index 0 should be the winning proposal"); | |
Assert.equal(ballotToTest.winnerName(), bytes32("candidate1"), "candidate1 should be the winner name"); | |
} | |
function checkWinninProposalWithReturnValue () public view returns (bool) { | |
return ballotToTest.winningProposal() == 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment