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

5788 - 4.x getPastEvents error #5819

Merged
merged 4 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 0 additions & 1 deletion packages/web3-core/src/web3_request_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ export class Web3RequestManager<
// Check if the provider throw an error instead of reject with error
response = error as JsonRpcResponse<ResponseType>;
}

return this._processJsonRpcResponse(payload, response, { legacy: false, error: false });
}

Expand Down
1 change: 0 additions & 1 deletion packages/web3-eth-contract/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,6 @@ export class Contract<Abi extends ContractAbi>
this.options,
abi,
filter ?? {},
returnFormat,
);

const logs = await getLogs(this, { fromBlock, toBlock, topics, address }, returnFormat);
Expand Down
21 changes: 16 additions & 5 deletions packages/web3-eth-contract/src/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ 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 { DataFormat, DEFAULT_RETURN_FORMAT, format, isNullish } from 'web3-utils';
import {
DataFormat,
DEFAULT_RETURN_FORMAT,
FMT_BYTES,
FMT_NUMBER,
format,
isNullish,
} from 'web3-utils';

import {
AbiConstructorFragment,
Expand Down Expand Up @@ -59,7 +66,6 @@ export const encodeEventABI = (
// eslint-disable-next-line @typescript-eslint/ban-types
topics?: (null | Topic | Topic[])[];
},
returnFormat: DataFormat = DEFAULT_RETURN_FORMAT,
) => {
const opts: {
filter: Filter;
Expand All @@ -72,11 +78,16 @@ export const encodeEventABI = (
};

if (!isNullish(options?.fromBlock)) {
opts.fromBlock = format(blockSchema.properties.number, options?.fromBlock, returnFormat);
opts.fromBlock = format(blockSchema.properties.number, options?.fromBlock, {
number: FMT_NUMBER.HEX,
bytes: FMT_BYTES.HEX,
});
}

if (!isNullish(options?.toBlock)) {
opts.toBlock = format(blockSchema.properties.number, options?.toBlock, returnFormat);
opts.toBlock = format(blockSchema.properties.number, options?.toBlock, {
number: FMT_NUMBER.HEX,
bytes: FMT_BYTES.HEX,
});
}

if (options?.topics && Array.isArray(options.topics)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ describe('contract', () => {

describeIf(isWs)('getPastEvents', () => {
// TODO: Debug why this tests is hanging the websocket
it('should return all past events', async () => {
it('should return all past events using earliest and latest options', async () => {
await contractDeployed.methods
.firesMultiValueEvent('New Greeting 1', 11, true)
.send(sendOptions);
Expand All @@ -201,5 +201,50 @@ describe('contract', () => {
}),
).toHaveLength(2);
});
it('should return all past events using number options', async () => {
await contractDeployed.methods
.firesMultiValueEvent('New Greeting 1', 11, true)
.send(sendOptions);
await contractDeployed.methods
.firesMultiValueEvent('New Greeting 2', 12, true)
.send(sendOptions);

expect(
await contractDeployed.getPastEvents('MultiValueEvent', {
fromBlock: 0,
toBlock: 1000,
}),
).toHaveLength(2);
});
it('should return all past events using string options', async () => {
await contractDeployed.methods
.firesMultiValueEvent('New Greeting 1', 11, true)
.send(sendOptions);
await contractDeployed.methods
.firesMultiValueEvent('New Greeting 2', 12, true)
.send(sendOptions);

expect(
await contractDeployed.getPastEvents('MultiValueEvent', {
fromBlock: '0',
toBlock: '1000',
}),
).toHaveLength(2);
});
it('should return all past events using bigint options', async () => {
await contractDeployed.methods
.firesMultiValueEvent('New Greeting 1', 11, true)
.send(sendOptions);
await contractDeployed.methods
.firesMultiValueEvent('New Greeting 2', 12, true)
.send(sendOptions);

expect(
await contractDeployed.getPastEvents('MultiValueEvent', {
fromBlock: BigInt(0),
toBlock: BigInt(1000),
}),
).toHaveLength(2);
});
});
});