@avalabs/bridge-unified
Advanced tools
Comparing version 0.0.0-chore-bump-ci-versions-20240923192820 to 0.0.0-chore-release-fix-20241204133559
import { Address } from 'viem'; | ||
import { z } from 'zod'; | ||
@@ -12,2 +13,20 @@ type Chain = { | ||
}; | ||
declare enum AvalancheChainIds { | ||
FUJI = "eip155:43113", | ||
MAINNET = "eip155:43114" | ||
} | ||
declare enum EthereumChainIds { | ||
MAINNET = "eip155:1", | ||
SEPOLIA = "eip155:11155111" | ||
} | ||
declare enum BitcoinChainIds { | ||
MAINNET = "bip122:000000000019d6689c085ae165831e93", | ||
TESTNET = "bip122:000000000933ea01ad0ee984209779ba" | ||
} | ||
declare const AVALANCHE_FUJI_CHAIN: Chain; | ||
declare const AVALANCHE_MAINNET_CHAIN: Chain; | ||
declare const ETHEREUM_SEPOLIA_CHAIN: Chain; | ||
declare const ETHEREUM_MAINNET_CHAIN: Chain; | ||
declare const BITCOIN_TESTNET_CHAIN: Chain; | ||
declare const BITCOIN_MAINNET_CHAIN: Chain; | ||
@@ -21,16 +40,103 @@ declare enum Environment { | ||
type RequestArguments = { | ||
method: string; | ||
params?: unknown[] | Record<string | number, unknown>; | ||
}; | ||
type Provider = { | ||
/** | ||
* EIP-1193 compatible request method | ||
* https://eips.ethereum.org/EIPS/eip-1193#request-1 | ||
*/ | ||
request: (args: RequestArguments) => Promise<unknown>; | ||
}; | ||
/** | ||
* Custom Bitcoin UTXO interface. | ||
*/ | ||
declare const BitcoinInputUTXO: z.ZodObject<{ | ||
txHash: z.ZodString; | ||
txHex: z.ZodOptional<z.ZodString>; | ||
index: z.ZodNumber; | ||
value: z.ZodNumber; | ||
script: z.ZodString; | ||
blockHeight: z.ZodNumber; | ||
confirmations: z.ZodNumber; | ||
}, "strip", z.ZodTypeAny, { | ||
txHash: string; | ||
value: number; | ||
index: number; | ||
script: string; | ||
blockHeight: number; | ||
confirmations: number; | ||
txHex?: string | undefined; | ||
}, { | ||
txHash: string; | ||
value: number; | ||
index: number; | ||
script: string; | ||
blockHeight: number; | ||
confirmations: number; | ||
txHex?: string | undefined; | ||
}>; | ||
type BitcoinInputUTXO = typeof BitcoinInputUTXO._output; | ||
declare const BitcoinInputUTXOWithOptionalScript: z.ZodObject<z.objectUtil.extendShape<{ | ||
txHash: z.ZodString; | ||
txHex: z.ZodOptional<z.ZodString>; | ||
index: z.ZodNumber; | ||
value: z.ZodNumber; | ||
script: z.ZodString; | ||
blockHeight: z.ZodNumber; | ||
confirmations: z.ZodNumber; | ||
}, { | ||
script: z.ZodOptional<z.ZodString>; | ||
}>, "strip", z.ZodTypeAny, { | ||
txHash: string; | ||
value: number; | ||
index: number; | ||
blockHeight: number; | ||
confirmations: number; | ||
txHex?: string | undefined; | ||
script?: string | undefined; | ||
}, { | ||
txHash: string; | ||
value: number; | ||
index: number; | ||
blockHeight: number; | ||
confirmations: number; | ||
txHex?: string | undefined; | ||
script?: string | undefined; | ||
}>; | ||
type BitcoinInputUTXOWithOptionalScript = typeof BitcoinInputUTXOWithOptionalScript._output; | ||
/** | ||
* Used for defining outputs when creating a transaction. | ||
*/ | ||
declare const BitcoinOutputUTXO: z.ZodObject<{ | ||
address: z.ZodString; | ||
value: z.ZodNumber; | ||
}, "strip", z.ZodTypeAny, { | ||
value: number; | ||
address: string; | ||
}, { | ||
value: number; | ||
address: string; | ||
}>; | ||
type BitcoinOutputUTXO = typeof BitcoinOutputUTXO._output; | ||
interface BitcoinTx { | ||
hash: string; | ||
fees: number; | ||
block: number; | ||
amount: number; | ||
confirmations: number; | ||
blockTime: number; | ||
addresses: string[]; | ||
inputs: { | ||
txid: string; | ||
vout: number; | ||
sequence: number; | ||
n: number; | ||
addresses: string[]; | ||
isAddress: boolean; | ||
value: number; | ||
}[]; | ||
outputs: { | ||
addresses: string[]; | ||
value: number; | ||
n: number; | ||
spent: boolean; | ||
hex: string; | ||
isAddress: boolean; | ||
}[]; | ||
} | ||
type Hex = `0x${string}`; | ||
type TransactionRequest = { | ||
type EvmTransactionRequest = { | ||
type?: number | null; | ||
@@ -47,5 +153,18 @@ data?: Hex | null; | ||
maxFeePerGas?: bigint | null; | ||
chainId?: string; | ||
}; | ||
type Dispatch = (signedTxHash: Hex) => Promise<Hex>; | ||
type Signer = (data: TransactionRequest, dispatch: Dispatch) => Promise<Hex>; | ||
type EvmDispatch = (signedTxHash: Hex) => Promise<Hex>; | ||
type EvmSign = (data: EvmTransactionRequest, dispatch: EvmDispatch, step: BridgeStepDetails) => Promise<Hex>; | ||
type EvmSigner = { | ||
sign: EvmSign; | ||
}; | ||
type BtcTransactionRequest = { | ||
inputs: BitcoinInputUTXO[]; | ||
outputs: BitcoinOutputUTXO[]; | ||
}; | ||
type BtcDispatch = (signedTxHash: string) => Promise<string>; | ||
type BtcSign = (data: BtcTransactionRequest, dispatch: BtcDispatch, step: BridgeStepDetails) => Promise<string>; | ||
type BtcSigner = { | ||
sign: BtcSign; | ||
}; | ||
@@ -69,2 +188,4 @@ declare enum ErrorCode { | ||
INCORRECT_HASH_PROVIDED = "INCORRECT_HASH_PROVIDED", | ||
INCORRECT_PROVIDER_PROVIDED = "INCORRECT_PROVIDER_PROVIDED", | ||
INCORRECT_SIGNER_PROVIDED = "INCORRECT_SIGNER_PROVIDED", | ||
INCORRECT_TXHASH_PROVIDED = "INCORRECT_TXHASH_PROVIDED", | ||
@@ -75,3 +196,4 @@ INVALID_PARAMS = "INVALID_PARAMS", | ||
WARDEN_CONFIG_MISMATCH = "WARDEN_CONFIG_MISMATCH", | ||
WARDEN_CONFIG_MISSING_NETWORK = "WARDEN_CONFIG_MISSING_NETWORK" | ||
WARDEN_CONFIG_MISSING_NETWORK = "WARDEN_CONFIG_MISSING_NETWORK", | ||
ADDRESS_IS_BLOCKED = "ADDRESS_IS_BLOCKED" | ||
} | ||
@@ -105,7 +227,50 @@ | ||
interface BitcoinFunctions { | ||
getChainHeight: () => Promise<number>; | ||
getUTXOs: (address: string, withScripts?: boolean) => Promise<{ | ||
confirmed: BitcoinInputUTXOWithOptionalScript[]; | ||
unconfirmed: BitcoinInputUTXOWithOptionalScript[]; | ||
}>; | ||
getTransaction: (hash: string) => Promise<BitcoinTx>; | ||
getFeeRates: () => Promise<{ | ||
low: number; | ||
medium: number; | ||
high: number; | ||
}>; | ||
getUtxoBalance: (address: string, withScripts?: boolean) => Promise<{ | ||
utxos: BitcoinInputUTXOWithOptionalScript[]; | ||
utxosUnconfirmed: BitcoinInputUTXOWithOptionalScript[]; | ||
}>; | ||
getScriptsForUtxos: (utxos: BitcoinInputUTXOWithOptionalScript[]) => Promise<BitcoinInputUTXO[]>; | ||
issueRawTx: (tx: string) => Promise<string>; | ||
} | ||
declare enum BridgeType { | ||
AVALANCHE_EVM = "avalanche-evm", | ||
AVALANCHE_BTC_AVA = "avalanche-btc-ava", | ||
AVALANCHE_AVA_BTC = "avalanche-ava-btc", | ||
CCTP = "cctp", | ||
ICTT_ERC20_ERC20 = "ictt-erc20-erc20" | ||
} | ||
declare const BTC_BRIDGE_TYPES: readonly [BridgeType.AVALANCHE_AVA_BTC, BridgeType.AVALANCHE_BTC_AVA]; | ||
declare const EVM_BRIDGE_TYPES: readonly [BridgeType.AVALANCHE_EVM, BridgeType.CCTP, BridgeType.ICTT_ERC20_ERC20]; | ||
type ArrayElement<A> = A extends readonly (infer T)[] ? T : never; | ||
type EvmBridgeInitializer = { | ||
type: ArrayElement<typeof EVM_BRIDGE_TYPES>; | ||
signer: EvmSigner; | ||
}; | ||
type AvaToBtcBridgeInitializer = { | ||
type: BridgeType.AVALANCHE_AVA_BTC; | ||
signer: EvmSigner; | ||
bitcoinFunctions: BitcoinFunctions; | ||
}; | ||
type BtcToAvaBridgeInitializer = { | ||
type: BridgeType.AVALANCHE_BTC_AVA; | ||
signer: BtcSigner; | ||
bitcoinFunctions: BitcoinFunctions; | ||
}; | ||
type BridgeInitializer = EvmBridgeInitializer | AvaToBtcBridgeInitializer | BtcToAvaBridgeInitializer; | ||
declare const isEvmBridgeInitializer: (initializer: BridgeInitializer) => initializer is EvmBridgeInitializer; | ||
declare const isAvaToBtcBridgeInitializer: (initializer: BridgeInitializer) => initializer is AvaToBtcBridgeInitializer; | ||
declare const isBtcToAvaBridgeInitializer: (initializer: BridgeInitializer) => initializer is BtcToAvaBridgeInitializer; | ||
type FeeParams = { | ||
@@ -116,3 +281,2 @@ asset: BridgeAsset; | ||
targetChain: Chain; | ||
provider?: Provider; | ||
}; | ||
@@ -129,2 +293,6 @@ declare enum BridgeSignatureReason { | ||
}; | ||
type GasSettings = { | ||
price: bigint; | ||
tip?: bigint; | ||
}; | ||
type TransferParams = { | ||
@@ -134,22 +302,39 @@ asset: BridgeAsset; | ||
fromAddress: string; | ||
toAddress?: string; | ||
toAddress: string; | ||
sourceChain: Chain; | ||
targetChain: Chain; | ||
sourceProvider?: Provider; | ||
targetProvider?: Provider; | ||
gasSettings?: GasSettings; | ||
onStepChange?: (stepDetails: BridgeStepDetails) => void; | ||
sign: Signer; | ||
}; | ||
type GasEstimationParams = Omit<TransferParams, 'sign'>; | ||
type GasEstimationParams = Omit<TransferParams, 'onStepChange' | 'toAddress'>; | ||
type TrackingParams = { | ||
bridgeTransfer: BridgeTransfer; | ||
sourceProvider?: Provider; | ||
targetProvider?: Provider; | ||
updateListener: (transfer: BridgeTransfer) => void; | ||
}; | ||
type AnalyzeTxParams = { | ||
chainId: string; | ||
from: string; | ||
to: string; | ||
tokenTransfers: { | ||
from?: string; | ||
to?: string; | ||
symbol: string; | ||
}[]; | ||
}; | ||
type AnalyzeTxResult = { | ||
isBridgeTx: true; | ||
bridgeType: BridgeType; | ||
sourceChainId?: string; | ||
targetChainId?: string; | ||
} | { | ||
isBridgeTx: false; | ||
}; | ||
type BridgeService = { | ||
type: BridgeType; | ||
analyzeTx: (params: AnalyzeTxParams) => AnalyzeTxResult; | ||
estimateGas: (params: GasEstimationParams) => Promise<bigint>; | ||
getAssets: () => ChainAssetMap; | ||
getFees: (params: FeeParams) => Promise<AssetFeeMap>; | ||
estimateReceiveAmount: (params: GasEstimationParams) => Promise<{ | ||
asset: Asset; | ||
amount: bigint; | ||
}>; | ||
transferAsset: (params: TransferParams) => Promise<BridgeTransfer>; | ||
@@ -160,2 +345,4 @@ trackTransfer: (transfer: TrackingParams) => { | ||
}; | ||
getAssets: () => ChainAssetMap; | ||
getFees: (params: FeeParams) => Promise<AssetFeeMap>; | ||
getMinimumTransferAmount: (params: FeeParams) => Promise<bigint>; | ||
@@ -189,3 +376,3 @@ }; | ||
type ChainAssetMap = Record<string, BridgeAsset[]>; | ||
type AssetFeeMap = Record<Address, bigint>; | ||
type AssetFeeMap = Record<string, Partial<Record<Address | 'NATIVE', bigint>>>; | ||
@@ -197,17 +384,9 @@ type BridgeServicesMap = Map<BridgeType, BridgeService>; | ||
}; | ||
declare const createUnifiedBridgeService: ({ environment, enabledBridgeServices }: BridgeServiceConfig) => { | ||
type UnifiedBridgeService = Omit<BridgeService, 'type'> & { | ||
canTransferAsset: (asset: BridgeAsset, targetChainId: string) => boolean; | ||
environment: Environment; | ||
getAssets: () => Promise<ChainAssetMap>; | ||
getFees: (params: FeeParams) => Promise<AssetFeeMap>; | ||
estimateGas: (params: GasEstimationParams) => Promise<bigint>; | ||
canTransferAsset: (asset: BridgeAsset, targetChainId: string) => boolean; | ||
transferAsset: (params: TransferParams) => Promise<BridgeTransfer>; | ||
trackTransfer: (params: TrackingParams) => { | ||
cancel: () => void; | ||
result: Promise<BridgeTransfer>; | ||
}; | ||
getMinimumTransferAmount: (params: FeeParams) => Promise<bigint>; | ||
}; | ||
declare const createUnifiedBridgeService: ({ environment, enabledBridgeServices }: BridgeServiceConfig) => UnifiedBridgeService; | ||
type Caip2ChainId = { | ||
@@ -220,6 +399,7 @@ namespace: string; | ||
toString: ({ namespace, reference }: Caip2ChainId) => string; | ||
toEip155HexChainId: ({ namespace, reference }: Caip2ChainId) => string; | ||
}; | ||
declare const getEnabledBridgeServices: (environment: Environment, disabledBridgeTypes: BridgeType[]) => Promise<BridgeServicesMap>; | ||
declare const getEnabledBridgeServices: (environment: Environment, enabledBridgeInitializers: BridgeInitializer[]) => Promise<BridgeServicesMap>; | ||
export { Asset, AssetFeeMap, BridgeAsset, BridgeService, BridgeServiceConfig, BridgeServiceFactory, BridgeServicesMap, BridgeSignatureReason, BridgeStepDetails, BridgeTransfer, BridgeType, Chain, ChainAssetMap, DestinationInfo, Dispatch, Environment, Erc20Asset, ErrorCode, ErrorReason, FeeParams, GasEstimationParams, Hex, NativeAsset, Provider, Signer, TokenType, TrackingParams, TransactionRequest, TransferParams, _default as caip2, createUnifiedBridgeService, getEnabledBridgeServices, isErc20Asset, isNativeAsset }; | ||
export { AVALANCHE_FUJI_CHAIN, AVALANCHE_MAINNET_CHAIN, AnalyzeTxParams, AnalyzeTxResult, ArrayElement, Asset, AssetFeeMap, AvaToBtcBridgeInitializer, AvalancheChainIds, BITCOIN_MAINNET_CHAIN, BITCOIN_TESTNET_CHAIN, BTC_BRIDGE_TYPES, BitcoinChainIds, BitcoinFunctions, BitcoinInputUTXO, BitcoinInputUTXOWithOptionalScript, BitcoinTx, BridgeAsset, BridgeInitializer, BridgeService, BridgeServiceConfig, BridgeServiceFactory, BridgeServicesMap, BridgeSignatureReason, BridgeStepDetails, BridgeTransfer, BridgeType, BtcDispatch, BtcSign, BtcSigner, BtcToAvaBridgeInitializer, BtcTransactionRequest, Chain, ChainAssetMap, DestinationInfo, ETHEREUM_MAINNET_CHAIN, ETHEREUM_SEPOLIA_CHAIN, EVM_BRIDGE_TYPES, Environment, Erc20Asset, ErrorCode, ErrorReason, EthereumChainIds, EvmBridgeInitializer, EvmDispatch, EvmSign, EvmSigner, EvmTransactionRequest, FeeParams, GasEstimationParams, GasSettings, Hex, NativeAsset, TokenType, TrackingParams, TransferParams, UnifiedBridgeService, _default as caip2, createUnifiedBridgeService, getEnabledBridgeServices, isAvaToBtcBridgeInitializer, isBtcToAvaBridgeInitializer, isErc20Asset, isEvmBridgeInitializer, isNativeAsset }; |
{ | ||
"name": "@avalabs/bridge-unified", | ||
"license": "Limited Ecosystem License", | ||
"version": "0.0.0-chore-bump-ci-versions-20240923192820", | ||
"version": "0.0.0-chore-release-fix-20241204133559", | ||
"main": "dist/index.cjs", | ||
@@ -13,6 +13,10 @@ "module": "dist/index.js", | ||
"dependencies": { | ||
"@noble/hashes": "1.5.0", | ||
"@scure/base": "1.1.9", | ||
"abitype": "0.9.3", | ||
"lodash": "4.17.21", | ||
"viem": "2.11.1", | ||
"zod": "3.23.8" | ||
"zod": "3.23.8", | ||
"@scure/btc-signer": "1.3.2", | ||
"coinselect": "3.1.13" | ||
}, | ||
@@ -19,0 +23,0 @@ "devDependencies": { |
@@ -19,3 +19,3 @@ <p align="center"> | ||
- **CCTP** - preferred for brdiging USDC between Ethereum and Avalanche C-Chain. See the `bridges/cctp` folder. | ||
- **CCTP** - preferred for bridging USDC between Ethereum and Avalanche C-Chain. See the `bridges/cctp` folder. | ||
@@ -44,4 +44,58 @@ Future bridges we plan to support: | ||
const environment = Environment.TEST; | ||
const evmSigner: EvmSigner = { | ||
sign: async ({ data, from, to, value }) => { | ||
return await window.ethereum.request({ | ||
method: 'eth_sendTransaction,', | ||
params:{ | ||
account: from, | ||
data: data ?? undefined, | ||
from, | ||
to: to ?? null, | ||
chain: undefined, | ||
value | ||
} | ||
}); | ||
}, | ||
}; | ||
const btcSigner: BtcSigner = { | ||
sign: async ({ inputs, outputs }) => { | ||
return await window.ethereum.request({ | ||
method: 'bitcoin_signTransaction', | ||
params: { inputs, outputs }, | ||
}); | ||
}, | ||
}; | ||
const cctpInitializer: EvmBridgeInitializer = { | ||
type: BridgeType.CCTP, | ||
signer: evmSigner, | ||
}; | ||
const icttErc20Initializer: EvmBridgeInitializer = { | ||
type: BridgeType.ICTT_ERC20_ERC20, | ||
signer: evmSigner, | ||
}; | ||
const avalancheEvmInitializer: EvmBridgeInitializer = { | ||
type: BridgeType.AVALANCHE_EVM, | ||
signer: evmSigner, | ||
}; | ||
const avalancheBtcInitializer: AvaToBtcBridgeInitializer = { | ||
type: BridgeType.AVALANCHE_AVA_BTC, | ||
signer: evmSigner, | ||
bitcoinFunctions: bitcoinProvider as BitcoinFunctions, | ||
}; | ||
const bitcoinAvaInitializer: BtcToAvaBridgeInitializer = { | ||
type: BridgeType.AVALANCHE_BTC_AVA, | ||
signer: btcSigner, | ||
bitcoinFunctions: bitcoinProvider as BitcoinFunctions, | ||
}; | ||
// use all available bridges | ||
const disabledBridgeTypes = [] | ||
const enabledBridgeInitializers: BridgeInitializer[] = [ | ||
cctpInitializer, | ||
icttErc20Initializer, | ||
avalancheEvmInitializer, | ||
avalancheBtcInitializer, | ||
bitcoinAvaInitializer, | ||
] | ||
@@ -74,3 +128,3 @@ // fetch all available bridge services | ||
// immediatelly stops tracking and rejects the tracker's promise | ||
// immediately stops tracking and rejects the tracker's promise | ||
// cancel() | ||
@@ -84,5 +138,5 @@ | ||
### getEnabledBridgeServices(environment, disabledBridgeTypes); | ||
### getEnabledBridgeServices(environment, enabledBridgeInitializers); | ||
Type: `(environment: Environment, disabledBridgeTypes: BridgeType[]) => Promise<BridgeServicesMap>` | ||
Type: `(environment: Environment, enabledBridgeInitializers: BridgeInitializer[]) => Promise<BridgeServicesMap>` | ||
@@ -97,7 +151,7 @@ Returns all available bridge services for a given environment (excluding disabledBridgeTypes). Any bridge service which fails to initialize will be absent from this returned value. | ||
#### disabledBridgeTypes | ||
#### enabledBridgeInitializers | ||
Type: `BridgeType[]` | ||
Type: `BridgeInitializer[]` | ||
Disables the integration of the provided `BridgeType`s. | ||
Enables the integration of the provided `BridgeType`s based on each `BridgeInitializer`. | ||
@@ -163,2 +217,16 @@ #### enabledBridgeServices | ||
Notes about TransferParams: | ||
fromAddress: The address where the bridge amount is from. | ||
toAddress: The address where the bridge amount is going to end up. | ||
For example, A user has an account with AddressC and AddressBtc. | ||
The user wants to bridge some funds from Ethereum to Avalanche using the same address. FromAddress and toAddress will be both AddressC. | ||
The user wants to bridge some funds from Bitcoin to Avalanche. | ||
FromAddress is AddressBtc and toAddress is AddressC. | ||
Some bridges allows you to bridge the tokens to a different address. (CCTP and ICTT ERC20). | ||
In this case, fromAddress is the address of the token is getting bridged from. And toAddress is the address which is going to receive the bridged funds. | ||
#### trackTransfer | ||
@@ -174,3 +242,3 @@ | ||
If it's still pending, rejects the tracker's promise (`result`) immediatelly and breaks its loop under the hood. | ||
If it's still pending, rejects the tracker's promise (`result`) immediately and breaks its loop under the hood. | ||
@@ -177,0 +245,0 @@ ###### result |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1138757
1298
243
8
6
+ Added@noble/hashes@1.5.0
+ Added@scure/base@1.1.9
+ Added@scure/btc-signer@1.3.2
+ Addedcoinselect@3.1.13
+ Added@noble/curves@1.4.2(transitive)
+ Added@noble/hashes@1.3.31.4.01.5.0(transitive)
+ Added@scure/btc-signer@1.3.2(transitive)
+ Addedcoinselect@3.1.13(transitive)
+ Addedmicro-packed@0.6.3(transitive)