Skip to content

Commit

Permalink
change default to data (#6622)
Browse files Browse the repository at this point in the history
* change default to data

* fix config tests

* fix test

* address feedback and update changelog
  • Loading branch information
Alex committed Nov 28, 2023
1 parent e6d8c14 commit b3ccd5c
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 49 deletions.
4 changes: 4 additions & 0 deletions packages/web3-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,7 @@ Documentation:
- Added `isMetaMaskProvider` function to check if provider is metamask (#6534)

## [Unreleased]

### Changed

- Web3config `contractDataInputFill` has been defaulted to `data`, istead of `input`. (#6622)
2 changes: 1 addition & 1 deletion packages/web3-core/src/web3_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export abstract class Web3Config
transactionConfirmationPollingInterval: undefined,
blockHeaderTimeout: 10,
maxListenersWarningThreshold: 100,
contractDataInputFill: 'input',
contractDataInputFill: 'data',
defaultNetworkId: undefined,
defaultChain: 'mainnet',
defaultHardfork: 'london',
Expand Down
34 changes: 2 additions & 32 deletions packages/web3-core/src/web3_request_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import {
Web3APISpec,
Web3BaseProvider,
Web3BaseProviderConstructor,
JsonRpcRequest
} from 'web3-types';
import { isNullish, isPromise, jsonRpc, isResponseRpcError } from 'web3-utils';
import {
Expand All @@ -51,7 +50,6 @@ import {
isLegacySendAsyncProvider,
isLegacySendProvider,
isWeb3Provider,
isMetaMaskProvider,
} from './utils.js';
import { Web3EventEmitter } from './web3_event_emitter.js';

Expand All @@ -68,24 +66,6 @@ const availableProviders: {
WebsocketProvider: WSProvider as Web3BaseProviderConstructor,
};


// if input was provided in params, change to data due to metamask only accepting data
const metamaskPayload = (payload: JsonRpcRequest) => {

if(Array.isArray(payload.params)) {
const params = payload.params[0] as Record<string, unknown>
if (params.input && !params.data) {

return {...payload,
params: [{...params,
data: params.data ?? params.input}]
}
}
}
return payload;

}

export class Web3RequestManager<
API extends Web3APISpec = EthExecutionAPI,
> extends Web3EventEmitter<{
Expand Down Expand Up @@ -208,20 +188,10 @@ export class Web3RequestManager<
);
}

let payload = jsonRpc.isBatchRequest(request)
? jsonRpc.toBatchPayload(request)
const payload = jsonRpc.isBatchRequest(request)
? jsonRpc.toBatchPayload(request)
: jsonRpc.toPayload(request);

if(isMetaMaskProvider(provider)){ // metamask send_transaction accepts data and not input, so we change it
if ((payload as JsonRpcRequest<ResponseType>).method === 'eth_sendTransaction'){
if(!jsonRpc.isBatchRequest(payload)){
payload = metamaskPayload(payload as JsonRpcRequest)
} else {
payload = payload.map(p => metamaskPayload(p as JsonRpcRequest))
}
}
}

if (isWeb3Provider(provider)) {
let response;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Object {
"accountProvider": undefined,
"config": Object {
"blockHeaderTimeout": 10,
"contractDataInputFill": "input",
"contractDataInputFill": "data",
"defaultAccount": undefined,
"defaultBlock": "latest",
"defaultChain": "mainnet",
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-core/test/unit/web3_config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const defaultConfig = {
useRpcCallSpecification: false,
},
handleRevert: false,
contractDataInputFill: 'input',
contractDataInputFill: 'data',
maxListenersWarningThreshold: 100,
transactionBlockTimeout: 50,
transactionConfirmationBlocks: 24,
Expand Down
6 changes: 5 additions & 1 deletion packages/web3-eth-contract/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,4 +347,8 @@ Documentation:

- Will populate `data` for transactions in contract for metamask provider instead of `input` (#6534)

## [Unreleased]
## [Unreleased]

### Changed

- `contractDataInputFill` has been defaulted to `data`, istead of `input`. (#6622)
16 changes: 8 additions & 8 deletions packages/web3-eth-contract/test/unit/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,9 @@ describe('Contract', () => {
.mockImplementation((_objInstance, _tx) => {
const newContract = contract.clone();
newContract.options.address = deployedAddr;
expect(_tx.input).toBeDefined();
expect(_tx.data).toBeDefined();
if (
_tx.input ===
_tx.data ===
'0xa41368620000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000548656c6c6f000000000000000000000000000000000000000000000000000000'
) {
// eslint-disable-next-line
Expand All @@ -330,7 +330,7 @@ describe('Contract', () => {

const deployedContract = await contract
.deploy({
input: GreeterBytecode,
data: GreeterBytecode,
arguments: ['My Greeting'],
})
.send(sendOptions);
Expand All @@ -340,7 +340,7 @@ describe('Contract', () => {
spyTx.mockClear();
});

it('send method on deployed contract should work using data', async () => {
it('send method on deployed contract should work using data (default)', async () => {
const arg = 'Hello';
const contract = new Contract(GreeterAbi);
sendOptions = {
Expand Down Expand Up @@ -537,13 +537,13 @@ describe('Contract', () => {

const spyEthCall = jest.spyOn(eth, 'call').mockImplementation((_objInstance, _tx) => {
expect(_tx.to).toStrictEqual(deployedAddr);
expect(_tx.input).toBe('0xcfae3217');
expect(_tx.data).toBe('0xcfae3217');
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return Promise.resolve(encodedArg) as any; // contract class should decode encodedArg
});
const deployedContract = await contract
.deploy({
input: GreeterBytecode,
data: GreeterBytecode,
arguments: ['My Greeting'],
})
.send(sendOptions);
Expand Down Expand Up @@ -759,7 +759,7 @@ describe('Contract', () => {
// @ts-expect-error fix-types
const spyEthCall = jest.spyOn(eth, 'call').mockImplementation((_objInstance, _tx) => {
expect(_tx.to).toBe('0x1230B93ffd14F2F022039675fA3fc3A46eE4C701');
expect(_tx.input).toBe(
expect(_tx.data).toBe(
'0x095ea7b300000000000000000000000000000000219ab540356cbb839cbe05303d7705fa0000000000000000000000000000000000000000000000000000000000000001',
);
return '0x00';
Expand Down Expand Up @@ -1449,7 +1449,7 @@ describe('Contract', () => {
.spyOn(eth, 'createAccessList')
.mockImplementation((_objInstance, _tx) => {
expect(_tx.to).toStrictEqual(deployedAddr);
expect(_tx.input).toBe('0xcfae3217');
expect(_tx.data).toBe('0xcfae3217');
expect(_tx.from).toBe(fromAddr);
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return Promise.resolve(result) as any; // contract class should decode encodedArg
Expand Down
6 changes: 3 additions & 3 deletions packages/web3/test/unit/web3.config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ describe('web3config web3 tests', () => {
expect(contract.getContextObject().config.contractDataInputFill).toBe("both");

// web3 config shouldn't change
expect(web3.getContextObject().config.contractDataInputFill).toBe("input");
expect(web3.config.contractDataInputFill).toBe("input");
expect(web3.eth.getContextObject().config.contractDataInputFill).toBe("input")
expect(web3.getContextObject().config.contractDataInputFill).toBe("data");
expect(web3.config.contractDataInputFill).toBe("data");
expect(web3.eth.getContextObject().config.contractDataInputFill).toBe("data")
});
it('should change web3 config context but not contract config context', async () => {
const web3 = new Web3("http://127.0.0.1:8545");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('ContractMethodWrappersPlugin', () => {
method: 'eth_call',
params: [
expect.objectContaining({
input: '0x70a082310000000000000000000000008da5e39ec14b57fb9bcd9aa2b4500e909119795d',
data: '0x70a082310000000000000000000000008da5e39ec14b57fb9bcd9aa2b4500e909119795d',
to: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
}),
'latest',
Expand Down Expand Up @@ -106,7 +106,7 @@ describe('ContractMethodWrappersPlugin', () => {
method: 'eth_sendTransaction',
params: [
expect.objectContaining({
input: '0xa9059cbb0000000000000000000000004f641def1e7845caab95ac717c80416082430d0d000000000000000000000000000000000000000000000000000000000000002a',
data: '0xa9059cbb0000000000000000000000004f641def1e7845caab95ac717c80416082430d0d000000000000000000000000000000000000000000000000000000000000002a',
from: sender,
gasPrice: expectedGasPrice,
maxFeePerGas: undefined,
Expand Down

0 comments on commit b3ccd5c

Please sign in to comment.