Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upgrade ethersjs to v6 #82

Draft
wants to merge 3 commits into
base: only-webpack-and-anvil
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,814 changes: 725 additions & 2,089 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"eslint": "^8.51.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.1",
"esmify": "2.1.1",
"esmify": "^0.1.2",
"jest": "^29.5.0",
"nock": "^13.3.1",
"prettier": "^3.0.3",
Expand Down Expand Up @@ -78,7 +78,7 @@
"bcrypto": "^5.4.0",
"cbor": "^9.0.1",
"eth-crypto": "^2.6.0",
"ethers": "^5.7.2",
"ethers": "^6.13.5",
"uniq": "^1.0.1"
}
}
16 changes: 8 additions & 8 deletions src/ResponseListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ import { Contract } from 'ethers'

import { FunctionsRouterSource } from './v1_contract_sources'

import type { BigNumber, providers } from 'ethers'
import type { BigNumberish, Provider } from 'ethers'

import { FulfillmentCode, type FunctionsResponse } from './types'

export class ResponseListener {
private functionsRouter: Contract
private provider: providers.Provider
private provider: Provider

constructor({
provider,
functionsRouterAddress,
}: {
provider: providers.Provider
provider: Provider
functionsRouterAddress: string
}) {
this.provider = provider
Expand All @@ -35,8 +35,8 @@ export class ResponseListener {
'RequestProcessed',
(
_requestId: string,
subscriptionId: BigNumber,
totalCostJuels: BigNumber,
subscriptionId: BigNumberish,
totalCostJuels: BigNumberish,
_,
resultCode: number,
response: string,
Expand Down Expand Up @@ -88,7 +88,7 @@ export class ResponseListener {

const check = async () => {
const receipt = await this.provider.waitForTransaction(txHash, confirmations, timeoutMs)
const updatedId = receipt.logs[0].topics[1]
const updatedId = receipt!.logs[0].topics[1]
if (updatedId !== requestId) {
requestId = updatedId
const response = await this.listenForResponse(requestId, timeoutMs)
Expand Down Expand Up @@ -122,8 +122,8 @@ export class ResponseListener {
'RequestProcessed',
(
requestId: string,
_subscriptionId: BigNumber,
totalCostJuels: BigNumber,
_subscriptionId: BigNumberish,
totalCostJuels: BigNumberish,
_,
resultCode: number,
response: string,
Expand Down
8 changes: 4 additions & 4 deletions src/SecretsManager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from 'axios'
import cbor from 'cbor'
import { Contract, utils } from 'ethers'
import { Contract, encodeBytes32String, isHexString } from 'ethers'
import EthCrypto from 'eth-crypto'

import { encrypt } from './tdh2.js'
Expand Down Expand Up @@ -44,7 +44,7 @@ export class SecretsManager {
}

public async initialize(): Promise<void> {
const donIdBytes32 = utils.formatBytes32String(this.donId!)
const donIdBytes32 = encodeBytes32String(this.donId!)

let functionsCoordinatorAddress: string
try {
Expand Down Expand Up @@ -121,7 +121,7 @@ export class SecretsManager {
throw Error(`URL ${url} did not return a JSON object with an encryptedSecrets field`)
}

if (!utils.isHexString(response.data.encryptedSecrets)) {
if (!isHexString(response.data.encryptedSecrets)) {
throw Error(`URL ${url} did not return a valid hex string for the encryptedSecrets field`)
}

Expand Down Expand Up @@ -197,7 +197,7 @@ export class SecretsManager {
this.isInitialized()
this.validateGatewayUrls(gatewayUrls)

if (!utils.isHexString(encryptedSecretsHexstring)) {
if (!isHexString(encryptedSecretsHexstring)) {
throw Error('encryptedSecretsHexstring must be a valid hex string')
}

Expand Down
42 changes: 25 additions & 17 deletions src/SubscriptionManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { BigNumber, Contract, utils } from 'ethers'
import {
BigNumberish,
Contract,
toBigInt,
isAddress,
formatEther,
AbiCoder,
encodeBytes32String,
} from 'ethers'

import {
LinkTokenSource,
Expand Down Expand Up @@ -83,7 +91,7 @@ export class SubscriptionManager {
public async isAllowlisted(addr: string): Promise<void> {
this.isInitialized()

if (this.functionsAllowList && !(await this.functionsAllowList.hasAccess(addr, []))) {
if (this.functionsAllowList && !(await this.functionsAllowList.hasAccess(addr, '0x'))) {
throw Error(
'This wallet has not been added to the allow list. For access, sign up here:\nhttps://functions.chain.link\n',
)
Expand All @@ -94,7 +102,7 @@ export class SubscriptionManager {
await this.isAllowlisted(await this.signer.getAddress())

if (subCreateConfig?.consumerAddress) {
if (!utils.isAddress(subCreateConfig.consumerAddress)) {
if (!isAddress(subCreateConfig.consumerAddress)) {
throw Error(
`Adding consumer contract failed - invalid address ${subCreateConfig.consumerAddress}`,
)
Expand Down Expand Up @@ -144,7 +152,7 @@ export class SubscriptionManager {
throw Error('Missing consumer contract address')
}

if (!utils.isAddress(consumerAddress)) {
if (!isAddress(consumerAddress)) {
throw Error(`Adding consumer contract failed - invalid address ${consumerAddress}`)
}

Expand Down Expand Up @@ -194,14 +202,14 @@ export class SubscriptionManager {
throw Error('Juels funding amount must be a string or BigInt')
}

let juelsAmountBN: BigNumber
let juelsAmountBN: BigNumberish
try {
juelsAmountBN = BigNumber.from(juelsAmount.toString())
juelsAmountBN = toBigInt(juelsAmount.toString())
} catch (error) {
throw Error(`Juels funding amount invalid:\n${error}`)
}

if (juelsAmountBN.lte(0)) {
if (juelsAmountBN <= 0) {
throw Error('Juels funding amount must be greater than 0')
}

Expand All @@ -214,30 +222,30 @@ export class SubscriptionManager {
// Ensure sufficient balance
const balance = await this.linkToken.balanceOf(this.signer.getAddress())

if (juelsAmountBN.gt(balance)) {
if (juelsAmountBN > balance) {
throw Error(
`Insufficient LINK balance. Trying to fund subscription with ${utils.formatEther(
`Insufficient LINK balance. Trying to fund subscription with ${formatEther(
juelsAmountBN,
)} LINK, but wallet '${await this.signer.getAddress()}' only has ${utils.formatEther(
)} LINK, but wallet '${await this.signer.getAddress()}' only has ${formatEther(
balance,
)} LINK.`,
)
}

const linkContractWithSigner = this.linkToken.connect(this.signer)
const linkContractWithSigner = this.linkToken.connect(this.signer) as Contract

try {
const fundSubTx = txOptions?.overrides
? await linkContractWithSigner.transferAndCall(
this.functionsRouter.address,
juelsAmountBN,
utils.defaultAbiCoder.encode(['uint64'], [subscriptionId]),
AbiCoder.defaultAbiCoder().encode(['uint64'], [subscriptionId]),
txOptions.overrides,
)
: await linkContractWithSigner.transferAndCall(
this.functionsRouter.address,
juelsAmountBN,
utils.defaultAbiCoder.encode(['uint64'], [subscriptionId]),
AbiCoder.defaultAbiCoder().encode(['uint64'], [subscriptionId]),
)
return await fundSubTx.wait(txOptions?.confirmations)
} catch (error) {
Expand Down Expand Up @@ -278,7 +286,7 @@ export class SubscriptionManager {
throw Error('Missing Subscription ID')
}

if (refundAddress && !utils.isAddress(refundAddress)) {
if (refundAddress && !isAddress(refundAddress)) {
throw Error(`'${refundAddress}' is an invalid address`)
}

Expand Down Expand Up @@ -327,7 +335,7 @@ export class SubscriptionManager {
throw Error('Missing consumer contract address')
}

if (!utils.isAddress(consumerAddress)) {
if (!isAddress(consumerAddress)) {
throw Error(`Removing consumer contract failed - invalid address ${consumerAddress}`)
}

Expand Down Expand Up @@ -378,7 +386,7 @@ export class SubscriptionManager {
throw Error('Missing Subscription Id')
}

if (newOwner && !utils.isAddress(newOwner)) {
if (newOwner && !isAddress(newOwner)) {
throw Error(`'${newOwner}' is an invalid address`)
}

Expand Down Expand Up @@ -485,7 +493,7 @@ export class SubscriptionManager {
throw Error('donId has invalid type')
}

const donIdBytes32 = utils.formatBytes32String(donId)
const donIdBytes32 = encodeBytes32String(donId)

await this.getSubscriptionInfo(subscriptionId)

Expand Down
6 changes: 3 additions & 3 deletions src/buildRequestCBOR.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import cbor from 'cbor'
import { utils } from 'ethers'
import { isHexString } from 'ethers'

import { Location, CodeLanguage } from './types'

Expand Down Expand Up @@ -39,7 +39,7 @@ export const buildRequestCBOR = (requestParams: FunctionsRequestParams): string
}

if (requestParams.encryptedSecretsReference) {
if (!utils.isHexString(requestParams.encryptedSecretsReference)) {
if (!isHexString(requestParams.encryptedSecretsReference)) {
throw Error('Invalid encryptedSecretsReference')
}
if (
Expand All @@ -66,7 +66,7 @@ export const buildRequestCBOR = (requestParams: FunctionsRequestParams): string
if (requestParams.bytesArgs) {
if (
!Array.isArray(requestParams.bytesArgs) ||
!requestParams.bytesArgs.every(arg => utils.isHexString(arg))
!requestParams.bytesArgs.every(arg => isHexString(arg))
) {
throw Error('Invalid bytesArgs')
}
Expand Down
15 changes: 10 additions & 5 deletions src/fetchRequestCommitment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Contract, utils } from 'ethers'
import { Contract, encodeBytes32String } from 'ethers'

import { FunctionsRouterSource, FunctionsCoordinatorSource } from './v1_contract_sources'

Expand All @@ -20,14 +20,19 @@ export const fetchRequestCommitment = async ({
if (toBlock > latestBlock) {
toBlock = latestBlock
}
// @ts-ignore
fromBlock = toBlock - pastBlocksToSearch
if (fromBlock < 0) {
fromBlock = 0
}
}

const functionsRouter = new Contract(functionsRouterAddress, FunctionsRouterSource.abi, provider)
const donIdBytes32 = utils.formatBytes32String(donId)
const functionsRouter = new Contract(
functionsRouterAddress,
FunctionsRouterSource.abi,
await provider.getSigner(),
)
const donIdBytes32 = encodeBytes32String(donId)
let functionsCoordinatorAddress: string
try {
functionsCoordinatorAddress = await functionsRouter.getContractById(donIdBytes32)
Expand All @@ -40,7 +45,7 @@ export const fetchRequestCommitment = async ({
const functionsCoordinator = new Contract(
functionsCoordinatorAddress,
FunctionsCoordinatorSource.abi,
provider,
await provider.getSigner(),
)
const eventFilter = functionsCoordinator.filters.OracleRequest(requestId)
const logs = await provider.getLogs({
Expand All @@ -55,7 +60,7 @@ export const fetchRequestCommitment = async ({
}

const event = functionsCoordinator.interface.parseLog(logs[0])
const commitmentData = event.args.commitment
const commitmentData = event!.args.commitment
const requestCommitment: RequestCommitment = {
requestId: commitmentData.requestId,
coordinator: commitmentData.coordinator,
Expand Down
Loading
Loading