UNPKG

@robertprp/intents-sdk

Version:

Shogun Network Intent-based cross-chain swaps SDK

118 lines 4.16 kB
import { createClient, createWalletClient, getContract, http, } from 'viem'; import { privateKeyToAccount } from 'viem/accounts'; import { arbitrum, base, optimism } from 'viem/chains'; import { ChainID } from '../../chains.js'; import { ERC20ABI } from './abi/erc20.js'; import { SOURCE_CHAIN_GUARD_ABI } from './abi/source-chain-guard.js'; import { PERMIT2_ABI } from './abi/permit2.js'; import { SINGLE_CHAIN_GUARD_LIMIT_ABI } from './abi/single-chain-guard-limit.js'; import { hyperEVM } from './connectors/hyperevm.js'; export const chainIdToProviderChainMap = { [ChainID.Arbitrum]: arbitrum, [ChainID.Optimism]: optimism, [ChainID.Base]: base, [ChainID.Hyperliquid]: hyperEVM, }; export class ChainProvider { constructor(config) { Object.defineProperty(this, "privateKey", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "chainId", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "publicClient", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "walletClient", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.privateKey = config.privateKey; this.chainId = config.chainId; this.publicClient = this.getClient(config.rpcProviderUrl); this.walletClient = this.getWalletClient(config.rpcProviderUrl); } static getClient(chainId, rpcProviderUrl) { const chain = chainIdToProviderChainMap[chainId]; return createClient({ chain, transport: http(rpcProviderUrl), }); } getClient(rpcProviderUrl) { return ChainProvider.getClient(this.chainId, rpcProviderUrl); } getAccount() { return privateKeyToAccount(this.privateKey); } getWalletClient(rpcProviderUrl) { const chain = chainIdToProviderChainMap[this.chainId]; return createWalletClient({ account: privateKeyToAccount(this.privateKey), chain, transport: http(rpcProviderUrl), }); } getPermit2Contract(permit2Address) { return getContract({ address: permit2Address, abi: PERMIT2_ABI, client: { wallet: this.walletClient, public: this.publicClient, chain: chainIdToProviderChainMap[this.chainId], account: this.walletClient.account, }, }); } getSingleChainAuctioneerContract(address) { return getContract({ address: address, abi: SINGLE_CHAIN_GUARD_LIMIT_ABI, client: { wallet: this.walletClient, public: this.publicClient, chain: chainIdToProviderChainMap[this.chainId], account: this.walletClient.account, }, }); } // Any type since the inferred type of this node exceeds the maximum length the compiler will serialize getCrossChainAuctioneerContract(auctioneerAddress) { return getContract({ address: auctioneerAddress, abi: SOURCE_CHAIN_GUARD_ABI, client: { wallet: this.walletClient, public: this.publicClient, chain: chainIdToProviderChainMap[this.chainId], account: this.walletClient.account, }, }); } getERC20Contract(tokenAddress) { return getContract({ address: tokenAddress, abi: ERC20ABI, client: { wallet: this.walletClient, public: this.publicClient, chain: chainIdToProviderChainMap[this.chainId], account: this.walletClient.account, }, }); } } //# sourceMappingURL=chain-provider.js.map