Skip to content

Instantly share code, notes, and snippets.

@yuyasugano
yuyasugano / Substrate module sample
Created April 25, 2019 08:33
Substrate token runtime
/// A runtime module template with necessary imports
/// Feel free to remove or edit this file as needed.
/// If you change the name of this file, make sure to update its references in runtime/src/lib.rs
/// If you remove this file, you can remove those references
/// For more guidance on Substrate modules, see the example module
/// https://github.com/paritytech/substrate/blob/master/srml/example/src/lib.rs
use rstd::prelude::*;
use support::{decl_module, decl_storage, decl_event, dispatch::Result, StorageValue, StorageMap, ensure};
@yuyasugano
yuyasugano / Substrate token.rs runtime
Created April 28, 2019 04:25
Substrate TCR sample
/// runtime module implementing the ERC20 token interface
/// with added lock and unlock functions for staking in TCR runtime
/// implements a custom type `TokenBalance` for representing account balance
/// `TokenBalance` type is exactly the same as the `Balance` type in `balances` SRML module
use rstd::prelude::*;
use parity_codec::Codec;
use support::{dispatch::Result, StorageMap, Parameter, StorageValue, decl_storage, decl_module, decl_event, ensure};
use system::{self, ensure_signed};
use runtime_primitives::traits::{CheckedSub, CheckedAdd, Member, SimpleArithmetic, As};
@yuyasugano
yuyasugano / SLCR Voting Changes
Last active May 11, 2019 05:44
SLCR Voting Commit and Reveal
/**
* @notice Commits vote using hash of choice and secret salt to conceal vote until reveal
* @param _pollID Integer identifier associated with target poll
* @param _secretHash Commit keccak256 hash of voter's choice and salt
* @param _numTokens The number of tokens to be commited towards the target poll
* @param _prevPollID The ID of the poll that the user has voted the maximum number of
* tokens in which is still less than or equal to numTokens
*/
// To commit a user must hold enough voting rights
require(voteTokenBalance[msg.sender] >= _numTokens, "Cannot commit because of shortage of ERC20 tokens");
@yuyasugano
yuyasugano / SLCR Voting Modifications
Created May 11, 2019 05:48
SLCR Voting voting rights and rescue
/**
* @notice Loads _numTokens ERC20 tokens into the voting contract for one-to-one voting rights
* @dev Assumes that msg.sender has approved voting contract to spend on their behalf
* @param _numTokens The number of votingTokens desired in exchange for ERC20 tokens
*/
function requestVotingRights(uint256 _numTokens) public {
require(token.balanceOf(msg.sender) >= _numTokens, "Cannot stake more than you have");
voteTokenBalance[msg.sender] = voteTokenBalance[msg.sender].add(_numTokens);
// Transfer tokens to voting contract
@yuyasugano
yuyasugano / Truffle Mocha MetaCoin describe
Last active May 21, 2019 10:42
Truffle Mocha MetaCoin Test refactoring
var MetaCoin = artifacts.require("./MetaCoin.sol");
contract('MetaCoin', function(accounts) {
describe('Token', function() {
it("should put 10000 MetaCoin in the first account", function() {
return MetaCoin.deployed().then(function(instance) {
return instance.getBalance.call(accounts[0]);
}).then(function(balance) {
assert.equal(balance.valueOf(), 10000, "10000 wasn't in the first account");
});
@yuyasugano
yuyasugano / Truffle Mocha MetaCoin async
Last active May 21, 2019 10:46
Truffle Mocha MetaCoin Test refactoring
var MetaCoin = artifacts.require("./MetaCoin.sol");
contract('MetaCoin', (accounts) => {
describe('Token', () => {
it("should put 10000 MetaCoin in the first account", async () => {
let instance = await MetaCoin.deployed();
let alice = accounts[0];
let balance = await instance.getBalance.call(alice);
assert.equal(balance.valueOf(), 10000, "10000 wasn't in the first account");
});
@yuyasugano
yuyasugano / Truffle Mocha MetaCoin each
Created May 21, 2019 11:15
Truffle Mocha MetaCoin Test refactoring
var MetaCoin = artifacts.require("./MetaCoin.sol");
contract('MetaCoin', (accounts) => {
describe('Token', () => {
let balance;
let instance;
const [alice, bob] = accounts;
before(async () => {
instance = await MetaCoin.deployed();
@yuyasugano
yuyasugano / Truffle Mocha MetaCoin library
Created May 21, 2019 11:48
Truffle Mocha MetaCoin Test refactoring
const MetaCoin = artifacts.require("./MetaCoin.sol");
const utils = require('./utils.js');
contract('MetaCoin', (accounts) => {
describe('Token', () => {
let balance;
let instance;
const [alice, bob] = accounts;
before(async () => {
@yuyasugano
yuyasugano / Truffle Mocha MetaCoin revert
Created May 22, 2019 09:55
Truffle Mocha MetaCoin Test refactoring
const MetaCoin = artifacts.require("./MetaCoin.sol");
const utils = require('./utils.js');
contract('MetaCoin', (accounts) => {
describe('Token', () => {
let balance;
let instance;
const [alice, bob] = accounts;
before(async () => {
@yuyasugano
yuyasugano / Truffle Mocha MetaCoin final
Created May 22, 2019 10:22
Truffle Mocha MetaCoin Test refactoring
const MetaCoin = artifacts.require("./MetaCoin.sol");
const utils = require('./utils.js');
contract('MetaCoin', (accounts) => {
describe('Token', () => {
let balance;
let instance;
const [alice, bob] = accounts;
before(async () => {