Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Created March 24, 2019 02:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miguelmota/82b2d9ad56749b1c467990c6c654085f to your computer and use it in GitHub Desktop.
Save miguelmota/82b2d9ad56749b1c467990c6c654085f to your computer and use it in GitHub Desktop.
Solidity calldata example
pragma solidity >=0.4.21 <0.6.0;
pragma experimental ABIEncoderV2;
import "zos-lib/contracts/Initializable.sol";
import "./SafeMath.sol";
import "./Roles.sol";
import "./Polls.sol";
contract MyContract is Initializable {
using SafeMath for uint256;
using Polls for Polls.Poll;
using Roles for Roles.Role;
Polls.Poll private polls;
Roles.Role private superowners;
constructor() public {
superowners.add(msg.sender);
}
modifier onlySelf {
require(msg.sender == address(this));
_;
}
modifier onlySuperOwner {
require(superowners.has(msg.sender));
_;
}
function addSuperOwner(address owner) public {
bytes32 key = keccak256(
abi.encode("addSuperOwner", owner)
);
require(polls.isNotActive(key));
bytes memory data = abi.encodeWithSelector(
bytes4(
keccak256("_addSuperOwner(address)")
),
owner
);
bytes memory data = abi.encodeWithSelector(
this._addSuperOwner.selector,
owner
);
polls.create(
key,
data,
address(this)
);
}
function _addSuperOwner(address owner) public onlySelf {
superowners.add(owner);
}
function isSuperOwner(address owner) public returns (bool) {
return superowners.has(owner);
}
function removeSuperOwner() public {
}
function execute(bytes32 key) public {
polls.execute(key);
}
}
pragma solidity >=0.4.21 <0.6.0;
library Polls {
struct PollItem {
bytes32 key;
bytes data;
bool active;
bool executed;
uint256 executedDate;
address caller;
uint256 threshold;
uint256 totalVoters;
}
struct Poll {
mapping (bytes32 => PollItem) polls;
}
function create(Poll storage polls, bytes32 key, bytes memory data, address caller) public {
PollItem memory poll;
poll.key = key;
poll.data = data;
poll.caller = caller;
poll.active = true;
polls.polls[key] = poll;
}
function execute(Poll storage polls, bytes32 key) public {
polls.polls[key].executed = true;
polls.polls[key].active = false;
bool result;
bytes memory data;
(result, data) = polls.polls[key].caller.call(polls.polls[key].data);
if (!result) {
revert("call failed");
}
}
function isNotActive(Poll storage polls, bytes32 key) public returns (bool) {
return polls.polls[key].active == false;
}
}
const MyContract = artifacts.require('./MyContract.sol');
contract('MyContract', accounts => {
let instance = null;
before('instantiate contract', async () => {
try {
instance = await MyContract.new();
} catch (error) {
// console.error(error)
assert.equal(error, undefined);
}
});
it('test callcode', async () => {
const newOwner = '0x8d92c638e65c6cedad89a6655bc7d2b14bfd7ce2'
const meta = await instance.addSuperOwner(newOwner)
console.log(JSON.stringify(meta, null, 2))
const key = web3.utils.keccak256(web3.eth.abi.encodeParameters(['string','address'], ["addSuperOwner", newOwner]))
console.log(key)
await instance.execute(key)
//const id = await instance.pollid.call()
//console.log(id)
const b = await instance.isSuperOwner.call('0x4ccA5F2f01746B1c13ca7a3Dab0462d225795D3A')
console.log(b)
})
it.skip('add assets', async () => {
// const account = accounts[0];
try {
const assets = [{
id: 2,
state: 2,
valuation: 50,
fingerprint: '0xabcd',
countdown: moment().unix(),
}, {
id: 3,
state: 1,
valuation: 70,
fingerprint: '0x1234',
countdown: moment().unix() + 1000,
}];
const result = await instance.addAssets(assets);
console.log(result);
const data = await web3.eth.getTransaction(result.tx);
console.log(data);
// const results = await instance.assets.call(2)
// console.log(results)
} catch (error) {
// console.error(error)
assert.equal(error, undefined);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment