@rarimo/providers-near
Version:
Features of the Rarimo SDK that provide access to wallets and the ability to interact with them on the NEAR blockchain.
129 lines (109 loc) • 3.34 kB
text/typescript
import type {
NearTransactionRequestBody,
NearTransactionResponse,
ProviderProxy,
TransactionRequestBody,
TransactionResponse,
} from '@rarimo/provider'
import {
ProviderEventBus,
ProviderEventBusEvents,
Providers,
} from '@rarimo/provider'
import type { Chain, ChainId } from '@rarimo/shared'
import { ChainNames, NEAR_CHAIN_IDS } from '@rarimo/shared'
import {
getNearExplorerAddressUrl,
getNearExplorerTxUrl,
handleNearError,
} from '@/helpers'
import { NearRawProvider } from '@/near-raw-provider'
import type { NearProviderRpcError } from '@/types'
export class NearProvider extends ProviderEventBus implements ProviderProxy {
readonly
/**
* @description In most cases, instead of using this constructor, pass the NearProvider class to {@link @rarimo/provider!createProvider}.
*/
constructor() {
super()
this.
}
static get providerType(): Providers {
return Providers.Near
}
get isConnected(): boolean {
return Boolean(this.
}
get chainId(): ChainId | undefined {
return this.
}
get address(): string | undefined {
return this.
}
async init(): Promise<void> {
try {
await this.
this.
this.
} catch (error) {
handleNearError(error as NearProviderRpcError)
}
}
const networkId = this.
this.
this.
}
async switchChain(chainId: ChainId): Promise<void> {
this.
this.
}
async connect(): Promise<void> {
try {
await this.
await this.
this.
} catch (error) {
handleNearError(error as NearProviderRpcError)
}
}
async disconnect(): Promise<void> {
try {
await this.
this.
this.
} catch (error) {
handleNearError(error as NearProviderRpcError)
}
}
getHashFromTxResponse(txResponse: NearTransactionResponse): string {
const transactionResponse = txResponse as NearTransactionResponse
return transactionResponse.transaction.hash
}
getTxUrl(chain: Chain, txHash: string): string {
return getNearExplorerTxUrl(chain, txHash)
}
getAddressUrl(chain: Chain, address: string): string {
return getNearExplorerAddressUrl(chain, address)
}
async signAndSendTx(
txRequestBody: TransactionRequestBody,
): Promise<TransactionResponse> {
try {
return await this.
txRequestBody as NearTransactionRequestBody,
)
} catch (error) {
handleNearError(error as NearProviderRpcError)
}
}
this.emit(event, {
address: this.
chainId: this.
isConnected: this.isConnected,
})
}
}