-
Notifications
You must be signed in to change notification settings - Fork 410
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
196 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import type { ThirdwebContract } from "../contract/index.js"; | ||
import type * as ethers5 from "ethers5"; | ||
import type * as ethers6 from "ethers6"; | ||
import * as universalethers from "ethers"; | ||
import type { RawClient } from "../client/client.js"; | ||
|
||
type Ethers5 = typeof ethers5; | ||
|
||
function isEthers5( | ||
ethers_: typeof ethers5 | typeof ethers6, | ||
): ethers_ is typeof ethers5 { | ||
return "providers" in ethers_; | ||
} | ||
|
||
function assertEthers5( | ||
ethers_: typeof ethers5 | typeof ethers6, | ||
): asserts ethers_ is typeof ethers5 { | ||
if (!isEthers5(ethers_)) { | ||
throw new Error( | ||
"You seem to be using ethers@6, please use the `ethers6Adapter()", | ||
); | ||
} | ||
} | ||
|
||
export function ethers5Adapter() { | ||
const ethers_ = universalethers; | ||
assertEthers5(ethers_); | ||
return { | ||
toProvider: (client: RawClient, chainId: number) => | ||
toProvider(ethers_, client, chainId), | ||
toContract: (contract: ThirdwebContract, abi?: ethers5.ContractInterface) => | ||
toContract(ethers_, contract, abi), | ||
}; | ||
} | ||
|
||
function toProvider( | ||
ethers: Ethers5, | ||
client: RawClient, | ||
chainId: number, | ||
): ethers5.providers.Provider { | ||
const url = `https://${chainId}.rpc.thirdweb.com/${client.clientId}`; | ||
const headers: HeadersInit = {}; | ||
if (client.secretKey) { | ||
headers["x-secret-key"] = client.secretKey; | ||
} | ||
return new ethers.providers.JsonRpcProvider({ | ||
url, | ||
headers: headers, | ||
}); | ||
} | ||
|
||
async function toContract( | ||
ethers: Ethers5, | ||
contract: ThirdwebContract, | ||
abi?: ethers5.ContractInterface, | ||
): Promise<ethers5.Contract> { | ||
// TODO handle signers as well | ||
// resolve the ABI if it is not explicitly passed | ||
if (!abi) { | ||
const { resolveAbi } = await import("../abi/resolveContractAbi.js"); | ||
// this is compatible with ethers5 | ||
abi = (await resolveAbi(contract)) as ethers5.ContractInterface; | ||
} | ||
const provider = toProvider(ethers, contract, contract.chainId); | ||
return new ethers.Contract(contract.address, abi, provider); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import type { ThirdwebContract } from "../contract/index.js"; | ||
import type * as ethers5 from "ethers5"; | ||
import type * as ethers6 from "ethers6"; | ||
import * as universalethers from "ethers"; | ||
import type { RawClient } from "../client/client.js"; | ||
|
||
type Ethers6 = typeof ethers6; | ||
|
||
function isEthers5( | ||
ethers_: typeof ethers5 | typeof ethers6, | ||
): ethers_ is typeof ethers5 { | ||
return "providers" in ethers_; | ||
} | ||
|
||
function assertEthers6( | ||
ethers_: typeof ethers5 | typeof ethers6, | ||
): asserts ethers_ is typeof ethers6 { | ||
if (isEthers5(ethers_)) { | ||
throw new Error( | ||
"You seem to be using ethers@5, please use the `ethers5Adapter()", | ||
); | ||
} | ||
} | ||
|
||
export function ethers6Adapter() { | ||
const ethers_ = universalethers; | ||
assertEthers6(ethers_); | ||
return { | ||
toProvider: (client: RawClient, chainId: number) => | ||
toProvider(ethers_, client, chainId), | ||
toContract: (contract: ThirdwebContract, abi?: ethers6.InterfaceAbi) => | ||
toContract(ethers_, contract, abi), | ||
}; | ||
} | ||
|
||
function toProvider(ethers_: Ethers6, client: RawClient, chainId: number) { | ||
const url = `https://${chainId}.rpc.thirdweb.com/${client.clientId}`; | ||
|
||
const fetchRequest = new ethers_.FetchRequest(url); | ||
if (client.secretKey) { | ||
fetchRequest.setHeader("x-secret-key", client.secretKey); | ||
} | ||
|
||
return new ethers_.JsonRpcProvider(fetchRequest, chainId, { | ||
staticNetwork: true, | ||
}); | ||
} | ||
|
||
async function toContract( | ||
ethers_: Ethers6, | ||
contract: ThirdwebContract, | ||
abi?: ethers6.InterfaceAbi, | ||
) { | ||
// TODO handle signers as well | ||
// resolve the ABI if it is not explicitly passed | ||
if (!abi) { | ||
const { resolveAbi } = await import("../abi/resolveContractAbi.js"); | ||
// this is compatible with Ethers6 | ||
abi = (await resolveAbi(contract)) as ethers6.InterfaceAbi; | ||
} | ||
const provider = toProvider(ethers_, contract, contract.chainId); | ||
return new ethers_.Contract(contract.address, abi, provider); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.