Skip to content

Instantly share code, notes, and snippets.

@DaveAppleton
Last active August 25, 2017 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DaveAppleton/a591c2b55afb3cf567b156559d8096c5 to your computer and use it in GitHub Desktop.
Save DaveAppleton/a591c2b55afb3cf567b156559d8096c5 to your computer and use it in GitHub Desktop.
smart contract to encode ABI keys
pragma solidity ^0.4.11;
contract ABIparams{
// call this with the function name and types but not variable names
// make sure there are no spaces
// e.g. "transfer(address,uint256)"
function hash(string str) public constant returns (bytes4) {
return (bytes4(sha3(str)));
}
// encode an addess by adding the necessary number of zeros for it to
// be used in an ABI encoded parameter
function addressVal(address a) public constant returns (bytes32) {
return bytes32(a);
}
// display an integer number in hex so that it can
// be used in an ABI encoded parameter
function num(uint256 n) public constant returns (bytes32) {
return bytes32(n);
}
// show an integer number as that number of ether so that it can
// be used in an ABI encoded parameter
function numInEther(uint256 n) public constant returns (bytes32) {
return num(n * 1 ether);
}
// get ABI code for transfer function where you specify the value exactly
function transferInUnits(address a, uint256 v)public constant returns (bytes4 hv,bytes32 av,bytes32 vv) {
hv = hash("transfer(address,uint256)");
av = addressVal(a);
vv = num(v);
return;
}
// get ABI code for transfer function where you specify the value in tokens
// which has 'decimal' places (eg 1 ether -> decimal = 18)
function transferInDecimals(address a, uint256 v, uint256 decimals)public constant returns (bytes4 hv,bytes32 av,bytes32 vv) {
hv = hash("transfer(address,uint256)");
av = addressVal(a);
vv = num(v * 10 ** decimals);
return;
}
// get ABI code for approve function where you specify the value exactly
function approve(address a, uint256 v) public constant returns (bytes4 hv,bytes32 av, bytes32 vv) {
hv = hash("approve(address,uint256)");
av = addressVal(a);
vv = num(v);
return;
}
// get ABI code for approve function where you specify the value in tokens
// which has 'decimal' places (eg 1 ether -> decimal = 18)
function approveInDecimals(address a, uint256 v, uint256 decimals)public constant returns (bytes4 hv,bytes32 av,bytes32 vv) {
hv = hash("approve(address,uint256)");
av = addressVal(a);
vv = num(v * 10 ** decimals);
return;
}
// get ABI code for transferFrom function where you specify the value exactly
function transferFromInUnits(address from, address to, uint256 v)public constant returns (bytes4 hv,bytes32 av1, bytes32 av2,bytes32 vv) {
hv = hash("transferFrom(address,address,uint256)");
av1 = addressVal(from);
av2 = addressVal(to);
vv = num(v);
return;
}
// get ABI code for transferFrom function where you specify the value in tokens
// which has 'decimal' places (eg 1 ether -> decimal = 18)
function transferFromInDecimals(address from, address to, uint256 v, uint256 decimals)public constant returns (bytes4 hv,bytes32 av1, bytes32 av2,bytes32 vv) {
hv = hash("transferFrom(address,address,uint256)");
av1 = addressVal(from);
av2 = addressVal(to);
vv = num(v * 10 ** decimals);
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment