Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.
Closed
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
11 changes: 9 additions & 2 deletions packages/interface-adapter/lib/adapter/web3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,23 @@ export class Web3InterfaceAdapter implements InterfaceAdapter {
return this.web3.eth.getBlockNumber();
}

public async getTransactionCostReport(receipt: TransactionReceipt): Promise<TransactionCostReport> {
public async getTransactionCostReport(
receipt: TransactionReceipt
): Promise<TransactionCostReport> {
const tx = await this.getTransaction(receipt.transactionHash);
const block = await this.getBlock(receipt.blockNumber);

if (!block) return null;

const balance = await this.getBalance(tx.from);
const gasPrice = new BN(tx.gasPrice);
// gasPrice has been deprecated in favor of effectiveGasPrice
// via https://github.com/ethereum/execution-specs/pull/251
// Note: this incidentally conforms to Arbitrum as well
// via https://github.com/trufflesuite/truffle/issues/4559
const gasPrice = new BN(receipt.effectiveGasPrice || tx.gasPrice);
const gas = new BN(receipt.gasUsed);
const value = new BN(tx.value);

const cost = gasPrice.mul(gas).add(value);

return {
Expand Down
60 changes: 60 additions & 0 deletions packages/interface-adapter/test/getTransactionReport.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { describe, it } from "mocha";
import assert from "assert";

import { Server } from "http";
import BN from "bn.js";

import Web3 from "web3";
import Ganache from "ganache-core";

import { createInterfaceAdapter } from "../lib";
import {
InterfaceAdapter,
Provider,
TransactionReceipt
} from "../lib/adapter/types";

const port = 12345;

async function prepareGanache(): Promise<{
server: Server;
interfaceAdapter: InterfaceAdapter;
}> {
return new Promise((resolve, reject) => {
const server = Ganache.server();
server.listen(port, () => {
const interfaceAdapter = createInterfaceAdapter({
provider: new Web3.providers.HttpProvider(`http://127.0.0.1:${port}`)
});
resolve({
server,
interfaceAdapter
});
});
});
}

describe("getTransactionReport", function () {
let provider: any;
let web3: Web3;

before("Create Provider", async function () {
provider = Ganache.provider({ seed: "decoder", gasLimit: 7000000 });
web3 = new Web3(provider);
});

it("calculates cost given an effectiveGasPrice", async function () {
const preparedGanache = await prepareGanache();
const accounts = await web3.eth.getAccounts();
const receipt = await web3.eth.sendTransaction({
from: accounts[0],
to: accounts[1]
});
const report = await preparedGanache.interfaceAdapter.getTransactionCostReport(
receipt
);
console.log("report", report);
assert.strictEqual(report.gasPrice, receipt.effectiveGasPrice);
await preparedGanache.server.close();
});
});