Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #5192 from trufflesuite/dbkit
Browse files Browse the repository at this point in the history
Change transaction type to match decoder transaction
  • Loading branch information
lsqproduction authored Jun 28, 2022
2 parents 048ff50 + 5f1ef3f commit d656ba9
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 37 deletions.
3 changes: 2 additions & 1 deletion packages/db-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@
"ttypescript": "^1.5.7",
"typedoc": "^0.20.19",
"typescript": "^4.1.4",
"typescript-transform-paths": "^2.1.0"
"typescript-transform-paths": "^2.1.0",
"web3-core": "1.5.3"
},
"keywords": [
"smart-contract",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState, useEffect } from "react";
import type { Transaction, TransactionReceipt } from "web3-core";
import type { ProjectDecoder, Log } from "@truffle/decoder";
import type { ProjectDecoder, Log, Transaction as DecoderTransaction } from "@truffle/decoder";
import type { LogDecoding, CalldataDecoding } from "@truffle/codec";

export interface UseDecodedTransactionOptions {
Expand Down Expand Up @@ -47,7 +47,8 @@ export function useDecodedTransaction({
}

useEffect(() => {
decoder.decodeTransaction(transaction).then(decoding => {
//Using unknown below for type casting as web3-core's Transaction type is currently incorrect
decoder.decodeTransaction(transaction as unknown as DecoderTransaction).then(decoding => {
setSummaryState({
decoding,
complete: true
Expand Down
14 changes: 13 additions & 1 deletion packages/decoder/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type { Provider } from "@truffle/encoder";

//StateVariable used to be defined here, so let's continue
//to export it
export { StateVariable, ExtrasAllowed };
export { StateVariable, ExtrasAllowed, RegularizedBlockSpecifier };

/**
* This type contains information needed to initialize the decoder.
Expand Down Expand Up @@ -267,6 +267,18 @@ export interface Transaction {
* Data sent with the transaction, as hex string.
*/
input: string;
/**
* A positive unsigned 8-bit number between 0 and 0x7f that represents the type of the transaction.
*/
type?: number;
/**
* The maximum fee per gas that the transaction is willing to pay in total.
*/
maxFeePerGas?: string;
/**
* The maximum fee per gas to give miners to incentivize them to include the transaction (Priority fee).
*/
maxPriorityFeePerGas?: string;
}

/**
Expand Down
68 changes: 35 additions & 33 deletions packages/encoder/lib/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import debugModule from "debug";
const debug = debugModule("decoder:adapter");
import type { BlockSpecifier, RegularizedBlockSpecifier } from "@truffle/codec";
import type { BlockSpecifier } from "@truffle/codec";
import type BN from "bn.js";
import type {
Provider as LegacyProvider,
Expand Down Expand Up @@ -82,12 +82,7 @@ type FormattedBlock = {
transactions: string[];
uncles: string[];
};
const stringWhitelist = [
"latest",
"pending",
"genesis",
"earliest"
];
const stringWhitelist = ["latest", "pending", "genesis", "earliest"];

const formatBlockSpecifier = (block: BlockSpecifier): string => {
if (typeof block === "string" && stringWhitelist.includes(block)) {
Expand Down Expand Up @@ -129,9 +124,8 @@ export type Provider = LegacyProvider | Eip1193Provider;
// EIP-1193 providers use `request()` instead of `send()`
// NOTE this provider returns `response.result` already unwrapped
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1193.md
const isEip1193Provider = (
provider: Provider
): provider is Eip1193Provider => "request" in provider;
const isEip1193Provider = (provider: Provider): provider is Eip1193Provider =>
"request" in provider;

/**
* @hidden
Expand All @@ -143,7 +137,7 @@ export class ProviderAdapter {
this.provider = provider;
}

private async sendRequest ({
private async sendRequest({
method,
params,
formatOutput
Expand All @@ -166,24 +160,23 @@ export class ProviderAdapter {

// HACK this uses a manual `new Promise` instead of promisify because
// users reported difficulty running this package in a browser extension
result = await new Promise(
(accept, reject) =>
send(
{
jsonrpc: "2.0",
id: new Date().getTime(),
method,
params
},
((error: Error, response: JsonRPCResponse) => {
if (error) {
return reject(error);
}
result = await new Promise((accept, reject) =>
send(
{
jsonrpc: "2.0",
id: new Date().getTime(),
method,
params
},
((error: Error, response: JsonRPCResponse) => {
if (error) {
return reject(error);
}

const { result: res } = response;
accept(res);
}) as Callback<JsonRPCResponse>
)
const { result: res } = response;
accept(res);
}) as Callback<JsonRPCResponse>
)
);
}
if (formatOutput) return formatOutput(result);
Expand All @@ -201,7 +194,9 @@ export class ProviderAdapter {
});
}

public async getBlockByNumber (block: BlockSpecifier): Promise<FormattedBlock> {
public async getBlockByNumber(
block: BlockSpecifier
): Promise<FormattedBlock> {
const blockToFetch = formatBlockSpecifier(block);
return await this.sendRequest({
method: "eth_getBlockByNumber",
Expand All @@ -221,23 +216,26 @@ export class ProviderAdapter {
});
}

public async getNetworkId (): Promise<number> {
public async getNetworkId(): Promise<number> {
return await this.sendRequest({
method: "net_version",
params: [],
formatOutput: result => parseInt(result)
});
}

public async getBlockNumber (): Promise<number> {
public async getBlockNumber(): Promise<number> {
return await this.sendRequest({
method: "eth_blockNumber",
params: [],
formatOutput: result => parseInt(result)
});
}

public async getBalance (address: string, block: BlockSpecifier): Promise<string> {
public async getBalance(
address: string,
block: BlockSpecifier
): Promise<string> {
return await this.sendRequest({
method: "eth_getBalance",
params: [address, formatBlockSpecifier(block)],
Expand All @@ -263,7 +261,11 @@ export class ProviderAdapter {
): Promise<string> {
return await this.sendRequest({
method: "eth_getStorageAt",
params: [address, `0x${position.toString(16)}`, formatBlockSpecifier(block)]
params: [
address,
`0x${position.toString(16)}`,
formatBlockSpecifier(block)
]
});
}
}

0 comments on commit d656ba9

Please sign in to comment.