Skip to content

Commit

Permalink
test: Fill message call and static call examples
Browse files Browse the repository at this point in the history
This increases coverage of calling by contract through call and static
call
  • Loading branch information
nddeluca committed Sep 25, 2024
1 parent 679b265 commit 72bd242
Showing 1 changed file with 138 additions and 3 deletions.
141 changes: 138 additions & 3 deletions tests/e2e-evm/test/abi_basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,9 +403,25 @@ describe("ABI_BasicTests", function () {
expect(txReceipt.gasUsed < txData.gas, "gas to not be exhausted").to.be.true;

let expectedBalance = startingBalance;
if (fallbackFunction && fallbackFunction.stateMutability === "payable") {
expectedBalance = startingBalance + txData.value;
}
const balance = await publicClient.getBalance({ address: ctx.address });
expect(balance).to.equal(expectedBalance);
});

it("can not receive plain transfers via message call", async function () {
const data = encodeFunctionData({
abi: caller.abi,
functionName: "functionCall",
args: [ctx.address, "0x"],
});
const txData = { to: caller.address, data: data, gas: contractCallerGas, value: 1n};
const startingBalance = await publicClient.getBalance({ address: ctx.address });

const txHash = await walletClient.sendTransaction(txData);
const txReceipt = await publicClient.waitForTransactionReceipt({ hash: txHash });
expect(txReceipt.status).to.equal("reverted");
expect(txReceipt.gasUsed < txData.gas, "gas to not be exhausted").to.be.true;

let expectedBalance = startingBalance;
const balance = await publicClient.getBalance({ address: ctx.address });
expect(balance).to.equal(expectedBalance);
});
Expand All @@ -425,6 +441,25 @@ describe("ABI_BasicTests", function () {
const balance = await publicClient.getBalance({ address: ctx.address });
expect(balance).to.equal(expectedBalance);
});

it("can plain transfers via message call", async function () {
const data = encodeFunctionData({
abi: caller.abi,
functionName: "functionCall",
args: [ctx.address, "0x"],
});
const txData = { to: caller.address, data: data, gas: contractCallerGas, value: 1n};
const startingBalance = await publicClient.getBalance({ address: ctx.address });

const txHash = await walletClient.sendTransaction(txData);
const txReceipt = await publicClient.waitForTransactionReceipt({ hash: txHash });
expect(txReceipt.status).to.equal("success");
expect(txReceipt.gasUsed < txData.gas, "gas to not be exhausted").to.be.true;

const expectedBalance = startingBalance + txData.value;
const balance = await publicClient.getBalance({ address: ctx.address });
expect(balance).to.equal(expectedBalance);
});
}

it(`can ${fallbackFunction ? "" : "not "}be called with a non-matching function selector`, async function () {
Expand All @@ -437,6 +472,34 @@ describe("ABI_BasicTests", function () {
expect(txReceipt.gasUsed < txData.gas, "gas to not be exhausted").to.be.true;
});

it(`can ${fallbackFunction ? "" : "not "}be called with a non-matching function selector via message call`, async function () {
const data = encodeFunctionData({
abi: caller.abi,
functionName: "functionCall",
args: [ctx.address, toFunctionSelector("does_not_exist()")],
});
const txData = { to: caller.address, data: data, gas: contractCallerGas};

const txHash = await walletClient.sendTransaction(txData);
const txReceipt = await publicClient.waitForTransactionReceipt({ hash: txHash });
expect(txReceipt.status).to.equal(fallbackFunction ? "success" : "reverted");
expect(txReceipt.gasUsed < txData.gas, "gas to not be exhausted").to.be.true;
});

it(`can ${fallbackFunction ? "" : "not "}be called with a non-matching function selector via static call`, async function () {
const data = encodeFunctionData({
abi: caller.abi,
functionName: "functionStaticCall",
args: [ctx.address, toFunctionSelector("does_not_exist()")],
});
const txData = { to: caller.address, data: data, gas: contractCallerGas};

const txHash = await walletClient.sendTransaction(txData);
const txReceipt = await publicClient.waitForTransactionReceipt({ hash: txHash });
expect(txReceipt.status).to.equal(fallbackFunction ? "success" : "reverted");
expect(txReceipt.gasUsed < txData.gas, "gas to not be exhausted").to.be.true;
});

it(`can ${fallbackFunction ? "" : "not "}be called with an invalid (short) function selector`, async function () {
const data: Hex = "0x010203";
const txData = { to: ctx.address, data: data, gas: defaultGas };
Expand All @@ -447,6 +510,34 @@ describe("ABI_BasicTests", function () {
expect(txReceipt.gasUsed < txData.gas, "gas to not be exhausted").to.be.true;
});

it(`can ${fallbackFunction ? "" : "not "}be called with an invalid (short) via message call`, async function () {
const data = encodeFunctionData({
abi: caller.abi,
functionName: "functionCall",
args: [ctx.address, "0x010203"],
});
const txData = { to: caller.address, data: data, gas: contractCallerGas};

const txHash = await walletClient.sendTransaction(txData);
const txReceipt = await publicClient.waitForTransactionReceipt({ hash: txHash });
expect(txReceipt.status).to.equal(fallbackFunction ? "success" : "reverted");
expect(txReceipt.gasUsed < txData.gas, "gas to not be exhausted").to.be.true;
});

it(`can ${fallbackFunction ? "" : "not "}be called with an invalid (short) via static call`, async function () {
const data = encodeFunctionData({
abi: caller.abi,
functionName: "functionStaticCall",
args: [ctx.address, "0x010203"],
});
const txData = { to: caller.address, data: data, gas: contractCallerGas};

const txHash = await walletClient.sendTransaction(txData);
const txReceipt = await publicClient.waitForTransactionReceipt({ hash: txHash });
expect(txReceipt.status).to.equal(fallbackFunction ? "success" : "reverted");
expect(txReceipt.gasUsed < txData.gas, "gas to not be exhausted").to.be.true;
});

if (fallbackFunction) {
it(`can ${fallbackFunction.stateMutability === "payable" ? "" : "not "}receive value with a non-matching function selector`, async function () {
const data = toFunctionSelector("does_not_exist()");
Expand All @@ -466,6 +557,28 @@ describe("ABI_BasicTests", function () {
expect(balance).to.equal(expectedBalance);
});

it(`can ${fallbackFunction.stateMutability === "payable" ? "" : "not "}receive value with a non-matching function selector via message call`, async function () {
const data = encodeFunctionData({
abi: caller.abi,
functionName: "functionCall",
args: [ctx.address, toFunctionSelector("does_not_exist()")],
});
const txData = { to: caller.address, data: data, gas: contractCallerGas, value: 1n};
const startingBalance = await publicClient.getBalance({ address: ctx.address });

const txHash = await walletClient.sendTransaction(txData);
const txReceipt = await publicClient.waitForTransactionReceipt({ hash: txHash });
expect(txReceipt.status).to.equal(fallbackFunction.stateMutability === "payable" ? "success" : "reverted");
expect(txReceipt.gasUsed < txData.gas, "gas to not be exhausted").to.be.true;

let expectedBalance = startingBalance;
if (fallbackFunction.stateMutability === "payable") {
expectedBalance = startingBalance + txData.value;
}
const balance = await publicClient.getBalance({ address: ctx.address });
expect(balance).to.equal(expectedBalance);
});

it(`can ${fallbackFunction.stateMutability === "payable" ? "" : "not "}recieve value with an invalid function selector`, async function () {
const data: Hex = "0x010203";
const txData = { to: ctx.address, data: data, gas: defaultGas, value: 1n };
Expand All @@ -483,6 +596,28 @@ describe("ABI_BasicTests", function () {
const balance = await publicClient.getBalance({ address: ctx.address });
expect(balance).to.equal(expectedBalance);
});

it(`can ${fallbackFunction.stateMutability === "payable" ? "" : "not "}recieve value with an invalid function selector via message call`, async function () {
const data = encodeFunctionData({
abi: caller.abi,
functionName: "functionCall",
args: [ctx.address, "0x010203"],
});
const txData = { to: caller.address, data: data, gas: contractCallerGas, value: 1n};
const startingBalance = await publicClient.getBalance({ address: ctx.address });

const txHash = await walletClient.sendTransaction(txData);
const txReceipt = await publicClient.waitForTransactionReceipt({ hash: txHash });
expect(txReceipt.status).to.equal(fallbackFunction.stateMutability === "payable" ? "success" : "reverted");
expect(txReceipt.gasUsed < txData.gas, "gas to not be exhausted").to.be.true;

let expectedBalance = startingBalance;
if (fallbackFunction.stateMutability === "payable") {
expectedBalance = startingBalance + txData.value;
}
const balance = await publicClient.getBalance({ address: ctx.address });
expect(balance).to.equal(expectedBalance);
});
}
});
}
Expand Down

0 comments on commit 72bd242

Please sign in to comment.