diff --git a/package.json b/package.json index 6c6aebca9..0854c7c99 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "@oraichain/kawaiiverse-txs": "^0.0.3", "@oraichain/orai-bitcoin": "2.0.0", "@oraichain/oraidex-common": "2.0.6", - "@oraichain/orai-token-inspector": "^0.2.8", + "@oraichain/orai-token-inspector": "^0.2.11", "@oraichain/oraidex-common-ui": "1.0.11", "@oraichain/oraidex-contracts-sdk": "1.0.55", "@oraichain/oraidex-universal-swap": "1.3.1", diff --git a/src/hooks/useLoadTokens.ts b/src/hooks/useLoadTokens.ts index 272d684c9..dc014ae98 100644 --- a/src/hooks/useLoadTokens.ts +++ b/src/hooks/useLoadTokens.ts @@ -385,6 +385,7 @@ async function loadSolEntries( programId: new PublicKey('TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA') }); + const nativeAmountToken = await loadNativeSolAmounts(chain.rpc, walletPublicKey); const storage = store.getState(); const allSolTokens = (storage.token.allOtherChainTokens || []).filter((t) => t.chainId === chain.chainId); let entries: [string, string][] = allSolTokens.map((item) => { @@ -395,6 +396,7 @@ async function loadSolEntries( ); if (findAmount) amount = findAmount.account.data.parsed.info.tokenAmount.amount; } + if (!item?.contractAddress) return [item.denom, nativeAmountToken]; return [item.denom, amount]; }); return entries; @@ -429,6 +431,22 @@ async function loadBtcAmounts(dispatch: Dispatch, btcAddress: string, chains: Cu async function loadSolAmounts(dispatch: Dispatch, solAddress: string, chains: CustomChainInfo[]) { try { + const chainss = chains.map((c) => { + return { + ...c, + currencies: [ + ...c.currencies, + { + coinDecimals: 9, + coinDenom: 'SOL', + coinGeckoId: 'solana', + coinImageUrl: 'https://assets.coingecko.com/coins/images/4128/standard/solana.png?1718769756', + coinMinimalDenom: 'sol' + } + ] + }; + }); + const amountDetails = Object.fromEntries( flatten(await Promise.all(chains.map((chain) => loadSolEntries(solAddress, chain)))) ); @@ -439,6 +457,17 @@ async function loadSolAmounts(dispatch: Dispatch, solAddress: string, chains: Cu } } +async function loadNativeSolAmounts(rpc, walletPublicKey) { + try { + const connection = new Connection(rpc); + const balance = await connection.getBalance(walletPublicKey); + return balance.toString(); + } catch (error) { + console.log('error: loadNativeSolAmounts', error); + return '0'; + } +} + const loadBalanceByToken = async (dispatch: Dispatch, addressTon: string, addressToken?: string) => { try { // get the decentralized RPC endpoint diff --git a/src/pages/Balance/index.tsx b/src/pages/Balance/index.tsx index 167acd098..7a798ace6 100644 --- a/src/pages/Balance/index.tsx +++ b/src/pages/Balance/index.tsx @@ -595,7 +595,15 @@ const Balance: React.FC = () => { const receiverAddress = ORAICHAIN_RELAYER_ADDRESS; const listNotCheckBalanceOraichainToSol = [ORAI]; - if (!listNotCheckBalanceOraichainToSol.includes(fromToken.denom)) { + + if (!fromToken.contractAddress && transferAmount < 0.01) { + return displayToast(TToastType.TX_FAILED, { + message: 'minimum bridge of solana native token is 0.01!' + }); + } + + const toTokenIsSolanaNative = !toToken?.contractAddress; + if (!toTokenIsSolanaNative && !listNotCheckBalanceOraichainToSol.includes(fromToken.denom)) { const web3Solana = new Web3SolanaProgramInteraction(); const bridgeBalance = fromToken.contractAddress === NATIVE_MINT.toBase58() diff --git a/src/program/web3.ts b/src/program/web3.ts index 69c05c10a..c4415e355 100644 --- a/src/program/web3.ts +++ b/src/program/web3.ts @@ -5,7 +5,8 @@ import { PublicKey, Transaction, TransactionExpiredTimeoutError, - TransactionInstruction + TransactionInstruction, + SystemProgram } from '@solana/web3.js'; import { createAssociatedTokenAccountInstruction, @@ -45,53 +46,74 @@ export class Web3SolanaProgramInteraction { oraiReceiverAddress: string ) => { try { - const mintPubkey = new PublicKey(token.contractAddress); - const relayerPubkey = new PublicKey(SOL_RELAYER_ADDRESS); - - const walletTokenAccount = getAssociatedTokenAddressSync(mintPubkey, wallet.publicKey); - const relayerTokenAccount = getAssociatedTokenAddressSync(mintPubkey, relayerPubkey); - console.log(relayerTokenAccount.toBase58(), mintPubkey.toBase58(), relayerPubkey.toBase58()); - const accountInfo = await this.connection.getAccountInfo(relayerTokenAccount); - console.log('----accountInfo----', accountInfo); - const lamports = accountInfo?.lamports || 0; - // check the connection - if (!wallet.publicKey || !this.connection) { - throw new Error('Warning: Wallet not connected'); - } + let transaction = new Transaction(); + if (token.contractAddress) { + const mintPubkey = new PublicKey(token.contractAddress); + const relayerPubkey = new PublicKey(SOL_RELAYER_ADDRESS); + + const walletTokenAccount = getAssociatedTokenAddressSync(mintPubkey, wallet.publicKey); + const relayerTokenAccount = getAssociatedTokenAddressSync(mintPubkey, relayerPubkey); + const accountInfo = await this.connection.getAccountInfo(relayerTokenAccount); + console.log('----accountInfo----', accountInfo); + const lamports = accountInfo?.lamports || 0; + // check the connection + if (!wallet.publicKey || !this.connection) { + throw new Error('Warning: Wallet not connected'); + } - const parsedAmount = toAmount(tokenAmountRaw, token.decimals); + const parsedAmount = toAmount(tokenAmountRaw, token.decimals); - let transaction = new Transaction(); - const updateCpIx = ComputeBudgetProgram.setComputeUnitPrice({ - microLamports: 1_000_000 - }); - const updateCuIx = ComputeBudgetProgram.setComputeUnitLimit({ - units: 500_000 - }); - transaction.add(updateCpIx, updateCuIx); + const updateCpIx = ComputeBudgetProgram.setComputeUnitPrice({ + microLamports: 1_000_000 + }); + const updateCuIx = ComputeBudgetProgram.setComputeUnitLimit({ + units: 500_000 + }); + transaction.add(updateCpIx, updateCuIx); + + if (lamports == 0) { + transaction.add( + createAssociatedTokenAccountInstruction(wallet.publicKey, relayerTokenAccount, relayerPubkey, mintPubkey) + ); + } - if (lamports == 0) { transaction.add( - createAssociatedTokenAccountInstruction(wallet.publicKey, relayerTokenAccount, relayerPubkey, mintPubkey) + createTransferCheckedInstruction( + walletTokenAccount, + mintPubkey, + relayerTokenAccount, + wallet.publicKey, + parsedAmount, + token.decimals + ) + ); + + transaction.add( + new TransactionInstruction({ + keys: [{ pubkey: wallet.publicKey, isSigner: true, isWritable: true }], + data: Buffer.from(oraiReceiverAddress, 'utf-8'), + programId: new PublicKey(MEMO_PROGRAM_ID) + }) + ); + } else { + const lamportsToSend = 0.01 * LAMPORTS_PER_SOL; + const transferTransaction = transaction.add( + SystemProgram.transfer({ + fromPubkey: wallet.publicKey, + toPubkey: new PublicKey(SOL_RELAYER_ADDRESS), + lamports: lamportsToSend + }) + ); + + transferTransaction.add( + new TransactionInstruction({ + keys: [{ pubkey: wallet.publicKey, isSigner: true, isWritable: true }], + data: Buffer.from(oraiReceiverAddress, 'utf-8'), + programId: new PublicKey(MEMO_PROGRAM_ID) + }) ); } - transaction.add( - createTransferCheckedInstruction( - walletTokenAccount, - mintPubkey, - relayerTokenAccount, - wallet.publicKey, - parsedAmount, - token.decimals - ) - ); - transaction.add( - new TransactionInstruction({ - keys: [{ pubkey: wallet.publicKey, isSigner: true, isWritable: true }], - data: Buffer.from(oraiReceiverAddress, 'utf-8'), - programId: new PublicKey(MEMO_PROGRAM_ID) - }) - ); + transaction.feePayer = wallet.publicKey; const blockhash = await this.connection.getLatestBlockhash(); transaction.recentBlockhash = blockhash.blockhash; diff --git a/yarn.lock b/yarn.lock index 535cc6805..347c0c073 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3924,10 +3924,10 @@ serialize-error "^8.1.0" varuint-bitcoin "^1.1.2" -"@oraichain/orai-token-inspector@^0.2.8": - version "0.2.8" - resolved "https://registry.yarnpkg.com/@oraichain/orai-token-inspector/-/orai-token-inspector-0.2.8.tgz#98165d55c99ed3d891c966a49f7d0ab8edb8e135" - integrity sha512-CBTebtuJbjadDMrpqO0iwaXcq3RWWWpmRWNuwfHx2AWI74TbgUScri+k9tB2t7YQoIPtYjGidh5pnsESV5ImMg== +"@oraichain/orai-token-inspector@^0.2.11": + version "0.2.11" + resolved "https://registry.yarnpkg.com/@oraichain/orai-token-inspector/-/orai-token-inspector-0.2.11.tgz#6e8021e05110a56358c74828ba6ecfdce26b821a" + integrity sha512-Dt6l+0WIuPrW8/IVcCQwJiSRqyI7tAUsAxnysmMDcvjbsc3N10g3dTMOKmJLPvDt+s5mLtvGO4X1Zb3B+9vCXQ== dependencies: "@keplr-wallet/types" "0.12.141" "@metaplex-foundation/mpl-token-metadata" "^3.3.0"