If we want to execute a raw transaction directly on chain, Okto provides a way to use one of the API's to send a transaction to the blockchain and execute it for the user.
Send a transaction
Create a new screen and name it send-raw-transaction
:
React Native
For example purposes we will execute a Polygon transaction.
send-raw-transaction.tsx
import { useOkto, type OktoContextType} from 'okto-sdk-react-native';
import { useState } from 'react';
const SendRawTransactionScreen = () => {
const { executeRawTransaction } = useOkto() as OktoContextType;
const [network, setNetwork] = useState<string>("POLYGON_TESTNET");
const [from, setFrom] = useState<string>("0x0342A54DD44E8744FD185579Af57845Cb0ac6cB0");
const [to, setTo] = useState<string>("0x80322ea18633A1f713e987d65Ae78AcCDAB6e55e");
const [data, setData] = useState<string>("0x");
const [value, setValue] = useState<string>("0x100000");
const sendTransaction = async () => {
const requestData = {
network_name: network,
transaction: {
from,
to,
data,
value,
},
}
executeRawTransaction(requestData)
.then((result) => {
console.log(result.jobId)
})
.catch((error) => {
console.error(`error:`, error);
});
}
}
Add Form Fields
Add the form fields to the send-raw-transaction.tsx
screen.
send-raw-transaction.tsx
import { View, TextInput, Button, Text } from "react-native";
import React, { useState } from 'react';
const SendRawTransactionScreen = () => {
const [network, setNetwork] = useState<string>("POLYGON_TESTNET");
const [from, setFrom] = useState<string>("0x0342A54DD44E8744FD185579Af57845Cb0ac6cB0");
const [to, setTo] = useState<string>("0x80322ea18633A1f713e987d65Ae78AcCDAB6e55e");
const [data, setData] = useState<string>("0x");
const [value, setValue] = useState<string>("0x100000");
const sendTransaction = async () => {
// Add the sendTransaction logic here
};
return (
<View>
<Text>Execute Raw Polygon Transaction</Text>
<TextInput
placeholder="From"
value={from}
onChangeText={setFrom}
/>
<TextInput
placeholder="To"
value={to}
onChangeText={setTo}
/>
<TextInput
placeholder="Data"
value={data}
onChangeText={setData}
/>
<TextInput
placeholder="Value"
value={value}
onChangeText={setValue}
/>
<Button title="Send Transaction" onPress={sendTransaction} />
</View>
)
}
With that you have successfully set up a way for users to execute a raw transaction to the blockchain. You can customize the transaction
object depending on the network you want to send the transaction to.