Git Product home page Git Product logo

poor-mans-abi-decode's Introduction

🤠✂️ Poor Man's DecodeABI()

Normally you have to cut up a byte array using a bunch of lines of assembly.

Here is a way to easily split up a byte array by 'calling' address(this) in Solidity!

screencast.png

Does DecodeABI() exist in Solidity yet? I don't think so, but here's a trick to do it easily...

example

Let's say we have this function in solidity:

function hello(bytes32 message,uint256 number,address addy) public returns (bool){
  emit Hello(message,number,addy);
  return true;
}
event Hello(bytes32 message,uint256 number,address addy);

Sure, we can call this directly and pass in the given arguments.

BUT, what if we want to pack the arguments into a byte array for standardization with flexibility?

A friend of mine, Steve Ellis, taught me this cool trick we called the "poor man's decode abi":

function abstracted(bytes data) public returns (bool success){
  uint256 value = 0;
  address to = address(this);
  uint256 gas = msg.gas;
  assembly {
    success := call(gas, to, value, add(data, 0x20), mload(data), 0, 0)
  }
}

For reference, here is how you do the encoding of the byte array in javascript:

contracts.Example.hello(
  this.state.web3.utils.toHex(this.state.broadcastText),
  this.state.broadcastNumber,
  this.state.address
).encodeABI()

(if you aren't using clevis and dapparatus, you would do 'contractname.methods.hello...')

poor-mans-abi-decode's People

Contributors

austintgriffith avatar

Stargazers

Darcy Turk avatar Will Pankiewicz avatar Amnot avatar Steve Ellis avatar Amar Singh avatar

Watchers

Steve Ellis avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.