Skip to content

Commit

Permalink
Add abiitem (#6778)
Browse files Browse the repository at this point in the history
* add abiitem

* update

* update contract

* update changelog

* update abi to fixture
  • Loading branch information
Alex authored Feb 6, 2024
1 parent a6b685e commit ed1460c
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 12 deletions.
82 changes: 82 additions & 0 deletions packages/web3-eth-contract/test/fixtures/Abi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
anonymous: false,
constant: true,
inputs: [
{
name: 'testMe',
type: 'uint256[3]'
},
{
name: 'inputA',
type: 'tuple',
components: [
{
name: 'a',
type: 'uint8'
},
{
name: 'b',
type: 'uint8'
}
]
},
{
name: 'inputB',
type: 'tuple[]',
components: [
{
name: 'a1',
type: 'uint256'
},
{
name: 'a2',
type: 'uint256'
}
]
},
{
name: 'inputC',
type: 'uint8',
indexed: false
}
],
name: "testName",
outputs: [
{
name: "test",
type: "uint256"
},
{
name: 'outputA',
type: 'tuple',
components: [
{
name: 'a',
type: 'uint8'
},
{
name: 'b',
type: 'uint8'
}
]
},
{
name: 'outputB',
type: 'tuple[]',
components: [
{
name: 'a1',
type: 'uint256'
},
{
name: 'a2',
type: 'uint256'
}
]
}
],
payable: false,
stateMutability: "pure",
type: "function",
gas: 175875
}
13 changes: 12 additions & 1 deletion packages/web3-eth-contract/test/unit/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import { ValidChains, Hardfork, AccessListResult, Address, ETH_DATA_FORMAT , DEF
import { Web3ContractError } from 'web3-errors';
import { Web3Context , Web3ConfigEvent } from 'web3-core';
import { Web3ValidatorError } from 'web3-validator';

import { AbiItem } from 'web3-utils';
import {Abi} from '../fixtures/Abi.json'
import { Contract } from '../../src';
import { sampleStorageContractABI } from '../fixtures/storage';
import { GreeterAbi, GreeterBytecode } from '../shared_fixtures/build/Greeter';
Expand Down Expand Up @@ -110,6 +111,16 @@ describe('Contract', () => {
expect(contract).toBeInstanceOf(Contract);
});

it('should init with abiItem, options and context', () => {
const contract = new Contract(
[Abi as AbiItem],
{ gas: '123' },
{ config: { defaultAccount: '0x00000000219ab540356cBB839Cbe05303d7705Fa' } },
);

expect(contract).toBeInstanceOf(Contract);
});

it('should init with abi, address and options', () => {
const contract = new Contract([], '0x00000000219ab540356cBB839Cbe05303d7705Fa', {
gas: '123',
Expand Down
6 changes: 5 additions & 1 deletion packages/web3-types/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,8 @@ Documentation:
- Interface `MetaMaskProvider` added and is part of `SupportedProviders` (#6534)
- `gasPrice` was added to `Transaction1559UnsignedAPI` type. (#6539)

## [Unreleased]
## [Unreleased]

### Added

- Adds missing exported type `AbiItem` from 1.x to v4 for compatabiltiy (#6678)
31 changes: 22 additions & 9 deletions packages/web3-types/src/eth_abi_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,6 @@ export type AbiErrorFragment = AbiBaseFragment & {
readonly inputs?: ReadonlyArray<AbiParameter>;
};

// https://docs.soliditylang.org/en/latest/abi-spec.html#json
export type AbiFragment =
| AbiConstructorFragment
| AbiFunctionFragment
| AbiEventFragment
| AbiErrorFragment
| AbiFallbackFragment;

export type ContractAbi = ReadonlyArray<AbiFragment>;

export type AbiInput =
| string
Expand All @@ -161,6 +152,28 @@ export type AbiInput =
}
| { readonly [key: string]: unknown };

export interface AbiOutput {
name: string;
type: string;
components?: AbiOutput[];
internalType?: string;
}



// https://docs.soliditylang.org/en/latest/abi-spec.html#json
export type AbiFragment =
| AbiConstructorFragment
| AbiFunctionFragment
| AbiEventFragment
| AbiErrorFragment
| AbiFallbackFragment

// to be compatible with web3js v1
export type AbiItem = AbiFragment
export type ContractAbi = ReadonlyArray<AbiFragment> | ReadonlyArray<AbiItem>


// https://docs.soliditylang.org/en/develop/abi-spec.html#json
export type JsonFunctionInterface = {
type: 'function';
Expand Down
6 changes: 5 additions & 1 deletion packages/web3-utils/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,8 @@ Documentation:

- Fixed an issue with detecting Uint8Array (#6486)

## [Unreleased]
## [Unreleased]

### Added

- Adds missing exported type `AbiItem` from 1.x to v4 for compatabiltiy (#6678)
2 changes: 2 additions & 0 deletions packages/web3-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ export * from './uuid.js';
export * from './web3_eip1193_provider.js';
export * from './socket_provider.js';
export * from './uint8array.js';
// for backwards compatibility with v1
export {AbiItem} from 'web3-types';

1 comment on commit ed1460c

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: ed1460c Previous: 6c075db Ratio
processingTx 9622 ops/sec (±3.46%) 9301 ops/sec (±4.81%) 0.97
processingContractDeploy 39633 ops/sec (±7.79%) 39129 ops/sec (±7.62%) 0.99
processingContractMethodSend 19838 ops/sec (±7.22%) 19443 ops/sec (±5.19%) 0.98
processingContractMethodCall 40569 ops/sec (±6.18%) 38971 ops/sec (±6.34%) 0.96
abiEncode 46622 ops/sec (±6.63%) 44252 ops/sec (±6.92%) 0.95
abiDecode 33060 ops/sec (±7.49%) 30419 ops/sec (±8.89%) 0.92
sign 1641 ops/sec (±4.67%) 1656 ops/sec (±4.08%) 1.01
verify 382 ops/sec (±0.42%) 373 ops/sec (±0.78%) 0.98

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.