Skip to content

Commit

Permalink
Added function types
Browse files Browse the repository at this point in the history
  • Loading branch information
byshape committed Jul 5, 2023
1 parent 62dd54b commit 164beea
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/asserts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export function assertRoughlyEqualValues(
expected: string | number | bigint,
actual: string | number | bigint,
relativeDiff: number,
) {
): void {
let expectedBN = BigInt(expected);
let actualBN = BigInt(actual);
expect(expectedBN * actualBN).to.be.gte(0, 'Values are of different sign');
Expand Down
22 changes: 11 additions & 11 deletions src/permit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ethers } from 'hardhat';
import { SignerWithAddress } from '@nomicfoundation/hardhat-ethers/signers';
import { AllowanceTransfer, PERMIT2_ADDRESS } from '@uniswap/permit2-sdk';
import { bytecode as permit2Bytecode } from './permit2.json';
import { DaiLikePermitMock, ERC20Permit } from '../typechain-types';
import { DaiLikePermitMock, ERC20Permit, IPermit2 } from '../typechain-types';

export const TypedDataVersion = SignTypedDataVersion.V4;
export const defaultDeadline = constants.MAX_UINT256;
Expand Down Expand Up @@ -35,20 +35,20 @@ export const DaiLikePermit = [
{ name: 'allowed', type: 'bool' },
];

export function trim0x(bigNumber: bigint | string) {
export function trim0x(bigNumber: bigint | string): string {
const s = bigNumber.toString();
if (s.startsWith('0x')) {
return s.substring(2);
}
return s;
}

export function cutSelector(data: string) {
export function cutSelector(data: string): string {
const hexPrefix = '0x';
return hexPrefix + data.substr(hexPrefix.length + 8);
}

export function domainSeparator(name: string, version: string, chainId: string, verifyingContract: string) {
export function domainSeparator(name: string, version: string, chainId: string, verifyingContract: string): string {
return (
'0x' +
TypedDataUtils.hashStruct(
Expand Down Expand Up @@ -96,7 +96,7 @@ export function buildDataLikeDai(
} as const;
}

export async function permit2Contract() {
export async function permit2Contract(): Promise<IPermit2> {
if ((await ethers.provider.getCode(PERMIT2_ADDRESS)) === '0x') {
await ethers.provider.send('hardhat_setCode', [PERMIT2_ADDRESS, permit2Bytecode]);
}
Expand All @@ -115,7 +115,7 @@ export async function getPermit(
value: string,
deadline = defaultDeadline.toString(),
compact = false,
) {
): Promise<string> {
const nonce = await permitContract.nonces(owner);
const name = await permitContract.name();
const data = buildData(
Expand Down Expand Up @@ -147,7 +147,7 @@ export async function getPermit2(
compact = false,
expiration = defaultDeadlinePermit2,
sigDeadline = defaultDeadlinePermit2,
) {
): Promise<string> {
const permitContract = await permit2Contract();
const nonce = (await permitContract.allowance(owner, token, spender)).nonce;
const details = {
Expand Down Expand Up @@ -179,7 +179,7 @@ export async function getPermitLikeDai(
allowed: boolean,
expiry = defaultDeadline.toString(),
compact = false,
) {
): Promise<string> {
const nonce = await permitContract.nonces(holder);
const name = await permitContract.name();
const data = buildDataLikeDai(
Expand All @@ -202,15 +202,15 @@ export async function getPermitLikeDai(
return compact ? compressPermit(permitCall) : decompressPermit(compressPermit(permitCall), constants.ZERO_ADDRESS, holder.address, spender);
}

export function withTarget(target: bigint | string, data: bigint | string) {
export function withTarget(target: bigint | string, data: bigint | string): string {
return target.toString() + trim0x(data);
}

// Type | EIP-2612 | DAI | Permit2
// Uncompressed | 224 | 256 | 352
// Compressed | 100 | 72 | 96

export function compressPermit(permit: string) {
export function compressPermit(permit: string): string {
const abiCoder = ethers.AbiCoder.defaultAbiCoder();
switch (permit.length) {
case 450: {
Expand Down Expand Up @@ -250,7 +250,7 @@ export function compressPermit(permit: string) {
}
}

export function decompressPermit(permit: string, token: string, owner: string, spender: string) {
export function decompressPermit(permit: string, token: string, owner: string, spender: string): string {
const abiCoder = ethers.AbiCoder.defaultAbiCoder();
switch (permit.length) {
case 202: {
Expand Down
6 changes: 3 additions & 3 deletions src/profileEVM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Op = {
memory: string[];
};

function _normalizeOp(ops: Op[], i: number) {
function _normalizeOp(ops: Op[], i: number): void {
if (ops[i].op === 'STATICCALL') {
ops[i].gasCost = ops[i].gasCost - ops[i + 1].gas;

Expand Down Expand Up @@ -97,7 +97,7 @@ export async function profileEVM(
provider: JsonRpcProvider | { send: (method: string, params: unknown[]) => Promise<any> },
txHash: string, instruction: string[],
optionalTraceFile?: PathLike | fs.FileHandle
) {
): Promise<number[]> {
const trace = await provider.send('debug_traceTransaction', [txHash]);

const str = JSON.stringify(trace);
Expand All @@ -117,7 +117,7 @@ export async function gasspectEVM(
txHash: string,
gasspectOptions: Record<string, unknown> = {},
optionalTraceFile?: PathLike | fs.FileHandle
) {
): Promise<string[]> {
const options = { ...gasspectOptionsDefault, ...gasspectOptions };

const trace = await provider.send('debug_traceTransaction', [txHash]);
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export function fixSignature(signature: string): string {
export async function signMessage(
signer: Wallet | { signMessage: (messageHex: string | Uint8Array) => Promise<string> },
messageHex: string | Uint8Array = '0x'
) {
): Promise<string> {
return fixSignature(await signer.signMessage(messageHex));
}

Expand Down

0 comments on commit 164beea

Please sign in to comment.