Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change contract.options.jsonInterface - ts issue #5474

Closed
1 task done
jdevcs opened this issue Sep 26, 2022 · 3 comments
Closed
1 task done

Change contract.options.jsonInterface - ts issue #5474

jdevcs opened this issue Sep 26, 2022 · 3 comments
Assignees
Labels
4.x 4.0 related Bug Addressing a bug

Comments

@jdevcs
Copy link
Contributor

jdevcs commented Sep 26, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

Changing contract abi via:
contract.options.jsonInterface = another_abi_json;

doesn't allow in ts with following error:

error TS2322: Type '({ anonymous: boolean; inputs: { indexed: boolean; internalType: string; name: string; type: string; }[]; name: string; type: string; outputs?: undefined; stateMutability?: undefined; } | { inputs: never[]; ... 4 more ...; anonymous?: undefined; } | { ...; })[]' is not assignable to type 'ContractAbiWithSignature'.

      Type '{ anonymous: boolean; inputs: { indexed: boolean; internalType: string; name: string; type: string; }[]; name: string; type: string; outputs?: undefined; stateMutability?: undefined; } | { inputs: never[]; ... 4 more ...; anonymous?: undefined; } | { ...; }' is not assignable to type 'AbiFragment & { signature: string; }'.
        Type '{ anonymous: boolean; inputs: { indexed: boolean; internalType: string; name: string; type: string; }[]; name: string; type: string; outputs?: undefined; stateMutability?: undefined; }' is not assignable to type 'AbiFragment & { signature: string; }'.
          Type '{ anonymous: boolean; inputs: { indexed: boolean; internalType: string; name: string; type: string; }[]; name: string; type: string; outputs?: undefined; stateMutability?: undefined; }' is not assignable to type 'AbiBaseFragment & { readonly name: never; readonly type: string; readonly stateMutability: string; readonly inputs: never; readonly outputs: never; readonly constant?: boolean | undefined; readonly payable?: boolean | undefined; } & { ...; }'.
            Type '{ anonymous: boolean; inputs: { indexed: boolean; internalType: string; name: string; type: string; }[]; name: string; type: string; outputs?: undefined; stateMutability?: undefined; }' is not assignable to type '{ readonly name: never; readonly type: string; readonly stateMutability: string; readonly inputs: never; readonly outputs: never; readonly constant?: boolean | undefined; readonly payable?: boolean | undefined; }'.

Expected Behavior

On contract instance contract.options.jsonInterface = another_abi_json; should reset and should regenerate the methods and events of the contract instance without ts error.

Steps to Reproduce

			const contract = new Contract(
				sampleStorageContractABI,
				addr,
			);

			contract.options.jsonInterface = sampleStorageContractABI__2;

Web3.js Version

4.0.1-alpha.0

Environment

  • Operating System:
  • Browser:
  • Node.js Version:
  • NPM Version:

Anything Else?

No response

@jdevcs jdevcs added Bug Addressing a bug 4.x 4.0 related labels Sep 26, 2022
@jdevcs
Copy link
Contributor Author

jdevcs commented Oct 4, 2022

Investigate to fix this if its not time consuming.
else make this as breaking change as for its alternative users can create new contract instance explicitly if abi changes,

@nikoulai nikoulai self-assigned this Oct 25, 2022
@nikoulai
Copy link
Contributor

I ran the next example and it seems to be working

acc = await createTempAccount();
// just to deploy the contracts, so we can use them later
const sendOptions = { from: acc.address, gas: '10000000' };

const tempcontract: Contract<typeof BasicAbi> = new Contract(BasicAbi, undefined, {
    provider: 'ws://localhost:8545',
});

const tempcontract2: Contract<typeof GreeterAbi> = new Contract(GreeterAbi, undefined, {
    provider: 'ws://localhost:8545',
});

const deployedBasicContract: any = await tempcontract
.deploy({
	data: BasicBytecode,
	arguments: [10, 'string init value'],
})
.send(sendOptions);

const deployedGreeterContract = await tempcontract2
.deploy({ data: GreeterBytecode, arguments: ['Greeting'] })
.send(sendOptions);

//end deploying

// test basic contract is working
const boolValue = await deployedBasicContract.methods.getBoolValue().call();

console.log('boolValue', boolValue);

// change interface and address
deployedBasicContract.options.jsonInterface = GreeterAbi;
deployedBasicContract['_address'] = deployedGreeterContract['_address'];

// test we use the other abi
const tx = await deployedBasicContract.methods
.setGreeting('New Greeting')
.send({ from: acc.address });

const res = await deployedBasicContract.methods.greet().call();

console.log('greeting', res);

I believe, what makes this example work is the declaration of deployedBasicContract as any, as it allows the contract to accept any abi.
I think a mention in the docs, on how to change jsonInterface for typescript users, and that they will lose type safeness would be enough. If we try to make the Abi type in Contract class more generic, we will lose type safeness for all cases.

(@jdevcs @luu-alex @avkos @Muhammad-Altabba )

@Muhammad-Altabba
Copy link
Contributor

Muhammad-Altabba commented Nov 22, 2022

It turned out that the declared type of jsonInterface is ContractAbiWithSignature and this is not accurate. Because, the user can assign any ContractAbi and then the signatures of the functions and events will be calculated. So, jsonInterface should accept assigning the type ContractAbi. But should return the type ContractAbiWithSignature.

So the following old definition:

	jsonInterface: ContractAbiWithSignature;

Needs to be replaced with:

	get jsonInterface(): ContractAbiWithSignature;
	set jsonInterface(value: ContractAbi);

And so this MR: #5645 should resolve this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4.x 4.0 related Bug Addressing a bug
Projects
None yet
Development

No branches or pull requests

3 participants