@xyo-network/chain-services 1.16.26 → 1.17.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/neutral/AccountBalance/accountBalanceServiceFromArchivist.d.ts +1 -1
- package/dist/neutral/AccountBalance/accountBalanceServiceFromArchivist.d.ts.map +1 -1
- package/dist/neutral/AccountBalance/index.d.ts +0 -1
- package/dist/neutral/AccountBalance/index.d.ts.map +1 -1
- package/dist/neutral/BaseService.d.ts +0 -3
- package/dist/neutral/BaseService.d.ts.map +1 -1
- package/dist/neutral/BlockReward/EvmBlockRewardViewer.d.ts +17 -0
- package/dist/neutral/BlockReward/EvmBlockRewardViewer.d.ts.map +1 -0
- package/dist/neutral/BlockReward/index.d.ts +1 -3
- package/dist/neutral/BlockReward/index.d.ts.map +1 -1
- package/dist/neutral/ChainBlockNumberIteration/ChainBlockNumberIterationService.d.ts +23 -11
- package/dist/neutral/ChainBlockNumberIteration/ChainBlockNumberIterationService.d.ts.map +1 -1
- package/dist/neutral/ChainService/Evm/Evm.d.ts +6 -5
- package/dist/neutral/ChainService/Evm/Evm.d.ts.map +1 -1
- package/dist/neutral/ChainService/Memory/Memory.d.ts +6 -4
- package/dist/neutral/ChainService/Memory/Memory.d.ts.map +1 -1
- package/dist/neutral/ChainValidator/XyoValidator.d.ts +4 -18
- package/dist/neutral/ChainValidator/XyoValidator.d.ts.map +1 -1
- package/dist/neutral/Election/BaseElectionService.d.ts +3 -3
- package/dist/neutral/Election/BaseElectionService.d.ts.map +1 -1
- package/dist/neutral/PendingTransactions/BasePendingTransactions.d.ts +9 -9
- package/dist/neutral/PendingTransactions/BasePendingTransactions.d.ts.map +1 -1
- package/dist/neutral/StakeIntent/XyoStakeIntentService.d.ts +7 -6
- package/dist/neutral/StakeIntent/XyoStakeIntentService.d.ts.map +1 -1
- package/dist/neutral/Time/BaseTimeSyncService.d.ts +3 -6
- package/dist/neutral/Time/BaseTimeSyncService.d.ts.map +1 -1
- package/dist/neutral/blockViewerFromChainIteratorAndArchivist.d.ts.map +1 -1
- package/dist/neutral/implementation/accountBalance.d.ts +7 -0
- package/dist/neutral/implementation/accountBalance.d.ts.map +1 -0
- package/dist/neutral/implementation/blockViewer.d.ts +7 -0
- package/dist/neutral/implementation/blockViewer.d.ts.map +1 -0
- package/dist/neutral/implementation/chain/evm.d.ts +7 -0
- package/dist/neutral/implementation/chain/evm.d.ts.map +1 -0
- package/dist/neutral/implementation/chain/index.d.ts +7 -0
- package/dist/neutral/implementation/chain/index.d.ts.map +1 -0
- package/dist/neutral/implementation/evm/index.d.ts +2 -0
- package/dist/neutral/implementation/evm/index.d.ts.map +1 -0
- package/dist/neutral/implementation/evm/initChainId.d.ts +4 -0
- package/dist/neutral/implementation/evm/initChainId.d.ts.map +1 -0
- package/dist/neutral/implementation/evm/initEvmProvider.d.ts +11 -0
- package/dist/neutral/implementation/evm/initEvmProvider.d.ts.map +1 -0
- package/dist/neutral/implementation/evm/initInfuraProvider.d.ts +6 -0
- package/dist/neutral/implementation/evm/initInfuraProvider.d.ts.map +1 -0
- package/dist/neutral/implementation/evm/initJsonRpcProvider.d.ts +6 -0
- package/dist/neutral/implementation/evm/initJsonRpcProvider.d.ts.map +1 -0
- package/dist/neutral/implementation/head/createBootstrapHead.d.ts +5 -0
- package/dist/neutral/implementation/head/createBootstrapHead.d.ts.map +1 -0
- package/dist/neutral/implementation/head/createForkedHead/config/getBridgeDestinationDetails.d.ts +8 -0
- package/dist/neutral/implementation/head/createForkedHead/config/getBridgeDestinationDetails.d.ts.map +1 -0
- package/dist/neutral/implementation/head/createForkedHead/config/getBridgeSourceDetails.d.ts +9 -0
- package/dist/neutral/implementation/head/createForkedHead/config/getBridgeSourceDetails.d.ts.map +1 -0
- package/dist/neutral/implementation/head/createForkedHead/config/getForkDetails.d.ts +4 -0
- package/dist/neutral/implementation/head/createForkedHead/config/getForkDetails.d.ts.map +1 -0
- package/dist/neutral/implementation/head/createForkedHead/config/index.d.ts +4 -0
- package/dist/neutral/implementation/head/createForkedHead/config/index.d.ts.map +1 -0
- package/dist/neutral/implementation/head/createForkedHead/createForkedHead.d.ts +5 -0
- package/dist/neutral/implementation/head/createForkedHead/createForkedHead.d.ts.map +1 -0
- package/dist/neutral/implementation/head/createForkedHead/getBridgeDestinationObservation.d.ts +11 -0
- package/dist/neutral/implementation/head/createForkedHead/getBridgeDestinationObservation.d.ts.map +1 -0
- package/dist/neutral/implementation/head/createForkedHead/getBridgeIntent.d.ts +11 -0
- package/dist/neutral/implementation/head/createForkedHead/getBridgeIntent.d.ts.map +1 -0
- package/dist/neutral/implementation/head/createForkedHead/getBridgeSourceObservation.d.ts +11 -0
- package/dist/neutral/implementation/head/createForkedHead/getBridgeSourceObservation.d.ts.map +1 -0
- package/dist/neutral/implementation/head/createForkedHead/getFirstBlockForNewChain.d.ts +13 -0
- package/dist/neutral/implementation/head/createForkedHead/getFirstBlockForNewChain.d.ts.map +1 -0
- package/dist/neutral/implementation/head/createForkedHead/index.d.ts +2 -0
- package/dist/neutral/implementation/head/createForkedHead/index.d.ts.map +1 -0
- package/dist/neutral/implementation/head/getForkFromBlock.d.ts +12 -0
- package/dist/neutral/implementation/head/getForkFromBlock.d.ts.map +1 -0
- package/dist/neutral/implementation/head/head.d.ts +11 -0
- package/dist/neutral/implementation/head/head.d.ts.map +1 -0
- package/dist/neutral/implementation/head/index.d.ts +2 -0
- package/dist/neutral/implementation/head/index.d.ts.map +1 -0
- package/dist/neutral/implementation/head/submitNewChain.d.ts +10 -0
- package/dist/neutral/implementation/head/submitNewChain.d.ts.map +1 -0
- package/dist/neutral/implementation/index.d.ts +13 -0
- package/dist/neutral/implementation/index.d.ts.map +1 -0
- package/dist/neutral/implementation/mempoolRunner.d.ts +7 -0
- package/dist/neutral/implementation/mempoolRunner.d.ts.map +1 -0
- package/dist/neutral/implementation/mempoolViewer.d.ts +7 -0
- package/dist/neutral/implementation/mempoolViewer.d.ts.map +1 -0
- package/dist/neutral/implementation/pendingTransactions.d.ts +9 -0
- package/dist/neutral/implementation/pendingTransactions.d.ts.map +1 -0
- package/dist/neutral/implementation/producer.d.ts +5 -0
- package/dist/neutral/implementation/producer.d.ts.map +1 -0
- package/dist/neutral/implementation/reward.d.ts +14 -0
- package/dist/neutral/implementation/reward.d.ts.map +1 -0
- package/dist/neutral/implementation/time.d.ts +6 -0
- package/dist/neutral/implementation/time.d.ts.map +1 -0
- package/dist/neutral/implementation/validator.d.ts +5 -0
- package/dist/neutral/implementation/validator.d.ts.map +1 -0
- package/dist/neutral/index.d.ts +2 -2
- package/dist/neutral/index.d.ts.map +1 -1
- package/dist/neutral/index.mjs +1623 -1150
- package/dist/neutral/index.mjs.map +1 -1
- package/dist/neutral/simple/block/index.d.ts +2 -0
- package/dist/neutral/simple/block/index.d.ts.map +1 -0
- package/dist/neutral/simple/block/runner/SimpleBlockRunner.d.ts +117 -0
- package/dist/neutral/simple/block/runner/SimpleBlockRunner.d.ts.map +1 -0
- package/dist/neutral/simple/block/runner/generateTransactionFeeTransfers.d.ts.map +1 -0
- package/dist/neutral/simple/block/runner/index.d.ts +2 -0
- package/dist/neutral/simple/block/runner/index.d.ts.map +1 -0
- package/dist/neutral/simple/block/runner/spec/SimpleBlockRewardViewer.d.ts +2 -0
- package/dist/neutral/simple/block/runner/spec/SimpleBlockRewardViewer.d.ts.map +1 -0
- package/dist/neutral/simple/index.d.ts +2 -0
- package/dist/neutral/simple/index.d.ts.map +1 -0
- package/package.json +30 -27
- package/src/AccountBalance/accountBalanceServiceFromArchivist.ts +38 -10
- package/src/AccountBalance/index.ts +0 -1
- package/src/BaseService.ts +0 -13
- package/src/BlockReward/EvmBlockRewardViewer.ts +50 -0
- package/src/BlockReward/index.ts +1 -3
- package/src/ChainBlockNumberIteration/ChainBlockNumberIterationService.ts +74 -6
- package/src/ChainService/Evm/Evm.ts +39 -36
- package/src/ChainService/Memory/Memory.ts +14 -7
- package/src/ChainValidator/XyoValidator.ts +29 -34
- package/src/Election/BaseElectionService.ts +5 -4
- package/src/NetworkStakeStepReward/BaseNetworkStakeStepRewardService.ts +18 -18
- package/src/PendingTransactions/BasePendingTransactions.ts +15 -9
- package/src/Schemas/BaseSchemasService.ts +1 -1
- package/src/StakeIntent/XyoStakeIntentService.ts +24 -18
- package/src/StepStake/BaseStepStakeService.ts +2 -2
- package/src/Time/BaseTimeSyncService.ts +6 -16
- package/src/blockViewerFromChainIteratorAndArchivist.ts +20 -1
- package/src/implementation/accountBalance.ts +29 -0
- package/src/implementation/blockViewer.ts +29 -0
- package/src/implementation/chain/evm.ts +39 -0
- package/src/implementation/chain/index.ts +46 -0
- package/src/implementation/evm/index.ts +1 -0
- package/src/implementation/evm/initChainId.ts +20 -0
- package/src/implementation/evm/initEvmProvider.ts +24 -0
- package/src/implementation/evm/initInfuraProvider.ts +26 -0
- package/src/implementation/evm/initJsonRpcProvider.ts +20 -0
- package/src/implementation/head/createBootstrapHead.ts +31 -0
- package/src/implementation/head/createForkedHead/config/getBridgeDestinationDetails.ts +27 -0
- package/src/implementation/head/createForkedHead/config/getBridgeSourceDetails.ts +19 -0
- package/src/implementation/head/createForkedHead/config/getForkDetails.ts +10 -0
- package/src/implementation/head/createForkedHead/config/index.ts +3 -0
- package/src/implementation/head/createForkedHead/createForkedHead.ts +29 -0
- package/src/implementation/head/createForkedHead/getBridgeDestinationObservation.ts +42 -0
- package/src/implementation/head/createForkedHead/getBridgeIntent.ts +51 -0
- package/src/implementation/head/createForkedHead/getBridgeSourceObservation.ts +46 -0
- package/src/implementation/head/createForkedHead/getFirstBlockForNewChain.ts +41 -0
- package/src/implementation/head/createForkedHead/index.ts +1 -0
- package/src/implementation/head/getForkFromBlock.ts +43 -0
- package/src/implementation/head/head.ts +49 -0
- package/src/implementation/head/index.ts +1 -0
- package/src/implementation/head/submitNewChain.ts +27 -0
- package/src/implementation/index.ts +12 -0
- package/src/implementation/mempoolRunner.ts +29 -0
- package/src/implementation/mempoolViewer.ts +29 -0
- package/src/implementation/pendingTransactions.ts +36 -0
- package/src/implementation/producer.ts +16 -0
- package/src/implementation/reward.ts +53 -0
- package/src/implementation/time.ts +26 -0
- package/src/implementation/validator.ts +15 -0
- package/src/index.ts +2 -2
- package/src/simple/block/index.ts +1 -0
- package/src/{BlockProducer/BaseBlockProducerService.ts → simple/block/runner/SimpleBlockRunner.ts} +111 -74
- package/src/simple/block/runner/index.ts +1 -0
- package/src/simple/block/runner/spec/SimpleBlockRewardViewer.ts +413 -0
- package/src/simple/index.ts +1 -0
- package/dist/neutral/AccountBalance/BaseAccountBalanceService.d.ts +0 -36
- package/dist/neutral/AccountBalance/BaseAccountBalanceService.d.ts.map +0 -1
- package/dist/neutral/AccountTransfers/BaseAccountTransfersService.d.ts +0 -13
- package/dist/neutral/AccountTransfers/BaseAccountTransfersService.d.ts.map +0 -1
- package/dist/neutral/AccountTransfers/index.d.ts +0 -2
- package/dist/neutral/AccountTransfers/index.d.ts.map +0 -1
- package/dist/neutral/BlockProducer/BaseBlockProducerService.d.ts +0 -85
- package/dist/neutral/BlockProducer/BaseBlockProducerService.d.ts.map +0 -1
- package/dist/neutral/BlockProducer/generateTransactionFeeTransfers.d.ts.map +0 -1
- package/dist/neutral/BlockProducer/index.d.ts +0 -2
- package/dist/neutral/BlockProducer/index.d.ts.map +0 -1
- package/dist/neutral/BlockReward/BaseBlockRewardService.d.ts +0 -10
- package/dist/neutral/BlockReward/BaseBlockRewardService.d.ts.map +0 -1
- package/dist/neutral/BlockReward/EvmBlockRewardService.d.ts +0 -18
- package/dist/neutral/BlockReward/EvmBlockRewardService.d.ts.map +0 -1
- package/dist/neutral/BlockReward/MemoryBlockRewardService.d.ts +0 -23
- package/dist/neutral/BlockReward/MemoryBlockRewardService.d.ts.map +0 -1
- package/src/AccountBalance/BaseAccountBalanceService.ts +0 -100
- package/src/AccountTransfers/BaseAccountTransfersService.ts +0 -36
- package/src/AccountTransfers/index.ts +0 -1
- package/src/BlockProducer/index.ts +0 -1
- package/src/BlockReward/BaseBlockRewardService.ts +0 -17
- package/src/BlockReward/EvmBlockRewardService.ts +0 -49
- package/src/BlockReward/MemoryBlockRewardService.ts +0 -72
- /package/dist/neutral/{BlockProducer → simple/block/runner}/generateTransactionFeeTransfers.d.ts +0 -0
- /package/src/{BlockProducer → simple/block/runner}/generateTransactionFeeTransfers.ts +0 -0
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import type { Hash } from '@xylabs/sdk-js'
|
|
1
|
+
import type { Hash, Promisable } from '@xylabs/sdk-js'
|
|
2
2
|
import { isDefined } from '@xylabs/sdk-js'
|
|
3
3
|
import type { ReadArchivist } from '@xyo-network/archivist-model'
|
|
4
4
|
import { PayloadBuilder } from '@xyo-network/payload-builder'
|
|
5
|
+
import type { Payload, WithHashMeta } from '@xyo-network/payload-model'
|
|
5
6
|
import type {
|
|
6
7
|
SignedHydratedBlockWithHashMeta,
|
|
7
8
|
XL1BlockNumber,
|
|
@@ -44,6 +45,18 @@ export function blockViewerFromChainIteratorAndArchivist(
|
|
|
44
45
|
}
|
|
45
46
|
return results
|
|
46
47
|
},
|
|
48
|
+
blocksByNumber: async function (number: XL1BlockNumber, limit = 10): Promise<SignedHydratedBlockWithHashMeta[]> {
|
|
49
|
+
const results: SignedHydratedBlockWithHashMeta[] = []
|
|
50
|
+
for (let i = 0; i < limit; i++) {
|
|
51
|
+
const block = await this.blockByNumber(number)
|
|
52
|
+
if (block) {
|
|
53
|
+
results.push(block)
|
|
54
|
+
} else {
|
|
55
|
+
break
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return results
|
|
59
|
+
},
|
|
47
60
|
currentBlock: async function (): Promise<SignedHydratedBlockWithHashMeta> {
|
|
48
61
|
const head = await chainIterator.head()
|
|
49
62
|
return this.blockByHash(head._hash) as Promise<SignedHydratedBlockWithHashMeta>
|
|
@@ -56,6 +69,12 @@ export function blockViewerFromChainIteratorAndArchivist(
|
|
|
56
69
|
const head = await chainIterator.head()
|
|
57
70
|
return head.block
|
|
58
71
|
},
|
|
72
|
+
payloadByHash: function (_hash: Hash): Promisable<WithHashMeta<Payload> | null> {
|
|
73
|
+
throw new Error('Function not implemented.')
|
|
74
|
+
},
|
|
75
|
+
payloadsByHash: function (_hashes: Hash[]): Promisable<WithHashMeta<Payload>[]> {
|
|
76
|
+
throw new Error('Function not implemented.')
|
|
77
|
+
},
|
|
59
78
|
} satisfies BlockViewer
|
|
60
79
|
return result
|
|
61
80
|
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { isDefined, type Promisable } from '@xylabs/sdk-js'
|
|
2
|
+
import type { Initializable, InitializableParams } from '@xyo-network/xl1-protocol'
|
|
3
|
+
import type { AccountBalanceViewer, Config } from '@xyo-network/xl1-protocol-sdk'
|
|
4
|
+
import {
|
|
5
|
+
AccountBalanceViewerRpcSchemas,
|
|
6
|
+
HttpRpcTransport,
|
|
7
|
+
JsonRpcAccountBalanceViewer,
|
|
8
|
+
} from '@xyo-network/xl1-rpc'
|
|
9
|
+
|
|
10
|
+
let balanceServiceSingleton: Promisable<AccountBalanceViewer> | undefined
|
|
11
|
+
|
|
12
|
+
export type InitAccountBalanceServiceParams = InitializableParams<{
|
|
13
|
+
config: Config
|
|
14
|
+
}>
|
|
15
|
+
|
|
16
|
+
export const initAccountBalanceService: Initializable<InitAccountBalanceServiceParams, AccountBalanceViewer>
|
|
17
|
+
= (params): Promisable<AccountBalanceViewer> => {
|
|
18
|
+
const { config, logger } = params
|
|
19
|
+
if (balanceServiceSingleton) return balanceServiceSingleton
|
|
20
|
+
const endpoint = config.services?.accountBalanceViewerEndpoint ?? config.services?.apiEndpoint
|
|
21
|
+
if (isDefined(endpoint)) {
|
|
22
|
+
const transport = new HttpRpcTransport(endpoint, { ...AccountBalanceViewerRpcSchemas })
|
|
23
|
+
const viewer = new JsonRpcAccountBalanceViewer(transport)
|
|
24
|
+
logger?.log('Using AccountBalanceViewer RPC service at', endpoint)
|
|
25
|
+
return viewer
|
|
26
|
+
} else {
|
|
27
|
+
throw new Error('No AccountBalanceViewer endpoint configured')
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { isDefined, type Promisable } from '@xylabs/sdk-js'
|
|
2
|
+
import type { Initializable, InitializableParams } from '@xyo-network/xl1-protocol'
|
|
3
|
+
import type { BlockViewer, Config } from '@xyo-network/xl1-protocol-sdk'
|
|
4
|
+
import {
|
|
5
|
+
BlockViewerRpcSchemas,
|
|
6
|
+
HttpRpcTransport,
|
|
7
|
+
JsonRpcBlockViewer,
|
|
8
|
+
} from '@xyo-network/xl1-rpc'
|
|
9
|
+
|
|
10
|
+
let blockViewerSingleton: Promisable<BlockViewer> | undefined
|
|
11
|
+
|
|
12
|
+
export type InitBlockViewerParams = InitializableParams<{
|
|
13
|
+
config: Config
|
|
14
|
+
}>
|
|
15
|
+
|
|
16
|
+
export const initBlockViewer: Initializable<InitBlockViewerParams, BlockViewer>
|
|
17
|
+
= (params): Promisable<BlockViewer> => {
|
|
18
|
+
const { config, logger } = params
|
|
19
|
+
if (blockViewerSingleton) return blockViewerSingleton
|
|
20
|
+
const endpoint = config.services?.apiEndpoint
|
|
21
|
+
if (isDefined(endpoint)) {
|
|
22
|
+
const transport = new HttpRpcTransport(endpoint, { ...BlockViewerRpcSchemas })
|
|
23
|
+
const viewer = new JsonRpcBlockViewer(transport)
|
|
24
|
+
logger?.log('Using BlockViewer RPC service at', endpoint)
|
|
25
|
+
return viewer
|
|
26
|
+
} else {
|
|
27
|
+
throw new Error('No BlockViewer endpoint configured')
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { Address, Promisable } from '@xylabs/sdk-js'
|
|
2
|
+
import {
|
|
3
|
+
asAddress, assertEx, isDefined, ZERO_ADDRESS,
|
|
4
|
+
} from '@xylabs/sdk-js'
|
|
5
|
+
import type { Initializable } from '@xyo-network/xl1-protocol'
|
|
6
|
+
import type { Config } from '@xyo-network/xl1-protocol-sdk'
|
|
7
|
+
import type { ContractRunner } from 'ethers'
|
|
8
|
+
import { Wallet } from 'ethers/wallet'
|
|
9
|
+
|
|
10
|
+
import { EvmChainService } from '../../ChainService/index.ts'
|
|
11
|
+
import type { BaseAccountableServiceParams } from '../../model/index.ts'
|
|
12
|
+
import { canUseEvmProvider, initEvmProvider } from '../evm/index.ts'
|
|
13
|
+
|
|
14
|
+
let chainStakeServiceSingleton: Promisable<EvmChainService> | undefined
|
|
15
|
+
|
|
16
|
+
export const canUseEvmContractChainService = (config: Pick<Config, 'chain' | 'evm'>) => {
|
|
17
|
+
const { id } = config.chain
|
|
18
|
+
return isDefined(id) && id !== ZERO_ADDRESS && canUseEvmProvider({ config })
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const initEvmContractChainService: Initializable<BaseAccountableServiceParams<Pick<Config, 'chain' | 'evm'>>, EvmChainService> = async ({
|
|
22
|
+
account, config, traceProvider, meterProvider, logger,
|
|
23
|
+
}) => {
|
|
24
|
+
if (chainStakeServiceSingleton) return chainStakeServiceSingleton
|
|
25
|
+
// Parse config
|
|
26
|
+
const emvStakingContractAddress = assertEx(config.chain.id, () => 'config.chain.id is required')
|
|
27
|
+
// Configure ContractRunner
|
|
28
|
+
const id: Address = assertEx(asAddress(emvStakingContractAddress), () => 'config.chain.id is not a valid address')
|
|
29
|
+
const provider = assertEx(await initEvmProvider({ config }))
|
|
30
|
+
const privateKey = assertEx(account.private?.hex, () => 'Account does not have a private key')
|
|
31
|
+
const runner: ContractRunner = new Wallet(privateKey, provider)
|
|
32
|
+
// Create service
|
|
33
|
+
chainStakeServiceSingleton = EvmChainService.create({
|
|
34
|
+
id, runner, traceProvider, meterProvider, logger,
|
|
35
|
+
})
|
|
36
|
+
const result = await chainStakeServiceSingleton
|
|
37
|
+
await result.start()
|
|
38
|
+
return result
|
|
39
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import {
|
|
2
|
+
assertEx, type CreatableName, type Promisable,
|
|
3
|
+
} from '@xylabs/sdk-js'
|
|
4
|
+
import type { Initializable } from '@xyo-network/xl1-protocol'
|
|
5
|
+
import type { ChainService } from '@xyo-network/xl1-protocol-sdk'
|
|
6
|
+
import type { Config } from 'cosmiconfig'
|
|
7
|
+
|
|
8
|
+
import { MemoryChainService } from '../../ChainService/index.ts'
|
|
9
|
+
import type { BaseAccountableServiceParams } from '../../model/index.ts'
|
|
10
|
+
import { canUseEvmContractChainService, initEvmContractChainService } from './evm.ts'
|
|
11
|
+
|
|
12
|
+
let chainStakeServiceSingleton: Promisable<ChainService> | undefined
|
|
13
|
+
|
|
14
|
+
export const initChainService: Initializable<BaseAccountableServiceParams<
|
|
15
|
+
Pick<Config, 'chain' | 'evm' | 'producer'>>,
|
|
16
|
+
ChainService> = ({
|
|
17
|
+
account, config, logger,
|
|
18
|
+
}) => {
|
|
19
|
+
logger?.log('ChainService: Initializing...')
|
|
20
|
+
const result = init({
|
|
21
|
+
config, name: 'ChainService' as CreatableName, account,
|
|
22
|
+
})
|
|
23
|
+
logger?.log('ChainService: Initialized')
|
|
24
|
+
return result
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export const initMemoryChainService: Initializable<BaseAccountableServiceParams<
|
|
28
|
+
Pick<Config, 'producer'>>, ChainService> = async ({ config }) => {
|
|
29
|
+
const result = await MemoryChainService.create({
|
|
30
|
+
config,
|
|
31
|
+
name: 'MemoryChainService' as CreatableName,
|
|
32
|
+
})
|
|
33
|
+
assertEx(await result.start(), () => 'Failed to start MemoryChainService')
|
|
34
|
+
return result
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const init: Initializable<BaseAccountableServiceParams<Pick<Config, 'chain' | 'evm' | 'producer'>>,
|
|
38
|
+
ChainService> = async (params): Promise<ChainService> => {
|
|
39
|
+
if (chainStakeServiceSingleton) return chainStakeServiceSingleton
|
|
40
|
+
const { config } = params
|
|
41
|
+
chainStakeServiceSingleton = canUseEvmContractChainService(config)
|
|
42
|
+
? await initEvmContractChainService({ ...params, name: 'ChainStakeService' as CreatableName })
|
|
43
|
+
: await initMemoryChainService(params)
|
|
44
|
+
// Create service
|
|
45
|
+
return chainStakeServiceSingleton
|
|
46
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './initEvmProvider.ts'
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import {
|
|
2
|
+
assertEx, hexFrom, isDefined, isHex,
|
|
3
|
+
} from '@xylabs/sdk-js'
|
|
4
|
+
import type { Config } from '@xyo-network/xl1-protocol-sdk'
|
|
5
|
+
|
|
6
|
+
export const canUseChainId = (config: Pick<Config, 'evm'>): boolean => {
|
|
7
|
+
return isDefined(config.evm.chainId)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const getChainId = (config: Pick<Config, 'evm'>) => {
|
|
11
|
+
const chainId = assertEx(config.evm.chainId, () => 'Missing config.evm.chainId')
|
|
12
|
+
if (isHex(chainId, { prefix: true })) {
|
|
13
|
+
const hex = hexFrom(chainId)
|
|
14
|
+
const parsed = Number.parseInt(hex, 16)
|
|
15
|
+
return parsed
|
|
16
|
+
} else {
|
|
17
|
+
const parsed = Number.parseInt(chainId, 10)
|
|
18
|
+
return parsed
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Logger } from '@xylabs/sdk-js'
|
|
2
|
+
import { assertEx } from '@xylabs/sdk-js'
|
|
3
|
+
import type { Config } from '@xyo-network/xl1-protocol-sdk'
|
|
4
|
+
import type { Provider } from 'ethers'
|
|
5
|
+
import type { JsonRpcProvider } from 'ethers/providers'
|
|
6
|
+
|
|
7
|
+
import { canUseInfuraProvider, initInfuraProvider } from './initInfuraProvider.ts'
|
|
8
|
+
import { canUseJsonRpcProvider, initJsonRpcProvider } from './initJsonRpcProvider.ts'
|
|
9
|
+
|
|
10
|
+
let provider: Promise<JsonRpcProvider> | undefined
|
|
11
|
+
|
|
12
|
+
export const initEvmProvider = async ({ config }: { config: Pick<Config, 'evm'>; logger?: Logger }): Promise<Provider> => {
|
|
13
|
+
if (provider) return provider
|
|
14
|
+
if (canUseInfuraProvider(config)) {
|
|
15
|
+
provider = initInfuraProvider(config)
|
|
16
|
+
} else if (canUseJsonRpcProvider(config)) {
|
|
17
|
+
provider = initJsonRpcProvider(config)
|
|
18
|
+
}
|
|
19
|
+
return assertEx(await provider, () => 'No provider available')
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const canUseEvmProvider = ({ config }: { config: Pick<Config, 'evm'> }) => {
|
|
23
|
+
return canUseInfuraProvider(config) || canUseJsonRpcProvider(config)
|
|
24
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { assertEx, isDefined } from '@xylabs/sdk-js'
|
|
2
|
+
import type { Config } from '@xyo-network/xl1-protocol-sdk'
|
|
3
|
+
import { InfuraProvider } from 'ethers/providers'
|
|
4
|
+
|
|
5
|
+
import { canUseChainId, getChainId } from './initChainId.ts'
|
|
6
|
+
|
|
7
|
+
let instance: Promise<InfuraProvider> | undefined
|
|
8
|
+
|
|
9
|
+
export const initInfuraProvider = (config: Pick<Config, 'evm'>) => {
|
|
10
|
+
if (instance) return instance
|
|
11
|
+
const providerConfig = getInfuraProviderConfig(config)
|
|
12
|
+
instance = Promise.resolve(new InfuraProvider(...providerConfig))
|
|
13
|
+
return instance
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const canUseInfuraProvider = (config: Pick<Config, 'evm'>): boolean => {
|
|
17
|
+
return canUseChainId(config)
|
|
18
|
+
&& isDefined(config.evm?.infura?.projectId)
|
|
19
|
+
&& isDefined(config.evm?.infura?.projectSecret)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const getInfuraProviderConfig = (config: Pick<Config, 'evm'>) => {
|
|
23
|
+
const projectId = assertEx(config.evm?.infura?.projectId, () => 'Missing config.evm.infura.projectId')
|
|
24
|
+
const projectSecret = assertEx(config.evm?.infura?.projectSecret, () => 'Missing config.evm.infura.projectSecret')
|
|
25
|
+
return [getChainId(config), projectId, projectSecret] as const
|
|
26
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { assertEx, isDefined } from '@xylabs/sdk-js'
|
|
2
|
+
import type { Config } from '@xyo-network/xl1-protocol-sdk'
|
|
3
|
+
import { JsonRpcProvider } from 'ethers/providers'
|
|
4
|
+
|
|
5
|
+
import { canUseChainId, getChainId } from './initChainId.ts'
|
|
6
|
+
|
|
7
|
+
export const initJsonRpcProvider = (config: Pick<Config, 'evm'>) => {
|
|
8
|
+
const providerConfig = getJsonRpcProviderConfig(config)
|
|
9
|
+
return Promise.resolve(new JsonRpcProvider(...providerConfig))
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const canUseJsonRpcProvider = (config: Pick<Config, 'evm'>) => {
|
|
13
|
+
return canUseChainId(config)
|
|
14
|
+
&& isDefined(config.evm.jsonRpc?.url)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const getJsonRpcProviderConfig = (config: Pick<Config, 'evm'>) => {
|
|
18
|
+
const jsonRpcUrl = assertEx(config.evm.jsonRpc?.url, () => 'Missing config.evm.jsonRpc.url')
|
|
19
|
+
return [jsonRpcUrl, getChainId(config)] as const
|
|
20
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import {
|
|
2
|
+
buildNextBlock, createDeclarationIntent, createGenesisBlock,
|
|
3
|
+
} from '@xyo-network/chain-protocol'
|
|
4
|
+
import type { WalletInstance } from '@xyo-network/wallet-model'
|
|
5
|
+
import type { SignedHydratedBlockWithHashMeta } from '@xyo-network/xl1-protocol'
|
|
6
|
+
import type { ChainService } from '@xyo-network/xl1-protocol-sdk'
|
|
7
|
+
|
|
8
|
+
export const createBootstrapHead = async (account: WalletInstance, chainService: ChainService): Promise<SignedHydratedBlockWithHashMeta[]> => {
|
|
9
|
+
const chainId = await chainService.chainId()
|
|
10
|
+
const chain: SignedHydratedBlockWithHashMeta[] = []
|
|
11
|
+
|
|
12
|
+
// Create genesis block
|
|
13
|
+
const genesisBlock = await createGenesisBlock(account, chainId, 10_000_000n, account.address)
|
|
14
|
+
chain.push(genesisBlock)
|
|
15
|
+
|
|
16
|
+
// Create producer declaration block
|
|
17
|
+
const producerDeclarationPayload = createDeclarationIntent(
|
|
18
|
+
account.address,
|
|
19
|
+
'producer',
|
|
20
|
+
genesisBlock[0].block,
|
|
21
|
+
genesisBlock[0].block + 10_000,
|
|
22
|
+
)
|
|
23
|
+
const producerDeclarationBlock = await buildNextBlock(
|
|
24
|
+
genesisBlock[0],
|
|
25
|
+
[],
|
|
26
|
+
[producerDeclarationPayload],
|
|
27
|
+
[account],
|
|
28
|
+
)
|
|
29
|
+
chain.push(producerDeclarationBlock)
|
|
30
|
+
return chain
|
|
31
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { toHex } from '@xylabs/sdk-js'
|
|
2
|
+
import type { BridgeDetailsDestinationFields } from '@xyo-network/xl1-protocol'
|
|
3
|
+
|
|
4
|
+
import { getForkBlockRewardHex } from './getForkDetails.ts'
|
|
5
|
+
|
|
6
|
+
// const bridgeableTokenContract = toHex('0x4865Cb10d55cfB0E60DD2B4F9b888f6a49B76733')
|
|
7
|
+
// const bridgeDestAddress = toHex('0xe53218d47913b5f9E58bb74F0a0eD790bbF21972')
|
|
8
|
+
// const destConfirmation = toHex('0x4fa05d7e799f36f1d45c441a4866eb4570a00a753a39377f404a8d113c01a657') // Eth TX for mint
|
|
9
|
+
const ethChainId = toHex('0x1')
|
|
10
|
+
const bridgeableTokenContract = toHex('0xf72aE3E0DA743033AbD7A407557D684c1aE66aed')
|
|
11
|
+
const bridgeDestAddress = toHex('0x0e65b65B10C791942665030402c35023d88B14dA')
|
|
12
|
+
const destConfirmation = toHex('0x950861b10523b52cdbb4a9ee52ed26601db555d2652bfec21c709e5e70d5b7d3') // Eth TX for mint
|
|
13
|
+
|
|
14
|
+
export const getBridgeDestChainId = () => ethChainId
|
|
15
|
+
export const getBridgeDestToken = () => bridgeableTokenContract
|
|
16
|
+
export const getBridgeDestAddress = () => bridgeDestAddress
|
|
17
|
+
export const getBridgeDestAmount = () => getForkBlockRewardHex()
|
|
18
|
+
export const getBridgeDestConfirmation = () => destConfirmation
|
|
19
|
+
|
|
20
|
+
export const getBridgeDestinationDetails = (): BridgeDetailsDestinationFields => {
|
|
21
|
+
return {
|
|
22
|
+
dest: getBridgeDestChainId(),
|
|
23
|
+
destToken: getBridgeDestToken(),
|
|
24
|
+
destAddress: getBridgeDestAddress(),
|
|
25
|
+
destAmount: getBridgeDestAmount(),
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { AccountInstance } from '@xyo-network/account-model'
|
|
2
|
+
import type { BridgeDetailsSourceFields } from '@xyo-network/xl1-protocol'
|
|
3
|
+
import type { ChainService } from '@xyo-network/xl1-protocol-sdk'
|
|
4
|
+
|
|
5
|
+
import { getForkBlockRewardHex } from './getForkDetails.ts'
|
|
6
|
+
|
|
7
|
+
export const getBridgeSrcChainId = (chainService: ChainService) => chainService.chainId()
|
|
8
|
+
export const getBridgeSrcAddress = (account: AccountInstance) => account.address
|
|
9
|
+
export const getBridgeSrcToken = (chainService: ChainService) => chainService.chainId()
|
|
10
|
+
export const getBridgeSrcAmount = () => getForkBlockRewardHex()
|
|
11
|
+
|
|
12
|
+
export const getBridgeSourceDetails = async (account: AccountInstance, chainService: ChainService): Promise<BridgeDetailsSourceFields> => {
|
|
13
|
+
return {
|
|
14
|
+
src: await getBridgeSrcChainId(chainService),
|
|
15
|
+
srcAddress: getBridgeSrcAddress(account),
|
|
16
|
+
srcToken: await getBridgeSrcToken(chainService),
|
|
17
|
+
srcAmount: getBridgeSrcAmount(),
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type Hex, toHex } from '@xylabs/sdk-js'
|
|
2
|
+
import { AttoXL1ConvertFactor } from '@xyo-network/xl1-protocol'
|
|
3
|
+
|
|
4
|
+
export const getForkBlockReward = (): bigint => {
|
|
5
|
+
return 18_000_000_000n * AttoXL1ConvertFactor.xl1 // 18b XL1 in AttoXL1
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export const getForkBlockRewardHex = (): Hex => {
|
|
9
|
+
return toHex(getForkBlockReward())
|
|
10
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { WalletInstance } from '@xyo-network/wallet-model'
|
|
2
|
+
import type { SignedBlockBoundWitnessWithHashMeta, SignedHydratedBlockWithHashMeta } from '@xyo-network/xl1-protocol'
|
|
3
|
+
import type { ChainService } from '@xyo-network/xl1-protocol-sdk'
|
|
4
|
+
|
|
5
|
+
import { getBridgeDestinationObservation } from './getBridgeDestinationObservation.ts'
|
|
6
|
+
import { getBridgeIntent } from './getBridgeIntent.ts'
|
|
7
|
+
import { getBridgeSourceObservation } from './getBridgeSourceObservation.ts'
|
|
8
|
+
import { getFirstBlockForNewChain } from './getFirstBlockForNewChain.ts'
|
|
9
|
+
|
|
10
|
+
export const createForkedHead = async (
|
|
11
|
+
forkFromBlock: SignedBlockBoundWitnessWithHashMeta,
|
|
12
|
+
account: WalletInstance,
|
|
13
|
+
chainService: ChainService,
|
|
14
|
+
): Promise<SignedHydratedBlockWithHashMeta[]> => {
|
|
15
|
+
const chain: SignedHydratedBlockWithHashMeta[] = []
|
|
16
|
+
// Build the first block for the new chain
|
|
17
|
+
const firstBlockForNewChain = await getFirstBlockForNewChain(forkFromBlock, account, chainService)
|
|
18
|
+
chain.push(firstBlockForNewChain)
|
|
19
|
+
// Build the bridge intent
|
|
20
|
+
const bridgeIntent = await getBridgeIntent(firstBlockForNewChain, account, chainService)
|
|
21
|
+
chain.push(bridgeIntent)
|
|
22
|
+
// Build the bridge source observation
|
|
23
|
+
const bridgeSourceObservation = await getBridgeSourceObservation(bridgeIntent, account, chainService)
|
|
24
|
+
chain.push(bridgeSourceObservation)
|
|
25
|
+
// Build the bridge destination observation
|
|
26
|
+
const bridgeDestinationObservation = await getBridgeDestinationObservation(bridgeSourceObservation, account, chainService)
|
|
27
|
+
chain.push(bridgeDestinationObservation)
|
|
28
|
+
return chain
|
|
29
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { buildNextBlock } from '@xyo-network/chain-protocol'
|
|
2
|
+
import { PayloadBuilder } from '@xyo-network/payload-builder'
|
|
3
|
+
import type { WalletInstance } from '@xyo-network/wallet-model'
|
|
4
|
+
import type {
|
|
5
|
+
BridgeDestinationObservation, BridgeDestinationObservationFields,
|
|
6
|
+
HydratedBlockWithHashMeta,
|
|
7
|
+
SignedHydratedBlockWithHashMeta,
|
|
8
|
+
} from '@xyo-network/xl1-protocol'
|
|
9
|
+
import { BridgeDestinationObservationSchema } from '@xyo-network/xl1-protocol'
|
|
10
|
+
import type { ChainService } from '@xyo-network/xl1-protocol-sdk'
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
getBridgeDestConfirmation, getBridgeDestinationDetails, getBridgeSourceDetails,
|
|
14
|
+
} from './config/index.ts'
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Get the bridge destination observation for a given block.
|
|
18
|
+
* @param previousBlock The previous block to base the observation on.
|
|
19
|
+
* @param account The wallet account to use for the observation.
|
|
20
|
+
* @param chainService The chain service to use for the observation.
|
|
21
|
+
*/
|
|
22
|
+
export const getBridgeDestinationObservation = async (
|
|
23
|
+
previousBlock: HydratedBlockWithHashMeta,
|
|
24
|
+
account: WalletInstance,
|
|
25
|
+
chainService: ChainService,
|
|
26
|
+
): Promise<SignedHydratedBlockWithHashMeta> => {
|
|
27
|
+
const bridgeDestinationObservationFields: BridgeDestinationObservationFields = {
|
|
28
|
+
...await getBridgeSourceDetails(account, chainService),
|
|
29
|
+
...getBridgeDestinationDetails(),
|
|
30
|
+
destConfirmation: getBridgeDestConfirmation(),
|
|
31
|
+
}
|
|
32
|
+
const bridgeDestinationObservation = new PayloadBuilder<BridgeDestinationObservation>({ schema: BridgeDestinationObservationSchema })
|
|
33
|
+
.fields(bridgeDestinationObservationFields)
|
|
34
|
+
.build()
|
|
35
|
+
|
|
36
|
+
return await buildNextBlock(
|
|
37
|
+
previousBlock[0],
|
|
38
|
+
[],
|
|
39
|
+
[bridgeDestinationObservation],
|
|
40
|
+
[account],
|
|
41
|
+
)
|
|
42
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { buildNextBlock, createTransferPayload } from '@xyo-network/chain-protocol'
|
|
2
|
+
import { PayloadBuilder } from '@xyo-network/payload-builder'
|
|
3
|
+
import type { WalletInstance } from '@xyo-network/wallet-model'
|
|
4
|
+
import type {
|
|
5
|
+
BridgeIntent, BridgeIntentFields,
|
|
6
|
+
SignedHydratedBlockWithHashMeta,
|
|
7
|
+
} from '@xyo-network/xl1-protocol'
|
|
8
|
+
import { BridgeIntentSchema, XYO_BRIDGE_ADDRESS } from '@xyo-network/xl1-protocol'
|
|
9
|
+
import type { ChainService } from '@xyo-network/xl1-protocol-sdk'
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
getBridgeDestinationDetails, getBridgeSourceDetails, getForkBlockReward,
|
|
13
|
+
} from './config/index.ts'
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Get the bridge intent for a given block.
|
|
17
|
+
* @param previousBlock The previous block to base the intent on.
|
|
18
|
+
* @param account The wallet account to use for the intent.
|
|
19
|
+
* @param chainService The chain service to use for the intent.
|
|
20
|
+
*/
|
|
21
|
+
export const getBridgeIntent = async (
|
|
22
|
+
previousBlock: SignedHydratedBlockWithHashMeta,
|
|
23
|
+
account: WalletInstance,
|
|
24
|
+
chainService: ChainService,
|
|
25
|
+
): Promise<SignedHydratedBlockWithHashMeta> => {
|
|
26
|
+
// Create transfer to bridge address
|
|
27
|
+
const transferPayload = createTransferPayload(
|
|
28
|
+
account.address,
|
|
29
|
+
{ [XYO_BRIDGE_ADDRESS]: getForkBlockReward() },
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
// Use timestamp as nonce
|
|
33
|
+
const nonce = `${Date.now()}`
|
|
34
|
+
|
|
35
|
+
// Create Bridge Intent
|
|
36
|
+
const bridgeIntentFields: BridgeIntentFields = {
|
|
37
|
+
...await getBridgeSourceDetails(account, chainService),
|
|
38
|
+
...getBridgeDestinationDetails(),
|
|
39
|
+
nonce,
|
|
40
|
+
}
|
|
41
|
+
const bridgeIntent = new PayloadBuilder<BridgeIntent>({ schema: BridgeIntentSchema })
|
|
42
|
+
.fields(bridgeIntentFields)
|
|
43
|
+
.build()
|
|
44
|
+
|
|
45
|
+
return await buildNextBlock(
|
|
46
|
+
previousBlock[0],
|
|
47
|
+
[],
|
|
48
|
+
[bridgeIntent, transferPayload],
|
|
49
|
+
[account],
|
|
50
|
+
)
|
|
51
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { buildNextBlock } from '@xyo-network/chain-protocol'
|
|
2
|
+
import { PayloadBuilder } from '@xyo-network/payload-builder'
|
|
3
|
+
import type { WalletInstance } from '@xyo-network/wallet-model'
|
|
4
|
+
import type {
|
|
5
|
+
BridgeSourceObservation, BridgeSourceObservationFields,
|
|
6
|
+
SignedHydratedBlockWithHashMeta,
|
|
7
|
+
} from '@xyo-network/xl1-protocol'
|
|
8
|
+
import { BridgeSourceObservationSchema } from '@xyo-network/xl1-protocol'
|
|
9
|
+
import type { ChainService } from '@xyo-network/xl1-protocol-sdk'
|
|
10
|
+
|
|
11
|
+
import { getBridgeDestinationDetails, getBridgeSourceDetails } from './config/index.ts'
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Get the bridge source observation for a given block.
|
|
15
|
+
* @param previousBlock The previous block to base the observation on.
|
|
16
|
+
* @param account The wallet account to use for the observation.
|
|
17
|
+
* @param chainService The chain service to use for the observation.
|
|
18
|
+
*/
|
|
19
|
+
export const getBridgeSourceObservation = async (
|
|
20
|
+
previousBlock: SignedHydratedBlockWithHashMeta,
|
|
21
|
+
account: WalletInstance,
|
|
22
|
+
chainService: ChainService,
|
|
23
|
+
): Promise<SignedHydratedBlockWithHashMeta> => {
|
|
24
|
+
// TODO: Find actual transfer TX from previous block/source intent?
|
|
25
|
+
// This hash should contain both the source intent and the transfer to the bridge address
|
|
26
|
+
// As the block is itself both a block and a transaction, we can use its hash for now
|
|
27
|
+
const srcTxHash = previousBlock[0]._hash
|
|
28
|
+
|
|
29
|
+
const bridgeSourceObservationFields: BridgeSourceObservationFields = {
|
|
30
|
+
...await getBridgeSourceDetails(account, chainService),
|
|
31
|
+
...getBridgeDestinationDetails(),
|
|
32
|
+
// Observation
|
|
33
|
+
srcConfirmation: srcTxHash,
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const bridgeSourceObservation = new PayloadBuilder<BridgeSourceObservation>({ schema: BridgeSourceObservationSchema })
|
|
37
|
+
.fields(bridgeSourceObservationFields)
|
|
38
|
+
.build()
|
|
39
|
+
|
|
40
|
+
return await buildNextBlock(
|
|
41
|
+
previousBlock[0],
|
|
42
|
+
[],
|
|
43
|
+
[bridgeSourceObservation],
|
|
44
|
+
[account],
|
|
45
|
+
)
|
|
46
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { buildBlock, type BuildNextBlockOptions } from '@xyo-network/chain-protocol'
|
|
2
|
+
import type { WithHashMeta } from '@xyo-network/payload-model'
|
|
3
|
+
import type { WalletInstance } from '@xyo-network/wallet-model'
|
|
4
|
+
import type {
|
|
5
|
+
BlockBoundWitness,
|
|
6
|
+
SignedHydratedBlockWithHashMeta,
|
|
7
|
+
} from '@xyo-network/xl1-protocol'
|
|
8
|
+
import { AttoXL1, XYO_STEP_REWARD_ADDRESS } from '@xyo-network/xl1-protocol'
|
|
9
|
+
import type { ChainService } from '@xyo-network/xl1-protocol-sdk'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Get the first block for the new forked chain.
|
|
13
|
+
* @param forkBlock The block to fork from
|
|
14
|
+
* @param account The wallet account to sign the block
|
|
15
|
+
* @param chainService The chain service instance
|
|
16
|
+
* @returns The first block for the new forked chain
|
|
17
|
+
*/
|
|
18
|
+
export const getFirstBlockForNewChain = async (
|
|
19
|
+
forkBlock: WithHashMeta<BlockBoundWitness>,
|
|
20
|
+
account: WalletInstance,
|
|
21
|
+
chainService: ChainService,
|
|
22
|
+
): Promise<SignedHydratedBlockWithHashMeta> => {
|
|
23
|
+
const {
|
|
24
|
+
_hash: previousBlockHash, block: previousBlockNumber, step_hashes: previousStepHashes = [], protocol,
|
|
25
|
+
} = forkBlock
|
|
26
|
+
const chainId = await chainService.chainId()
|
|
27
|
+
const options: BuildNextBlockOptions = {
|
|
28
|
+
blockPayloads: [],
|
|
29
|
+
chainId,
|
|
30
|
+
previousBlockHash,
|
|
31
|
+
previousBlockNumber,
|
|
32
|
+
previousStepHashes,
|
|
33
|
+
signers: [account],
|
|
34
|
+
txs: [],
|
|
35
|
+
protocol,
|
|
36
|
+
chainStepRewardAddress: XYO_STEP_REWARD_ADDRESS,
|
|
37
|
+
stepRewardPoolBalance: AttoXL1(0n),
|
|
38
|
+
}
|
|
39
|
+
// Can not use buildNextBlock because we need to control the chain id change here
|
|
40
|
+
return await buildBlock(options)
|
|
41
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './createForkedHead.ts'
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { Address, Hash } from '@xylabs/sdk-js'
|
|
2
|
+
import {
|
|
3
|
+
asHash, hexFromBigInt, isDefined,
|
|
4
|
+
} from '@xylabs/sdk-js'
|
|
5
|
+
import type { ArchivistInstance } from '@xyo-network/archivist-model'
|
|
6
|
+
import type { SignedBlockBoundWitnessWithHashMeta } from '@xyo-network/xl1-protocol'
|
|
7
|
+
import { isSignedBlockBoundWitnessWithHashMeta } from '@xyo-network/xl1-protocol'
|
|
8
|
+
import type { ChainService } from '@xyo-network/xl1-protocol-sdk'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Determine if the chain should fork based on the current head and chain configuration.
|
|
12
|
+
* @param head The current head block
|
|
13
|
+
* @param chainService The chain service instance
|
|
14
|
+
* @param chainArchivist The chain archivist instance
|
|
15
|
+
* @returns The block to fork from if the chain should fork, undefined otherwise
|
|
16
|
+
*/
|
|
17
|
+
export const getForkFromBlock = async (
|
|
18
|
+
head: SignedBlockBoundWitnessWithHashMeta,
|
|
19
|
+
chainService: ChainService,
|
|
20
|
+
chainArchivist: ArchivistInstance,
|
|
21
|
+
): Promise<SignedBlockBoundWitnessWithHashMeta | undefined> => {
|
|
22
|
+
// If the head's chain doesn't match our chainId, we may need to fork
|
|
23
|
+
if (head.chain !== await chainService.chainId()) {
|
|
24
|
+
// Get the forked at hash from the chain service
|
|
25
|
+
const forkedAtBigInt = await chainService.forkedAtHash()
|
|
26
|
+
const forkedAtHex = hexFromBigInt(forkedAtBigInt) // Validate it's a proper hex string
|
|
27
|
+
const forkedAtHash: Hash | undefined = asHash(forkedAtHex)
|
|
28
|
+
if (isDefined(forkedAtHash)) {
|
|
29
|
+
// If we have a forkedAtHash, we need to check if the correct block exists
|
|
30
|
+
const [forkedAtBlock] = await chainArchivist.get([forkedAtHash])
|
|
31
|
+
const forkedChainId: Address = await chainService.forkedChainId()
|
|
32
|
+
const forkedAtBlockNumber: number = Number(await chainService.forkedAtBlockNumber())
|
|
33
|
+
// If we found the block hash we should fork at, check if it's a valid BlockBoundWitness
|
|
34
|
+
if (isSignedBlockBoundWitnessWithHashMeta(forkedAtBlock)
|
|
35
|
+
// And it is on the correct forked from chain
|
|
36
|
+
&& forkedAtBlock.chain === forkedChainId
|
|
37
|
+
// And it is the correct forked at block number
|
|
38
|
+
&& forkedAtBlock.block === forkedAtBlockNumber) {
|
|
39
|
+
return forkedAtBlock
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|