@parity/api
Version:
The Parity Promise-based API library for interfacing with Ethereum over RPC
66 lines (65 loc) • 2.77 kB
JavaScript
;
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
// This file is part of Parity.
//
// SPDX-License-Identifier: MIT
Object.defineProperty(exports, "__esModule", { value: true });
var encode_1 = require("./encode");
var ABI = {
type: 'function',
name: 'valid',
inputs: [{ type: 'uint256' }, { type: 'bool' }]
};
var RESULT = [
'f87fa141',
'0000000000000000000000000000000000000000000000000000000000000123',
'0000000000000000000000000000000000000000000000000000000000000001'
].join('');
var VARIABLE = [
'5a6fbce0',
'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470',
'0000000000000000000000000000000000000000000000000000000000000040',
'000000000000000000000000000000000000000000000000000000000000000f',
'687474703a2f2f666f6f2e6261722f0000000000000000000000000000000000'
].join('');
describe('util/encode', function () {
describe('encodeMethodCallAbi', function () {
it('encodes calls with the correct result', function () {
expect(encode_1.encodeMethodCallAbi(ABI, [0x123, true])).toEqual("0x" + RESULT);
});
});
describe('abiEncode', function () {
it('encodes calls with the correct result', function () {
expect(encode_1.abiEncode('valid', ['uint256', 'bool'], [0x123, true])).toEqual("0x" + RESULT);
});
it('encodes variable values', function () {
expect(encode_1.abiEncode('hintUrl', ['bytes32', 'string'], [
'0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470',
'http://foo.bar/'
])).toEqual("0x" + VARIABLE);
});
it('encodes only the data with null name', function () {
expect(encode_1.abiEncode(undefined, ['uint256', 'bool'], [0x123, true])).toEqual("0x" + RESULT.substr(8));
});
});
describe('abiUnencode', function () {
it('decodes data correctly from abi', function () {
expect(encode_1.abiUnencode([
{
name: 'test',
type: 'function',
inputs: [{ type: 'uint', name: 'arga' }]
}
], '0x1acb6f7700000000000000000000000000000038')).toEqual(['test', { arga: 56 }, [56]]);
});
it('returns null when function not found', function () {
expect(encode_1.abiUnencode([], '0x12345678')).toBe(null);
});
});
// Same example as in abi/util/signature.spec.js
describe('abiSignature', function () {
it('encodes baz(uint32,bool) correctly', function () {
expect(encode_1.abiSignature('baz', ['uint32', 'bool'])).toEqual('0xcdcd77c0992ec5bbfc459984220f8c45084cc24d9b6efed1fae540db8de801d2');
});
});
});