import { TokenPrice, MsgSendToEth, TokenStaticFactory, MsgBroadcasterWithPk } from '@injectivelabs/sdk-ts'
import { BigNumberInBase } from '@injectivelabs/utils'
import { ChainId } from '@injectivelabs/ts-types'
import { getNetworkEndpoints, Network } from '@injectivelabs/networks'
// refer to https://github.com/InjectiveLabs/injective-lists
import { tokens } from '../data/tokens.json'
export const tokenStaticFactory = new TokenStaticFactory(tokens as TokenStatic[])
const tokenPriceMap = new TokenPrice(Network.Mainnet)
const tokenService = new TokenService({
chainId: ChainId.Mainnet,
network: Network.Mainnet,
})
const ETH_BRIDGE_FEE_IN_USD = 10
const endpointsForNetwork = getNetworkEndpoints(Network.Mainnet)
const tokenSymbol = 'INJ'
const tokenMeta = tokenStaticFactory.toToken(tokenSymbol)
const amount = 1
const injectiveAddress = 'inj1...'
const destinationAddress = '0x...' // ethereum address
const tokenDenom = `peggy${tokenMeta.erc20.address}`
if (!tokenMeta) {
return
}
const tokenUsdPrice = tokenPriceMap[tokenMeta.coinGeckoId]
const amountToFixed = new BigNumberInBase(amount).toWei(tokenMeta.decimals).toFixed()
const bridgeFeeInToken = new BigNumberInBase(ETH_BRIDGE_FEE_IN_USD).dividedBy(tokenUsdPrice).toFixed()
const msg = MsgSendToEth.fromJSON({
injectiveAddress,
address: destinationAddress,
amount: {
denom: tokenDenom,
amount: amountToFixed,
},
bridgeFee: {
denom: tokenDenom,
amount: bridgeFeeInToken,
},
})
const txHash = await new MsgBroadcasterWithPk({
privateKey,
network: Network.Mainnet,
}).broadcast({
msgs: msg,
})