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

Zk plugin related fixes #7124

Merged
merged 10 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2564,3 +2564,15 @@ If there are any bugs, improvements, optimizations or any new feature proposal f
- `getName` reverse resolution

## [Unreleased]

### Fixed

#### web3-eth

- Fixed geth issue when running a new instance, transactions will index when there are no blocks created (#7098)

### Added

#### web3-eth-contract

- `populateTransaction` was added to contract methods (#7124)
7 changes: 6 additions & 1 deletion packages/web3-eth-contract/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -386,4 +386,9 @@ Documentation:

- `defaultReturnFormat` was added to all methods that have `ReturnType` param. (#6947)

## [Unreleased]
## [Unreleased]

### Added

- `populateTransaction` was added to contract methods (#7124)

24 changes: 23 additions & 1 deletion packages/web3-eth-contract/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1268,7 +1268,29 @@ export class Contract<Abi extends ContractAbi>

send: (options?: PayableTxOptions | NonPayableTxOptions): ContractMethodSend =>
this._contractMethodSend(methodAbi, abiParams, internalErrorsAbis, options),

populateTransaction: (
options?: PayableTxOptions | NonPayableTxOptions,
contractOptions?: ContractOptions,
) => {
let modifiedContractOptions = contractOptions ?? this.options;
modifiedContractOptions = {
...modifiedContractOptions,
input: undefined,
from: modifiedContractOptions?.from ?? this.defaultAccount ?? undefined,
};
const tx = getSendTxParams({
abi,
params,
options: { ...options, dataInputFill: this.config.contractDataInputFill },
contractOptions: modifiedContractOptions,
});
// @ts-expect-error remove unnecessary field
if (tx.dataInputFill) {
// @ts-expect-error remove unnecessary field
delete tx.dataInputFill;
}
return tx;
},
estimateGas: async <ReturnFormat extends DataFormat = typeof DEFAULT_RETURN_FORMAT>(
options?: PayableCallOptions | NonPayableCallOptions,
returnFormat: ReturnFormat = this
Expand Down
17 changes: 16 additions & 1 deletion packages/web3-eth-contract/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ import {
DataFormat,
DEFAULT_RETURN_FORMAT,
FormatType,
TransactionCall,
} from 'web3-types';
import { NewHeadsSubscription, SendTransactionEvents } from 'web3-eth';
import type { ContractOptions } from 'web3-types';
import { LogsSubscription } from './log_subscription.js';

export type NonPayableTxOptions = NonPayableCallOptions;
export type PayableTxOptions = PayableCallOptions;
export { ContractAbiWithSignature, EventLog, ContractOptions } from 'web3-types';
export type { ContractAbiWithSignature, EventLog, ContractOptions } from 'web3-types';

export interface ContractEventOptions {
/**
Expand Down Expand Up @@ -177,6 +179,10 @@ export interface NonPayableMethodObject<Inputs = unknown[], Outputs = unknown[]>
FormatType<TransactionReceipt, typeof DEFAULT_RETURN_FORMAT>,
SendTransactionEvents<typeof DEFAULT_RETURN_FORMAT>
>;
populateTransaction(
tx?: PayableTxOptions | NonPayableTxOptions,
contractOptions?: ContractOptions,
): TransactionCall;

/**
* Returns the amount of gas consumed by executing the method locally without creating a new transaction on the blockchain.
Expand Down Expand Up @@ -375,6 +381,15 @@ export interface PayableMethodObject<Inputs = unknown[], Outputs = unknown[]> {
SendTransactionEvents<typeof DEFAULT_RETURN_FORMAT>
>;

/**
*
* @param tx
* @param contractOptions
*/
populateTransaction(
tx?: PayableTxOptions | NonPayableTxOptions,
contractOptions?: ContractOptions,
): TransactionCall;
/**
* Returns the amount of gas consumed by executing the method locally without creating a new transaction on the blockchain.
* The returned amount can be used as a gas estimate for executing the transaction publicly. The actual gas used can be
Expand Down
44 changes: 39 additions & 5 deletions packages/web3-eth-contract/test/unit/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/

import * as eth from 'web3-eth';
import { ValidChains, Hardfork, AccessListResult, Address, ETH_DATA_FORMAT , DEFAULT_RETURN_FORMAT } from 'web3-types';
import {
ValidChains,
Hardfork,
AccessListResult,
Address,
ETH_DATA_FORMAT,
DEFAULT_RETURN_FORMAT,
} from 'web3-types';
import { Web3ContractError } from 'web3-errors';
import { Web3Context , Web3ConfigEvent } from 'web3-core';
import { Web3Context, Web3ConfigEvent } from 'web3-core';
import { Web3ValidatorError } from 'web3-validator';
import { AbiItem } from 'web3-utils';
import { stringify } from 'flatted';
import {Abi} from '../fixtures/AbiItem'
import { Abi } from '../fixtures/AbiItem';
import { Contract } from '../../src';
import { sampleStorageContractABI } from '../fixtures/storage';
import { GreeterAbi, GreeterBytecode } from '../shared_fixtures/build/Greeter';
Expand Down Expand Up @@ -485,7 +492,7 @@ describe('Contract', () => {
// calling with wrong parameters should throw
try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
await(deployedContract.methods.setGreeting as any)(arg, 'test').send(sendOptions);
await (deployedContract.methods.setGreeting as any)(arg, 'test').send(sendOptions);
expect(true).toBe(false);
} catch (error) {
// eslint-disable-next-line jest/no-conditional-expect
Expand All @@ -499,7 +506,7 @@ describe('Contract', () => {
// calling with wrong parameters should throw
try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
await(deployedContract.methods.setGreeting as any)(arg, true, 'test').send(
await (deployedContract.methods.setGreeting as any)(arg, true, 'test').send(
sendOptions,
);
expect(true).toBe(false);
Expand Down Expand Up @@ -1842,5 +1849,32 @@ describe('Contract', () => {
const contract = new Contract(GreeterAbi, web3Context);
expect(contract.config).toStrictEqual(web3Context.config);
});

it('should populate method to tx object', () => {
const expectedProvider = 'http://127.0.0.1:8545';
const web3Context = new Web3Context({
provider: expectedProvider,
config: { handleRevert: true, defaultTransactionType: '0x2' },
});
const contract = new Contract(
GreeterAbi,
'0x00000000219ab540356cBB839Cbe05303d7705F1',
web3Context,
);

const tx = contract.methods
.greet()
.populateTransaction({ from: '0x00000000219ab540356cBB839Cbe05303d7705F2' });
expect(tx).toEqual({
to: '0x00000000219AB540356cBb839cbe05303D7705F1',
gas: undefined,
gasPrice: undefined,
from: '0x00000000219ab540356cBB839Cbe05303d7705F2',
input: undefined,
maxPriorityFeePerGas: undefined,
maxFeePerGas: undefined,
data: '0xcfae3217',
});
});
});
});
115 changes: 58 additions & 57 deletions packages/web3-types/src/eth_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export enum BlockTags {
PENDING = 'pending',
SAFE = 'safe',
FINALIZED = 'finalized',
COMMITTED = 'committed',
}
export type BlockTag = `${BlockTags}`;

Expand Down Expand Up @@ -138,8 +139,8 @@ export interface Withdrawals {
}

export interface BlockOutput {
readonly gasLimit: bigint | number;
readonly gasUsed: bigint | number;
readonly gasLimit: bigint | number;
readonly gasUsed: bigint | number;
readonly size: bigint | number;
readonly timestamp: bigint | number;
readonly number?: bigint | number;
Expand All @@ -152,54 +153,54 @@ export interface BlockOutput {

// Added properties
readonly blobGasUsed?: bigint | number;
readonly excessBlobGas?: bigint | number;
readonly extraData?: Bytes;
readonly hash?: HexString32Bytes;
readonly logsBloom?: Bytes;
readonly nonce?: bigint | number;
readonly parentBeaconBlockRoot?: HexString32Bytes;
readonly receiptsRoot?: HexString32Bytes;
readonly sha3Uncles: HexString32Bytes[];
readonly stateRoot?: HexString32Bytes;
readonly transactionsRoot?: HexString32Bytes;
readonly withdrawalsRoot?: HexString32Bytes;
readonly mixHash?: HexString32Bytes;
readonly uncles?: Uncles;
readonly withdrawals?: Withdrawals[];
readonly excessBlobGas?: bigint | number;
readonly extraData?: Bytes;
readonly hash?: HexString32Bytes;
readonly logsBloom?: Bytes;
readonly nonce?: bigint | number;
readonly parentBeaconBlockRoot?: HexString32Bytes;
readonly receiptsRoot?: HexString32Bytes;
readonly sha3Uncles: HexString32Bytes[];
readonly stateRoot?: HexString32Bytes;
readonly transactionsRoot?: HexString32Bytes;
readonly withdrawalsRoot?: HexString32Bytes;
readonly mixHash?: HexString32Bytes;
readonly uncles?: Uncles;
readonly withdrawals?: Withdrawals[];
}

export interface BlockHeaderOutput {
readonly baseFeePerGas?: Numbers;
readonly blobGasUsed?: Numbers;
readonly difficulty?: Numbers;
readonly excessBlobGas?: Numbers;
readonly extraData?: Bytes;
readonly gasLimit: Numbers;
readonly gasUsed: Numbers;
readonly hash?: HexString32Bytes;
readonly logsBloom?: Bytes;
readonly miner?: HexString;
readonly nonce?: Numbers;
readonly number?: Numbers;
readonly parentBeaconBlockRoot?: HexString32Bytes;
readonly parentHash?: HexString32Bytes;
readonly receiptsRoot?: HexString32Bytes;
readonly sha3Uncles: HexString32Bytes[];
readonly stateRoot?: HexString32Bytes;
readonly timestamp: Numbers;
readonly transactionsRoot?: HexString32Bytes;
readonly withdrawalsRoot?: HexString32Bytes;

// These fields are returned when the RPC client is Nethermind,
// but aren't available in other clients such as Geth
readonly author?: Address;
readonly totalDifficulty?: Numbers;
readonly size?: Numbers;
readonly excessDataGas?: Numbers;
readonly mixHash?: HexString32Bytes;
readonly transactions?: TransactionOutput[];
readonly uncles?: Uncles;
readonly withdrawals?: Withdrawals[];
readonly baseFeePerGas?: Numbers;
readonly blobGasUsed?: Numbers;
readonly difficulty?: Numbers;
readonly excessBlobGas?: Numbers;
readonly extraData?: Bytes;
readonly gasLimit: Numbers;
readonly gasUsed: Numbers;
readonly hash?: HexString32Bytes;
readonly logsBloom?: Bytes;
readonly miner?: HexString;
readonly nonce?: Numbers;
readonly number?: Numbers;
readonly parentBeaconBlockRoot?: HexString32Bytes;
readonly parentHash?: HexString32Bytes;
readonly receiptsRoot?: HexString32Bytes;
readonly sha3Uncles: HexString32Bytes[];
readonly stateRoot?: HexString32Bytes;
readonly timestamp: Numbers;
readonly transactionsRoot?: HexString32Bytes;
readonly withdrawalsRoot?: HexString32Bytes;

// These fields are returned when the RPC client is Nethermind,
// but aren't available in other clients such as Geth
readonly author?: Address;
readonly totalDifficulty?: Numbers;
readonly size?: Numbers;
readonly excessDataGas?: Numbers;
readonly mixHash?: HexString32Bytes;
readonly transactions?: TransactionOutput[];
readonly uncles?: Uncles;
readonly withdrawals?: Withdrawals[];
}

export interface ReceiptInput {
Expand Down Expand Up @@ -553,13 +554,13 @@ export interface Eip712TypedData {
/**
* To contain the gas Fee Data to be used with EIP-1559 transactions.
* EIP-1559 was applied to Ethereum after London hardfork.
*
*
* Typically you will only need `maxFeePerGas` and `maxPriorityFeePerGas` for a transaction following EIP-1559.
* However, if you want to get informed about the fees of last block, you can use `baseFeePerGas` too.
*
*
*
*
* @see https://eips.ethereum.org/EIPS/eip-1559
*
*
*/
export interface FeeData {
/**
Expand All @@ -569,20 +570,20 @@ export interface FeeData {

/**
* The baseFeePerGas returned from the last available block.
*
*
* If EIP-1559 is not supported, this will be `undefined`
*
* However, the user will only pay (the future baseFeePerGas + the maxPriorityFeePerGas).
*
* However, the user will only pay (the future baseFeePerGas + the maxPriorityFeePerGas).
* And this value is just for getting informed about the fees of last block.
*/
readonly baseFeePerGas?: Numbers;

/**
* The maximum fee that the user would be willing to pay per-gas.
*
*
* However, the user will only pay (the future baseFeePerGas + the maxPriorityFeePerGas).
* And the `maxFeePerGas` could be used to prevent paying more than it, if `baseFeePerGas` went too high.
*
*
* If EIP-1559 is not supported, this will be `undefined`
*/
readonly maxFeePerGas?: Numbers;
Expand All @@ -593,4 +594,4 @@ export interface FeeData {
* If EIP-1559 is not supported, this will be `undefined`
*/
readonly maxPriorityFeePerGas?: Numbers;
}
}
Loading