Skip to content

Commit

Permalink
fix config to emit properly
Browse files Browse the repository at this point in the history
  • Loading branch information
luu-alex committed Oct 25, 2023
1 parent bb17b1f commit 62ed18d
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 20 deletions.
4 changes: 4 additions & 0 deletions packages/web3-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,7 @@ Documentation:
- Fix the issue: "Uncaught TypeError: Class extends value undefined is not a constructor or null #6371". (#6398)

## [Unreleased]

### Added

- Added to `Web3Config` property `disableSuggestedFee` allowing users to disable the suggested fee web3js recommends in metamask.
5 changes: 4 additions & 1 deletion packages/web3-core/src/web3_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,15 @@ export abstract class Web3Config

public constructor(options?: Partial<Web3ConfigOptions>) {
super();

this.setConfig(options ?? {});
}

public setConfig(options: Partial<Web3ConfigOptions>) {
// TODO: Improve and add key check
const keys = Object.keys(options) as (keyof Web3ConfigOptions)[];
for (const key of keys) {
this._triggerConfigChange(key, options[key])
}
Object.assign(this.config, options);
}

Expand Down
1 change: 0 additions & 1 deletion packages/web3-core/src/web3_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ export class Web3Context<
| Web3ContextInitOptions<API, RegisteredSubs>,
) {
super();

// If "providerOrContext" is provided as "string" or an objects matching "SupportedProviders" interface
if (
isNullish(providerOrContext) ||
Expand Down
7 changes: 1 addition & 6 deletions packages/web3-eth-abi/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,11 @@ Documentation:

- Dependencies updated

## [4.1.3]

### Fixed

- Fix issue with default config with babel (and React): "TypeError: Cannot convert a BigInt value to a number #6187" (#6506)

## [4.1.4]

### Fixed

- Fix issue with default config with babel (and React): "TypeError: Cannot convert a BigInt value to a number #6187" (#6506)
- Bug fix of `ERR_UNSUPPORTED_DIR_IMPORT` in ABI (#6535)

## [Unreleased]
4 changes: 4 additions & 0 deletions packages/web3-eth-contract/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,7 @@ Documentation:
- Dependencies updated

## [Unreleased]

### Fixed

- Will populate `data` for transactions in contract for metamask provider instead of `input` (#65)
4 changes: 4 additions & 0 deletions packages/web3-eth-contract/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@ export class Contract<Abi extends ContractAbi>
provider,
registeredSubscriptions: contractSubscriptions,
});

// console.log(this.getContextObject());
if (
!isNullish(options) &&
!isNullish(options.data) &&
Expand Down Expand Up @@ -380,6 +382,8 @@ export class Contract<Abi extends ContractAbi>
(options as ContractInitOptions)?.dataInputFill ??
this.config.contractDataInputFill;
}
this.config.contractDataInputFill = this.getContextObject().config.contractDataInputFill;
// console.log(this.config)
this._parseAndSetJsonInterface(jsonInterface, returnDataFormat);

if (!isNullish(address)) {
Expand Down
4 changes: 3 additions & 1 deletion packages/web3-eth-contract/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ export const getEstimateGasParams = ({
export const isContractInitOptions = (options: unknown): options is ContractInitOptions =>
typeof options === 'object' &&
!isNullish(options) &&
Object.keys(options).length !== 0 &&
[
'input',
'data',
Expand All @@ -182,7 +183,8 @@ export const isContractInitOptions = (options: unknown): options is ContractInit
].some(key => key in options);

export const isWeb3ContractContext = (options: unknown): options is Web3ContractContext =>
typeof options === 'object' && !isNullish(options) && !isContractInitOptions(options);
typeof options === 'object' && !isNullish(options) &&
Object.keys(options).length !== 0 && !isContractInitOptions(options);

export const getCreateAccessListParams = ({
abi,
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-eth/src/rpc_method_wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ export function sendTransaction<
},
ETH_DATA_FORMAT,
);

try {
transactionFormatted = await sendTxHelper.populateGasPrice({
transaction,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ export const prepareTransactionForSigning = async (
validateTransactionForSigning(
formattedTransaction as unknown as FormatType<Transaction, typeof ETH_DATA_FORMAT>,
);

return TransactionFactory.fromTxData(
getEthereumjsTxDataFromTransaction(formattedTransaction),
getEthereumjsTransactionOptions(formattedTransaction, web3Context),
Expand Down
2 changes: 2 additions & 0 deletions packages/web3-eth/src/web3_eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ export class Web3Eth extends Web3Context<Web3EthExecutionAPI, RegisteredSubscrip
...(providerOrContext as Web3ContextInitOptions),
registeredSubscriptions,
});
// super.link(providerOrContext as Web3Context);

}

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/web3/src/web3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
Web3ContextInitOptions,
Web3ContextObject,
Web3SubscriptionConstructor,
isSupportedProvider,
isSupportedProvider
} from 'web3-core';
import { Web3Eth, RegisteredSubscription, registeredSubscriptions } from 'web3-eth';
import Contract from 'web3-eth-contract';
Expand Down Expand Up @@ -143,7 +143,7 @@ export class Web3<
jsonInterface,
addressOrOptions,
options ?? {},
self.getContextObject() as Web3ContextObject,
self.getContextObject() as Web3ContextObject, // self.getcontextobject is not the same as web3 get contextobject?
);
} else {
throw new InvalidMethodParamsError();
Expand All @@ -156,7 +156,7 @@ export class Web3<
const eth = self.use(Web3Eth);

// Eth Module
this.eth = Object.assign(eth, {
this.eth = Object.assign(eth, { // need to set context here
// ENS module
ens: self.use(ENS, registryAddresses.main), // registry address defaults to main network

Expand Down
45 changes: 39 additions & 6 deletions packages/web3/test/integration/web3config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/

import Web3 from '../../src/index';
// import { TransactionRevertInstructionError } from 'web3-errors';
import { SupportedProviders, Web3BaseProvider } from 'web3-types';
import {
closeOpenConnection,
Expand All @@ -24,6 +23,8 @@ import {
isWs,
waitForOpenConnection,
} from '../shared_fixtures/system_tests_utils';
import Web3 from '../../src/index';
// import { BasicAbi, BasicBytecode } from '../shared_fixtures/build/Basic';

describe('Web3 instance', () => {
let provider: string | SupportedProviders;
Expand Down Expand Up @@ -62,7 +63,23 @@ describe('Web3 instance', () => {
}
});

it.only('should be able to create the web3config and change for default type transaction ', async () => {
it('should be send transaction, change for defaultTransactionType and sucesfully send transaction with different type', async () => {
let transaction = {
from: accounts[0],
to: accounts[0],
value: 100000,
}

const receipt = await web3.eth.sendTransaction(transaction);
expect(receipt.type).toEqual(BigInt(2))

web3.setConfig({defaultTransactionType: "0x0"});

const receipt2 = await web3.eth.sendTransaction(transaction);
expect(receipt2.type).toEqual(BigInt(0))
});

it('defaultChain should be send transaction, change for defaultTransactionType and sucesfully send transaction with different type', async () => {
let transaction = {
from: accounts[0],
to: accounts[0],
Expand All @@ -72,12 +89,28 @@ describe('Web3 instance', () => {
const receipt = await web3.eth.sendTransaction(transaction);
expect(receipt.type).toEqual(BigInt(2))

// const tempWeb3 = new Web3(provider, {defaultTransactionType: "0x0"});
web3.setConfig({defaultTransactionType: "0x0"});
console.log(web3.getContextObject());
console.log(web3.eth.getContextObject());

const receipt2 = await web3.eth.sendTransaction(transaction);
expect(receipt2.type).toEqual(BigInt(0))
});

it('create contract and change contract data to input', async () => {
const web3 = new Web3(provider);
let transaction = {
from: accounts[0],
to: accounts[0],
value: 100000,
}

const receipt = await web3.eth.sendTransaction(transaction);
expect(receipt.type).toEqual(BigInt(2))

web3.setConfig({defaultTransactionType: "0x0"});

const receipt2 = await web3.eth.sendTransaction(transaction);
expect(receipt2.type).toEqual(BigInt(0))
});


})
56 changes: 56 additions & 0 deletions packages/web3/test/unit/web3Config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.

// import Web3ProviderBase from '../../src/index'
// import {ProviderOptions} from '../../types'
import { transactionBuilder } from 'web3-eth';
import {Web3Context} from 'web3-core';
import { Web3 } from '../../src';

describe('web3config web3 tests', () => {
Expand All @@ -28,6 +30,30 @@ describe('web3config web3 tests', () => {
// }
});
describe('web3config contract', () => {

it('create web3context with configs and should set it for web3', async () => {
const context = new Web3Context("");
context.setConfig({defaultTransactionType: "0x0"});
const web3 = new Web3(context);
expect(web3.getContextObject().config.defaultTransactionType).toBe("0x0");
expect(web3.config.defaultTransactionType).toBe("0x0");
});

it.only('should be able to create web3 and setconfig ', async () => {
const web3 = new Web3("");
web3.setConfig({defaultTransactionType: "0x0"});
expect(web3.config.defaultTransactionType).toBe("0x0");
expect(web3.getContextObject().config.defaultTransactionType).toBe("0x0");

expect(web3.eth.config.defaultTransactionType).toBe("0x0");
expect(web3.eth.getContextObject().config.defaultTransactionType).toBe("0x0");

web3.setConfig({contractDataInputFill:"both"});
const contract = new web3.eth.Contract([]);
expect(contract.config.contractDataInputFill).toBe("both")
expect(contract.getContextObject().config.contractDataInputFill).toBe("both");
});

it('default', () => {
const web3 = new Web3('http://127.0.0.1:8545');
// web3.setConfig({ contractDataInputFill: "data" });
Expand Down Expand Up @@ -71,5 +97,35 @@ describe('web3config web3 tests', () => {
expect(true).toBeTruthy();
});
})
it('defaultChain', async () => {
const web3 = new Web3('http://127.0.0.1:8545');
// default
expect(web3.defaultChain).toBe('mainnet');

// after set
web3.setConfig({
defaultChain: 'ropsten',
});
expect(web3.defaultChain).toBe('ropsten');

// set by create new instance
// eth2 = new web3({
// provider: web3Eth.provider,
// config: {
// defaultChain: 'rinkeby',
// },
// });
// expect(eth2.defaultChain).toBe('rinkeby');
const res = await transactionBuilder({
transaction: {
from: '0xEB014f8c8B418Db6b45774c326A0E64C78914dC0',
to: '0x3535353535353535353535353535353535353535',
value: '0x174876e800',
gas: '0x5208',
},
web3Context: web3.getContextObject() as Web3Context,
});
expect(res.chain).toBe('ropsten');
});

});

0 comments on commit 62ed18d

Please sign in to comment.