-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6e45a85
commit 741845a
Showing
10 changed files
with
138 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const SERVICE_NAME = { | ||
BRIDGING_PAYMENT: 'bridgingPayment', | ||
VFT_GATEWAY: 'vftGateway', | ||
VFT: 'vft', | ||
} as const; | ||
|
||
const QUERY_NAME = { | ||
VFT_GATEWAY_ADDRESS: 'vftGatewayAddress', | ||
FT_ADDRESSES: 'varaToEthAddresses', | ||
BALANCE: 'balanceOf', | ||
DECIMALS: 'decimals', | ||
GET_CONFIG: 'getConfig', | ||
} as const; | ||
|
||
export { SERVICE_NAME, QUERY_NAME }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,120 +1,28 @@ | ||
import { HexString } from '@gear-js/api'; | ||
import { useProgram, useProgramQuery, useApi } from '@gear-js/react-hooks'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { useState } from 'react'; | ||
import { useConfig } from 'wagmi'; | ||
import { readContract } from 'wagmi/actions'; | ||
import { useMemo, useState } from 'react'; | ||
|
||
import { | ||
BRIDGING_PAYMENT_CONTRACT_ADDRESS, | ||
BridgingPaymentProgram, | ||
FUNGIBLE_TOKEN_ABI, | ||
NETWORK_INDEX, | ||
VftGatewayProgram, | ||
VftProgram, | ||
} from '../consts'; | ||
import { NETWORK_INDEX } from '../consts'; | ||
import { getOptions } from '../utils'; | ||
|
||
function useTokens() { | ||
const wagmiConfig = useConfig(); | ||
|
||
const { data: program } = useProgram({ | ||
library: BridgingPaymentProgram, | ||
id: BRIDGING_PAYMENT_CONTRACT_ADDRESS, | ||
}); | ||
|
||
const { data: vftGatewayAddress } = useProgramQuery({ | ||
program, | ||
serviceName: 'bridgingPayment', | ||
functionName: 'vftGatewayAddress', | ||
args: [], | ||
}); | ||
|
||
const { data: vftGatewayProgram } = useProgram({ | ||
library: VftGatewayProgram, | ||
id: vftGatewayAddress?.toString() as HexString, | ||
}); | ||
|
||
const { data: ftAdresses } = useProgramQuery({ | ||
program: vftGatewayProgram, | ||
serviceName: 'vftGateway', | ||
functionName: 'varaToEthAddresses', | ||
args: [], | ||
}); | ||
|
||
const { api, isApiReady } = useApi(); | ||
|
||
const { data, isPending } = useQuery({ | ||
queryKey: ['options', ftAdresses], | ||
|
||
queryFn: () => { | ||
if (!api || !ftAdresses) throw new Error('Api or ftAdresses is not ready'); | ||
|
||
const promises = ftAdresses.map(async (addressPair) => { | ||
const varaAddress = addressPair[0].toString() as HexString; | ||
const ethAddress = addressPair[1].toString() as HexString; | ||
|
||
// TODO: read decimals only for selected? | ||
const vftProgram = new VftProgram(api, varaAddress); | ||
const varaSymbol = await vftProgram.vft.symbol(); | ||
const varaDecimals = await vftProgram.vft.decimals(); | ||
|
||
const ethSymbol = await readContract(wagmiConfig, { | ||
address: ethAddress.toString() as HexString, | ||
abi: FUNGIBLE_TOKEN_ABI, | ||
functionName: 'symbol', | ||
}); | ||
|
||
const ethDecimals = await readContract(wagmiConfig, { | ||
address: ethAddress.toString() as HexString, | ||
abi: FUNGIBLE_TOKEN_ABI, | ||
functionName: 'decimals', | ||
}); | ||
|
||
return [ | ||
{ address: varaAddress, symbol: varaSymbol, decimals: varaDecimals }, | ||
{ address: ethAddress, symbol: ethSymbol, decimals: ethDecimals }, | ||
]; | ||
}); | ||
|
||
return Promise.all(promises); | ||
}, | ||
|
||
enabled: isApiReady && Boolean(ftAdresses), | ||
}); | ||
|
||
const isLoading = isPending; | ||
|
||
return { data, isLoading }; | ||
} | ||
|
||
const getOptions = (data: ReturnType<typeof useTokens>['data']) => { | ||
const varaOptions: { label: string; value: string }[] = []; | ||
const ethOptions: { label: string; value: string }[] = []; | ||
|
||
data?.forEach(([vara, eth], index) => { | ||
varaOptions.push({ value: index.toString(), label: vara.symbol }); | ||
ethOptions.push({ value: index.toString(), label: eth.symbol }); | ||
}); | ||
|
||
return { varaOptions, ethOptions }; | ||
}; | ||
import { useFTSymbols } from './use-ft-symbols'; | ||
import { useFTAddresses } from './vara'; | ||
|
||
function useBridge(networkIndex: number) { | ||
const isVaraNetwork = networkIndex === NETWORK_INDEX.VARA; | ||
const nativeSymbol = isVaraNetwork ? 'VARA' : 'ETH'; | ||
|
||
const tokens = useTokens(); | ||
const [pair, setPair] = useState('0'); | ||
const { data: ftAddresses } = useFTAddresses(); | ||
const { data: ftSymbols, isPending } = useFTSymbols(ftAddresses); | ||
|
||
const { varaOptions, ethOptions } = getOptions(tokens.data); | ||
const { varaOptions, ethOptions } = useMemo(() => getOptions(ftSymbols), [ftSymbols]); | ||
const options = { from: isVaraNetwork ? varaOptions : ethOptions, to: isVaraNetwork ? ethOptions : varaOptions }; | ||
|
||
const bridge = tokens.data?.[Number(pair)][networkIndex]; | ||
const { address } = bridge || {}; | ||
|
||
const nativeSymbol = isVaraNetwork ? 'VARA' : 'ETH'; | ||
const symbol = { value: bridge?.symbol, native: nativeSymbol }; | ||
const [pair, setPair] = useState('0'); | ||
const pairIndex = Number(pair); | ||
const address = ftAddresses?.[pairIndex][networkIndex].toString() as HexString | undefined; | ||
const symbol = ftSymbols?.[pairIndex][networkIndex]; | ||
|
||
return { address, options, symbol, pair: { value: pair, set: setPair }, isLoading: tokens.isLoading }; | ||
return { address, options, symbol, nativeSymbol, pair: { value: pair, set: setPair }, isLoading: isPending }; | ||
} | ||
|
||
export { useBridge }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { HexString } from '@gear-js/api'; | ||
import { useApi } from '@gear-js/react-hooks'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { ActorId, H160 } from 'sails-js'; | ||
import { useConfig } from 'wagmi'; | ||
import { readContract } from 'wagmi/actions'; | ||
|
||
import { VftProgram, FUNGIBLE_TOKEN_ABI } from '../consts'; | ||
|
||
function useFTSymbols(addresses: [ActorId, H160][] | undefined) { | ||
const { api, isApiReady } = useApi(); | ||
const wagmiConfig = useConfig(); | ||
|
||
const readVaraSymbol = (address: HexString) => { | ||
if (!api) throw new Error('Api is not initialized'); | ||
|
||
return new VftProgram(api, address).vft.symbol(); | ||
}; | ||
|
||
const readEthSymbol = (address: HexString) => | ||
readContract(wagmiConfig, { address, abi: FUNGIBLE_TOKEN_ABI, functionName: 'symbol' }); | ||
|
||
const readSymbols = () => { | ||
if (!addresses) throw new Error('Fungible token addresses are not found'); | ||
|
||
return Promise.all( | ||
addresses.map((pair) => | ||
Promise.all([readVaraSymbol(pair[0].toString() as HexString), readEthSymbol(pair[1].toString() as HexString)]), | ||
), | ||
); | ||
}; | ||
|
||
return useQuery({ | ||
queryKey: ['ftSymbols', addresses], | ||
queryFn: readSymbols, | ||
enabled: isApiReady && Boolean(addresses), | ||
}); | ||
} | ||
|
||
export { useFTSymbols }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { useFTAddresses } from './use-ft-addresses'; | ||
import { useFungibleTokenBalance } from './use-fungible-token-balance'; | ||
import { useHandleVaraSubmit } from './use-handle-vara-submit'; | ||
import { useVaraConfig } from './use-vara-config'; | ||
|
||
export { useVaraConfig, useFungibleTokenBalance, useHandleVaraSubmit }; | ||
export { useVaraConfig, useFungibleTokenBalance, useHandleVaraSubmit, useFTAddresses }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { HexString } from '@gear-js/api'; | ||
import { useProgram, useProgramQuery } from '@gear-js/react-hooks'; | ||
|
||
import { | ||
BridgingPaymentProgram, | ||
BRIDGING_PAYMENT_CONTRACT_ADDRESS, | ||
VftGatewayProgram, | ||
SERVICE_NAME, | ||
QUERY_NAME, | ||
} from '../../consts'; | ||
|
||
function useFTAddresses() { | ||
const { data: program } = useProgram({ | ||
library: BridgingPaymentProgram, | ||
id: BRIDGING_PAYMENT_CONTRACT_ADDRESS, | ||
}); | ||
|
||
const { data: vftGatewayAddress } = useProgramQuery({ | ||
program, | ||
serviceName: SERVICE_NAME.BRIDGING_PAYMENT, | ||
functionName: QUERY_NAME.VFT_GATEWAY_ADDRESS, | ||
args: [], | ||
}); | ||
|
||
const { data: vftGatewayProgram } = useProgram({ | ||
library: VftGatewayProgram, | ||
id: vftGatewayAddress?.toString() as HexString, | ||
}); | ||
|
||
return useProgramQuery({ | ||
program: vftGatewayProgram, | ||
serviceName: SERVICE_NAME.VFT_GATEWAY, | ||
functionName: QUERY_NAME.FT_ADDRESSES, | ||
args: [], | ||
}); | ||
} | ||
|
||
export { useFTAddresses }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters