Skip to content

Commit 2095da3

Browse files
feat: add input boxes (#1515)
* feat: add send usd app * fix: cargo tomls * fix: pre-commit * fix: improve code quality * fix: fix names and texts * fix: pre-commit * feat: add send usd example to monorepo * fix: connection endpoint for send usd example * fix: priceUpdateData * fix: packages * fix: remove unused variables * fix: packages * fix: test * fix: tests * fix: test * remove file * fix * go * Try removing test script * Remove npm run test from the text * Add input box * is this the same * pre-commit --------- Co-authored-by: keyvan <[email protected]>
1 parent a8dbabc commit 2095da3

File tree

1 file changed

+72
-19
lines changed
  • target_chains/solana/examples/send_usd/app/src

1 file changed

+72
-19
lines changed

target_chains/solana/examples/send_usd/app/src/App.tsx

Lines changed: 72 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
ConnectionProvider,
66
WalletProvider,
77
useAnchorWallet,
8+
useConnection,
89
} from "@solana/wallet-adapter-react";
910
import {
1011
WalletDisconnectButton,
@@ -20,20 +21,31 @@ import * as buffer from "buffer";
2021
import { AnchorProvider, BN, Program, Wallet } from "@coral-xyz/anchor";
2122
import { SendUSDApp, IDL } from "./idl/send_usd_app";
2223
import { PriceServiceConnection } from "@pythnetwork/price-service-client";
24+
import { useState } from "react";
2325
window.Buffer = buffer.Buffer;
2426

2527
require("@solana/wallet-adapter-react-ui/styles.css");
2628

2729
const SOL_PRICE_FEED_ID =
2830
"0xef0d8b6fda2ceba41da15d4095d1da392a0d2f8ed0c6c7bc0f4cfac8c280b56d";
29-
async function postPriceUpdate(connection: Connection, wallet?: AnchorWallet) {
30-
if (wallet === undefined) {
31+
const SEND_USD_PROGRAM_ID: PublicKey = new PublicKey(
32+
"2e5gZD3suxgJgkCg4pkoogxDKszy1SAwokz8mNeZUj4M"
33+
);
34+
const HERMES_URL = "https://hermes.pyth.network/";
35+
const DEVNET_RPC_URL = "https://api.devnet.solana.com";
36+
37+
async function postPriceUpdate(
38+
connection: Connection,
39+
wallet: AnchorWallet | undefined,
40+
destination: PublicKey | undefined,
41+
amount: number | undefined
42+
) {
43+
if (!(wallet && destination && amount)) {
3144
return;
3245
} else {
33-
const priceServiceConnection = new PriceServiceConnection(
34-
"https://hermes.pyth.network/",
35-
{ priceFeedRequestConfig: { binary: true } }
36-
);
46+
const priceServiceConnection = new PriceServiceConnection(HERMES_URL, {
47+
priceFeedRequestConfig: { binary: true },
48+
});
3749
const pythSolanaReceiver = new PythSolanaReceiver({
3850
connection,
3951
wallet: wallet as Wallet,
@@ -45,12 +57,12 @@ async function postPriceUpdate(connection: Connection, wallet?: AnchorWallet) {
4557

4658
const sendUsdApp = new Program<SendUSDApp>(
4759
IDL as SendUSDApp,
48-
new PublicKey("2e5gZD3suxgJgkCg4pkoogxDKszy1SAwokz8mNeZUj4M"),
49-
new AnchorProvider(connection, wallet, { commitment: "processed" })
60+
SEND_USD_PROGRAM_ID,
61+
new AnchorProvider(connection, wallet, AnchorProvider.defaultOptions())
5062
);
5163

5264
const transactionBuilder = pythSolanaReceiver.newTransactionBuilder({
53-
closeUpdateAccounts: false,
65+
closeUpdateAccounts: true,
5466
});
5567
await transactionBuilder.addPostPriceUpdates([priceUpdateData[0]]);
5668

@@ -61,11 +73,9 @@ async function postPriceUpdate(connection: Connection, wallet?: AnchorWallet) {
6173
return [
6274
{
6375
instruction: await sendUsdApp.methods
64-
.send(new BN(1))
76+
.send(new BN(amount))
6577
.accounts({
66-
destination: new PublicKey(
67-
"BTwXQZS3EzfxBkv2A54estmn9YbmcpmRWeFP4f3avLi4"
68-
),
78+
destination,
6979
priceUpdate: getPriceUpdateAccount(SOL_PRICE_FEED_ID),
7080
})
7181
.instruction(),
@@ -84,14 +94,22 @@ async function postPriceUpdate(connection: Connection, wallet?: AnchorWallet) {
8494
}
8595
}
8696

87-
function Button() {
88-
const connection = new Connection("https://api.devnet.solana.com");
97+
function Button(props: {
98+
destination: PublicKey | undefined;
99+
amount: number | undefined;
100+
}) {
101+
const connectionContext = useConnection();
89102
const wallet = useAnchorWallet();
90103

91104
return (
92105
<button
93106
onClick={async () => {
94-
await postPriceUpdate(connection, wallet);
107+
await postPriceUpdate(
108+
connectionContext.connection,
109+
wallet,
110+
props.destination,
111+
props.amount
112+
);
95113
}}
96114
>
97115
Send
@@ -100,17 +118,52 @@ function Button() {
100118
}
101119

102120
function App() {
121+
const [destination, setDestination] = useState<PublicKey>();
122+
const [amount, setAmount] = useState<number>();
123+
124+
const handleSetDestination = (event: any) => {
125+
try {
126+
setDestination(new PublicKey(event.target.value));
127+
} catch (e) {
128+
setDestination(undefined);
129+
}
130+
};
131+
const handleSetAmount = (event: any) => {
132+
try {
133+
setAmount(parseInt(event.target.value));
134+
} catch (e) {
135+
setAmount(undefined);
136+
}
137+
};
138+
103139
return (
104-
<ConnectionProvider endpoint={"https://api.devnet.solana.com"}>
140+
<ConnectionProvider endpoint={DEVNET_RPC_URL}>
105141
<WalletProvider wallets={[]} autoConnect>
106142
<WalletModalProvider>
107143
<div className="App">
108144
<header className="App-header">
109145
<img src={logo} className="App-logo" alt="logo" />
110146
<WalletMultiButton />
111147
<WalletDisconnectButton />
112-
<p>Click to send a 1 usd to the hard-coded account</p>
113-
<Button />
148+
<p>Click to send the amount of USD in SOL</p>
149+
<p style={{ fontSize: "16px" }}>
150+
Destination (paste a Solana public key)
151+
</p>
152+
<input
153+
type="text"
154+
value={destination ? destination.toString() : ""}
155+
onChange={handleSetDestination}
156+
style={{ width: "100%", height: "40px", fontSize: "16px" }}
157+
/>
158+
<p style={{ fontSize: "16px" }}>Amount (USD)</p>
159+
<input
160+
type="text"
161+
value={amount ? amount.toString() : ""}
162+
onChange={handleSetAmount}
163+
style={{ width: "100%", height: "40px", fontSize: "16px" }}
164+
/>
165+
166+
<Button destination={destination} amount={amount} />
114167
</header>
115168
</div>
116169
</WalletModalProvider>

0 commit comments

Comments
 (0)