@@ -22,6 +22,15 @@ import { toDecimals, truncateString } from "../../../utils/string";
2222import variables from "../../../utils/variables" ;
2323import "./index.scss" ;
2424
25+ import { createTxRaw } from "@tharsis/proto" ;
26+ import { generateEndpointAccount } from "@tharsis/provider" ;
27+ import {
28+ generateEndpointBroadcast ,
29+ generatePostBodyBroadcast
30+ } from "@tharsis/provider/dist/rest/broadcast" ;
31+ import { createTxIBCMsgTransfer } from "@tharsis/transactions" ;
32+ import Long from "long" ;
33+
2534const Deposit = ( { lang, chain, address, handleRefresh, balances, assetMap } ) => {
2635 const [ isModalOpen , setIsModalOpen ] = useState ( false ) ;
2736 const [ sourceAddress , setSourceAddress ] = useState ( "" ) ;
@@ -84,25 +93,131 @@ const Deposit = ({ lang, chain, address, handleRefresh, balances, assetMap }) =>
8493 setIsModalOpen ( true ) ;
8594 } ;
8695
87- const signIBCTx = ( ) => {
96+
97+ const handleEvmIBC = async ( ) => {
8898 setInProgress ( true ) ;
8999
100+ try {
101+ const timeout = Math . floor ( new Date ( ) . getTime ( ) / 1000 ) + 600 ;
102+ const timeoutTimestampNanoseconds =
103+ Long . fromNumber ( timeout ) . multiply ( 1_000_000_000 ) ;
104+
105+ const ibcMsg = {
106+ receiver : address ,
107+ sender : sourceAddress ,
108+ sourceChannel : chain . destChannelId ,
109+ sourcePort : "transfer" ,
110+ timeoutTimestamp : String ( timeoutTimestampNanoseconds ) ,
111+ amount : getAmount ( amount , assetMap [ chain ?. ibcDenomHash ] ?. decimals ) ,
112+ denom : chain ?. coinMinimalDenom ,
113+ revisionNumber : Number ( proofHeight . revision_number ) ,
114+ revisionHeight : Number ( proofHeight . revision_height ) + 100 ,
115+ } ;
116+ const chainInfoForMsg = {
117+ chainId : comdex . chainId || 0 ,
118+ cosmosChainId : chain . chainInfo ?. chainId ,
119+ } ;
120+
121+ let accountResponse = await fetch (
122+ `${ chain . chainInfo ?. rest } ${ generateEndpointAccount ( sourceAddress ) } `
123+ ) ;
124+ let accountResult = await accountResponse . json ( ) ;
125+
126+ const sender = {
127+ accountAddress : accountResult . account . base_account . address ,
128+ sequence : accountResult . account . base_account . sequence ,
129+ accountNumber : accountResult . account . base_account . account_number ,
130+ pubkey : accountResult . account . base_account . pub_key ?. key || "" ,
131+ } ;
132+
133+ const fee = {
134+ amount : "20" ,
135+ denom : chain . chainInfo ?. coinMinimalDenom ,
136+ gas : "200000" ,
137+ } ;
138+
139+ const transferMsg = createTxIBCMsgTransfer (
140+ chainInfoForMsg ,
141+ sender ,
142+ fee ,
143+ "ibc_transfer" ,
144+ ibcMsg
145+ ) ;
146+
147+ const sign = await window ?. keplr ?. signDirect (
148+ chain . chainInfo ?. chainId ,
149+ sourceAddress ,
150+ {
151+ bodyBytes : transferMsg . signDirect . body . serializeBinary ( ) ,
152+ authInfoBytes : transferMsg . signDirect . authInfo . serializeBinary ( ) ,
153+ chainId : chainInfoForMsg . cosmosChainId ,
154+ accountNumber : new Long ( sender . accountNumber ) ,
155+ } ,
156+ { isEthereum : true }
157+ ) ;
158+
159+ if ( sign !== undefined ) {
160+ let rawTx = createTxRaw (
161+ sign . signed . bodyBytes ,
162+ sign . signed . authInfoBytes ,
163+ [ new Uint8Array ( Buffer . from ( sign . signature . signature , "base64" ) ) ]
164+ ) ;
165+
166+ // Broadcast it
167+ const postOptions = {
168+ method : "POST" ,
169+ headers : { "Content-Type" : "application/json" } ,
170+ body : generatePostBodyBroadcast ( rawTx ) ,
171+ } ;
172+ try {
173+ let broadcastPost = await fetch (
174+ `${ chain . chainInfo ?. rest } /${ generateEndpointBroadcast ( ) } ` ,
175+ postOptions
176+ ) ;
177+ let response = await broadcastPost . json ( ) ;
178+
179+ if ( response . tx_response ?. txhash ) {
180+ message . loading (
181+ "Transaction Broadcasting, Waiting for transaction to be included in the block"
182+ ) ;
183+
184+ handleHash ( response . tx_response ?. txhash ) ;
185+ }
186+ } catch ( e ) {
187+ resetValues ( ) ;
188+ return ;
189+ }
190+ }
191+ } catch ( e ) {
192+ resetValues ( ) ;
193+ return ;
194+ }
195+ } ;
196+
197+ const signIBCTx = ( ) => {
90198 if ( ! proofHeight ?. revision_height ) {
91199 message . error ( "Unable to get the latest block height" ) ;
92- setInProgress ( false ) ;
93200 return ;
94201 }
202+
203+ if ( chain ?. chainInfo ?. features ?. includes ( "eth-address-gen" ) ) {
204+ // handle evm based token deposits
205+ return handleEvmIBC ( ) ;
206+ }
207+
208+ setInProgress ( true ) ;
209+
95210 const data = {
96211 msg : {
97212 typeUrl : "/ibc.applications.transfer.v1.MsgTransfer" ,
98213 value : {
99214 source_port : "transfer" ,
100215 source_channel : chain . destChannelId ,
101216 token : {
102- denom : chain . coinMinimalDenom ,
217+ denom : chain ? .coinMinimalDenom ,
103218 amount : getAmount (
104219 amount ,
105- assetMap [ chain ?. ibcDenomHash ] ?. decimals
220+ assetMap [ chain ?. coinMinimalDenom ] ?. decimals
106221 ) ,
107222 } ,
108223 sender : sourceAddress ,
@@ -120,7 +235,6 @@ const Deposit = ({ lang, chain, address, handleRefresh, balances, assetMap }) =>
120235 } ;
121236
122237 aminoSignIBCTx ( chain . chainInfo , data , ( error , result ) => {
123- setInProgress ( false ) ;
124238 if ( error ) {
125239 if ( result ?. transactionHash ) {
126240 message . error (
@@ -135,7 +249,6 @@ const Deposit = ({ lang, chain, address, handleRefresh, balances, assetMap }) =>
135249 }
136250
137251 resetValues ( ) ;
138-
139252 return ;
140253 }
141254
0 commit comments