Reverse String

This example shows how to use our eni_revserse.so to reverse a string on Lity contract.’

Compiler StringReverse Contract

There is a simple contract that leverage ENI to reverse a string.

pragma lity ^1.2.4;
contract StringReverse {

      function test() public returns (string) {
              string memory ret;
              ret = eni("reverse", "Hello, world");
              return ret;
      }

      function reverse(string src) public returns (string) {
              string memory ret;
              ret = eni("reverse", src);
              return ret;
      }
}

and we could compile it using lityc:

$ mkdir output
$ lityc --abi --bin -o output StringReverse.sol
$ cat output/StringReverse.abi
[{"constant":true,"inputs":[{"name":"src","type":"string"}],"name":"reverse","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":true,"inputs":[],"name":"test","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"pure","type":"function"}]
$ cat output/StringReverse.bin
608060405234801561001057600080fd5b5061041a806100206000396000f30060806040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063064767aa14610051578063f8a8fd6d14610133575b600080fd5b34801561005d57600080fd5b506100b8600480360381019080803590602001908201803590602001908080601f01602080910402602001604051908101604052809392919081815260200183838082843782019150505050505091929192905050506101c3565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100f85780820151818401526020810190506100dd565b50505050905090810190601f1680156101255780820d805160018360200d6101000a0d1916815260200191505b509250505060405180910390f35b34801561013f57600080fd5b506101486102ef565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561018857808201518184015260208101905061016d565b50505050905090810190601f1680156101b55780820d805160018360200d6101000a0d1916815260200191505b509250505060405180910390f35b60608060405160206040519081016040526001905260206040519081016040527f0400000000000000000000000000000000000000000000000000000000000000905260206040519081016040526001905260206040519081016040527f04000000000000000000000000000000000000000000000000000000000000009052602060405190810160405280600090528480516020019081601f0160209004602002604051908101604052905b6020831015156102955780518252602082019150602081019050602083039250610270565b6001836020036101000a0d801982511681845116808217855250505050505080604051819003602090039052907f7265766572736500000000000000000000000000000000000000000000000000f5905080915050919050565b60608060405160206040519081016040526001905260206040519081016040527f0400000000000000000000000000000000000000000000000000000000000000905260206040519081016040526001905260206040519081016040527f04000000000000000000000000000000000000000000000000000000000000009052602060405190810160405280600090526040805190810160405280600c90526020017f48656c6c6f2c20776f726c64000000000000000000000000000000000000000081525080604051819003602090039052907f7265766572736500000000000000000000000000000000000000000000000000f5905080915050905600a165627a7a72305820a1c239c84518355b762a293aaac7ce64779685d62750506e782b51360c623f620029

Deploy contract to Travis locally

After we get contract ABI and bytecode, we could deploy it to Travis chain.

# Get Travis console
travis attach http://127.0.0.1:8545

# Deploy contract (in Travis console)
personal.unlockAccount(cmt.accounts[0], '1234');
abi = [{"constant":true,"inputs":[{"name":"src","type":"string"}],"name":"reverse","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":true,"inputs":[],"name":"test","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"pure","type":"function"}]
bytecode = "0x608060405234801561001057600080fd5b5061041a806100206000396000f30060806040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063064767aa14610051578063f8a8fd6d14610133575b600080fd5b34801561005d57600080fd5b506100b8600480360381019080803590602001908201803590602001908080601f01602080910402602001604051908101604052809392919081815260200183838082843782019150505050505091929192905050506101c3565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100f85780820151818401526020810190506100dd565b50505050905090810190601f1680156101255780820d805160018360200d6101000a0d1916815260200191505b509250505060405180910390f35b34801561013f57600080fd5b506101486102ef565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561018857808201518184015260208101905061016d565b50505050905090810190601f1680156101b55780820d805160018360200d6101000a0d1916815260200191505b509250505060405180910390f35b60608060405160206040519081016040526001905260206040519081016040527f0400000000000000000000000000000000000000000000000000000000000000905260206040519081016040526001905260206040519081016040527f04000000000000000000000000000000000000000000000000000000000000009052602060405190810160405280600090528480516020019081601f0160209004602002604051908101604052905b6020831015156102955780518252602082019150602081019050602083039250610270565b6001836020036101000a0d801982511681845116808217855250505050505080604051819003602090039052907f7265766572736500000000000000000000000000000000000000000000000000f5905080915050919050565b60608060405160206040519081016040526001905260206040519081016040527f0400000000000000000000000000000000000000000000000000000000000000905260206040519081016040526001905260206040519081016040527f04000000000000000000000000000000000000000000000000000000000000009052602060405190810160405280600090526040805190810160405280600c90526020017f48656c6c6f2c20776f726c64000000000000000000000000000000000000000081525080604051819003602090039052907f7265766572736500000000000000000000000000000000000000000000000000f5905080915050905600a165627a7a72305820a1c239c84518355b762a293aaac7ce64779685d62750506e782b51360c623f620029"
contract = web3.cmt.contract(abi);
c = contract.new(
  {
    from: web3.cmt.accounts[0],
    data: bytecode,
    gas: "4700000"
  },
  function(e, contract) {
    console.log("contract address: " + contract.address);
    console.log("transactionHash: " + contract.transactionHash);
  }
);

Use contract to reverse a string

# Use test() function to show reverse of "Hello, world"
> c.test.call();
"dlrow ,olleH"

# Use reverse() function to reverse a string
> c.reverse.call('abcdef');
"fedcba"