-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: remove PythContractFactory class
- Loading branch information
1 parent
0f8a1f4
commit e46c461
Showing
4 changed files
with
115 additions
and
167 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
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,18 @@ | ||
import { getContract, Address, GetContractReturnType } from "viem"; | ||
import { PythAbi } from "./pyth-abi"; | ||
import { SuperWalletClient } from "./super-wallet"; | ||
|
||
export type PythContract = GetContractReturnType< | ||
typeof PythAbi, | ||
SuperWalletClient | ||
>; | ||
|
||
export const createPythContract = ( | ||
client: SuperWalletClient, | ||
address: Address | ||
): PythContract => | ||
getContract({ | ||
client, | ||
abi: PythAbi, | ||
address, | ||
}); |
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,71 @@ | ||
import { | ||
createPublicClient, | ||
createWalletClient, | ||
defineChain, | ||
http, | ||
webSocket, | ||
Account, | ||
Chain, | ||
publicActions, | ||
Client, | ||
RpcSchema, | ||
WalletActions, | ||
PublicActions, | ||
WebSocketTransport, | ||
HttpTransport, | ||
Transport, | ||
} from "viem"; | ||
import { mnemonicToAccount } from "viem/accounts"; | ||
import * as chains from "viem/chains"; | ||
import { isWsEndpoint } from "../utils"; | ||
|
||
const UNKNOWN_CHAIN_CONFIG = { | ||
name: "Unknown", | ||
nativeCurrency: { | ||
name: "Unknown", | ||
symbol: "Unknown", | ||
decimals: 18, | ||
}, | ||
rpcUrls: { | ||
default: { | ||
http: [], | ||
}, | ||
}, | ||
}; | ||
|
||
export type SuperWalletClient = Client< | ||
Transport, | ||
Chain, | ||
Account, | ||
RpcSchema, | ||
PublicActions<Transport, Chain, Account> & WalletActions<Chain, Account> | ||
>; | ||
|
||
// Get the transport based on the endpoint | ||
const getTransport = (endpoint: string): WebSocketTransport | HttpTransport => | ||
isWsEndpoint(endpoint) ? webSocket(endpoint) : http(endpoint); | ||
|
||
// Get the chain corresponding to the chainId. If the chain is not found, it will return | ||
// an unknown chain which should work fine in most of the cases. We might need to update | ||
// the viem package to support new chains if they don't work as expected with the unknown | ||
// chain. | ||
const getChainById = (chainId: number): Chain => | ||
Object.values(chains).find((chain) => chain.id === chainId) || | ||
defineChain({ id: chainId, ...UNKNOWN_CHAIN_CONFIG }); | ||
|
||
export const createClient = async ( | ||
endpoint: string, | ||
mnemonic: string | ||
): Promise<SuperWalletClient> => { | ||
const transport = getTransport(endpoint); | ||
|
||
const chainId = await createPublicClient({ | ||
transport, | ||
}).getChainId(); | ||
|
||
return createWalletClient({ | ||
transport, | ||
account: mnemonicToAccount(mnemonic), | ||
chain: getChainById(chainId), | ||
}).extend(publicActions); | ||
}; |