forked from polkadot-js/extension
-
Notifications
You must be signed in to change notification settings - Fork 66
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
19 changed files
with
559 additions
and
53 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
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
47 changes: 47 additions & 0 deletions
47
packages/extension-base/src/services/balance-service/helpers/subscribe/cardano/index.ts
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,47 @@ | ||
// Copyright 2019-2022 @subwallet/extension-base | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import {ASTAR_REFRESH_BALANCE_INTERVAL} from '@subwallet/extension-base/constants'; | ||
import {_CardanoApi} from '@subwallet/extension-base/services/chain-service/types'; | ||
import {BalanceItem, SusbcribeCardanoPalletBallance} from '@subwallet/extension-base/types'; | ||
import {APIItemState} from "@subwallet/extension-base/background/KoniTypes"; | ||
import {filterAssetsByChainAndType} from "@subwallet/extension-base/utils"; | ||
import {_AssetType} from "@subwallet/chain-list/types"; | ||
|
||
async function getCardanoBalance (addresses: string[], cardanoApi: _CardanoApi): Promise<bigint[]> { | ||
return await Promise.all(addresses.map(async (address) => { | ||
try { | ||
return await cardanoApi. | ||
} catch (e) { | ||
return BigInt(0); | ||
} | ||
})) | ||
} | ||
|
||
export function subscribeCardanoBalance (params: SusbcribeCardanoPalletBallance) { | ||
const { addresses, assetMap, callback, cardanoApi, chainInfo } = params; | ||
const chain = chainInfo.slug; | ||
const nativeTokenInfo = filterAssetsByChainAndType(assetMap, chain, [_AssetType.NATIVE, _AssetType.CIP26]); | ||
|
||
function getBalance () { | ||
getCardanoBalance(addresses, cardanoApi) | ||
.then((balances) => { | ||
return balances.map((balance, index): BalanceItem => { | ||
return { | ||
address: addresses[index], | ||
tokenSlug: nativeTokenSlug, | ||
state: APIItemState.READY, | ||
free: balance.toString(), | ||
locked: '0' // todo: check staking on cardano | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
getBalance(); | ||
const interval = setInterval(getBalance, ASTAR_REFRESH_BALANCE_INTERVAL); | ||
|
||
return () => { | ||
clearInterval(interval); | ||
}; | ||
} |
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
134 changes: 134 additions & 0 deletions
134
packages/extension-base/src/services/chain-service/handler/CardanoApi.ts
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,134 @@ | ||
// Copyright 2019-2022 @subwallet/extension-base authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { BlockFrostAPI } from '@blockfrost/blockfrost-js'; | ||
import { _ApiOptions } from '@subwallet/extension-base/services/chain-service/handler/types'; | ||
import { _CardanoApi, _ChainConnectionStatus } from '@subwallet/extension-base/services/chain-service/types'; | ||
import { createPromiseHandler, PromiseHandler } from '@subwallet/extension-base/utils'; | ||
import { BehaviorSubject } from 'rxjs'; | ||
|
||
export const API_KEY = { | ||
mainnet: 'mainnet6uE9JH3zGYquaxRKA7IMhEuzRUB58uGK', | ||
testnet: 'preprodcnP5RADcrWMlf2cQe4ZKm4cjRvrBQFXM' | ||
}; | ||
|
||
export class CardanoApi implements _CardanoApi { | ||
chainSlug: string; | ||
private api: CardanoApi; | ||
apiUrl: string; | ||
apiError?: string; | ||
apiRetry = 0; | ||
public readonly isApiConnectedSubject = new BehaviorSubject(false); | ||
public readonly connectionStatusSubject = new BehaviorSubject(_ChainConnectionStatus.DISCONNECTED); | ||
isApiReady = false; | ||
isApiReadyOnce = false; | ||
isReadyHandler: PromiseHandler<_CardanoApi>; | ||
|
||
providerName: string; | ||
|
||
constructor (chainSlug: string, apiUrl: string, { isTestnet, providerName }: _ApiOptions) { | ||
this.chainSlug = chainSlug; | ||
this.apiUrl = apiUrl; | ||
this.providerName = providerName || 'unknown'; | ||
this.api = this.createProvider(isTestnet); | ||
this.isReadyHandler = createPromiseHandler<_CardanoApi>(); | ||
|
||
this.connect(); | ||
} | ||
|
||
get isApiConnected (): boolean { | ||
return this.isApiConnectedSubject.getValue(); | ||
} | ||
|
||
get connectionStatus (): _ChainConnectionStatus { | ||
return this.connectionStatusSubject.getValue(); | ||
} | ||
|
||
private updateConnectionStatus (status: _ChainConnectionStatus): void { | ||
const isConnected = status === _ChainConnectionStatus.CONNECTED; | ||
|
||
if (isConnected !== this.isApiConnectedSubject.value) { | ||
this.isApiConnectedSubject.next(isConnected); | ||
} | ||
|
||
if (status !== this.connectionStatusSubject.value) { | ||
this.connectionStatusSubject.next(status); | ||
} | ||
} | ||
|
||
get isReady (): Promise<_CardanoApi> { | ||
return this.isReadyHandler.promise; | ||
} | ||
|
||
async updateApiUrl (apiUrl: string) { | ||
if (this.apiUrl === apiUrl) { | ||
return; | ||
} | ||
|
||
await this.disconnect(); | ||
|
||
this.apiUrl = apiUrl; | ||
this.api = this.createProvider(); | ||
} | ||
|
||
async recoverConnect () { | ||
await this.disconnect(); | ||
this.connect(); | ||
|
||
await this.isReadyHandler.promise; | ||
} | ||
|
||
private createProvider (isTestnet = true) { | ||
const projectId = isTestnet ? API_KEY.testnet : API_KEY.mainnet; | ||
|
||
return new BlockFrostAPI({ | ||
projectId | ||
}); | ||
} | ||
|
||
private getJsonRpc (url: string) { | ||
return `${url}/jsonRPC`; | ||
} | ||
|
||
connect (): void { | ||
this.updateConnectionStatus(_ChainConnectionStatus.CONNECTING); | ||
// There isn't a persistent network connection underlying TonClient. Cant check connection status. | ||
// this.isApiReadyOnce = true; | ||
this.onConnect(); | ||
} | ||
|
||
async disconnect () { | ||
this.onDisconnect(); | ||
this.updateConnectionStatus(_ChainConnectionStatus.DISCONNECTED); | ||
|
||
return Promise.resolve(); | ||
} | ||
|
||
destroy () { | ||
// Todo: implement this in the future | ||
return this.disconnect(); | ||
} | ||
|
||
onConnect (): void { | ||
if (!this.isApiConnected) { | ||
console.log(`Connected to ${this.chainSlug} at ${this.apiUrl}`); | ||
this.isApiReady = true; | ||
|
||
if (this.isApiReadyOnce) { | ||
this.isReadyHandler.resolve(this); | ||
} | ||
} | ||
|
||
this.updateConnectionStatus(_ChainConnectionStatus.CONNECTED); | ||
} | ||
|
||
onDisconnect (): void { | ||
this.updateConnectionStatus(_ChainConnectionStatus.DISCONNECTED); | ||
|
||
if (this.isApiConnected) { | ||
console.warn(`Disconnected from ${this.chainSlug} of ${this.apiUrl}`); | ||
this.isApiReady = false; | ||
this.isReadyHandler = createPromiseHandler<_CardanoApi>(); | ||
} | ||
} | ||
} |
Oops, something went wrong.