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

feat: log warning on legacy-style eth_getTransactionReceipt calls #779

Merged
merged 10 commits into from
Feb 19, 2021
11 changes: 11 additions & 0 deletions src/chains/ethereum/ethereum/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,17 @@ export default class EthereumApi implements types.Api {
if (transaction) {
return receipt.toJSON(block, transaction);
} else {
// check to see if the transaction is pending
const tx = this.#blockchain.transactions.transactionPool.find(txHash);
davidmurdoch marked this conversation as resolved.
Show resolved Hide resolved
if (tx != null) {
this.#options.logging.logger.log(
" > Ganache `eth_getTransactionReceipt` warning: the requested transaction has not yet been mined."
);
this.#options.logging.logger.log(
" > After it has been mined you will be able to obtain the receipt. See https://trfl.co/v7-instamine for more details."
);
eggplantzzz marked this conversation as resolved.
Show resolved Hide resolved
}

return null;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/chains/ethereum/ethereum/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ export default class EthereumProvider
: JSON.stringify(params, null, 2).split("\n").join("\n > ")
}`
);
} else {
options.logging.logger.log(method);
eggplantzzz marked this conversation as resolved.
Show resolved Hide resolved
}
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import getProvider from "../../helpers/getProvider";
import assert from "assert";
import EthereumProvider from "../../../src/provider";

describe("api", () => {
describe("eth", () => {
describe("getTransactionReceipt", () => {
let provider: EthereumProvider;
eggplantzzz marked this conversation as resolved.
Show resolved Hide resolved
let logger;

beforeEach(async () => {
// create a logger to test output
logger = {
clearLoggedStuff: function () {
this.loggedStuff = "";
eggplantzzz marked this conversation as resolved.
Show resolved Hide resolved
},
loggedStuff: "",
log: function (message) {
if (message) {
this.loggedStuff = this.loggedStuff.concat(message);
eggplantzzz marked this conversation as resolved.
Show resolved Hide resolved
}
}
};
provider = await getProvider({ logging: { logger } });
});

eggplantzzz marked this conversation as resolved.
Show resolved Hide resolved
it("returns the receipt for the transaction", async () => {
const [from] = await provider.send("eth_accounts");
await provider.send("eth_subscribe", ["newHeads"]);

const hash = await provider.send("eth_sendTransaction", [
{
from,
to: from
}
]);

// wait for the tx to be mined
await provider.once("message");
const receipt = await provider.send("eth_getTransactionReceipt", [
hash
]);
assert(receipt);
eggplantzzz marked this conversation as resolved.
Show resolved Hide resolved
});

it("returns null if the transaction does not exist", async () => {
const result = await provider.send("eth_getTransactionReceipt", [
"0x0"
]);
assert.strictEqual(result, null);
eggplantzzz marked this conversation as resolved.
Show resolved Hide resolved
});

it("logs a warning if the transaction hasn't been mined yet", async () => {
logger.clearLoggedStuff();
eggplantzzz marked this conversation as resolved.
Show resolved Hide resolved
const [from] = await provider.send("eth_accounts");

const hash = await provider.send("eth_sendTransaction", [
{
from,
to: from
}
]);

// do not wait for the tx to be mined which will create a warning
await provider.send("eth_getTransactionReceipt", [hash]);
eggplantzzz marked this conversation as resolved.
Show resolved Hide resolved
assert(
logger.loggedStuff.includes(
"Ganache `eth_getTransactionReceipt` warning"
eggplantzzz marked this conversation as resolved.
Show resolved Hide resolved
)
);
});
});
});
});