Skip to content

Commit

Permalink
Format. Replace fixed nonce
Browse files Browse the repository at this point in the history
  • Loading branch information
fgamundi committed Nov 23, 2023
1 parent b029383 commit 785fc87
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
sendRawTransaction,
} from "@moonwall/util";
import { expectEVMResult } from "helpers";
import { getSignatureParameters } from "util/ethereum";
import { encodeFunctionData, fromHex } from "viem";
import { getSignatureParameters } from "./test-precompile-call-permit";

const PRECOMPILE_BATCH_ADDRESS = "0x0000000000000000000000000000000000000801";
const PRECOMPILE_CALL_PERMIT_ADDRESS = "0x0000000000000000000000000000000000000802";
Expand All @@ -25,10 +25,13 @@ describeSuite({
test: async function () {
const { abi: batchInterface } = fetchCompiledContract("Batch");

let aliceNonce = (await context.polkadotJs().query.system.account(ALITH_ADDRESS)).nonce.toNumber();

// each tx have a different gas limit to ensure it doesn't impact gas used
const batchAllTx = await createViemTransaction(context, {
to: PRECOMPILE_BATCH_ADDRESS,
gas: 1114112n,
nonce: aliceNonce++,
data: encodeFunctionData({
abi: batchInterface,
functionName: "batchAll",
Expand All @@ -44,7 +47,7 @@ describeSuite({
const batchSomeTx = await createViemTransaction(context, {
to: PRECOMPILE_BATCH_ADDRESS,
gas: 1179648n,
nonce: 1,
nonce: aliceNonce++,
data: encodeFunctionData({
abi: batchInterface,
functionName: "batchSome",
Expand All @@ -60,7 +63,7 @@ describeSuite({
const batchSomeUntilFailureTx = await createViemTransaction(context, {
to: PRECOMPILE_BATCH_ADDRESS,
gas: 1245184n,
nonce: 2,
nonce: aliceNonce++,
data: encodeFunctionData({
abi: batchInterface,
functionName: "batchSomeUntilFailure",
Expand Down Expand Up @@ -89,9 +92,9 @@ describeSuite({
.viem("public")
.getTransactionReceipt({ hash: batchSomeUntilFailureResult as `0x${string}` });

expect(batchAllReceipt["gasUsed"]).to.equal(23320n);
expect(batchSomeReceipt["gasUsed"]).to.equal(23320n);
expect(batchSomeUntilFailureReceipt["gasUsed"]).to.equal(23320n);
expect(batchAllReceipt["gasUsed"]).to.equal(44_932n);
expect(batchSomeReceipt["gasUsed"]).to.equal(44_932n);
expect(batchSomeUntilFailureReceipt["gasUsed"]).to.equal(44_932n);
},
});

Expand Down Expand Up @@ -132,7 +135,7 @@ describeSuite({

it({
id: "T03",
title: "shouldn't be able to call from another precompile",
title: "shouldn't be able to be called from another precompile",
test: async function () {
const { abi: batchInterface } = fetchCompiledContract("Batch");
const { abi: callPermitAbi } = fetchCompiledContract("CallPermit");
Expand Down Expand Up @@ -230,24 +233,14 @@ describeSuite({
const { v, r, s } = getSignatureParameters(signature);

const { result: baltatharForAlithResult } = await context.createBlock(
await context.writeContract({
await createViemTransaction(context, {
privateKey: BALTATHAR_PRIVATE_KEY,
contractName: "CallPermit",
contractAddress: PRECOMPILE_CALL_PERMIT_ADDRESS,
functionName: "dispatch",
args: [
ALITH_ADDRESS,
PRECOMPILE_BATCH_ADDRESS,
0,
batchData,
200_000,
9999999999,
v,
r,
s,
],
rawTxOnly: true,
gas: 200_000n,
to: PRECOMPILE_CALL_PERMIT_ADDRESS,
data: encodeFunctionData({
abi: callPermitAbi,
functionName: "dispatch",
args: [ALITH_ADDRESS, PRECOMPILE_BATCH_ADDRESS, 0, batchData, 200_000, 9999999999, v, r, s],
}),
})
);
expectEVMResult(baltatharForAlithResult!.events, "Revert");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,10 @@ import { expect, beforeAll, describeSuite, fetchCompiledContract, deployCreateCo
import { ALITH_ADDRESS, BALTATHAR_ADDRESS, BALTATHAR_PRIVATE_KEY, createViemTransaction } from "@moonwall/util";
import { Abi, encodeFunctionData, fromHex } from "viem";
import { expectEVMResult } from "../../../helpers";
import { getSignatureParameters } from "util/ethereum";

const PRECOMPILE_CALL_PERMIT_ADDRESS = "0x0000000000000000000000000000000000000802";

export function getSignatureParameters(signature: string) {
const r = signature.slice(0, 66); // 32 bytes
const s = `0x${signature.slice(66, 130)}`; // 32 bytes
let v = fromHex(`0x${signature.slice(130, 132)}`, "number"); // 1 byte

if (![27, 28].includes(v)) v += 27; // not sure why we coerce 27

return {
r,
s,
v,
};
}

describeSuite({
id: "DF0902",
title: "Precompile - Call Permit - foo",
Expand Down
15 changes: 15 additions & 0 deletions test/util/ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ethers } from "ethers";
import { FMT_BYTES, FMT_NUMBER } from "web3";

import Debug from "debug";
import { fromHex } from "viem";
const debug = Debug("test:transaction");

export const createTransaction = async (
Expand Down Expand Up @@ -194,3 +195,17 @@ export async function waitUntilEthTxIncluded(promise, web3, txHash) {
await promise();
}
}

export function getSignatureParameters(signature: string) {
const r = signature.slice(0, 66); // 32 bytes
const s = `0x${signature.slice(66, 130)}`; // 32 bytes
let v = fromHex(`0x${signature.slice(130, 132)}`, "number"); // 1 byte

if (![27, 28].includes(v)) v += 27; // not sure why we coerce 27

return {
r,
s,
v,
};
}

0 comments on commit 785fc87

Please sign in to comment.