Skip to content

Commit

Permalink
Add missing custom plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
ezynda3 committed Aug 22, 2022
1 parent be35760 commit 73b73ee
Showing 1 changed file with 105 additions and 0 deletions.
105 changes: 105 additions & 0 deletions plugins/relay/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { getRelayProvider } from '../../utils/relayer'
import { extendEnvironment } from 'hardhat/config'
import { getFeeData } from '../../utils/gas'
import { BigNumber, BigNumberish, ethers, providers } from 'ethers'
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers'
import { getDeployFunction } from '../../utils/deploy'

extendEnvironment((hre) => {
const provider = getRelayProvider(hre.network.name)
if (provider) {
hre.ethers = {
...hre.ethers,
provider: provider,
getSigner: async (address: string): Promise<SignerWithAddress> => {
address
const signer = await RelaySignerWithAddress.create(provider.getSigner())
return <SignerWithAddress>(<unknown>signer)
},
getSigners: async (): Promise<SignerWithAddress[]> => {
const signer = await RelaySignerWithAddress.create(provider.getSigner())
return [<SignerWithAddress>(<unknown>signer)]
},
}

hre.getNamedAccounts = async () => {
return {
deployer: await provider.getSigner().getAddress(),
}
}

// @ts-ignore
hre.deployments.deploy = getDeployFunction(hre)
}
})

class RelaySignerWithAddress extends ethers.Signer {
public static async create(signer: ethers.providers.JsonRpcSigner) {
return new RelaySignerWithAddress(await signer.getAddress(), signer)
}

private constructor(
public readonly address: string,
private readonly _signer: ethers.providers.JsonRpcSigner
) {
super()
;(this as any).provider = _signer.provider
}

public async getAddress(): Promise<string> {
return this.address
}

public signMessage(message: string | ethers.utils.Bytes): Promise<string> {
return this._signer.signMessage(message)
}

public signTransaction(
transaction: ethers.utils.Deferrable<ethers.providers.TransactionRequest>
): Promise<string> {
return this._signer.signTransaction(transaction)
}

public async sendTransaction(
transaction: ethers.utils.Deferrable<ethers.providers.TransactionRequest>
): Promise<ethers.providers.TransactionResponse> {
const network = (await this.provider?.getNetwork())?.name
const feeData = await getFeeData(
this.provider as providers.Provider,
network || ''
)
let newTx: ethers.utils.Deferrable<ethers.providers.TransactionRequest>
if (feeData.maxFeePerGas === null) {
newTx = {
...transaction,
maxFeePerGas: BigNumber.from(0),
maxPriorityFeePerGas: BigNumber.from(0),
gasPrice: feeData.gasPrice as BigNumberish,
}
} else {
newTx = {
...transaction,
maxFeePerGas: feeData.maxFeePerGas as BigNumberish,
maxPriorityFeePerGas: feeData.maxPriorityFeePerGas as BigNumberish,
}
}
return this._signer.sendTransaction(newTx)
}

public connect(provider: ethers.providers.Provider): RelaySignerWithAddress {
return new RelaySignerWithAddress(
this.address,
this._signer.connect(provider)
)
}

public _signTypedData(
...params: Parameters<ethers.providers.JsonRpcSigner['_signTypedData']>
): Promise<string> {
return this._signer._signTypedData(...params)
}

public toJSON() {
return `<SignerWithAddress ${this.address}>`
}
}

0 comments on commit 73b73ee

Please sign in to comment.