Search in sources :

Example 21 with TypeReference

use of org.fisco.bcos.web3j.abi.TypeReference in project web3sdk by FISCO-BCOS.

the class TransactionDecoderTest method testStaticParams1.

@Test
public void testStaticParams1() throws JsonProcessingException, TransactionException, BaseException {
    /*
        function test(uint256[4] _u,int256[4] _i,bool[4] _b,address[4] _addr,bytes32[4] _bs32,string[4] _s,bytes[4] _bs) public constant returns (uint256[2],int256[2],bool[2],address[2],bytes32[2],string[2],bytes[2]) {

           }
           */
    TransactionDecoder decode = TransactionDecoderFactory.buildTransactionDecoder("[{\"constant\":true,\"inputs\":[{\"name\":\"_u\",\"type\":\"uint256[4]\"},{\"name\":\"_i\",\"type\":\"int256[4]\"},{\"name\":\"_b\",\"type\":\"bool[4]\"},{\"name\":\"_addr\",\"type\":\"address[4]\"},{\"name\":\"_bs32\",\"type\":\"bytes32[4]\"},{\"name\":\"_s\",\"type\":\"string[4]\"},{\"name\":\"_bs\",\"type\":\"bytes[4]\"}],\"name\":\"test\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256[2]\"},{\"name\":\"\",\"type\":\"int256[2]\"},{\"name\":\"\",\"type\":\"bool[2]\"},{\"name\":\"\",\"type\":\"address[2]\"},{\"name\":\"\",\"type\":\"bytes32[2]\"},{\"name\":\"\",\"type\":\"string[2]\"},{\"name\":\"\",\"type\":\"bytes[2]\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"_u\",\"type\":\"uint256\"},{\"name\":\"_i\",\"type\":\"int256\"},{\"name\":\"_b\",\"type\":\"bool\"},{\"name\":\"_addr\",\"type\":\"address\"},{\"name\":\"_bs32\",\"type\":\"bytes32\"},{\"name\":\"_s\",\"type\":\"string\"},{\"name\":\"_bs\",\"type\":\"bytes\"}],\"name\":\"test\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"},{\"name\":\"\",\"type\":\"int256\"},{\"name\":\"\",\"type\":\"bool\"},{\"name\":\"\",\"type\":\"address\"},{\"name\":\"\",\"type\":\"bytes32\"},{\"name\":\"\",\"type\":\"string\"},{\"name\":\"\",\"type\":\"bytes\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"_u\",\"type\":\"uint256[]\"},{\"name\":\"_i\",\"type\":\"int256[]\"},{\"name\":\"_b\",\"type\":\"bool[]\"},{\"name\":\"_addr\",\"type\":\"address[]\"},{\"name\":\"_bs32\",\"type\":\"bytes32[]\"},{\"name\":\"_s\",\"type\":\"string[]\"},{\"name\":\"_bs\",\"type\":\"bytes[]\"}],\"name\":\"test\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256[]\"},{\"name\":\"\",\"type\":\"int256[]\"},{\"name\":\"\",\"type\":\"bool[]\"},{\"name\":\"\",\"type\":\"address[]\"},{\"name\":\"\",\"type\":\"bytes32[]\"},{\"name\":\"\",\"type\":\"string[]\"},{\"name\":\"\",\"type\":\"bytes[]\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"_u\",\"type\":\"uint256\"},{\"indexed\":false,\"name\":\"_i\",\"type\":\"int256\"},{\"indexed\":false,\"name\":\"_b\",\"type\":\"bool\"},{\"indexed\":false,\"name\":\"_addr\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"_bs32\",\"type\":\"bytes32\"},{\"indexed\":false,\"name\":\"_s\",\"type\":\"string\"},{\"indexed\":false,\"name\":\"_bs\",\"type\":\"bytes\"}],\"name\":\"TestEventSimpleParams\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"_u\",\"type\":\"uint256[]\"},{\"indexed\":false,\"name\":\"_i\",\"type\":\"int256[]\"},{\"indexed\":false,\"name\":\"_b\",\"type\":\"bool[]\"},{\"indexed\":false,\"name\":\"_addr\",\"type\":\"address[]\"},{\"indexed\":false,\"name\":\"_bs32\",\"type\":\"bytes32[]\"},{\"indexed\":false,\"name\":\"_s\",\"type\":\"string[]\"},{\"indexed\":false,\"name\":\"_bs\",\"type\":\"bytes[]\"}],\"name\":\"TestEventDArrayParams\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"_u\",\"type\":\"uint256[4]\"},{\"indexed\":false,\"name\":\"_i\",\"type\":\"int256[4]\"},{\"indexed\":false,\"name\":\"_b\",\"type\":\"bool[4]\"},{\"indexed\":false,\"name\":\"_addr\",\"type\":\"address[4]\"},{\"indexed\":false,\"name\":\"_bs32\",\"type\":\"bytes32[4]\"},{\"indexed\":false,\"name\":\"_s\",\"type\":\"string[4]\"},{\"indexed\":false,\"name\":\"_bs\",\"type\":\"bytes[4]\"}],\"name\":\"TestEventSArrayParams\",\"type\":\"event\"}]", "");
    List<Type> test1Params = Arrays.asList(new StaticArray4<Uint256>(new Uint256(0), new Uint256(0), new Uint256(0), new Uint256(0)), new StaticArray4<Int256>(new Int256(0), new Int256(0), new Int256(0), new Int256(0)), new StaticArray4<Bool>(new Bool(true), new Bool(false), new Bool(true), new Bool(false)), new StaticArray4<Address>(new Address("0x0"), new Address("0x0"), new Address("0x0"), new Address("0x0")), new StaticArray4<Bytes32>(new Bytes32("                                ".getBytes()), new Bytes32("                                ".getBytes()), new Bytes32("                                ".getBytes()), new Bytes32("                                ".getBytes())), new StaticArray4<Utf8String>(new Utf8String(""), new Utf8String(""), new Utf8String(""), new Utf8String("")), new StaticArray4<DynamicBytes>(new DynamicBytes("".getBytes()), new DynamicBytes("".getBytes()), new DynamicBytes("".getBytes()), new DynamicBytes("".getBytes())));
    Function test1 = new Function("test", test1Params, Arrays.asList(new TypeReference<Uint256>() {
    }, new TypeReference<Int256>() {
    }, new TypeReference<Address>() {
    }, new TypeReference<Utf8String>() {
    }, new TypeReference<DynamicBytes>() {
    }, new TypeReference<Bytes32>() {
    }));
    assertThat(decode.decodeInputReturnJson(FunctionEncoder.encode(test1)), is("{\"function\":\"test(uint256[4],int256[4],bool[4],address[4],bytes32[4],string[4],bytes[4])\",\"methodID\":\"0x5682504e\",\"result\":[{\"name\":\"_u\",\"type\":\"uint256[4]\",\"data\":[0,0,0,0]},{\"name\":\"_i\",\"type\":\"int256[4]\",\"data\":[0,0,0,0]},{\"name\":\"_b\",\"type\":\"bool[4]\",\"data\":[true,false,true,false]},{\"name\":\"_addr\",\"type\":\"address[4]\",\"data\":[\"0x0000000000000000000000000000000000000000\",\"0x0000000000000000000000000000000000000000\",\"0x0000000000000000000000000000000000000000\",\"0x0000000000000000000000000000000000000000\"]},{\"name\":\"_bs32\",\"type\":\"bytes32[4]\",\"data\":[\"\",\"\",\"\",\"\"]},{\"name\":\"_s\",\"type\":\"string[4]\",\"data\":[\"\",\"\",\"\",\"\"]},{\"name\":\"_bs\",\"type\":\"bytes[4]\",\"data\":[\"\",\"\",\"\",\"\"]}]}"));
    InputAndOutputResult inputAndOutputResult1 = decode.decodeInputReturnObject(FunctionEncoder.encode(test1));
    List<ResultEntity> resultInputList = inputAndOutputResult1.getResult();
    assertThat(transEntitytoType(resultInputList), is(test1Params));
    List<Type> test1Output = Arrays.asList(new StaticArray2<Uint256>(new Uint256(0), new Uint256(0)), new StaticArray2<Int256>(new Int256(0), new Int256(0)), new StaticArray2<Bool>(new Bool(true), new Bool(false)), new StaticArray2<Address>(new Address("0x0"), new Address("0x0")), new StaticArray2<Bytes32>(new Bytes32("                                ".getBytes()), new Bytes32("                                ".getBytes())), new StaticArray2<Utf8String>(new Utf8String(""), new Utf8String("")), new StaticArray2<DynamicBytes>(new DynamicBytes("".getBytes()), new DynamicBytes("".getBytes())));
    assertThat(decode.decodeOutputReturnJson(FunctionEncoder.encode(test1), FunctionEncoder.encodeConstructor(test1Output)), is("{\"function\":\"test(uint256[4],int256[4],bool[4],address[4],bytes32[4],string[4],bytes[4])\",\"methodID\":\"0x5682504e\",\"result\":[{\"name\":\"\",\"type\":\"uint256[2]\",\"data\":[0,0]},{\"name\":\"\",\"type\":\"int256[2]\",\"data\":[0,0]},{\"name\":\"\",\"type\":\"bool[2]\",\"data\":[true,false]},{\"name\":\"\",\"type\":\"address[2]\",\"data\":[\"0x0000000000000000000000000000000000000000\",\"0x0000000000000000000000000000000000000000\"]},{\"name\":\"\",\"type\":\"bytes32[2]\",\"data\":[\"\",\"\"]},{\"name\":\"\",\"type\":\"string[2]\",\"data\":[\"\",\"\"]},{\"name\":\"\",\"type\":\"bytes[2]\",\"data\":[\"\",\"\"]}]}"));
    InputAndOutputResult inputAndOutputResult = decode.decodeOutputReturnObject(FunctionEncoder.encode(test1), FunctionEncoder.encodeConstructor(test1Output));
    List<ResultEntity> resultOutputList = inputAndOutputResult.getResult();
    assertThat(transEntitytoType(resultOutputList), is(test1Output));
}
Also used : Address(org.fisco.bcos.web3j.abi.datatypes.Address) Bytes32(org.fisco.bcos.web3j.abi.datatypes.generated.Bytes32) Function(org.fisco.bcos.web3j.abi.datatypes.Function) DynamicBytes(org.fisco.bcos.web3j.abi.datatypes.DynamicBytes) Bool(org.fisco.bcos.web3j.abi.datatypes.Bool) TypeReference(org.fisco.bcos.web3j.abi.TypeReference) Int256(org.fisco.bcos.web3j.abi.datatypes.generated.Int256) Utf8String(org.fisco.bcos.web3j.abi.datatypes.Utf8String) Type(org.fisco.bcos.web3j.abi.datatypes.Type) NamedType(org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition.NamedType) Uint256(org.fisco.bcos.web3j.abi.datatypes.generated.Uint256) Test(org.junit.Test)

Example 22 with TypeReference

use of org.fisco.bcos.web3j.abi.TypeReference in project web3sdk by FISCO-BCOS.

the class TransactionDecoderTest method testMixedEvent.

@Test
public void testMixedEvent() throws BaseException, IOException {
    /*
         * event TestEventSimpleParams(uint256 _u,int256 _i,bool _b,address _addr,bytes32 _bs32, string _s,bytes _bs);
         * event TestEventDArrayParams(uint256[] _u,int256[] _i,bool[] _b,address[] _addr,bytes32[] _bs32, string[] _s,bytes[] _bs);
         * event TestEventSArrayParams(uint256[4] _u,int256[4] _i,bool[4] _b,address[4] _addr,bytes32[4] _bs32, string[4] _s,bytes[4] _bs);
         */
    TransactionDecoder decode = TransactionDecoderFactory.buildTransactionDecoder("[{\"constant\":true,\"inputs\":[{\"name\":\"_u\",\"type\":\"uint256[4]\"},{\"name\":\"_i\",\"type\":\"int256[4]\"},{\"name\":\"_b\",\"type\":\"bool[4]\"},{\"name\":\"_addr\",\"type\":\"address[4]\"},{\"name\":\"_bs32\",\"type\":\"bytes32[4]\"},{\"name\":\"_s\",\"type\":\"string[4]\"},{\"name\":\"_bs\",\"type\":\"bytes[4]\"}],\"name\":\"test\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256[2]\"},{\"name\":\"\",\"type\":\"int256[2]\"},{\"name\":\"\",\"type\":\"bool[2]\"},{\"name\":\"\",\"type\":\"address[2]\"},{\"name\":\"\",\"type\":\"bytes32[2]\"},{\"name\":\"\",\"type\":\"string[2]\"},{\"name\":\"\",\"type\":\"bytes[2]\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"_u\",\"type\":\"uint256\"},{\"name\":\"_i\",\"type\":\"int256\"},{\"name\":\"_b\",\"type\":\"bool\"},{\"name\":\"_addr\",\"type\":\"address\"},{\"name\":\"_bs32\",\"type\":\"bytes32\"},{\"name\":\"_s\",\"type\":\"string\"},{\"name\":\"_bs\",\"type\":\"bytes\"}],\"name\":\"test\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"},{\"name\":\"\",\"type\":\"int256\"},{\"name\":\"\",\"type\":\"bool\"},{\"name\":\"\",\"type\":\"address\"},{\"name\":\"\",\"type\":\"bytes32\"},{\"name\":\"\",\"type\":\"string\"},{\"name\":\"\",\"type\":\"bytes\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"_u\",\"type\":\"uint256[]\"},{\"name\":\"_i\",\"type\":\"int256[]\"},{\"name\":\"_b\",\"type\":\"bool[]\"},{\"name\":\"_addr\",\"type\":\"address[]\"},{\"name\":\"_bs32\",\"type\":\"bytes32[]\"},{\"name\":\"_s\",\"type\":\"string[]\"},{\"name\":\"_bs\",\"type\":\"bytes[]\"}],\"name\":\"test\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256[]\"},{\"name\":\"\",\"type\":\"int256[]\"},{\"name\":\"\",\"type\":\"bool[]\"},{\"name\":\"\",\"type\":\"address[]\"},{\"name\":\"\",\"type\":\"bytes32[]\"},{\"name\":\"\",\"type\":\"string[]\"},{\"name\":\"\",\"type\":\"bytes[]\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"_u\",\"type\":\"uint256\"},{\"indexed\":false,\"name\":\"_i\",\"type\":\"int256\"},{\"indexed\":false,\"name\":\"_b\",\"type\":\"bool\"},{\"indexed\":false,\"name\":\"_addr\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"_bs32\",\"type\":\"bytes32\"},{\"indexed\":false,\"name\":\"_s\",\"type\":\"string\"},{\"indexed\":false,\"name\":\"_bs\",\"type\":\"bytes\"}],\"name\":\"TestEventSimpleParams\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"_u\",\"type\":\"uint256[]\"},{\"indexed\":false,\"name\":\"_i\",\"type\":\"int256[]\"},{\"indexed\":false,\"name\":\"_b\",\"type\":\"bool[]\"},{\"indexed\":false,\"name\":\"_addr\",\"type\":\"address[]\"},{\"indexed\":false,\"name\":\"_bs32\",\"type\":\"bytes32[]\"},{\"indexed\":false,\"name\":\"_s\",\"type\":\"string[]\"},{\"indexed\":false,\"name\":\"_bs\",\"type\":\"bytes[]\"}],\"name\":\"TestEventDArrayParams\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"_u\",\"type\":\"uint256[4]\"},{\"indexed\":false,\"name\":\"_i\",\"type\":\"int256[4]\"},{\"indexed\":false,\"name\":\"_b\",\"type\":\"bool[4]\"},{\"indexed\":false,\"name\":\"_addr\",\"type\":\"address[4]\"},{\"indexed\":false,\"name\":\"_bs32\",\"type\":\"bytes32[4]\"},{\"indexed\":false,\"name\":\"_s\",\"type\":\"string[4]\"},{\"indexed\":false,\"name\":\"_bs\",\"type\":\"bytes[4]\"}],\"name\":\"TestEventSArrayParams\",\"type\":\"event\"}]", "");
    List<TypeReference<?>> event1TypeList = Arrays.asList(new TypeReference<Uint256>() {
    }, new TypeReference<Int256>() {
    }, new TypeReference<Bool>() {
    }, new TypeReference<Address>() {
    }, new TypeReference<Bytes32>() {
    }, new TypeReference<Utf8String>() {
    }, new TypeReference<DynamicBytes>() {
    });
    Event event1 = new Event("TestEventSimpleParams", event1TypeList);
    List<TypeReference<?>> event2TypeList = Arrays.asList(new TypeReference<DynamicArray<Uint256>>() {
    }, new TypeReference<DynamicArray<Int256>>() {
    }, new TypeReference<DynamicArray<Bool>>() {
    }, new TypeReference<DynamicArray<Address>>() {
    }, new TypeReference<DynamicArray<Bytes32>>() {
    }, new TypeReference<DynamicArray<Utf8String>>() {
    }, new TypeReference<DynamicArray<DynamicBytes>>() {
    });
    Event event2 = new Event("TestEventDArrayParams", event2TypeList);
    List<TypeReference<?>> event3TypeList = Arrays.asList(new TypeReference<StaticArray4<Uint256>>() {
    }, new TypeReference<StaticArray4<Int256>>() {
    }, new TypeReference<StaticArray4<Bool>>() {
    }, new TypeReference<StaticArray4<Address>>() {
    }, new TypeReference<StaticArray4<Bytes32>>() {
    }, new TypeReference<StaticArray4<Utf8String>>() {
    }, new TypeReference<StaticArray4<DynamicBytes>>() {
    });
    Event event3 = new Event("TestEventSArrayParams", event3TypeList);
    List<Type> eventDataParams1 = Arrays.asList(new Uint256(111111), new Int256(-1111111), new Bool(false), new Address("0x692a70d2e424a56d2c6c27aa97d1a86395877b3a"), new Bytes32("abcdefghiabcdefghiabcdefghiabhji".getBytes()), new Utf8String("章鱼小丸子ljjkl;adjsfkljlkjl"), new DynamicBytes("sadfljkjkljkl".getBytes()));
    List<String> topics1 = new ArrayList<String>();
    topics1.add(EventEncoder.encode(event1));
    Log log1 = new Log();
    log1.setData(FunctionEncoder.encodeConstructor(eventDataParams1));
    log1.setTopics(topics1);
    List<Type> eventDataParams2 = Arrays.asList(new DynamicArray<Uint256>(new Uint256(11111), new Uint256(22222), new Uint256(33333)), new DynamicArray<Int256>(new Int256(-1111111), new Int256(-3333333), new Int256(-2222222)), new DynamicArray<Bool>(new Bool(false), new Bool(true), new Bool(false)), new DynamicArray<Address>(new Address("0x692a70d2e424a56d2c6c27aa97d1a86395877b3a"), new Address("0x692a70d2e424a56d2c6c27aa97d1a86395877b3a")), new DynamicArray<Bytes32>(new Bytes32("abcdefghiabcdefghiabcdefghiabhji".getBytes()), new Bytes32("abcdefghiabcdefghiabcdefghiabhji".getBytes())), new DynamicArray<Utf8String>(new Utf8String(""), new Utf8String("章鱼小丸子ljjkl;adjsfkljlkjl"), new Utf8String("章鱼小丸子ljjkl;adjsfkljlkjl")), new DynamicArray<DynamicBytes>(new DynamicBytes("".getBytes()), new DynamicBytes("sadfljkjkljkl".getBytes()), new DynamicBytes("章鱼小丸子ljjkl;adjsfkljlkjl".getBytes())));
    List<String> topics2 = new ArrayList<String>();
    topics2.add(EventEncoder.encode(event2));
    Log log2 = new Log();
    log2.setData(FunctionEncoder.encodeConstructor(eventDataParams2));
    log2.setTopics(topics2);
    List<Type> eventDataParams3 = Arrays.asList(new StaticArray4<Uint256>(new Uint256(11111), new Uint256(22222), new Uint256(33333), new Uint256(44444)), new StaticArray4<Int256>(new Int256(-1111111), new Int256(-2222222), new Int256(-3333333), new Int256(-4444444)), new StaticArray4<Bool>(new Bool(true), new Bool(false), new Bool(true), new Bool(false)), new StaticArray4<Address>(new Address("0x692a70d2e424a56d2c6c27aa97d1a86395877b3a"), new Address("0x692a70d2e424a56d2c6c27aa97d1a86395877b3a"), new Address("0x0"), new Address("0x692a70d2e424a56d2c6c27aa97d1a86395877b3a")), new StaticArray4<Bytes32>(new Bytes32("abcdefghiabcdefghiabcdefghiabhji".getBytes()), new Bytes32("abcdefghiabcdefghiabcdefghiabhji".getBytes()), new Bytes32("00000000000000000000000000000000".getBytes()), new Bytes32("abcdefghiabcdefghiabcdefghiabhji".getBytes())), new StaticArray4<Utf8String>(new Utf8String("章鱼小丸子ljjkl;adjsfkljlkjl"), new Utf8String("xxxfjlk"), new Utf8String("fdajl;jkdsafjkljkadfjklf"), new Utf8String("")), new StaticArray4<DynamicBytes>(new DynamicBytes("sadfljkjkljkl".getBytes()), new DynamicBytes("".getBytes()), new DynamicBytes("sadfljkjkljkl".getBytes()), new DynamicBytes("章鱼小丸子ljjkl;adjsfkljlkjl".getBytes())));
    List<String> topics3 = new ArrayList<String>();
    topics3.add(EventEncoder.encode(event3));
    Log log3 = new Log();
    log3.setData(FunctionEncoder.encodeConstructor(eventDataParams3));
    log3.setTopics(topics3);
    List<Log> logList = new ArrayList<Log>();
    logList.add(log1);
    logList.add(log2);
    logList.add(log3);
    // System.out.println(" => " + decode.decodeEventReturnJson(logList));
    assertThat(decode.decodeEventReturnJson(logList), is("{\"TestEventSimpleParams(uint256,int256,bool,address,bytes32,string,bytes)\":[[{\"name\":\"_u\",\"type\":\"uint256\",\"data\":111111,\"indexed\":false},{\"name\":\"_i\",\"type\":\"int256\",\"data\":-1111111,\"indexed\":false},{\"name\":\"_b\",\"type\":\"bool\",\"data\":false,\"indexed\":false},{\"name\":\"_addr\",\"type\":\"address\",\"data\":\"0x692a70d2e424a56d2c6c27aa97d1a86395877b3a\",\"indexed\":false},{\"name\":\"_bs32\",\"type\":\"bytes32\",\"data\":\"abcdefghiabcdefghiabcdefghiabhji\",\"indexed\":false},{\"name\":\"_s\",\"type\":\"string\",\"data\":\"章鱼小丸子ljjkl;adjsfkljlkjl\",\"indexed\":false},{\"name\":\"_bs\",\"type\":\"bytes\",\"data\":\"sadfljkjkljkl\",\"indexed\":false}]],\"TestEventDArrayParams(uint256[],int256[],bool[],address[],bytes32[],string[],bytes[])\":[[{\"name\":\"_u\",\"type\":\"uint256[]\",\"data\":[11111,22222,33333],\"indexed\":false},{\"name\":\"_i\",\"type\":\"int256[]\",\"data\":[-1111111,-3333333,-2222222],\"indexed\":false},{\"name\":\"_b\",\"type\":\"bool[]\",\"data\":[false,true,false],\"indexed\":false},{\"name\":\"_addr\",\"type\":\"address[]\",\"data\":[\"0x692a70d2e424a56d2c6c27aa97d1a86395877b3a\",\"0x692a70d2e424a56d2c6c27aa97d1a86395877b3a\"],\"indexed\":false},{\"name\":\"_bs32\",\"type\":\"bytes32[]\",\"data\":[\"abcdefghiabcdefghiabcdefghiabhji\",\"abcdefghiabcdefghiabcdefghiabhji\"],\"indexed\":false},{\"name\":\"_s\",\"type\":\"string[]\",\"data\":[\"\",\"章鱼小丸子ljjkl;adjsfkljlkjl\",\"章鱼小丸子ljjkl;adjsfkljlkjl\"],\"indexed\":false},{\"name\":\"_bs\",\"type\":\"bytes[]\",\"data\":[\"\",\"sadfljkjkljkl\",\"章鱼小丸子ljjkl;adjsfkljlkjl\"],\"indexed\":false}]],\"TestEventSArrayParams(uint256[4],int256[4],bool[4],address[4],bytes32[4],string[4],bytes[4])\":[[{\"name\":\"_u\",\"type\":\"uint256[4]\",\"data\":[11111,22222,33333,44444],\"indexed\":false},{\"name\":\"_i\",\"type\":\"int256[4]\",\"data\":[-1111111,-2222222,-3333333,-4444444],\"indexed\":false},{\"name\":\"_b\",\"type\":\"bool[4]\",\"data\":[true,false,true,false],\"indexed\":false},{\"name\":\"_addr\",\"type\":\"address[4]\",\"data\":[\"0x692a70d2e424a56d2c6c27aa97d1a86395877b3a\",\"0x692a70d2e424a56d2c6c27aa97d1a86395877b3a\",\"0x0000000000000000000000000000000000000000\",\"0x692a70d2e424a56d2c6c27aa97d1a86395877b3a\"],\"indexed\":false},{\"name\":\"_bs32\",\"type\":\"bytes32[4]\",\"data\":[\"abcdefghiabcdefghiabcdefghiabhji\",\"abcdefghiabcdefghiabcdefghiabhji\",\"00000000000000000000000000000000\",\"abcdefghiabcdefghiabcdefghiabhji\"],\"indexed\":false},{\"name\":\"_s\",\"type\":\"string[4]\",\"data\":[\"章鱼小丸子ljjkl;adjsfkljlkjl\",\"xxxfjlk\",\"fdajl;jkdsafjkljkadfjklf\",\"\"],\"indexed\":false},{\"name\":\"_bs\",\"type\":\"bytes[4]\",\"data\":[\"sadfljkjkljkl\",\"\",\"sadfljkjkljkl\",\"章鱼小丸子ljjkl;adjsfkljlkjl\"],\"indexed\":false}]]}"));
}
Also used : Address(org.fisco.bcos.web3j.abi.datatypes.Address) ArrayList(java.util.ArrayList) Utf8String(org.fisco.bcos.web3j.abi.datatypes.Utf8String) Bytes32(org.fisco.bcos.web3j.abi.datatypes.generated.Bytes32) Bool(org.fisco.bcos.web3j.abi.datatypes.Bool) Log(org.fisco.bcos.web3j.protocol.core.methods.response.Log) Utf8String(org.fisco.bcos.web3j.abi.datatypes.Utf8String) DynamicArray(org.fisco.bcos.web3j.abi.datatypes.DynamicArray) DynamicBytes(org.fisco.bcos.web3j.abi.datatypes.DynamicBytes) TypeReference(org.fisco.bcos.web3j.abi.TypeReference) Int256(org.fisco.bcos.web3j.abi.datatypes.generated.Int256) Type(org.fisco.bcos.web3j.abi.datatypes.Type) NamedType(org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition.NamedType) StaticArray4(org.fisco.bcos.web3j.abi.datatypes.generated.StaticArray4) Event(org.fisco.bcos.web3j.abi.datatypes.Event) Uint256(org.fisco.bcos.web3j.abi.datatypes.generated.Uint256) Test(org.junit.Test)

Example 23 with TypeReference

use of org.fisco.bcos.web3j.abi.TypeReference in project web3sdk by FISCO-BCOS.

the class TransactionDecoderTest method testEventSArray.

@Test
public void testEventSArray() throws BaseException, IOException {
    /*
        		event TestEventSArrayParams(uint256[4] _u,int256[4] _i,bool[4] _b,address[4] _addr,bytes32[4] _bs32, string[4] _s,bytes[4] _bs);
        */
    TransactionDecoder decode = TransactionDecoderFactory.buildTransactionDecoder("[{\"constant\":true,\"inputs\":[{\"name\":\"_u\",\"type\":\"uint256[4]\"},{\"name\":\"_i\",\"type\":\"int256[4]\"},{\"name\":\"_b\",\"type\":\"bool[4]\"},{\"name\":\"_addr\",\"type\":\"address[4]\"},{\"name\":\"_bs32\",\"type\":\"bytes32[4]\"},{\"name\":\"_s\",\"type\":\"string[4]\"},{\"name\":\"_bs\",\"type\":\"bytes[4]\"}],\"name\":\"test\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256[2]\"},{\"name\":\"\",\"type\":\"int256[2]\"},{\"name\":\"\",\"type\":\"bool[2]\"},{\"name\":\"\",\"type\":\"address[2]\"},{\"name\":\"\",\"type\":\"bytes32[2]\"},{\"name\":\"\",\"type\":\"string[2]\"},{\"name\":\"\",\"type\":\"bytes[2]\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"_u\",\"type\":\"uint256\"},{\"name\":\"_i\",\"type\":\"int256\"},{\"name\":\"_b\",\"type\":\"bool\"},{\"name\":\"_addr\",\"type\":\"address\"},{\"name\":\"_bs32\",\"type\":\"bytes32\"},{\"name\":\"_s\",\"type\":\"string\"},{\"name\":\"_bs\",\"type\":\"bytes\"}],\"name\":\"test\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"},{\"name\":\"\",\"type\":\"int256\"},{\"name\":\"\",\"type\":\"bool\"},{\"name\":\"\",\"type\":\"address\"},{\"name\":\"\",\"type\":\"bytes32\"},{\"name\":\"\",\"type\":\"string\"},{\"name\":\"\",\"type\":\"bytes\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"_u\",\"type\":\"uint256[]\"},{\"name\":\"_i\",\"type\":\"int256[]\"},{\"name\":\"_b\",\"type\":\"bool[]\"},{\"name\":\"_addr\",\"type\":\"address[]\"},{\"name\":\"_bs32\",\"type\":\"bytes32[]\"},{\"name\":\"_s\",\"type\":\"string[]\"},{\"name\":\"_bs\",\"type\":\"bytes[]\"}],\"name\":\"test\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256[]\"},{\"name\":\"\",\"type\":\"int256[]\"},{\"name\":\"\",\"type\":\"bool[]\"},{\"name\":\"\",\"type\":\"address[]\"},{\"name\":\"\",\"type\":\"bytes32[]\"},{\"name\":\"\",\"type\":\"string[]\"},{\"name\":\"\",\"type\":\"bytes[]\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"_u\",\"type\":\"uint256\"},{\"indexed\":false,\"name\":\"_i\",\"type\":\"int256\"},{\"indexed\":false,\"name\":\"_b\",\"type\":\"bool\"},{\"indexed\":false,\"name\":\"_addr\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"_bs32\",\"type\":\"bytes32\"},{\"indexed\":false,\"name\":\"_s\",\"type\":\"string\"},{\"indexed\":false,\"name\":\"_bs\",\"type\":\"bytes\"}],\"name\":\"TestEventSimpleParams\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"_u\",\"type\":\"uint256[]\"},{\"indexed\":false,\"name\":\"_i\",\"type\":\"int256[]\"},{\"indexed\":false,\"name\":\"_b\",\"type\":\"bool[]\"},{\"indexed\":false,\"name\":\"_addr\",\"type\":\"address[]\"},{\"indexed\":false,\"name\":\"_bs32\",\"type\":\"bytes32[]\"},{\"indexed\":false,\"name\":\"_s\",\"type\":\"string[]\"},{\"indexed\":false,\"name\":\"_bs\",\"type\":\"bytes[]\"}],\"name\":\"TestEventDArrayParams\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"_u\",\"type\":\"uint256[4]\"},{\"indexed\":false,\"name\":\"_i\",\"type\":\"int256[4]\"},{\"indexed\":false,\"name\":\"_b\",\"type\":\"bool[4]\"},{\"indexed\":false,\"name\":\"_addr\",\"type\":\"address[4]\"},{\"indexed\":false,\"name\":\"_bs32\",\"type\":\"bytes32[4]\"},{\"indexed\":false,\"name\":\"_s\",\"type\":\"string[4]\"},{\"indexed\":false,\"name\":\"_bs\",\"type\":\"bytes[4]\"}],\"name\":\"TestEventSArrayParams\",\"type\":\"event\"}]", "");
    List<TypeReference<?>> eventTypeList = Arrays.asList(new TypeReference<StaticArray4<Uint256>>() {
    }, new TypeReference<StaticArray4<Int256>>() {
    }, new TypeReference<StaticArray4<Bool>>() {
    }, new TypeReference<StaticArray4<Address>>() {
    }, new TypeReference<StaticArray4<Bytes32>>() {
    }, new TypeReference<StaticArray4<Utf8String>>() {
    }, new TypeReference<StaticArray4<DynamicBytes>>() {
    });
    Event event = new Event("TestEventSArrayParams", eventTypeList);
    List<Type> eventDataParams1 = Arrays.asList(new StaticArray4<Uint256>(new Uint256(11111), new Uint256(22222), new Uint256(33333), new Uint256(44444)), new StaticArray4<Int256>(new Int256(-1111111), new Int256(-2222222), new Int256(-3333333), new Int256(-4444444)), new StaticArray4<Bool>(new Bool(true), new Bool(false), new Bool(true), new Bool(false)), new StaticArray4<Address>(new Address("0x692a70d2e424a56d2c6c27aa97d1a86395877b3a"), new Address("0x692a70d2e424a56d2c6c27aa97d1a86395877b3a"), new Address("0x0"), new Address("0x692a70d2e424a56d2c6c27aa97d1a86395877b3a")), new StaticArray4<Bytes32>(new Bytes32("abcdefghiabcdefghiabcdefghiabhji".getBytes()), new Bytes32("abcdefghiabcdefghiabcdefghiabhji".getBytes()), new Bytes32("00000000000000000000000000000000".getBytes()), new Bytes32("abcdefghiabcdefghiabcdefghiabhji".getBytes())), new StaticArray4<Utf8String>(new Utf8String("章鱼小丸子ljjkl;adjsfkljlkjl"), new Utf8String("xxxfjlk"), new Utf8String("fdajl;jkdsafjkljkadfjklf"), new Utf8String("")), new StaticArray4<DynamicBytes>(new DynamicBytes("sadfljkjkljkl".getBytes()), new DynamicBytes("".getBytes()), new DynamicBytes("sadfljkjkljkl".getBytes()), new DynamicBytes("章鱼小丸子ljjkl;adjsfkljlkjl".getBytes())));
    List<Type> eventDataParams2 = Arrays.asList(new StaticArray4<Uint256>(new Uint256(0), new Uint256(0), new Uint256(0), new Uint256(0)), new StaticArray4<Int256>(new Int256(0), new Int256(0), new Int256(0), new Int256(0)), new StaticArray4<Bool>(new Bool(true), new Bool(false), new Bool(true), new Bool(false)), new StaticArray4<Address>(new Address("0x0"), new Address("0x0"), new Address("0x0"), new Address("0x0")), new StaticArray4<Bytes32>(new Bytes32("                                ".getBytes()), new Bytes32("                                ".getBytes()), new Bytes32("                                ".getBytes()), new Bytes32("                                ".getBytes())), new StaticArray4<Utf8String>(new Utf8String(""), new Utf8String(""), new Utf8String(""), new Utf8String("")), new StaticArray4<DynamicBytes>(new DynamicBytes("".getBytes()), new DynamicBytes("".getBytes()), new DynamicBytes("".getBytes()), new DynamicBytes("".getBytes())));
    List<String> topics = new ArrayList<String>();
    topics.add(EventEncoder.encode(event));
    Log log1 = new Log();
    log1.setData(FunctionEncoder.encodeConstructor(eventDataParams1));
    log1.setTopics(topics);
    Log log2 = new Log();
    log2.setData(FunctionEncoder.encodeConstructor(eventDataParams2));
    log2.setTopics(topics);
    AbiDefinition abiDefinition = null;
    Tuple2<AbiDefinition, List<EventResultEntity>> tupleResult1 = decode.decodeEventReturnObject(log1);
    assertThat(transEntitytoType0(tupleResult1.getValue2()), is(eventDataParams1));
    abiDefinition = tupleResult1.getValue1();
    Tuple2<AbiDefinition, List<EventResultEntity>> tupleResult2 = decode.decodeEventReturnObject(log2);
    assertThat(transEntitytoType0(tupleResult2.getValue2()), is(eventDataParams2));
    List<Log> logList1 = new ArrayList<Log>();
    logList1.add(log1);
    Map<String, List<List<EventResultEntity>>> mapResult1 = decode.decodeEventReturnObject(logList1);
    assertThat(transEntitytoType0(mapResult1.get(decodeMethodSign(abiDefinition)).get(0)), is(eventDataParams1));
    // System.out.println("111 => " + decode.decodeEventReturnJson(logList1));
    assertThat(decode.decodeEventReturnJson(logList1), is("{\"TestEventSArrayParams(uint256[4],int256[4],bool[4],address[4],bytes32[4],string[4],bytes[4])\":[[{\"name\":\"_u\",\"type\":\"uint256[4]\",\"data\":[11111,22222,33333,44444],\"indexed\":false},{\"name\":\"_i\",\"type\":\"int256[4]\",\"data\":[-1111111,-2222222,-3333333,-4444444],\"indexed\":false},{\"name\":\"_b\",\"type\":\"bool[4]\",\"data\":[true,false,true,false],\"indexed\":false},{\"name\":\"_addr\",\"type\":\"address[4]\",\"data\":[\"0x692a70d2e424a56d2c6c27aa97d1a86395877b3a\",\"0x692a70d2e424a56d2c6c27aa97d1a86395877b3a\",\"0x0000000000000000000000000000000000000000\",\"0x692a70d2e424a56d2c6c27aa97d1a86395877b3a\"],\"indexed\":false},{\"name\":\"_bs32\",\"type\":\"bytes32[4]\",\"data\":[\"abcdefghiabcdefghiabcdefghiabhji\",\"abcdefghiabcdefghiabcdefghiabhji\",\"00000000000000000000000000000000\",\"abcdefghiabcdefghiabcdefghiabhji\"],\"indexed\":false},{\"name\":\"_s\",\"type\":\"string[4]\",\"data\":[\"章鱼小丸子ljjkl;adjsfkljlkjl\",\"xxxfjlk\",\"fdajl;jkdsafjkljkadfjklf\",\"\"],\"indexed\":false},{\"name\":\"_bs\",\"type\":\"bytes[4]\",\"data\":[\"sadfljkjkljkl\",\"\",\"sadfljkjkljkl\",\"章鱼小丸子ljjkl;adjsfkljlkjl\"],\"indexed\":false}]]}"));
    List<Log> logList2 = new ArrayList<Log>();
    logList2.add(log1);
    logList2.add(log2);
    Map<String, List<List<EventResultEntity>>> mapResult2 = decode.decodeEventReturnObject(logList2);
    assertThat(transEntitytoType0(mapResult2.get(decodeMethodSign(abiDefinition)).get(0)), is(eventDataParams1));
    assertThat(transEntitytoType0(mapResult2.get(decodeMethodSign(abiDefinition)).get(1)), is(eventDataParams2));
    // System.out.println("222 => " + decode.decodeEventReturnJson(logList2));
    assertThat(decode.decodeEventReturnJson(logList2), is("{\"TestEventSArrayParams(uint256[4],int256[4],bool[4],address[4],bytes32[4],string[4],bytes[4])\":[[{\"name\":\"_u\",\"type\":\"uint256[4]\",\"data\":[11111,22222,33333,44444],\"indexed\":false},{\"name\":\"_i\",\"type\":\"int256[4]\",\"data\":[-1111111,-2222222,-3333333,-4444444],\"indexed\":false},{\"name\":\"_b\",\"type\":\"bool[4]\",\"data\":[true,false,true,false],\"indexed\":false},{\"name\":\"_addr\",\"type\":\"address[4]\",\"data\":[\"0x692a70d2e424a56d2c6c27aa97d1a86395877b3a\",\"0x692a70d2e424a56d2c6c27aa97d1a86395877b3a\",\"0x0000000000000000000000000000000000000000\",\"0x692a70d2e424a56d2c6c27aa97d1a86395877b3a\"],\"indexed\":false},{\"name\":\"_bs32\",\"type\":\"bytes32[4]\",\"data\":[\"abcdefghiabcdefghiabcdefghiabhji\",\"abcdefghiabcdefghiabcdefghiabhji\",\"00000000000000000000000000000000\",\"abcdefghiabcdefghiabcdefghiabhji\"],\"indexed\":false},{\"name\":\"_s\",\"type\":\"string[4]\",\"data\":[\"章鱼小丸子ljjkl;adjsfkljlkjl\",\"xxxfjlk\",\"fdajl;jkdsafjkljkadfjklf\",\"\"],\"indexed\":false},{\"name\":\"_bs\",\"type\":\"bytes[4]\",\"data\":[\"sadfljkjkljkl\",\"\",\"sadfljkjkljkl\",\"章鱼小丸子ljjkl;adjsfkljlkjl\"],\"indexed\":false}],[{\"name\":\"_u\",\"type\":\"uint256[4]\",\"data\":[0,0,0,0],\"indexed\":false},{\"name\":\"_i\",\"type\":\"int256[4]\",\"data\":[0,0,0,0],\"indexed\":false},{\"name\":\"_b\",\"type\":\"bool[4]\",\"data\":[true,false,true,false],\"indexed\":false},{\"name\":\"_addr\",\"type\":\"address[4]\",\"data\":[\"0x0000000000000000000000000000000000000000\",\"0x0000000000000000000000000000000000000000\",\"0x0000000000000000000000000000000000000000\",\"0x0000000000000000000000000000000000000000\"],\"indexed\":false},{\"name\":\"_bs32\",\"type\":\"bytes32[4]\",\"data\":[\"\",\"\",\"\",\"\"],\"indexed\":false},{\"name\":\"_s\",\"type\":\"string[4]\",\"data\":[\"\",\"\",\"\",\"\"],\"indexed\":false},{\"name\":\"_bs\",\"type\":\"bytes[4]\",\"data\":[\"\",\"\",\"\",\"\"],\"indexed\":false}]]}"));
}
Also used : Address(org.fisco.bcos.web3j.abi.datatypes.Address) ArrayList(java.util.ArrayList) Utf8String(org.fisco.bcos.web3j.abi.datatypes.Utf8String) Bytes32(org.fisco.bcos.web3j.abi.datatypes.generated.Bytes32) DynamicBytes(org.fisco.bcos.web3j.abi.datatypes.DynamicBytes) Bool(org.fisco.bcos.web3j.abi.datatypes.Bool) AbiDefinition(org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition) ArrayList(java.util.ArrayList) List(java.util.List) TypeReference(org.fisco.bcos.web3j.abi.TypeReference) Log(org.fisco.bcos.web3j.protocol.core.methods.response.Log) Int256(org.fisco.bcos.web3j.abi.datatypes.generated.Int256) Utf8String(org.fisco.bcos.web3j.abi.datatypes.Utf8String) Type(org.fisco.bcos.web3j.abi.datatypes.Type) NamedType(org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition.NamedType) StaticArray4(org.fisco.bcos.web3j.abi.datatypes.generated.StaticArray4) Event(org.fisco.bcos.web3j.abi.datatypes.Event) Uint256(org.fisco.bcos.web3j.abi.datatypes.generated.Uint256) Test(org.junit.Test)

Example 24 with TypeReference

use of org.fisco.bcos.web3j.abi.TypeReference in project web3sdk by FISCO-BCOS.

the class TransactionDecoderTest method testEventSimple.

/*
     * event TestEventSimpleParams(uint256 _u,int256 _i,bool _b,address _addr,bytes32 _bs32, string _s,bytes _bs);
     * event TestEventDArrayParams(uint256[] _u,int256[] _i,bool[] _b,address[] _addr,bytes32[] _bs32, string[] _s,bytes[] _bs);
     * event TestEventSArrayParams(uint256[4] _u,int256[4] _i,bool[4] _b,address[4] _addr,bytes32[4] _bs32, string[4] _s,bytes[4] _bs);
     */
@Test
public void testEventSimple() throws BaseException, IOException {
    /*
         	event TestEventSimpleParams(uint256 _u,int256 _i,bool _b,address _addr,bytes32 _bs32, string _s,bytes _bs);
        */
    TransactionDecoder decode = TransactionDecoderFactory.buildTransactionDecoder("[{\"constant\":true,\"inputs\":[{\"name\":\"_u\",\"type\":\"uint256[4]\"},{\"name\":\"_i\",\"type\":\"int256[4]\"},{\"name\":\"_b\",\"type\":\"bool[4]\"},{\"name\":\"_addr\",\"type\":\"address[4]\"},{\"name\":\"_bs32\",\"type\":\"bytes32[4]\"},{\"name\":\"_s\",\"type\":\"string[4]\"},{\"name\":\"_bs\",\"type\":\"bytes[4]\"}],\"name\":\"test\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256[2]\"},{\"name\":\"\",\"type\":\"int256[2]\"},{\"name\":\"\",\"type\":\"bool[2]\"},{\"name\":\"\",\"type\":\"address[2]\"},{\"name\":\"\",\"type\":\"bytes32[2]\"},{\"name\":\"\",\"type\":\"string[2]\"},{\"name\":\"\",\"type\":\"bytes[2]\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"_u\",\"type\":\"uint256\"},{\"name\":\"_i\",\"type\":\"int256\"},{\"name\":\"_b\",\"type\":\"bool\"},{\"name\":\"_addr\",\"type\":\"address\"},{\"name\":\"_bs32\",\"type\":\"bytes32\"},{\"name\":\"_s\",\"type\":\"string\"},{\"name\":\"_bs\",\"type\":\"bytes\"}],\"name\":\"test\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"},{\"name\":\"\",\"type\":\"int256\"},{\"name\":\"\",\"type\":\"bool\"},{\"name\":\"\",\"type\":\"address\"},{\"name\":\"\",\"type\":\"bytes32\"},{\"name\":\"\",\"type\":\"string\"},{\"name\":\"\",\"type\":\"bytes\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"_u\",\"type\":\"uint256[]\"},{\"name\":\"_i\",\"type\":\"int256[]\"},{\"name\":\"_b\",\"type\":\"bool[]\"},{\"name\":\"_addr\",\"type\":\"address[]\"},{\"name\":\"_bs32\",\"type\":\"bytes32[]\"},{\"name\":\"_s\",\"type\":\"string[]\"},{\"name\":\"_bs\",\"type\":\"bytes[]\"}],\"name\":\"test\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256[]\"},{\"name\":\"\",\"type\":\"int256[]\"},{\"name\":\"\",\"type\":\"bool[]\"},{\"name\":\"\",\"type\":\"address[]\"},{\"name\":\"\",\"type\":\"bytes32[]\"},{\"name\":\"\",\"type\":\"string[]\"},{\"name\":\"\",\"type\":\"bytes[]\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"_u\",\"type\":\"uint256\"},{\"indexed\":false,\"name\":\"_i\",\"type\":\"int256\"},{\"indexed\":false,\"name\":\"_b\",\"type\":\"bool\"},{\"indexed\":false,\"name\":\"_addr\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"_bs32\",\"type\":\"bytes32\"},{\"indexed\":false,\"name\":\"_s\",\"type\":\"string\"},{\"indexed\":false,\"name\":\"_bs\",\"type\":\"bytes\"}],\"name\":\"TestEventSimpleParams\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"_u\",\"type\":\"uint256[]\"},{\"indexed\":false,\"name\":\"_i\",\"type\":\"int256[]\"},{\"indexed\":false,\"name\":\"_b\",\"type\":\"bool[]\"},{\"indexed\":false,\"name\":\"_addr\",\"type\":\"address[]\"},{\"indexed\":false,\"name\":\"_bs32\",\"type\":\"bytes32[]\"},{\"indexed\":false,\"name\":\"_s\",\"type\":\"string[]\"},{\"indexed\":false,\"name\":\"_bs\",\"type\":\"bytes[]\"}],\"name\":\"TestEventDArrayParams\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"_u\",\"type\":\"uint256[4]\"},{\"indexed\":false,\"name\":\"_i\",\"type\":\"int256[4]\"},{\"indexed\":false,\"name\":\"_b\",\"type\":\"bool[4]\"},{\"indexed\":false,\"name\":\"_addr\",\"type\":\"address[4]\"},{\"indexed\":false,\"name\":\"_bs32\",\"type\":\"bytes32[4]\"},{\"indexed\":false,\"name\":\"_s\",\"type\":\"string[4]\"},{\"indexed\":false,\"name\":\"_bs\",\"type\":\"bytes[4]\"}],\"name\":\"TestEventSArrayParams\",\"type\":\"event\"}]", "");
    List<TypeReference<?>> eventTypeList = Arrays.asList(new TypeReference<Uint256>() {
    }, new TypeReference<Int256>() {
    }, new TypeReference<Bool>() {
    }, new TypeReference<Address>() {
    }, new TypeReference<Bytes32>() {
    }, new TypeReference<Utf8String>() {
    }, new TypeReference<DynamicBytes>() {
    });
    Event event = new Event("TestEventSimpleParams", eventTypeList);
    List<Type> eventDataParams1 = Arrays.asList(new Uint256(111111), new Int256(-1111111), new Bool(false), new Address("0x692a70d2e424a56d2c6c27aa97d1a86395877b3a"), new Bytes32("abcdefghiabcdefghiabcdefghiabhji".getBytes()), new Utf8String("章鱼小丸子ljjkl;adjsfkljlkjl"), new DynamicBytes("sadfljkjkljkl".getBytes()));
    List<Type> eventDataParams2 = Arrays.asList(new Uint256(0), new Int256(0), new Bool(true), new Address("0x0"), new Bytes32("                                ".getBytes()), new Utf8String(""), new DynamicBytes("".getBytes()));
    List<Type> eventDataParams3 = Arrays.asList(new Uint256(654321), new Int256(123456), new Bool(false), new Address("0x692a70d2e424a56d2c6c27aa97d1a86395877b3a"), new Bytes32("                                ".getBytes()), new Utf8String("jlkjlkjasdfjlkj   fsadjkljlk j章鱼小丸子"), new DynamicBytes("jlkjlkjasdfjlkj   fsadjkljlk j章鱼小丸子".getBytes()));
    List<String> topics = new ArrayList<String>();
    topics.add(EventEncoder.encode(event));
    Log log1 = new Log();
    log1.setData(FunctionEncoder.encodeConstructor(eventDataParams1));
    log1.setTopics(topics);
    Log log2 = new Log();
    log2.setData(FunctionEncoder.encodeConstructor(eventDataParams2));
    log2.setTopics(topics);
    Log log3 = new Log();
    log3.setData(FunctionEncoder.encodeConstructor(eventDataParams3));
    log3.setTopics(topics);
    AbiDefinition abiDefinition = null;
    Tuple2<AbiDefinition, List<EventResultEntity>> tupleResult1 = decode.decodeEventReturnObject(log1);
    assertThat(transEntitytoType0(tupleResult1.getValue2()), is(eventDataParams1));
    abiDefinition = tupleResult1.getValue1();
    Tuple2<AbiDefinition, List<EventResultEntity>> tupleResult2 = decode.decodeEventReturnObject(log2);
    assertThat(transEntitytoType0(tupleResult2.getValue2()), is(eventDataParams2));
    Tuple2<AbiDefinition, List<EventResultEntity>> tupleResult3 = decode.decodeEventReturnObject(log3);
    assertThat(transEntitytoType0(tupleResult3.getValue2()), is(eventDataParams3));
    List<Log> logList1 = new ArrayList<Log>();
    logList1.add(log1);
    Map<String, List<List<EventResultEntity>>> mapResult1 = decode.decodeEventReturnObject(logList1);
    assertThat(transEntitytoType0(mapResult1.get(decodeMethodSign(abiDefinition)).get(0)), is(eventDataParams1));
    assertThat(decode.decodeEventReturnJson(logList1), is("{\"TestEventSimpleParams(uint256,int256,bool,address,bytes32,string,bytes)\":[[{\"name\":\"_u\",\"type\":\"uint256\",\"data\":111111,\"indexed\":false},{\"name\":\"_i\",\"type\":\"int256\",\"data\":-1111111,\"indexed\":false},{\"name\":\"_b\",\"type\":\"bool\",\"data\":false,\"indexed\":false},{\"name\":\"_addr\",\"type\":\"address\",\"data\":\"0x692a70d2e424a56d2c6c27aa97d1a86395877b3a\",\"indexed\":false},{\"name\":\"_bs32\",\"type\":\"bytes32\",\"data\":\"abcdefghiabcdefghiabcdefghiabhji\",\"indexed\":false},{\"name\":\"_s\",\"type\":\"string\",\"data\":\"章鱼小丸子ljjkl;adjsfkljlkjl\",\"indexed\":false},{\"name\":\"_bs\",\"type\":\"bytes\",\"data\":\"sadfljkjkljkl\",\"indexed\":false}]]}"));
    List<Log> logList2 = new ArrayList<Log>();
    logList2.add(log1);
    logList2.add(log2);
    Map<String, List<List<EventResultEntity>>> mapResult2 = decode.decodeEventReturnObject(logList2);
    assertThat(transEntitytoType0(mapResult2.get(decodeMethodSign(abiDefinition)).get(0)), is(eventDataParams1));
    assertThat(transEntitytoType0(mapResult2.get(decodeMethodSign(abiDefinition)).get(1)), is(eventDataParams2));
    assertThat(decode.decodeEventReturnJson(logList2), is("{\"TestEventSimpleParams(uint256,int256,bool,address,bytes32,string,bytes)\":[[{\"name\":\"_u\",\"type\":\"uint256\",\"data\":111111,\"indexed\":false},{\"name\":\"_i\",\"type\":\"int256\",\"data\":-1111111,\"indexed\":false},{\"name\":\"_b\",\"type\":\"bool\",\"data\":false,\"indexed\":false},{\"name\":\"_addr\",\"type\":\"address\",\"data\":\"0x692a70d2e424a56d2c6c27aa97d1a86395877b3a\",\"indexed\":false},{\"name\":\"_bs32\",\"type\":\"bytes32\",\"data\":\"abcdefghiabcdefghiabcdefghiabhji\",\"indexed\":false},{\"name\":\"_s\",\"type\":\"string\",\"data\":\"章鱼小丸子ljjkl;adjsfkljlkjl\",\"indexed\":false},{\"name\":\"_bs\",\"type\":\"bytes\",\"data\":\"sadfljkjkljkl\",\"indexed\":false}],[{\"name\":\"_u\",\"type\":\"uint256\",\"data\":0,\"indexed\":false},{\"name\":\"_i\",\"type\":\"int256\",\"data\":0,\"indexed\":false},{\"name\":\"_b\",\"type\":\"bool\",\"data\":true,\"indexed\":false},{\"name\":\"_addr\",\"type\":\"address\",\"data\":\"0x0000000000000000000000000000000000000000\",\"indexed\":false},{\"name\":\"_bs32\",\"type\":\"bytes32\",\"data\":\"\",\"indexed\":false},{\"name\":\"_s\",\"type\":\"string\",\"data\":\"\",\"indexed\":false},{\"name\":\"_bs\",\"type\":\"bytes\",\"data\":\"\",\"indexed\":false}]]}"));
    List<Log> logList3 = new ArrayList<Log>();
    logList3.add(log1);
    logList3.add(log2);
    logList3.add(log3);
    Map<String, List<List<EventResultEntity>>> mapResult3 = decode.decodeEventReturnObject(logList3);
    assertThat(transEntitytoType0(mapResult3.get(decodeMethodSign(abiDefinition)).get(0)), is(eventDataParams1));
    assertThat(transEntitytoType0(mapResult3.get(decodeMethodSign(abiDefinition)).get(1)), is(eventDataParams2));
    assertThat(transEntitytoType0(mapResult3.get(decodeMethodSign(abiDefinition)).get(2)), is(eventDataParams3));
    assertThat(decode.decodeEventReturnJson(logList3), is("{\"TestEventSimpleParams(uint256,int256,bool,address,bytes32,string,bytes)\":[[{\"name\":\"_u\",\"type\":\"uint256\",\"data\":111111,\"indexed\":false},{\"name\":\"_i\",\"type\":\"int256\",\"data\":-1111111,\"indexed\":false},{\"name\":\"_b\",\"type\":\"bool\",\"data\":false,\"indexed\":false},{\"name\":\"_addr\",\"type\":\"address\",\"data\":\"0x692a70d2e424a56d2c6c27aa97d1a86395877b3a\",\"indexed\":false},{\"name\":\"_bs32\",\"type\":\"bytes32\",\"data\":\"abcdefghiabcdefghiabcdefghiabhji\",\"indexed\":false},{\"name\":\"_s\",\"type\":\"string\",\"data\":\"章鱼小丸子ljjkl;adjsfkljlkjl\",\"indexed\":false},{\"name\":\"_bs\",\"type\":\"bytes\",\"data\":\"sadfljkjkljkl\",\"indexed\":false}],[{\"name\":\"_u\",\"type\":\"uint256\",\"data\":0,\"indexed\":false},{\"name\":\"_i\",\"type\":\"int256\",\"data\":0,\"indexed\":false},{\"name\":\"_b\",\"type\":\"bool\",\"data\":true,\"indexed\":false},{\"name\":\"_addr\",\"type\":\"address\",\"data\":\"0x0000000000000000000000000000000000000000\",\"indexed\":false},{\"name\":\"_bs32\",\"type\":\"bytes32\",\"data\":\"\",\"indexed\":false},{\"name\":\"_s\",\"type\":\"string\",\"data\":\"\",\"indexed\":false},{\"name\":\"_bs\",\"type\":\"bytes\",\"data\":\"\",\"indexed\":false}],[{\"name\":\"_u\",\"type\":\"uint256\",\"data\":654321,\"indexed\":false},{\"name\":\"_i\",\"type\":\"int256\",\"data\":123456,\"indexed\":false},{\"name\":\"_b\",\"type\":\"bool\",\"data\":false,\"indexed\":false},{\"name\":\"_addr\",\"type\":\"address\",\"data\":\"0x692a70d2e424a56d2c6c27aa97d1a86395877b3a\",\"indexed\":false},{\"name\":\"_bs32\",\"type\":\"bytes32\",\"data\":\"\",\"indexed\":false},{\"name\":\"_s\",\"type\":\"string\",\"data\":\"jlkjlkjasdfjlkj   fsadjkljlk j章鱼小丸子\",\"indexed\":false},{\"name\":\"_bs\",\"type\":\"bytes\",\"data\":\"jlkjlkjasdfjlkj   fsadjkljlk j章鱼小丸子\",\"indexed\":false}]]}"));
}
Also used : Address(org.fisco.bcos.web3j.abi.datatypes.Address) ArrayList(java.util.ArrayList) Utf8String(org.fisco.bcos.web3j.abi.datatypes.Utf8String) Bytes32(org.fisco.bcos.web3j.abi.datatypes.generated.Bytes32) DynamicBytes(org.fisco.bcos.web3j.abi.datatypes.DynamicBytes) Bool(org.fisco.bcos.web3j.abi.datatypes.Bool) AbiDefinition(org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition) ArrayList(java.util.ArrayList) List(java.util.List) TypeReference(org.fisco.bcos.web3j.abi.TypeReference) Log(org.fisco.bcos.web3j.protocol.core.methods.response.Log) Int256(org.fisco.bcos.web3j.abi.datatypes.generated.Int256) Utf8String(org.fisco.bcos.web3j.abi.datatypes.Utf8String) Type(org.fisco.bcos.web3j.abi.datatypes.Type) NamedType(org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition.NamedType) Event(org.fisco.bcos.web3j.abi.datatypes.Event) Uint256(org.fisco.bcos.web3j.abi.datatypes.generated.Uint256) Test(org.junit.Test)

Example 25 with TypeReference

use of org.fisco.bcos.web3j.abi.TypeReference in project web3sdk by FISCO-BCOS.

the class CallContractTest method testSyncCallContract.

private static void testSyncCallContract(CallContract callContract, String address) {
    CallResult contractResult;
    contractResult = callContract.call(address, "getStringOld", new Utf8String("hello world"), new Int256(10086), new Bool(true));
    List<TypeReference<?>> referencesList = Arrays.<TypeReference<?>>asList(new TypeReference<Utf8String>() {
    });
    List<Type> returnList1 = FunctionReturnDecoder.decode(contractResult.getOutput(), Utils.convert(referencesList));
    System.out.println("call getStringOld: " + (String) returnList1.get(0).getValue());
    TransactionReceipt receipt;
    receipt = callContract.sendTransaction(gasPrice, gasLimit, address, "setAndget", new Utf8String("hello world"), new Int256(10086));
    referencesList = Arrays.<TypeReference<?>>asList(new TypeReference<Utf8String>() {
    }, new TypeReference<Int256>() {
    });
    List<Type> returnList2 = FunctionReturnDecoder.decode(receipt.getOutput(), Utils.convert(referencesList));
    System.out.println("call setAndget: " + (String) returnList2.get(0).getValue() + ", " + (BigInteger) returnList2.get(1).getValue());
    receipt = callContract.sendTransaction(address, "setAndget", new Utf8String("hello world"), new Int256(10086));
    referencesList = Arrays.<TypeReference<?>>asList(new TypeReference<Utf8String>() {
    }, new TypeReference<Int256>() {
    });
    List<Type> returnList3 = FunctionReturnDecoder.decode(receipt.getOutput(), Utils.convert(referencesList));
    System.out.println("default call setAndget: " + (String) returnList3.get(0).getValue() + ", " + (BigInteger) returnList3.get(1).getValue());
    contractResult = callContract.call(address, "getArray", new StaticArray2(typeMap(Arrays.asList(BigInteger.valueOf(-1), BigInteger.valueOf(2)), Int16.class)), new DynamicArray(typeMap(Arrays.asList(BigInteger.valueOf(2), BigInteger.valueOf(2)), Uint16.class)));
    List<Type> returnList4 = callContract.decode(contractResult.getOutput(), new TypeReference<StaticArray2<Int16>>() {
    }, new TypeReference<DynamicArray<Int16>>() {
    });
    System.out.println("call getArray: " + callContract.convertList((List<Type>) returnList4.get(0).getValue()) + ", " + callContract.convertList((List<Type>) returnList4.get(1).getValue()));
    List<List<BigInteger>> dyadicArray = new ArrayList<List<BigInteger>>();
    dyadicArray.add(Arrays.asList(BigInteger.valueOf(-1), BigInteger.valueOf(2)));
    dyadicArray.add(Arrays.asList(BigInteger.valueOf(-1), BigInteger.valueOf(992)));
    byte[] bytes = new byte[] { 'a', 'b' };
    contractResult = callContract.call(address, "newTest", new StaticArray2(typeMap(dyadicArray, StaticArray2.class, Int256.class)), new DynamicBytes(bytes));
    List<Type> returnList5 = callContract.decode(contractResult.getOutput(), new TypeReference<StaticArray2<StaticArray2<Int256>>>() {
    }, new TypeReference<DynamicBytes>() {
    });
    System.out.println("call newTest: " + callContract.convertListList((List<StaticArray<Int256>>) returnList5.get(0).getValue()) + ", " + new String((byte[]) returnList5.get(1).getValue()) + ", " + dyadicArray);
}
Also used : ArrayList(java.util.ArrayList) Utf8String(org.fisco.bcos.web3j.abi.datatypes.Utf8String) Int16(org.fisco.bcos.web3j.abi.datatypes.generated.Int16) DynamicBytes(org.fisco.bcos.web3j.abi.datatypes.DynamicBytes) Bool(org.fisco.bcos.web3j.abi.datatypes.Bool) Uint16(org.fisco.bcos.web3j.abi.datatypes.generated.Uint16) ArrayList(java.util.ArrayList) List(java.util.List) TypeReference(org.fisco.bcos.web3j.abi.TypeReference) StaticArray(org.fisco.bcos.web3j.abi.datatypes.StaticArray) TransactionReceipt(org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceipt) Utf8String(org.fisco.bcos.web3j.abi.datatypes.Utf8String) Int256(org.fisco.bcos.web3j.abi.datatypes.generated.Int256) Type(org.fisco.bcos.web3j.abi.datatypes.Type) DynamicArray(org.fisco.bcos.web3j.abi.datatypes.DynamicArray) BigInteger(java.math.BigInteger) StaticArray2(org.fisco.bcos.web3j.abi.datatypes.generated.StaticArray2)

Aggregations

TypeReference (org.fisco.bcos.web3j.abi.TypeReference)47 Type (org.fisco.bcos.web3j.abi.datatypes.Type)45 Utf8String (org.fisco.bcos.web3j.abi.datatypes.Utf8String)42 Function (org.fisco.bcos.web3j.abi.datatypes.Function)38 Tuple1 (org.fisco.bcos.web3j.tuples.generated.Tuple1)26 EncryptType (org.fisco.bcos.web3j.crypto.EncryptType)11 Int256 (org.fisco.bcos.web3j.abi.datatypes.generated.Int256)10 ArrayList (java.util.ArrayList)9 NamedType (org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition.NamedType)9 Bool (org.fisco.bcos.web3j.abi.datatypes.Bool)8 DynamicBytes (org.fisco.bcos.web3j.abi.datatypes.DynamicBytes)8 Bytes32 (org.fisco.bcos.web3j.abi.datatypes.generated.Bytes32)8 Address (org.fisco.bcos.web3j.abi.datatypes.Address)7 Uint256 (org.fisco.bcos.web3j.abi.datatypes.generated.Uint256)7 Test (org.junit.Test)7 Event (org.fisco.bcos.web3j.abi.datatypes.Event)6 AbiDefinition (org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition)6 Tuple2 (org.fisco.bcos.web3j.tuples.generated.Tuple2)6 List (java.util.List)5 DynamicArray (org.fisco.bcos.web3j.abi.datatypes.DynamicArray)5