import React, { useState } from 'react'
import { Button, Text, TextInput, View } from 'react-native'
import { usePortal } from '@portal-hq/core'
const SolanaSendTx = () => {
const portal = usePortal()
const [toAddress, setToAddress] = useState('')
const [txHash, setTxHash] = useState('')
const handleSend = async () => {
try {
const chainId = 'solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1' // Solana Devnet
// Build the transaction using the simplified API
const { transaction } = await portal.api.buildTransaction(
toAddress, // recipient address
'NATIVE', // token type (native SOL)
'0.001', // amount in SOL
chainId // CAIP-2 Chain, or friendly name like "solana-devnet"
)
// Send the transaction
const newTxHash = await portal.request({
method: 'sol_signAndSendTransaction',
params: [transaction], // transaction is already base64 encoded
chainId,
})
setTxHash(newTxHash)
setToAddress('')
} catch (error) {
console.error('Failed to send transaction:', error)
}
}
return (
<View>
<Text>Send 0.001 SOL</Text>
<TextInput
onChangeText={setToAddress}
placeholder="Enter recipient address"
value={toAddress}
/>
<View style={{ marginTop: 10 }}>
<Button
disabled={!portal || !toAddress || !toAddress.length}
onPress={handleSend}
title="Send SOL"
/>
</View>
{txHash?.length > 0 && (
<View>
<Text>Successfully sent transaction!</Text>
<Text>Transaction Hash: {txHash}</Text>
</View>
)}
</View>
)
}
export default SolanaSendTx