Skip to content

Commit

Permalink
support sol bridge native
Browse files Browse the repository at this point in the history
  • Loading branch information
haunv3 committed Jan 17, 2025
1 parent 8b0dde0 commit 404c938
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 48 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
29 changes: 29 additions & 0 deletions src/hooks/useLoadTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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;
Expand Down Expand Up @@ -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))))
);
Expand All @@ -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
Expand Down
10 changes: 9 additions & 1 deletion src/pages/Balance/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,15 @@ const Balance: React.FC<BalanceProps> = () => {

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()
Expand Down
106 changes: 64 additions & 42 deletions src/program/web3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
PublicKey,
Transaction,
TransactionExpiredTimeoutError,
TransactionInstruction
TransactionInstruction,
SystemProgram
} from '@solana/web3.js';
import {
createAssociatedTokenAccountInstruction,
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 404c938

Please sign in to comment.