@xyo-network/chain-bridge 1.19.14 → 1.19.16
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/node/BridgeActor.d.ts +3 -2
- package/dist/node/BridgeActor.d.ts.map +1 -1
- package/dist/node/config/getBridgeEscrowAddress.d.ts +3 -3
- package/dist/node/config/getBridgeEscrowAddress.d.ts.map +1 -1
- package/dist/node/config/getBridgeFeesAddress.d.ts +3 -3
- package/dist/node/config/getBridgeFeesAddress.d.ts.map +1 -1
- package/dist/node/config/getBridgeSettings.d.ts +2 -2
- package/dist/node/config/getBridgeSettings.d.ts.map +1 -1
- package/dist/node/config/getBridgeWalletAccount.d.ts +3 -3
- package/dist/node/config/getBridgeWalletAccount.d.ts.map +1 -1
- package/dist/node/config/getFeeStructure.d.ts +2 -2
- package/dist/node/config/getFeeStructure.d.ts.map +1 -1
- package/dist/node/config/getGateway.d.ts +3 -2
- package/dist/node/config/getGateway.d.ts.map +1 -1
- package/dist/node/config/getMaxBridgeAmount.d.ts +2 -2
- package/dist/node/config/getMaxBridgeAmount.d.ts.map +1 -1
- package/dist/node/config/getMinBridgeAmount.d.ts +2 -2
- package/dist/node/config/getMinBridgeAmount.d.ts.map +1 -1
- package/dist/node/config/getRemoteChainId.d.ts +3 -2
- package/dist/node/config/getRemoteChainId.d.ts.map +1 -1
- package/dist/node/config/getRemoteTokenAddress.d.ts +2 -2
- package/dist/node/config/getRemoteTokenAddress.d.ts.map +1 -1
- package/dist/node/config/getTransferAddresses.d.ts +2 -2
- package/dist/node/config/getTransferAddresses.d.ts.map +1 -1
- package/dist/node/config/getXl1ChainId.d.ts +3 -2
- package/dist/node/config/getXl1ChainId.d.ts.map +1 -1
- package/dist/node/config/getXl1TokenAddress.d.ts +2 -2
- package/dist/node/config/getXl1TokenAddress.d.ts.map +1 -1
- package/dist/node/index.mjs +178 -624
- package/dist/node/index.mjs.map +1 -1
- package/dist/node/interface/service/Relay/LiquidityPoolBridgeRelay/LiquidityPoolBridgeRelay.d.ts +1 -1
- package/dist/node/interface/service/Relay/LiquidityPoolBridgeRelay/LiquidityPoolBridgeRelay.d.ts.map +1 -1
- package/dist/node/modules/EVMLiquidityBridgeTransactionCompletionMonitorSentinel/EVMLiquidityBridgeTransactionCompletionMonitorSentinel.d.ts +1 -2
- package/dist/node/modules/EVMLiquidityBridgeTransactionCompletionMonitorSentinel/EVMLiquidityBridgeTransactionCompletionMonitorSentinel.d.ts.map +1 -1
- package/dist/node/modules/XL1TransactionCompletionMonitorSentinel/XL1TransactionCompletionMonitorSentinel.d.ts +1 -2
- package/dist/node/modules/XL1TransactionCompletionMonitorSentinel/XL1TransactionCompletionMonitorSentinel.d.ts.map +1 -1
- package/dist/node/queue/connection.d.ts +2 -2
- package/dist/node/queue/connection.d.ts.map +1 -1
- package/dist/node/queue/workers/EthTransactionPreparation.d.ts.map +1 -1
- package/dist/node/queue/workers/util/index.d.ts +2 -0
- package/dist/node/queue/workers/util/index.d.ts.map +1 -1
- package/dist/node/queue/workers/util/validateSufficientAllowance.d.ts +19 -0
- package/dist/node/queue/workers/util/validateSufficientAllowance.d.ts.map +1 -0
- package/dist/node/queue/workers/util/validateSufficientBalance.d.ts +19 -0
- package/dist/node/queue/workers/util/validateSufficientBalance.d.ts.map +1 -0
- package/dist/node/server/addFlowProducer.d.ts +2 -2
- package/dist/node/server/addFlowProducer.d.ts.map +1 -1
- package/dist/node/server/addWorkers.d.ts +2 -2
- package/dist/node/server/addWorkers.d.ts.map +1 -1
- package/dist/node/server/app.d.ts +3 -3
- package/dist/node/server/app.d.ts.map +1 -1
- package/dist/node/server/index.d.ts +0 -2
- package/dist/node/server/index.d.ts.map +1 -1
- package/dist/node/server/routes/addRoutes.d.ts +3 -2
- package/dist/node/server/routes/addRoutes.d.ts.map +1 -1
- package/dist/node/server/routes/bridge/addBridgeRoutes.d.ts +3 -2
- package/dist/node/server/routes/bridge/addBridgeRoutes.d.ts.map +1 -1
- package/dist/node/server/routes/bridge/routeDefinitions/getRouteDefinitions.d.ts +3 -2
- package/dist/node/server/routes/bridge/routeDefinitions/getRouteDefinitions.d.ts.map +1 -1
- package/dist/node/server/routes/bridge/routeDefinitions/pathParams/ChainIdPathParam.d.ts +2 -2
- package/dist/node/server/routes/bridge/routeDefinitions/pathParams/ChainIdPathParam.d.ts.map +1 -1
- package/dist/node/server/routes/bridge/routeDefinitions/routes/bridgeConfig.d.ts +2 -2
- package/dist/node/server/routes/bridge/routeDefinitions/routes/bridgeConfig.d.ts.map +1 -1
- package/dist/node/server/routes/bridge/routeDefinitions/routes/bridgeFromRemoteStatus.d.ts +2 -2
- package/dist/node/server/routes/bridge/routeDefinitions/routes/bridgeFromRemoteStatus.d.ts.map +1 -1
- package/dist/node/server/routes/bridge/routeDefinitions/routes/bridgeToRemote.d.ts +3 -3
- package/dist/node/server/routes/bridge/routeDefinitions/routes/bridgeToRemote.d.ts.map +1 -1
- package/dist/node/server/routes/bridge/routeDefinitions/routes/bridgeToRemoteEstimate.d.ts +4 -3
- package/dist/node/server/routes/bridge/routeDefinitions/routes/bridgeToRemoteEstimate.d.ts.map +1 -1
- package/dist/node/server/routes/bridge/routeDefinitions/routes/bridgeToRemoteStatus.d.ts +2 -2
- package/dist/node/server/routes/bridge/routeDefinitions/routes/bridgeToRemoteStatus.d.ts.map +1 -1
- package/dist/node/server/server.d.ts +3 -10
- package/dist/node/server/server.d.ts.map +1 -1
- package/dist/node/{manifest → services}/getIterableMap.d.ts +2 -2
- package/dist/node/services/getIterableMap.d.ts.map +1 -0
- package/dist/node/services/getServices.d.ts +9 -0
- package/dist/node/services/getServices.d.ts.map +1 -0
- package/dist/node/services/index.d.ts +1 -0
- package/dist/node/services/index.d.ts.map +1 -1
- package/dist/node/util/BridgeFees.d.ts +7 -0
- package/dist/node/util/BridgeFees.d.ts.map +1 -0
- package/dist/node/util/bridgeFeesAsBigInt.d.ts +7 -0
- package/dist/node/util/bridgeFeesAsBigInt.d.ts.map +1 -0
- package/dist/node/util/calculateBridgeFees.d.ts +4 -8
- package/dist/node/util/calculateBridgeFees.d.ts.map +1 -1
- package/dist/node/util/createBridgeTransfer.d.ts +14 -0
- package/dist/node/util/createBridgeTransfer.d.ts.map +1 -0
- package/dist/node/util/generateBridgeEstimate.d.ts +3 -2
- package/dist/node/util/generateBridgeEstimate.d.ts.map +1 -1
- package/dist/node/util/index.d.ts +3 -0
- package/dist/node/util/index.d.ts.map +1 -1
- package/dist/node/util/validateBridgeEstimate.d.ts +3 -2
- package/dist/node/util/validateBridgeEstimate.d.ts.map +1 -1
- package/dist/node/util/validateBridgeEstimateExact.d.ts +3 -2
- package/dist/node/util/validateBridgeEstimateExact.d.ts.map +1 -1
- package/dist/node/util/validateBridgeTransaction.d.ts +3 -2
- package/dist/node/util/validateBridgeTransaction.d.ts.map +1 -1
- package/package.json +46 -59
- package/src/BridgeActor.ts +3 -3
- package/src/config/getBridgeEscrowAddress.ts +5 -5
- package/src/config/getBridgeFeesAddress.ts +5 -5
- package/src/config/getBridgeSettings.ts +2 -2
- package/src/config/getBridgeWalletAccount.ts +7 -4
- package/src/config/getFeeStructure.ts +3 -3
- package/src/config/getGateway.ts +5 -4
- package/src/config/getMaxBridgeAmount.ts +3 -3
- package/src/config/getMinBridgeAmount.ts +3 -3
- package/src/config/getRemoteChainId.ts +4 -3
- package/src/config/getRemoteTokenAddress.ts +3 -3
- package/src/config/getTransferAddresses.ts +2 -2
- package/src/config/getXl1ChainId.ts +4 -3
- package/src/config/getXl1TokenAddress.ts +3 -3
- package/src/interface/service/Observer/LiquidityPoolBridgeObserver/LiquidityPoolBridgeObserver.ts +1 -1
- package/src/interface/service/Relay/LiquidityPoolBridgeRelay/LiquidityPoolBridgeRelay.ts +1 -1
- package/src/interface/util/getBridgeIntentIdentifier.ts +1 -1
- package/src/modules/EVMLiquidityBridgeTransactionCompletionMonitorSentinel/EVMLiquidityBridgeTransactionCompletionMonitorSentinel.ts +2 -3
- package/src/modules/XL1TransactionCompletionMonitorSentinel/XL1TransactionCompletionMonitorSentinel.ts +2 -3
- package/src/queue/connection.ts +3 -3
- package/src/queue/workers/EthTransactionMonitor.ts +1 -1
- package/src/queue/workers/EthTransactionPreparation.ts +14 -4
- package/src/queue/workers/EthTransactionSubmission.ts +1 -1
- package/src/queue/workers/Xl1TransactionMonitor.ts +1 -1
- package/src/queue/workers/Xl1TransactionPreparation.ts +1 -1
- package/src/queue/workers/Xl1TransactionSubmission.ts +1 -1
- package/src/queue/workers/util/index.ts +2 -0
- package/src/queue/workers/util/submitEthTransaction.ts +1 -1
- package/src/queue/workers/util/validateSufficientAllowance.ts +38 -0
- package/src/queue/workers/util/validateSufficientBalance.ts +37 -0
- package/src/server/addFlowProducer.ts +2 -2
- package/src/server/addWorkers.ts +2 -2
- package/src/server/app.ts +3 -4
- package/src/server/index.ts +2 -2
- package/src/server/routes/addRoutes.ts +3 -2
- package/src/server/routes/bridge/addBridgeRoutes.ts +3 -2
- package/src/server/routes/bridge/routeDefinitions/getRouteDefinitions.ts +3 -2
- package/src/server/routes/bridge/routeDefinitions/pathParams/ChainIdPathParam.ts +3 -2
- package/src/server/routes/bridge/routeDefinitions/routes/bridgeConfig.ts +3 -2
- package/src/server/routes/bridge/routeDefinitions/routes/bridgeFromRemoteStatus.ts +4 -3
- package/src/server/routes/bridge/routeDefinitions/routes/bridgeToRemote.ts +6 -4
- package/src/server/routes/bridge/routeDefinitions/routes/bridgeToRemoteEstimate.ts +4 -3
- package/src/server/routes/bridge/routeDefinitions/routes/bridgeToRemoteStatus.ts +4 -5
- package/src/server/server.ts +7 -48
- package/src/{manifest → services}/getIterableMap.ts +2 -2
- package/src/{manifest → services}/getServices.ts +9 -13
- package/src/services/index.ts +1 -0
- package/src/util/BridgeFees.ts +7 -0
- package/src/util/bridgeFeesAsBigInt.ts +15 -0
- package/src/util/calculateBridgeFees.ts +5 -11
- package/src/util/createBridgeTransfer.ts +31 -0
- package/src/util/generateBridgeEstimate.ts +9 -17
- package/src/util/index.ts +3 -0
- package/src/util/validateBridgeEstimate.ts +4 -5
- package/src/util/validateBridgeEstimateExact.ts +4 -5
- package/src/util/validateBridgeTransaction.ts +4 -3
- package/dist/node/indexers/index.d.ts +0 -2
- package/dist/node/indexers/index.d.ts.map +0 -1
- package/dist/node/manifest/getIterableMap.d.ts.map +0 -1
- package/dist/node/manifest/getModuleLocator.d.ts +0 -15
- package/dist/node/manifest/getModuleLocator.d.ts.map +0 -1
- package/dist/node/manifest/getNode.d.ts +0 -15
- package/dist/node/manifest/getNode.d.ts.map +0 -1
- package/dist/node/manifest/getServices.d.ts +0 -13
- package/dist/node/manifest/getServices.d.ts.map +0 -1
- package/dist/node/manifest/index.d.ts +0 -7
- package/dist/node/manifest/index.d.ts.map +0 -1
- package/dist/node/manifest/nodeManifest.d.ts +0 -6
- package/dist/node/manifest/nodeManifest.d.ts.map +0 -1
- package/dist/node/manifest/private/index.d.ts +0 -5
- package/dist/node/manifest/private/index.d.ts.map +0 -1
- package/dist/node/manifest/public/index.d.ts +0 -14
- package/dist/node/manifest/public/index.d.ts.map +0 -1
- package/dist/node/server/routes/dataLake/addDataLakeRoutes.d.ts +0 -3
- package/dist/node/server/routes/dataLake/addDataLakeRoutes.d.ts.map +0 -1
- package/dist/node/server/routes/dataLake/archivistMiddleware.d.ts +0 -10
- package/dist/node/server/routes/dataLake/archivistMiddleware.d.ts.map +0 -1
- package/dist/node/server/routes/dataLake/index.d.ts +0 -2
- package/dist/node/server/routes/dataLake/index.d.ts.map +0 -1
- package/src/indexers/index.ts +0 -1
- package/src/manifest/getModuleLocator.ts +0 -153
- package/src/manifest/getNode.ts +0 -40
- package/src/manifest/index.ts +0 -6
- package/src/manifest/node.json +0 -17
- package/src/manifest/nodeManifest.ts +0 -8
- package/src/manifest/private/index.ts +0 -4
- package/src/manifest/public/Ethereum.json +0 -48
- package/src/manifest/public/XL1.json +0 -48
- package/src/manifest/public/index.ts +0 -23
- package/src/server/routes/dataLake/addDataLakeRoutes.ts +0 -9
- package/src/server/routes/dataLake/archivistMiddleware.ts +0 -85
- package/src/server/routes/dataLake/index.ts +0 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { assertEx, isDefined } from '@xylabs/sdk-js'
|
|
2
|
-
import { PayloadBuilder } from '@xyo-network/
|
|
2
|
+
import { PayloadBuilder } from '@xyo-network/sdk-js'
|
|
3
3
|
import { type SignedHydratedTransaction } from '@xyo-network/xl1-sdk'
|
|
4
4
|
import type { Job } from 'bullmq'
|
|
5
5
|
import { Worker } from 'bullmq'
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
assertEx, hexToBigInt, toEthAddress,
|
|
3
3
|
} from '@xylabs/sdk-js'
|
|
4
|
-
import { PayloadBuilder } from '@xyo-network/
|
|
4
|
+
import { PayloadBuilder } from '@xyo-network/sdk-js'
|
|
5
5
|
import type { BridgeableToken, LiquidityPoolBridge } from '@xyo-network/typechain'
|
|
6
6
|
import { isBridgeIntent, type SignedHydratedTransaction } from '@xyo-network/xl1-sdk'
|
|
7
7
|
import type { Wallet } from 'ethers'
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { Promisable } from '@xylabs/sdk-js'
|
|
2
|
+
import { assertEx, hexToBigInt } from '@xylabs/sdk-js'
|
|
3
|
+
import type { BridgeableToken, LiquidityPoolBridge } from '@xyo-network/typechain'
|
|
4
|
+
import { isBridgeIntent, type SignedHydratedTransaction } from '@xyo-network/xl1-sdk'
|
|
5
|
+
|
|
6
|
+
interface IAsyncLogger {
|
|
7
|
+
log: (message: string) => Promisable<unknown>
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Checks if the liquidity source has sufficient allowance for the bridge to execute the transaction. This doesn't
|
|
12
|
+
* ensure that (due to race conditions) the transaction will succeed, but is a quick check to avoid
|
|
13
|
+
* submitting transactions that are likely to fail.
|
|
14
|
+
* @param tx The transaction containing a bridge intent
|
|
15
|
+
* @param bridgeableToken The contract for the token being bridged
|
|
16
|
+
* @param bridge The contract for the liquidity bridge
|
|
17
|
+
* @param logger Optional logger for asynchronous logging
|
|
18
|
+
* @returns True if the liquidity source allowance is sufficient to execute the bridge
|
|
19
|
+
*/
|
|
20
|
+
export const validateSufficientAllowance = async (
|
|
21
|
+
tx: SignedHydratedTransaction,
|
|
22
|
+
bridgeableToken: BridgeableToken,
|
|
23
|
+
bridge: LiquidityPoolBridge,
|
|
24
|
+
logger?: IAsyncLogger,
|
|
25
|
+
) => {
|
|
26
|
+
// Get the amount being bridged from the bridge intent
|
|
27
|
+
const bridgeIntent = assertEx(tx[1].find(isBridgeIntent), () => 'No bridge intent found')
|
|
28
|
+
const amount = hexToBigInt(bridgeIntent.destAmount)
|
|
29
|
+
|
|
30
|
+
// Get the addresses
|
|
31
|
+
const liquiditySourceAddress = await bridge.liquiditySource()
|
|
32
|
+
const bridgeAddress = await bridge.getAddress()
|
|
33
|
+
|
|
34
|
+
// Check the allowance of the liquidity source for the bridge
|
|
35
|
+
const remainingAllowance = await bridgeableToken.allowance(liquiditySourceAddress, bridgeAddress)
|
|
36
|
+
await logger?.log(`Remaining allowance: ${remainingAllowance.toString()}`)
|
|
37
|
+
return remainingAllowance >= amount
|
|
38
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Promisable } from '@xylabs/sdk-js'
|
|
2
|
+
import { assertEx, hexToBigInt } from '@xylabs/sdk-js'
|
|
3
|
+
import type { BridgeableToken, LiquidityPoolBridge } from '@xyo-network/typechain'
|
|
4
|
+
import { isBridgeIntent, type SignedHydratedTransaction } from '@xyo-network/xl1-sdk'
|
|
5
|
+
|
|
6
|
+
interface IAsyncLogger {
|
|
7
|
+
log: (message: string) => Promisable<unknown>
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Checks if the liquidity source has sufficient balance for the bridge to execute the transaction. This doesn't
|
|
12
|
+
* ensure that (due to race conditions) the transaction will succeed, but is a quick check to avoid
|
|
13
|
+
* submitting transactions that are likely to fail.
|
|
14
|
+
* @param tx The transaction containing a bridge intent
|
|
15
|
+
* @param bridgeableToken The contract for the token being bridged
|
|
16
|
+
* @param bridge The contract for the liquidity bridge
|
|
17
|
+
* @param logger Optional logger for asynchronous logging
|
|
18
|
+
* @returns True if the liquidity source balance is sufficient to execute the bridge
|
|
19
|
+
*/
|
|
20
|
+
export const validateSufficientBalance = async (
|
|
21
|
+
tx: SignedHydratedTransaction,
|
|
22
|
+
bridgeableToken: BridgeableToken,
|
|
23
|
+
bridge: LiquidityPoolBridge,
|
|
24
|
+
logger?: IAsyncLogger,
|
|
25
|
+
) => {
|
|
26
|
+
// Get the amount being bridged from the bridge intent
|
|
27
|
+
const bridgeIntent = assertEx(tx[1].find(isBridgeIntent), () => 'No bridge intent found')
|
|
28
|
+
const amount = hexToBigInt(bridgeIntent.destAmount)
|
|
29
|
+
|
|
30
|
+
// Get the addresses
|
|
31
|
+
const liquiditySourceAddress = await bridge.liquiditySource()
|
|
32
|
+
|
|
33
|
+
// Check the balance of the liquidity source for the bridge
|
|
34
|
+
const balance = await bridgeableToken.balanceOf(liquiditySourceAddress)
|
|
35
|
+
await logger?.log(`Remaining balance: ${balance.toString()}`)
|
|
36
|
+
return balance >= amount
|
|
37
|
+
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { BridgeConfig } from '@xyo-network/chain-orchestration'
|
|
2
2
|
import type { Express } from 'express'
|
|
3
3
|
|
|
4
4
|
import { getConnection, getFlowProducer } from '../queue/index.ts'
|
|
5
5
|
|
|
6
|
-
export const addFlowProducer = (app: Express, config:
|
|
6
|
+
export const addFlowProducer = (app: Express, config: BridgeConfig): Express => {
|
|
7
7
|
const connection = getConnection(config)
|
|
8
8
|
const flowProducer = getFlowProducer(connection)
|
|
9
9
|
app.flowProducer = flowProducer
|
package/src/server/addWorkers.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { BridgeConfig } from '@xyo-network/chain-orchestration'
|
|
2
2
|
|
|
3
3
|
import { createWorkers, getConnection } from '../queue/index.ts'
|
|
4
4
|
import type { IBridgeServiceCollection } from '../services/index.ts'
|
|
5
5
|
|
|
6
|
-
export const addWorkers = (config:
|
|
6
|
+
export const addWorkers = (config: BridgeConfig, services: IBridgeServiceCollection) => {
|
|
7
7
|
const connection = getConnection(config)
|
|
8
8
|
createWorkers(connection, services)
|
|
9
9
|
}
|
package/src/server/app.ts
CHANGED
|
@@ -2,8 +2,8 @@ import {
|
|
|
2
2
|
customPoweredByHeader, disableCaseSensitiveRouting, disableExpressDefaultPoweredByHeader, getJsonBodyParser, getJsonBodyParserOptions, responseProfiler,
|
|
3
3
|
standardErrors, standardResponses,
|
|
4
4
|
} from '@xylabs/express'
|
|
5
|
-
import type {
|
|
6
|
-
import type {
|
|
5
|
+
import type { BridgeConfig } from '@xyo-network/chain-orchestration'
|
|
6
|
+
import type { XyoGatewayRunner } from '@xyo-network/xl1-sdk'
|
|
7
7
|
import compression from 'compression'
|
|
8
8
|
import cors from 'cors'
|
|
9
9
|
import type { Express } from 'express'
|
|
@@ -13,7 +13,7 @@ import { addFlowProducer } from './addFlowProducer.ts'
|
|
|
13
13
|
import { addInstrumentation } from './instrumentation.ts'
|
|
14
14
|
import { addRoutes } from './routes/index.ts'
|
|
15
15
|
|
|
16
|
-
export const getApp = (
|
|
16
|
+
export const getApp = (config: BridgeConfig, gateway: XyoGatewayRunner): Express => {
|
|
17
17
|
addInstrumentation()
|
|
18
18
|
const app = express()
|
|
19
19
|
app.set('etag', false)
|
|
@@ -25,7 +25,6 @@ export const getApp = (node: NodeInstance, config: Config, gateway: XyoGatewayRu
|
|
|
25
25
|
disableExpressDefaultPoweredByHeader(app)
|
|
26
26
|
app.use(customPoweredByHeader)
|
|
27
27
|
disableCaseSensitiveRouting(app)
|
|
28
|
-
app.node = node
|
|
29
28
|
addFlowProducer(app, config)
|
|
30
29
|
addRoutes(app, config, gateway)
|
|
31
30
|
app.use(standardErrors)
|
package/src/server/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export * from './app.ts'
|
|
2
2
|
export * from './server.ts'
|
|
3
3
|
|
|
4
|
-
import type { NodeInstance } from '@xyo-network/
|
|
4
|
+
// import type { NodeInstance } from '@xyo-network/sdk-js'
|
|
5
5
|
import type { FlowProducer } from 'bullmq'
|
|
6
6
|
|
|
7
7
|
// import type { IBridgeServiceCollection } from '../services/index.ts'
|
|
@@ -11,7 +11,7 @@ declare global {
|
|
|
11
11
|
namespace Express {
|
|
12
12
|
interface Application {
|
|
13
13
|
flowProducer: FlowProducer
|
|
14
|
-
node: NodeInstance
|
|
14
|
+
// node: NodeInstance
|
|
15
15
|
// services: IBridgeServiceCollection
|
|
16
16
|
}
|
|
17
17
|
}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { BridgeConfig } from '@xyo-network/chain-orchestration'
|
|
2
|
+
import type { XyoGatewayRunner } from '@xyo-network/xl1-sdk'
|
|
2
3
|
import type { Express } from 'express'
|
|
3
4
|
|
|
4
5
|
import { addBridgeRoutes } from './bridge/index.ts'
|
|
5
6
|
|
|
6
|
-
export const addRoutes = (app: Express, config:
|
|
7
|
+
export const addRoutes = (app: Express, config: BridgeConfig, gateway: XyoGatewayRunner) => {
|
|
7
8
|
addBridgeRoutes(app, config, gateway)
|
|
8
9
|
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { BridgeConfig } from '@xyo-network/chain-orchestration'
|
|
2
|
+
import type { XyoGatewayRunner } from '@xyo-network/xl1-sdk'
|
|
2
3
|
import type { Express } from 'express'
|
|
3
4
|
|
|
4
5
|
import { getRouteDefinitions } from './routeDefinitions/index.ts'
|
|
5
6
|
|
|
6
|
-
export const addBridgeRoutes = (app: Express, config:
|
|
7
|
+
export const addBridgeRoutes = (app: Express, config: BridgeConfig, gateway: XyoGatewayRunner) => {
|
|
7
8
|
const routeDefinitions = getRouteDefinitions(config, gateway)
|
|
8
9
|
for (const definition of routeDefinitions) {
|
|
9
10
|
app[definition.method](definition.path, definition.handlers)
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import type { RouteDefinition } from '@xylabs/express'
|
|
2
|
-
import type {
|
|
2
|
+
import type { BridgeConfig } from '@xyo-network/chain-orchestration'
|
|
3
|
+
import type { XyoGatewayRunner } from '@xyo-network/xl1-sdk'
|
|
3
4
|
|
|
4
5
|
import {
|
|
5
6
|
makeBridgeConfigRoute, makeBridgeFromRemoteStatusRoute, makeBridgeToRemoteEstimateRoute, makeBridgeToRemoteRoute, makeBridgeToRemoteStatusRoute,
|
|
6
7
|
} from './routes/index.ts'
|
|
7
8
|
|
|
8
|
-
export const getRouteDefinitions = (config:
|
|
9
|
+
export const getRouteDefinitions = (config: BridgeConfig, gateway: XyoGatewayRunner): RouteDefinition[] => {
|
|
9
10
|
return [
|
|
10
11
|
makeBridgeConfigRoute(config),
|
|
11
12
|
makeBridgeToRemoteEstimateRoute(config, gateway),
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
2
|
asHex, HexZod, isUndefined,
|
|
3
3
|
} from '@xylabs/sdk-js'
|
|
4
|
-
import type {
|
|
4
|
+
import type { BridgeConfig } from '@xyo-network/chain-orchestration'
|
|
5
|
+
import type { ChainId } from '@xyo-network/xl1-sdk'
|
|
5
6
|
|
|
6
7
|
import { getRemoteChainId } from '../../../../../config/index.ts'
|
|
7
8
|
|
|
8
|
-
export const getRemoteChainIdZod = (config:
|
|
9
|
+
export const getRemoteChainIdZod = (config: BridgeConfig) => {
|
|
9
10
|
const remoteChainId: ChainId = getRemoteChainId(config)
|
|
10
11
|
return HexZod.superRefine((val, ctx) => {
|
|
11
12
|
const chainId = asHex(val)
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { RouteDefinition } from '@xylabs/express'
|
|
2
2
|
import { requestHandlerValidator } from '@xylabs/express'
|
|
3
|
-
import {
|
|
3
|
+
import type { BridgeConfig } from '@xyo-network/chain-orchestration'
|
|
4
|
+
import { BridgeSettingsZod } from '@xyo-network/chain-orchestration'
|
|
4
5
|
import type { z } from 'zod'
|
|
5
6
|
|
|
6
7
|
import { getBridgeSettings } from '../../../../../config/index.ts'
|
|
@@ -10,7 +11,7 @@ export type BridgeConfigResponse = z.infer<typeof BridgeConfigResponseZod>
|
|
|
10
11
|
|
|
11
12
|
const validateRequest = requestHandlerValidator({ response: BridgeConfigResponseZod })
|
|
12
13
|
|
|
13
|
-
export const makeBridgeConfigRoute = (config:
|
|
14
|
+
export const makeBridgeConfigRoute = (config: BridgeConfig): RouteDefinition => {
|
|
14
15
|
return {
|
|
15
16
|
method: 'get',
|
|
16
17
|
path: '/bridge/chains/:chainId/config',
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import type { RouteDefinition } from '@xylabs/express'
|
|
2
2
|
import { requestHandlerValidator } from '@xylabs/express'
|
|
3
3
|
import { toAddress, toHex } from '@xylabs/sdk-js'
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
4
|
+
import type { BridgeConfig } from '@xyo-network/chain-orchestration'
|
|
5
|
+
import { PayloadZodStrictOfSchema } from '@xyo-network/sdk-js'
|
|
6
|
+
import type { BridgeDestinationObservation } from '@xyo-network/xl1-sdk'
|
|
6
7
|
import {
|
|
7
8
|
BridgeDestinationObservationFieldsZod,
|
|
8
9
|
BridgeDestinationObservationSchema,
|
|
@@ -12,7 +13,7 @@ import { z } from 'zod'
|
|
|
12
13
|
import { getBridgeSettings } from '../../../../../config/index.ts'
|
|
13
14
|
import { getRemoteChainIdZod } from '../pathParams/index.ts'
|
|
14
15
|
|
|
15
|
-
export const makeBridgeFromRemoteStatusRoute = (config:
|
|
16
|
+
export const makeBridgeFromRemoteStatusRoute = (config: BridgeConfig): RouteDefinition => {
|
|
16
17
|
const params = z.object({
|
|
17
18
|
chainId: getRemoteChainIdZod(config),
|
|
18
19
|
nonce: z.string().nonempty(),
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import type { RouteDefinition } from '@xylabs/express'
|
|
2
2
|
import { requestHandlerValidator } from '@xylabs/express'
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
3
|
+
import type { BridgeConfig } from '@xyo-network/chain-orchestration'
|
|
4
|
+
import {
|
|
5
|
+
PayloadBuilder, PayloadZodLooseOfSchema, PayloadZodStrictOfSchema,
|
|
6
|
+
} from '@xyo-network/sdk-js'
|
|
5
7
|
import type {
|
|
6
|
-
BridgeSourceObservation, BridgeSourceObservationFields,
|
|
8
|
+
BridgeSourceObservation, BridgeSourceObservationFields, SignedHydratedTransaction,
|
|
7
9
|
} from '@xyo-network/xl1-sdk'
|
|
8
10
|
import {
|
|
9
11
|
BridgeIntentFieldsZod, BridgeIntentSchema, BridgeSourceObservationFieldsZod, BridgeSourceObservationSchema, SignedTransactionBoundWitnessZod, TransferZod,
|
|
@@ -24,7 +26,7 @@ export type BridgeToRemoteBody = z.infer<typeof BridgeToRemoteBodyZod>
|
|
|
24
26
|
export const BridgeToRemoteResponseZod = PayloadZodStrictOfSchema(BridgeSourceObservationSchema).extend(BridgeSourceObservationFieldsZod.shape)
|
|
25
27
|
export type BridgeToRemoteResponse = z.infer<typeof BridgeToRemoteResponseZod>
|
|
26
28
|
|
|
27
|
-
export const makeBridgeToRemoteRoute = (config:
|
|
29
|
+
export const makeBridgeToRemoteRoute = (config: BridgeConfig): RouteDefinition => {
|
|
28
30
|
const params = z.object({ chainId: getRemoteChainIdZod(config) })
|
|
29
31
|
|
|
30
32
|
const validateRequest = requestHandlerValidator({
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import type { RouteDefinition } from '@xylabs/express'
|
|
2
2
|
import { requestHandlerValidator } from '@xylabs/express'
|
|
3
3
|
import { assertEx, toAddress } from '@xylabs/sdk-js'
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
4
|
+
import type { BridgeConfig } from '@xyo-network/chain-orchestration'
|
|
5
|
+
import { PayloadZodStrictOfSchema } from '@xyo-network/sdk-js'
|
|
6
|
+
import type { XyoGatewayRunner } from '@xyo-network/xl1-sdk'
|
|
6
7
|
import {
|
|
7
8
|
BridgeIntentFieldsZod, BridgeIntentSchema, buildUnsignedTransaction, toXL1BlockNumber, TransactionBoundWitnessZod, TransferZod,
|
|
8
9
|
} from '@xyo-network/xl1-sdk'
|
|
@@ -27,7 +28,7 @@ export const BridgeToRemoteEstimateResponseZod = z.tuple([
|
|
|
27
28
|
|
|
28
29
|
export type BridgeToRemoteEstimateResponse = z.infer<typeof BridgeToRemoteEstimateResponseZod>
|
|
29
30
|
|
|
30
|
-
export const makeBridgeToRemoteEstimateRoute = (config:
|
|
31
|
+
export const makeBridgeToRemoteEstimateRoute = (config: BridgeConfig, gateway: XyoGatewayRunner): RouteDefinition => {
|
|
31
32
|
const params = z.object({ chainId: getRemoteChainIdZod(config) })
|
|
32
33
|
const validateRequest = requestHandlerValidator({
|
|
33
34
|
params, body: BridgeToRemoteEstimateBodyZod, response: BridgeToRemoteEstimateResponseZod,
|
|
@@ -3,10 +3,9 @@ import { requestHandlerValidator } from '@xylabs/express'
|
|
|
3
3
|
import {
|
|
4
4
|
asAddress, asHex, toHex,
|
|
5
5
|
} from '@xylabs/sdk-js'
|
|
6
|
-
import {
|
|
7
|
-
import
|
|
8
|
-
|
|
9
|
-
} from '@xyo-network/xl1-sdk'
|
|
6
|
+
import type { BridgeConfig } from '@xyo-network/chain-orchestration'
|
|
7
|
+
import { PayloadZodStrictOfSchema } from '@xyo-network/sdk-js'
|
|
8
|
+
import type { BridgeDestinationObservation, BridgeSourceObservation } from '@xyo-network/xl1-sdk'
|
|
10
9
|
import {
|
|
11
10
|
BridgeDestinationObservationFieldsZod, BridgeDestinationObservationSchema, BridgeIntentFieldsZod, BridgeIntentSchema, BridgeSourceObservationFieldsZod,
|
|
12
11
|
BridgeSourceObservationSchema,
|
|
@@ -32,7 +31,7 @@ export const BridgeToRemoteStatusResponseZod = z.union([
|
|
|
32
31
|
])
|
|
33
32
|
export type BridgeToRemoteStatusResponse = z.infer<typeof BridgeToRemoteStatusResponseZod>
|
|
34
33
|
|
|
35
|
-
export const makeBridgeToRemoteStatusRoute = (config:
|
|
34
|
+
export const makeBridgeToRemoteStatusRoute = (config: BridgeConfig): RouteDefinition => {
|
|
36
35
|
const params = z.object({
|
|
37
36
|
chainId: getRemoteChainIdZod(config),
|
|
38
37
|
nonce: z.string().nonempty(),
|
package/src/server/server.ts
CHANGED
|
@@ -1,59 +1,18 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
} from '@xylabs/sdk-js'
|
|
4
|
-
import { boot } from '@xyo-network/bios'
|
|
5
|
-
import type { BiosExternalInterface } from '@xyo-network/bios-model'
|
|
6
|
-
import type { NodeInstance } from '@xyo-network/node-model'
|
|
7
|
-
import { HDWallet } from '@xyo-network/wallet'
|
|
8
|
-
import type { Config, XyoGatewayRunner } from '@xyo-network/xl1-sdk'
|
|
1
|
+
import type { BridgeConfigContext } from '@xyo-network/chain-orchestration'
|
|
2
|
+
import type { XyoGatewayRunner } from '@xyo-network/xl1-sdk'
|
|
9
3
|
|
|
10
|
-
import {
|
|
4
|
+
import { getServices } from '../services/index.ts'
|
|
11
5
|
import { addWorkers } from './addWorkers.ts'
|
|
12
6
|
import { getApp } from './app.ts'
|
|
13
7
|
|
|
14
8
|
const hostname = '::'
|
|
15
9
|
// const hostname = '0.0.0.0'
|
|
16
10
|
|
|
17
|
-
|
|
18
|
-
const getSeedPhrase = async (bios: BiosExternalInterface, config: Config, logger?: Logger): Promise<string> => {
|
|
19
|
-
const storedSeedPhrase = await bios.seedPhraseStore.get('os')
|
|
20
|
-
logger?.debug(`[Bridge] Stored mnemonic: ${storedSeedPhrase}`)
|
|
21
|
-
const { mnemonic } = config.actors.api
|
|
22
|
-
if (isString(storedSeedPhrase) && isString(mnemonic)) {
|
|
23
|
-
logger?.warn('[Bridge] Stored mnemonic does not match supplied. Updating stored mnemonic to supplied.')
|
|
24
|
-
await bios.seedPhraseStore.set('os', mnemonic)
|
|
25
|
-
} else {
|
|
26
|
-
let seedPhrase: string
|
|
27
|
-
if (isString(mnemonic)) {
|
|
28
|
-
seedPhrase = mnemonic
|
|
29
|
-
} else {
|
|
30
|
-
seedPhrase = HDWallet.generateMnemonic()
|
|
31
|
-
logger?.log('[Bridge] No mnemonic provided, using random mnemonic. This is not recommended for production use.')
|
|
32
|
-
logger?.log(`[Bridge] Mnemonic: ${seedPhrase}`)
|
|
33
|
-
}
|
|
34
|
-
await bios.seedPhraseStore.set('os', seedPhrase)
|
|
35
|
-
}
|
|
36
|
-
return assertEx(await bios.seedPhraseStore.get('os'), () => 'Unable to acquire mnemonic from bios')
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
interface GetServerContext {
|
|
40
|
-
config: Config
|
|
41
|
-
logger?: Logger
|
|
42
|
-
node?: NodeInstance
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export const getServer = async (context: GetServerContext, gateway: XyoGatewayRunner) => {
|
|
11
|
+
export const getServer = async (context: BridgeConfigContext, gateway: XyoGatewayRunner) => {
|
|
46
12
|
const { logger, config } = context
|
|
47
|
-
const { port
|
|
48
|
-
const
|
|
49
|
-
const
|
|
50
|
-
const wallet = await HDWallet.fromPhrase(seedPhrase)
|
|
51
|
-
const nodeContext = {
|
|
52
|
-
wallet, logger, config,
|
|
53
|
-
}
|
|
54
|
-
const node = context.node ?? await getNode(nodeContext)
|
|
55
|
-
const app = getApp(node, config, gateway)
|
|
56
|
-
const services = await getServices({ config, logger }, gateway)
|
|
13
|
+
const { port } = config
|
|
14
|
+
const app = getApp(config, gateway)
|
|
15
|
+
const services = await getServices(context, gateway)
|
|
57
16
|
addWorkers(config, services)
|
|
58
17
|
const server = app.listen(port, hostname, () => logger?.log(`[Bridge] Server listening at http://${hostname}:${port}`))
|
|
59
18
|
server.setTimeout(20_000)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BaseMongoSdk, type BaseMongoSdkPrivateConfig } from '@xylabs/mongo'
|
|
2
2
|
import { MongoMap } from '@xyo-network/chain-protocol'
|
|
3
|
-
import type {
|
|
3
|
+
import type { BaseConfig, IterableMap } from '@xyo-network/xl1-sdk'
|
|
4
4
|
import { hasMongoConfig, mapToMapType } from '@xyo-network/xl1-sdk'
|
|
5
5
|
import type { Document } from 'mongodb'
|
|
6
6
|
|
|
@@ -11,7 +11,7 @@ import type { Document } from 'mongodb'
|
|
|
11
11
|
* @returns The iterable map
|
|
12
12
|
*/
|
|
13
13
|
export const getIterableMap = async <K extends {} = string, V extends Document = Document>(
|
|
14
|
-
config:
|
|
14
|
+
config: BaseConfig,
|
|
15
15
|
collection: string,
|
|
16
16
|
): Promise<IterableMap<K, V>> => {
|
|
17
17
|
const mongoConfig = config.storage?.mongo
|
|
@@ -1,34 +1,30 @@
|
|
|
1
|
-
import type { Hash
|
|
1
|
+
import type { Hash } from '@xylabs/sdk-js'
|
|
2
2
|
import { assertEx, isDefined } from '@xylabs/sdk-js'
|
|
3
|
+
import type { BridgeConfig, BridgeConfigContext } from '@xyo-network/chain-orchestration'
|
|
3
4
|
import { initEvmProvider } from '@xyo-network/chain-orchestration'
|
|
4
5
|
import { BridgeableToken__factory, LiquidityPoolBridge__factory } from '@xyo-network/typechain'
|
|
5
6
|
import { HDWallet } from '@xyo-network/wallet'
|
|
6
|
-
import type {
|
|
7
|
+
import type { XyoGatewayRunner } from '@xyo-network/xl1-sdk'
|
|
7
8
|
import { getAddress, Wallet } from 'ethers'
|
|
8
9
|
|
|
9
|
-
import type {
|
|
10
|
-
EthTxState, IBridgeServiceCollection, Xl1TxState,
|
|
11
|
-
} from '../services/index.ts'
|
|
10
|
+
import type { EthTxState } from './EthTxState.ts'
|
|
12
11
|
import { getIterableMap } from './getIterableMap.ts'
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
config: Config
|
|
16
|
-
logger?: Logger
|
|
17
|
-
}
|
|
12
|
+
import type { IBridgeServiceCollection } from './IBridgeServiceCollection.ts'
|
|
13
|
+
import type { Xl1TxState } from './Xl1TxState.ts'
|
|
18
14
|
|
|
19
15
|
/**
|
|
20
16
|
* Used for retrieving a service collection
|
|
21
17
|
* @returns A service collection
|
|
22
18
|
*/
|
|
23
|
-
export const getServices = async (context:
|
|
19
|
+
export const getServices = async (context: BridgeConfigContext, gateway: XyoGatewayRunner): Promise<IBridgeServiceCollection> => {
|
|
24
20
|
const { config } = context
|
|
25
21
|
|
|
26
22
|
const ethTxStateMap = await getIterableMap<Hash, EthTxState>(config, 'liquidity_bridge_xl1_to_eth_eth_tx_state')
|
|
27
23
|
const xl1TxStateMap = await getIterableMap<Hash, Xl1TxState>(config, 'liquidity_bridge_xl1_to_eth_xl1_tx_state')
|
|
28
|
-
const provider = await initEvmProvider(
|
|
24
|
+
const provider = await initEvmProvider(context)
|
|
29
25
|
const {
|
|
30
26
|
remoteBridgeContractAddress, remoteChainWalletPrivateKey, remoteTokenAddress, mnemonic,
|
|
31
|
-
} = config
|
|
27
|
+
} = config as BridgeConfig
|
|
32
28
|
const account = isDefined(mnemonic) ? await HDWallet.fromPhrase(mnemonic) : await HDWallet.random()
|
|
33
29
|
const wallet = new Wallet(remoteChainWalletPrivateKey, provider)
|
|
34
30
|
const bridgeableToken = BridgeableToken__factory.connect(getAddress(remoteTokenAddress), wallet)
|
package/src/services/index.ts
CHANGED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { hexToBigInt } from '@xylabs/sdk-js'
|
|
2
|
+
|
|
3
|
+
import type { BridgeFees } from './BridgeFees.ts'
|
|
4
|
+
|
|
5
|
+
export const bridgeFeesAsBigInt = (fees: BridgeFees): {
|
|
6
|
+
feeFixed: bigint
|
|
7
|
+
feeVariable: bigint
|
|
8
|
+
srcAmount: bigint
|
|
9
|
+
} => {
|
|
10
|
+
return {
|
|
11
|
+
feeFixed: hexToBigInt(fees.feeFixed),
|
|
12
|
+
feeVariable: hexToBigInt(fees.feeVariable),
|
|
13
|
+
srcAmount: hexToBigInt(fees.srcAmount),
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -1,16 +1,12 @@
|
|
|
1
1
|
import type { Hex } from '@xylabs/sdk-js'
|
|
2
2
|
import { hexToBigInt, toHex } from '@xylabs/sdk-js'
|
|
3
|
-
import type {
|
|
3
|
+
import type { BridgeConfig } from '@xyo-network/chain-orchestration'
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
import type { BridgeFees } from './BridgeFees.ts'
|
|
6
6
|
|
|
7
|
-
export
|
|
8
|
-
destAmount: Hex
|
|
9
|
-
feeFixed: Hex
|
|
10
|
-
feeVariable: Hex
|
|
11
|
-
}
|
|
7
|
+
export type FeeStructure = Pick<BridgeConfig, 'feeFixed' | 'feeRateBasisPoints'>
|
|
12
8
|
|
|
13
|
-
export const calculateBridgeFees = (srcAmount: Hex, feeStructure: FeeStructure):
|
|
9
|
+
export const calculateBridgeFees = (srcAmount: Hex, feeStructure: FeeStructure): BridgeFees => {
|
|
14
10
|
const { feeFixed, feeRateBasisPoints } = feeStructure
|
|
15
11
|
|
|
16
12
|
// Format source input
|
|
@@ -32,8 +28,6 @@ export const calculateBridgeFees = (srcAmount: Hex, feeStructure: FeeStructure):
|
|
|
32
28
|
const destAmount = toHex(destAmountBigInt)
|
|
33
29
|
|
|
34
30
|
return {
|
|
35
|
-
|
|
36
|
-
feeFixed,
|
|
37
|
-
feeVariable,
|
|
31
|
+
feeFixed, feeVariable, srcAmount,
|
|
38
32
|
}
|
|
39
33
|
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { Address, Hex } from '@xylabs/sdk-js'
|
|
2
|
+
import { hexToBigInt } from '@xylabs/sdk-js'
|
|
3
|
+
import type { Transfer } from '@xyo-network/xl1-sdk'
|
|
4
|
+
import { createTransferPayload } from '@xyo-network/xl1-sdk'
|
|
5
|
+
|
|
6
|
+
import type { BridgeFees } from './BridgeFees.ts'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Calculates the transfer representing the bridging of funds including the appropriate amounts for the bridge fees
|
|
10
|
+
* @param sender The client attempting to bridge
|
|
11
|
+
* @param srcAmount The amount the client is attempting to bridge
|
|
12
|
+
* @param escrowAddress The address in which bridged funds will be held until the transfer is executed on the destination chain
|
|
13
|
+
* @param feesAddress The address to which bridge fees will be paid
|
|
14
|
+
* @param context An object containing the necessary fee and amount information to construct the transfer context
|
|
15
|
+
* @returns A transfer representing the bridging of funds including the appropriate amounts for the bridge fees
|
|
16
|
+
*/
|
|
17
|
+
export const createBridgeTransfer = (sender: Address, srcAmount: Hex, escrowAddress: Address, feesAddress: Address, context: BridgeFees): Transfer => {
|
|
18
|
+
const { feeFixed, feeVariable } = context
|
|
19
|
+
const escrowAmount = hexToBigInt(srcAmount)
|
|
20
|
+
const feesAmount = hexToBigInt(feeFixed) + hexToBigInt(feeVariable)
|
|
21
|
+
const transfers: Record<Address, bigint>
|
|
22
|
+
// If the escrow and fee addresses are the same
|
|
23
|
+
= escrowAddress === feesAddress
|
|
24
|
+
// combine the amounts into a single transfer to that address
|
|
25
|
+
? { [feesAddress]: escrowAmount + feesAmount }
|
|
26
|
+
// otherwise, create separate transfers to the escrow and fees addresses
|
|
27
|
+
: { [escrowAddress]: escrowAmount, [feesAddress]: feesAmount }
|
|
28
|
+
const transfer = createTransferPayload(sender, transfers, context)
|
|
29
|
+
|
|
30
|
+
return transfer
|
|
31
|
+
}
|
|
@@ -1,21 +1,22 @@
|
|
|
1
1
|
import type { Hex } from '@xylabs/sdk-js'
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { toAddress } from '@xylabs/sdk-js'
|
|
3
|
+
import type { BridgeConfig } from '@xyo-network/chain-orchestration'
|
|
4
|
+
import { PayloadBuilder } from '@xyo-network/sdk-js'
|
|
4
5
|
import type {
|
|
5
|
-
BridgeIntent, BridgeIntentFields,
|
|
6
|
-
Transfer,
|
|
6
|
+
BridgeIntent, BridgeIntentFields, Transfer,
|
|
7
7
|
} from '@xyo-network/xl1-sdk'
|
|
8
|
-
import { BridgeIntentSchema
|
|
8
|
+
import { BridgeIntentSchema } from '@xyo-network/xl1-sdk'
|
|
9
9
|
import { v4 } from 'uuid'
|
|
10
10
|
|
|
11
11
|
import { getBridgeSettings } from '../config/index.ts'
|
|
12
12
|
import { calculateBridgeFees } from './calculateBridgeFees.ts'
|
|
13
|
+
import { createBridgeTransfer } from './createBridgeTransfer.ts'
|
|
13
14
|
|
|
14
15
|
export const generateBridgeEstimate = async (
|
|
15
16
|
srcAddress: Hex,
|
|
16
17
|
srcAmount: Hex,
|
|
17
18
|
destAddress: Hex,
|
|
18
|
-
config:
|
|
19
|
+
config: BridgeConfig,
|
|
19
20
|
nonceOverride?: string,
|
|
20
21
|
): Promise<[BridgeIntent, Transfer]> => {
|
|
21
22
|
const {
|
|
@@ -26,9 +27,6 @@ export const generateBridgeEstimate = async (
|
|
|
26
27
|
|
|
27
28
|
// Calculate fees and destination amount
|
|
28
29
|
const fees = calculateBridgeFees(srcAmount, { feeFixed, feeRateBasisPoints })
|
|
29
|
-
const {
|
|
30
|
-
destAmount, feeFixed: feeFixedAmount, feeVariable,
|
|
31
|
-
} = fees
|
|
32
30
|
|
|
33
31
|
// Generate a unique nonce for the bridge intent
|
|
34
32
|
const nonce = nonceOverride ?? v4()
|
|
@@ -44,20 +42,14 @@ export const generateBridgeEstimate = async (
|
|
|
44
42
|
// Destination
|
|
45
43
|
dest: remoteChainId,
|
|
46
44
|
destAddress,
|
|
47
|
-
destAmount,
|
|
45
|
+
destAmount: srcAmount, // This is a 1:1 bridge. Fees are handled at the transfer level, not the bridge intent level.
|
|
48
46
|
destToken: remoteTokenAddress,
|
|
49
47
|
nonce,
|
|
50
48
|
}
|
|
51
49
|
const bridgeIntent: BridgeIntent = new PayloadBuilder<BridgeIntent>({ schema: BridgeIntentSchema }).fields(bridgeIntentFields).build()
|
|
52
50
|
|
|
53
51
|
// Build the transfer
|
|
54
|
-
const
|
|
55
|
-
destAmount, feeFixed: feeFixedAmount, feeVariable,
|
|
56
|
-
}
|
|
57
|
-
const transfer: Transfer = createTransferPayload(sender, {
|
|
58
|
-
[escrowAddress]: hexToBigInt(destAmount),
|
|
59
|
-
[feesAddress]: hexToBigInt(feeFixedAmount) + hexToBigInt(feeVariable),
|
|
60
|
-
}, context)
|
|
52
|
+
const transfer = createBridgeTransfer(sender, srcAmount, escrowAddress, feesAddress, fees)
|
|
61
53
|
|
|
62
54
|
return [bridgeIntent, transfer]
|
|
63
55
|
}
|