Skip to content

Commit

Permalink
Merge pull request #69 from CityOfZion/CU-86du15m7h-1
Browse files Browse the repository at this point in the history
CU-86du15m7h - Plan, Structure and Implement BsLib support to multich…
  • Loading branch information
lopescode authored Jul 15, 2024
2 parents 71ade0f + f0e5249 commit 0bd0815
Show file tree
Hide file tree
Showing 55 changed files with 1,103 additions and 935 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@cityofzion/blockchain-service",
"comment": "Remove PartialNetwork type",
"type": "patch"
}
],
"packageName": "@cityofzion/blockchain-service"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@cityofzion/bs-ethereum",
"comment": "Adapt constants to work with multi chain",
"type": "patch"
}
],
"packageName": "@cityofzion/bs-ethereum"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@cityofzion/bs-neo-legacy",
"comment": "Adapt constants to work with multi chain",
"type": "patch"
}
],
"packageName": "@cityofzion/bs-neo-legacy"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@cityofzion/bs-neo3",
"comment": "Adapt constants to work with multi chain",
"type": "patch"
}
],
"packageName": "@cityofzion/bs-neo3"
}
6 changes: 1 addition & 5 deletions packages/blockchain-service/src/BSAggregator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AccountWithDerivationPath, BlockchainService, PartialNetwork } from './interfaces'
import { AccountWithDerivationPath, BlockchainService } from './interfaces'

export class BSAggregator<
BSCustomName extends string = string,
Expand All @@ -12,10 +12,6 @@ export class BSAggregator<
this.#blockchainServices = Object.values(blockchainServices)
}

setNetwork(network: PartialNetwork) {
this.#blockchainServices.forEach(bs => bs.setNetwork(network))
}

addBlockchain(name: BSCustomName, blockchain: BSCustom) {
if (this.blockchainServicesByName[name]) throw new Error(`The blockchain ${name} already exist`)
this.blockchainServicesByName[name] = blockchain
Expand Down
9 changes: 2 additions & 7 deletions packages/blockchain-service/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ export type Network<T extends string = string> = {
url: string
}

export type PartialNetwork<T extends string = string> = {
id: T
name?: string
url?: string
}

export type IntentTransferParam = {
receiverAddress: string
tokenHash: string
Expand All @@ -51,7 +45,7 @@ export interface BlockchainService<BSCustomName extends string = string, BSAvail
blockchainDataService: BlockchainDataService
tokens: Token[]
network: Network<BSAvailableNetworks>
setNetwork: (partialNetwork: PartialNetwork<BSAvailableNetworks>) => void
setNetwork: (partialNetwork: Network<BSAvailableNetworks>) => void
generateAccountFromMnemonic(mnemonic: string | string, index: number): AccountWithDerivationPath
generateAccountFromKey(key: string): Account
decrypt(keyOrJson: string, password: string): Promise<Account>
Expand Down Expand Up @@ -233,6 +227,7 @@ export type BuildNftUrlParams = {
}
export interface ExplorerService {
buildTransactionUrl(hash: string): string
buildContractUrl(contractHash: string): string
buildNftUrl(params: BuildNftUrlParams): string
}

Expand Down
56 changes: 26 additions & 30 deletions packages/bs-ethereum/src/BSEthereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
ExchangeDataService,
Network,
NftDataService,
PartialNetwork,
Token,
TransferParam,
} from '@cityofzion/blockchain-service'
Expand All @@ -22,16 +21,9 @@ import { BitqueryEDSEthereum } from './BitqueryEDSEthereum'
import { GhostMarketNDSEthereum } from './GhostMarketNDSEthereum'
import { RpcBDSEthereum } from './RpcBDSEthereum'
import { BitqueryBDSEthereum } from './BitqueryBDSEthereum'
import { LedgerServiceEthereum, LedgerSigner } from './LedgerServiceEthereum'
import { EthersLedgerServiceEthereum, EthersLedgerSigner } from './EthersLedgerServiceEthereum'
import Transport from '@ledgerhq/hw-transport'
import {
AvailableNetworkIds,
BITQUERY_MIRROR_NETWORK_BY_NETWORK_ID,
DEFAULT_URL_BY_NETWORK_ID,
DERIVATION_PATH,
NATIVE_ASSET_BY_NETWORK_ID,
NETWORK_NAME_BY_NETWORK_ID,
} from './constants'
import { AvailableNetworkIds, BSEthereumHelper } from './BSEthereumHelper'

export class BSEthereum<BSCustomName extends string = string>
implements
Expand All @@ -42,42 +34,46 @@ export class BSEthereum<BSCustomName extends string = string>
BSWithLedger
{
readonly blockchainName: BSCustomName
readonly feeToken: Token
readonly derivationPath: string

feeToken!: Token
blockchainDataService!: BlockchainDataService
exchangeDataService!: ExchangeDataService
ledgerService: LedgerServiceEthereum
tokens: Token[]
ledgerService: EthersLedgerServiceEthereum
tokens!: Token[]
nftDataService!: NftDataService
network!: Network<AvailableNetworkIds>

constructor(
blockchainName: BSCustomName,
network: PartialNetwork<AvailableNetworkIds>,
network?: Network<AvailableNetworkIds>,
getLedgerTransport?: (account: Account) => Promise<Transport>
) {
network = network ?? BSEthereumHelper.DEFAULT_NETWORK

this.blockchainName = blockchainName
this.ledgerService = new LedgerServiceEthereum(getLedgerTransport)
this.derivationPath = DERIVATION_PATH
this.tokens = [NATIVE_ASSET_BY_NETWORK_ID[network.id]]
this.feeToken = NATIVE_ASSET_BY_NETWORK_ID[network.id]
this.ledgerService = new EthersLedgerServiceEthereum(getLedgerTransport)
this.derivationPath = BSEthereumHelper.DERIVATION_PATH

this.setNetwork(network)
}

setNetwork(partialNetwork: PartialNetwork<AvailableNetworkIds>) {
const network = {
id: partialNetwork.id,
name: partialNetwork.name ?? NETWORK_NAME_BY_NETWORK_ID[partialNetwork.id],
url: partialNetwork.url ?? DEFAULT_URL_BY_NETWORK_ID[partialNetwork.id],
}
#setTokens(network: Network<AvailableNetworkIds>) {
const nativeAsset = BSEthereumHelper.getNativeAsset(network)
this.tokens = [nativeAsset]
this.feeToken = nativeAsset
}

setNetwork(network: Network<AvailableNetworkIds>) {
this.#setTokens(network)

this.network = network

const bitqueryNetwork = BITQUERY_MIRROR_NETWORK_BY_NETWORK_ID[partialNetwork.id]
const bitqueryNetwork = BitqueryBDSEthereum.MIRROR_NETWORK_BY_NETWORK_ID[network.id]

this.blockchainDataService = bitqueryNetwork ? new BitqueryBDSEthereum(network) : new RpcBDSEthereum(network)

this.exchangeDataService = new BitqueryEDSEthereum(network.id)
this.exchangeDataService = new BitqueryEDSEthereum(network, this.tokens)
this.nftDataService = new GhostMarketNDSEthereum(network)
}

Expand Down Expand Up @@ -165,7 +161,7 @@ export class BSEthereum<BSCustomName extends string = string>

let signer: ethers.Signer
if (ledgerTransport) {
signer = new LedgerSigner(ledgerTransport, provider)
signer = new EthersLedgerSigner(ledgerTransport, provider)
} else {
signer = new ethers.Wallet(param.senderAccount.key, provider)
}
Expand All @@ -175,7 +171,7 @@ export class BSEthereum<BSCustomName extends string = string>

let transactionParams: ethers.utils.Deferrable<ethers.providers.TransactionRequest>

const isNative = NATIVE_ASSET_BY_NETWORK_ID[this.network.id].hash === param.intent.tokenHash
const isNative = this.feeToken.hash === param.intent.tokenHash
if (isNative) {
transactionParams = {
to: param.intent.receiverAddress,
Expand Down Expand Up @@ -206,7 +202,7 @@ export class BSEthereum<BSCustomName extends string = string>

let signer: ethers.Signer
if (ledgerTransport) {
signer = new LedgerSigner(ledgerTransport, provider)
signer = new EthersLedgerSigner(ledgerTransport, provider)
} else {
signer = new ethers.Wallet(param.senderAccount.key, provider)
}
Expand All @@ -215,7 +211,7 @@ export class BSEthereum<BSCustomName extends string = string>

let estimated: ethers.BigNumber

const isNative = NATIVE_ASSET_BY_NETWORK_ID[this.network.id].hash === param.intent.tokenHash
const isNative = this.feeToken.hash === param.intent.tokenHash
const decimals = param.intent.tokenDecimals ?? 18
const amount = ethersBigNumber.parseFixed(param.intent.amount, decimals)

Expand Down
Loading

0 comments on commit 0bd0815

Please sign in to comment.