Skip to content

Commit

Permalink
fix: downgraded ethers v6 to ethers v5
Browse files Browse the repository at this point in the history
  • Loading branch information
hydroxylase committed Sep 10, 2024
1 parent 6f1b3cd commit cce989b
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 20 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@ubiquity-dao/rpc-handler": "1.3.0",
"@uniswap/permit2-sdk": "^1.2.0",
"dotenv": "^16.4.4",
"ethers": "5.7.2",
"libsodium-wrappers": "^0.7.13"
},
"devDependencies": {
Expand Down
10 changes: 5 additions & 5 deletions src/handlers/generate-erc20-permit.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PERMIT2_ADDRESS, PermitTransferFrom, SignatureTransfer } from "@uniswap/permit2-sdk";
import { ethers, keccak256, MaxInt256, parseUnits, toUtf8Bytes } from "ethers";
import { ethers, utils, constants } from "ethers";
import { Context, Logger } from "../types/context";
import { PermitReward, TokenType } from "../types";
import { decryptKeys } from "../utils";
Expand Down Expand Up @@ -105,17 +105,17 @@ export async function generateErc20PermitSignature(
const permitTransferFromData: PermitTransferFrom = {
permitted: {
token: tokenAddress,
amount: parseUnits(amount.toString(), tokenDecimals),
amount: utils.parseUnits(amount.toString(), tokenDecimals),
},
spender: _walletAddress,
nonce: BigInt(keccak256(toUtf8Bytes(`${_userId}-${_issueNodeId}`))),
deadline: MaxInt256,
nonce: BigInt(utils.keccak256(utils.toUtf8Bytes(`${_userId}-${_issueNodeId}`))),
deadline: constants.MaxInt256,
};

const { domain, types, values } = SignatureTransfer.getPermitData(permitTransferFromData, PERMIT2_ADDRESS, _evmNetworkId);

const signature = await adminWallet
.signTypedData(
._signTypedData(
{
name: domain.name,
version: domain.version,
Expand Down
8 changes: 4 additions & 4 deletions src/handlers/generate-erc721-permit.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MaxUint256 } from "@uniswap/permit2-sdk";
import { ethers, keccak256, toUtf8Bytes } from "ethers";
import { ethers, utils } from "ethers";
import { Context, Logger } from "../types/context";
import { PermitReward, TokenType } from "../types";
import { isIssueEvent } from "../types/typeguards";
Expand Down Expand Up @@ -129,8 +129,8 @@ export async function generateErc721PermitSignature(
const erc721SignatureData: Erc721PermitSignatureData = {
beneficiary: _walletAddress,
deadline: MaxUint256.toBigInt(),
keys: metadata.map(([key]) => keccak256(toUtf8Bytes(key))),
nonce: BigInt(keccak256(toUtf8Bytes(`${_userId}-${_issueNodeId}`))),
keys: metadata.map(([key]) => utils.keccak256(utils.toUtf8Bytes(key))),
nonce: BigInt(utils.keccak256(utils.toUtf8Bytes(`${_userId}-${_issueNodeId}`))),
values: metadata.map(([, value]) => value),
};

Expand All @@ -141,7 +141,7 @@ export async function generateErc721PermitSignature(
chainId: _evmNetworkId,
};

const signature = await adminWallet.signTypedData(domain, types, erc721SignatureData).catch((error: unknown) => {
const signature = await adminWallet._signTypedData(domain, types, erc721SignatureData).catch((error: unknown) => {
_logger.error("Failed to sign typed data", error);
throw new Error(`Failed to sign typed data: ${error}`);
});
Expand Down
6 changes: 3 additions & 3 deletions src/handlers/register-wallet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ethers, ZeroAddress } from "ethers";
import { ethers, constants } from "ethers";
import { Context } from "../types/context";

export async function registerWallet(context: Context, address: string | null) {
Expand Down Expand Up @@ -26,7 +26,7 @@ export async function registerWallet(context: Context, address: string | null) {
return false;
}

if (address === ZeroAddress) {
if (address === constants.AddressZero) {
logger.error("Skipping to register a wallet address because user is trying to set their address to null address");
return false;
}
Expand Down Expand Up @@ -58,7 +58,7 @@ export async function registerWallet(context: Context, address: string | null) {
export async function resolveAddress(ensName: string): Promise<string | null> {
// Gets the Ethereum address associated with an ENS (Ethereum Name Service) name
// Explicitly set provider to Ethereum mainnet
const provider = new ethers.JsonRpcProvider(`https://eth.llamarpc.com`); // mainnet required for ENS
const provider = new ethers.providers.JsonRpcProvider(`https://eth.llamarpc.com`); // mainnet required for ENS
return await provider.resolveName(ensName).catch((err) => {
console.trace({ err });
return null;
Expand Down
6 changes: 3 additions & 3 deletions src/utils/get-fastest-provider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RPCHandler, HandlerConstructorConfig } from "@ubiquity-dao/rpc-handler";
import { JsonRpcProvider } from "ethers";
import { providers } from "ethers";

function getHandler(networkId: number | string) {
const config = {
Expand All @@ -15,11 +15,11 @@ function getHandler(networkId: number | string) {
return new RPCHandler(config as HandlerConstructorConfig);
}

export async function getFastestProvider(networkId: number | string): Promise<JsonRpcProvider> {
export async function getFastestProvider(networkId: number | string): Promise<providers.JsonRpcProvider> {
try {
const handler = getHandler(networkId);
const provider = await handler.getFastestRpcProvider();
return new JsonRpcProvider(provider.connection.url);
return new providers.JsonRpcProvider(provider.connection.url);
} catch (e) {
throw new Error(`Failed to get fastest provider for networkId: ${networkId}`);
}
Expand Down
8 changes: 4 additions & 4 deletions tests/generate-erc721-permit.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MaxUint256 } from "@uniswap/permit2-sdk";
import { BaseWallet, keccak256, toUtf8Bytes, TypedDataDomain, TypedDataField } from "ethers";
import { Wallet, utils, TypedDataDomain, TypedDataField } from "ethers";
import { generateErc721PermitSignature } from "../src";
import { Context } from "../src/types/context";
import { Env } from "../src/types/env";
Expand Down Expand Up @@ -69,7 +69,7 @@ describe("generateErc721PermitSignature", () => {
(context.adapters.supabase.wallet.getWalletByUserId as jest.Mock).mockReturnValue(SPENDER);
(context.adapters.supabase.user.getUserIdByWallet as jest.Mock).mockReturnValue(userId);
jest
.spyOn(BaseWallet.prototype, "signTypedData")
.spyOn(Wallet.prototype, "_signTypedData")
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.mockImplementation((domain: TypedDataDomain, types: Record<string, TypedDataField[]>, value: Record<string, unknown>) => {
return Promise.resolve("0x0");
Expand Down Expand Up @@ -100,10 +100,10 @@ describe("generateErc721PermitSignature", () => {
expect(result.erc721Request?.metadata).toBeDefined();
expect(result.beneficiary).toBe(SPENDER);
expect(result.deadline).toBe(MaxUint256.toString());
expect(result.nonce).toBe(BigInt(keccak256(toUtf8Bytes(`${userId}-${issueId}`))).toString());
expect(result.nonce).toBe(BigInt(utils.keccak256(utils.toUtf8Bytes(`${userId}-${issueId}`))).toString());
expect(result.erc721Request?.values).toEqual([organizationName, repositoryName, issueNumber, userId, contributionType]);
expect(result.networkId).toBe(context.config.evmNetworkId);
const keysHashed = keys.map((key) => keccak256(toUtf8Bytes(key)));
const keysHashed = keys.map((key) => utils.keccak256(utils.toUtf8Bytes(key)));
expect(result.erc721Request?.keys).toEqual(keysHashed);
}

Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3582,7 +3582,7 @@ esutils@^2.0.2:
resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.3.tgz#74d2eb4de0b8da1293711910d50775b9b710ef64"
integrity sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==

ethers@^5.7.0:
ethers@5.7.2, ethers@^5.7.0:
version "5.7.2"
resolved "https://registry.yarnpkg.com/ethers/-/ethers-5.7.2.tgz#3a7deeabbb8c030d4126b24f84e525466145872e"
integrity sha512-wswUsmWo1aOK8rR7DIKiWSw9DbLWe6x98Jrn8wcTflTVvaXhAMaB5zGAXy0GYQEQp9iO1iSHWVyARQm11zUtyg==
Expand Down

0 comments on commit cce989b

Please sign in to comment.