Skip to content

Expose Contract options #79

@ondratra

Description

@ondratra

Contracts in web3x have their abi, eth (provider), and default options declared as private, so there is no way to get to these parameters used to construct the Contract. Web3js exposes these parameters as options property. Could we get the same in web3x?
https://web3js.readthedocs.io/en/v1.2.0/web3-eth-contract.html#new-contract

Real-world use case

I have a contract with ERC721 ABI "loaded in it". Since some older contracts follow the initial draft of ERC721 they miss getApproved and isApprovedForAll methods. One such example is CryptoKitties contract, which fortunately implement getApproved functionality via it's own kittyIndexToApproved.

At some point in code I want to do this:

const regularERC721Contract = new Contract<void>(eth, erc721ABI, contractAddress)

... more code here ...

// then clone contract and to append ABI for CK function `kittyIndexToApproved`
const extraAbi: ContractEntryDefinition[] = [{
    constant: true,
    inputs: [
        {
            'name': '',
            'type': 'uint256'
        }
    ],
    name: 'kittyIndexToApproved',
    outputs: [
        {
            'name': '',
            'type': 'address'
        }
    ],
    payable: false,
    stateMutability: 'view' as 'view',
    type: 'function' as 'function'
}]

const oldAbi = []
    .concat((contract as any).contractAbi.functions)
    .concat((contract as any).contractAbi.events)
    .concat((contract as any).contractAbi.ctor)
    .map(item => item.entry)
const newAbi: ContractAbi = new ContractAbi([
    ...oldAbi,
    ...abiToAppend,
])

const ckContract = new Contract<void>((contract as any).eth, newAbi, contract.address, (contract as any).defaultOptions)

Because of missing options property, I have to hack my way to Contract's private properties (via casting contract to any) that may be changed in the future.

With options feature code will be reduced to:

const regularERC721Contract = new Contract<void>(eth, erc721ABI, contractAddress)
const extraAbi: ContractEntryDefinition[] = [{
    ...
}]
const newAbi: ContractAbi = new ContractAbi([
    ...regularERC721Contract.options.jsonInterface,
    ...extraAbi
])

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions