Merge pull request #795 from ethereum/codingTuple

decode / encode tuple
pull/7/head
yann300 7 years ago committed by GitHub
commit 4f664fc7fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      remix-lib/src/execution/txFormat.js
  2. 10
      remix-lib/src/execution/txHelper.js
  3. 3
      remix-lib/src/execution/txListener.js
  4. 47
      remix-lib/test/txFormat.js

@ -365,7 +365,8 @@ module.exports = {
var outputTypes = []
for (i = 0; i < fnabi.outputs.length; i++) {
outputTypes.push(fnabi.outputs[i].type)
var type = fnabi.outputs[i].type
outputTypes.push(type === 'tuple' ? helper.makeFullTupleTypeDefinition(fnabi.outputs[i]) : type)
}
if (!response.length) response = new Uint8Array(32 * fnabi.outputs.length) // ensuring the data is at least filled by 0 cause `AbiCoder` throws if there's not engouh data

@ -2,12 +2,20 @@
var ethers = require('ethers')
module.exports = {
makeFullTupleTypeDefinition: function (typeDef) {
if (typeDef && typeDef.type === 'tuple' && typeDef.components) {
var innerTypes = typeDef.components.map((innerType) => innerType.type)
return 'tuple(' + innerTypes.join(',') + ')'
}
return typeDef.type
},
encodeParams: function (funABI, args) {
var types = []
if (funABI.inputs && funABI.inputs.length) {
for (var i = 0; i < funABI.inputs.length; i++) {
var type = funABI.inputs[i].type
types.push(type)
types.push(type === 'tuple' ? this.makeFullTupleTypeDefinition(funABI.inputs[i]) : type)
if (args.length < types.length) {
args.push('')
}

@ -329,7 +329,8 @@ class TxListener {
var inputTypes = []
for (var i = 0; i < abi.inputs.length; i++) {
inputTypes.push(abi.inputs[i].type)
var type = abi.inputs[i].type
inputTypes.push(type === 'tuple' ? txHelper.makeFullTupleTypeDefinition(abi.inputs[i]) : type)
}
var abiCoder = new ethers.utils.AbiCoder()
var decoded = abiCoder.decode(inputTypes, data)

@ -2,6 +2,7 @@
var tape = require('tape')
var txFormat = require('../src/execution/txFormat')
var txHelper = require('../src/execution/txHelper')
var util = require('../src/util')
var compiler = require('solc')
var compilerInput = require('../src/helpers/compilerHelper').compilerInput
var executionContext = require('../src/execution/execution-context')
@ -14,7 +15,7 @@ tape('ContractParameters - (TxFormat.buildData) - format input parameters', func
output = JSON.parse(output)
var contract = output.contracts['test.sol']['uintContractTest']
context = { output, contract }
var bytecode = '608060405234801561001057600080fd5b50610111806100206000396000f300608060405260043610603f576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680634b521953146044575b600080fd5b348015604f57600080fd5b50609660048036038101908080359060200190929190803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803590602001909291905050506098565b005b8260008190555081600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505050505600a165627a7a7230582037f597c9a62fb1df1e054664a26243a8cde5c0122020c0b178fd655fff8b10f70029'
var bytecode = '608060405234801561001057600080fd5b50610111806100206000396000f300608060405260043610603f576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680634b521953146044575b600080fd5b348015604f57600080fd5b50609660048036038101908080359060200190929190803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803590602001909291905050506098565b005b8260008190555081600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505050505600a165627a7a723058209f014353c529016497846216787d1cdca7fe9116c4c1acc2218a45aae9fe6e520029'
t.test('(TxFormat.buildData)', function (st) {
st.plan(3)
testWithInput(st, '123123, "0xf7a10e525d4b168f45f74db1b61f63d3e7619ea8", "34"', bytecode + '000000000000000000000000000000000000000000000000000000000001e0f3000000000000000000000000f7a10e525d4b168f45f74db1b61f63d3e7619ea80000000000000000000000000000000000000000000000000000000000000022')
@ -95,7 +96,7 @@ function testLinkLibrary2 (st, callbackDeployLibraries) {
'lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2': '0xf7a10e525d4b168f45f74db1b61f63d3e7619e33'
}
}
var data = '608060405234801561001057600080fd5b5061026b806100206000396000f300608060405260043610610041576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680636d4ce63c14610046575b600080fd5b34801561005257600080fd5b5061005b61005d565b005b73f7a10e525d4b168f45f74db1b61f63d3e7619e116344733ae16040518163ffffffff167c010000000000000000000000000000000000000000000000000000000002815260040160006040518083038186803b1580156100bd57600080fd5b505af41580156100d1573d6000803e3d6000fd5b5050505073f7a10e525d4b168f45f74db1b61f63d3e7619e336344733ae16040518163ffffffff167c010000000000000000000000000000000000000000000000000000000002815260040160006040518083038186803b15801561013557600080fd5b505af4158015610149573d6000803e3d6000fd5b5050505073f7a10e525d4b168f45f74db1b61f63d3e7619e336344733ae16040518163ffffffff167c010000000000000000000000000000000000000000000000000000000002815260040160006040518083038186803b1580156101ad57600080fd5b505af41580156101c1573d6000803e3d6000fd5b5050505073f7a10e525d4b168f45f74db1b61f63d3e7619e116344733ae16040518163ffffffff167c010000000000000000000000000000000000000000000000000000000002815260040160006040518083038186803b15801561022557600080fd5b505af4158015610239573d6000803e3d6000fd5b505050505600a165627a7a7230582090b0bbf59fc9b1f13331039529d287ce9a9e712ce22dc71025510293d18104bc0029'
var data = '608060405234801561001057600080fd5b5061026b806100206000396000f300608060405260043610610041576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680636d4ce63c14610046575b600080fd5b34801561005257600080fd5b5061005b61005d565b005b73f7a10e525d4b168f45f74db1b61f63d3e7619e116344733ae16040518163ffffffff167c010000000000000000000000000000000000000000000000000000000002815260040160006040518083038186803b1580156100bd57600080fd5b505af41580156100d1573d6000803e3d6000fd5b5050505073f7a10e525d4b168f45f74db1b61f63d3e7619e336344733ae16040518163ffffffff167c010000000000000000000000000000000000000000000000000000000002815260040160006040518083038186803b15801561013557600080fd5b505af4158015610149573d6000803e3d6000fd5b5050505073f7a10e525d4b168f45f74db1b61f63d3e7619e336344733ae16040518163ffffffff167c010000000000000000000000000000000000000000000000000000000002815260040160006040518083038186803b1580156101ad57600080fd5b505af41580156101c1573d6000803e3d6000fd5b5050505073f7a10e525d4b168f45f74db1b61f63d3e7619e116344733ae16040518163ffffffff167c010000000000000000000000000000000000000000000000000000000002815260040160006040518083038186803b15801561022557600080fd5b505af4158015610239573d6000803e3d6000fd5b505050505600a165627a7a72305820006fbc873d7822d8c26cfbdfeb92ee52e618dd5d048cfb6c6f7a313ec53dd16d0029'
var deployMsg = ['creation of library test.sol:lib1 pending...',
'creation of library test.sol:lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2 pending...']
@ -161,6 +162,28 @@ tape('test fallback function', function (t) {
})
})
tape('test abiEncoderV2', function (t) {
var functionId = '0x56d89238'
var encodedData = '0x000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000170000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000042ed123b0bd8203c2700000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000090746573745f737472696e675f746573745f737472696e675f746573745f737472696e675f746573745f737472696e675f746573745f737472696e675f746573745f737472696e675f746573745f737472696e675f746573745f737472696e675f746573745f737472696e675f746573745f737472696e675f746573745f737472696e675f746573745f737472696e675f00000000000000000000000000000000'
var value1 = '1'
var value2 = '1234567890123456789543'
var value3 = 'test_string_test_string_test_string_test_string_test_string_test_string_test_string_test_string_test_string_test_string_test_string_test_string_'
var decodedData = `[${value1}, ${value2}, "${value3}"], 23`
t.test('(abiEncoderV2)', function (st) {
st.plan(2)
var output = compiler.compileStandardWrapper(compilerInput(abiEncoderV2))
output = JSON.parse(output)
var contract = output.contracts['test.sol']['test']
txFormat.encodeFunctionCall(decodedData, contract.abi[0], (error, encoded) => {
console.log(error)
st.equal(encoded.dataHex, functionId + encodedData.replace('0x', ''))
})
var decoded = txFormat.decodeResponse(util.hexToIntArray(encodedData), contract.abi[0])
console.log(decoded)
st.equal(decoded[0], `tuple(uint256,uint256,string): ${value1},${value2},${value3}`)
})
})
var uintContract = `contract uintContractTest {
uint _tp;
address _ap;
@ -208,3 +231,23 @@ contract fallbackFunctionContract {
function () {}
}`
var abiEncoderV2 = `pragma experimental ABIEncoderV2;
contract test {
struct p {
uint a;
uint b;
string s;
}
function t (p _p, uint _i) returns (p) {
return _p;
}
function t () returns (p) {
p mm;
mm.a = 123;
mm.b = 133;
return mm;
}
}`

Loading…
Cancel
Save