-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidate.tsx
More file actions
125 lines (99 loc) · 3.67 KB
/
Copy pathValidate.tsx
File metadata and controls
125 lines (99 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import {
Connection,
PublicKey,
Transaction,
clusterApiUrl,
} from "@solana/web3.js"
import {
AnchorProvider,
setProvider,
} from "@project-serum/anchor"
import styles from '../styles/PingButton.module.css'
import idl from "../xfluencer.json"
import { useAnchorWallet, useConnection, useWallet } from '@solana/wallet-adapter-react';
import * as anchor from "@coral-xyz/anchor";
import { FC } from "react";
import { utf8 } from "@coral-xyz/anchor/dist/cjs/utils/bytes";
import * as utils from "./utils";
import { SendTransactionOptions } from "@solana/wallet-adapter-base";
const programId = new PublicKey(idl.metadata.address);
interface ValidateProps {
validator: string,
business: string,
influencer: string,
percentageFee : number,
orderCode: number,
targetState: number,
textButton: string,
network: string
}
export const Validate: FC<ValidateProps> = ({validator,
business,
influencer,
percentageFee,
orderCode,
targetState,
textButton,
network}) => {
const wallet = useAnchorWallet()
const connection = new Connection(network,
{
commitment: "confirmed",
confirmTransactionInitialTimeout: 30000
});
const program = utils.getAnchorProgram(connection);
const provider = new AnchorProvider(connection, wallet, {})
setProvider(provider)
const { publicKey, signTransaction, sendTransaction } = useWallet()
if (!connection || !publicKey) {
const msg = "Wallet is not connected"
console.warn(msg)
return (
<div className={styles.buttonContainer}>
<button className={styles.button} disabled>{textButton} ({msg})</button>
</div>
)
} else {
console.log("wallet",wallet.publicKey.toString())
}
const onClick = async () => {
if (!connection || !publicKey) {
console.warn("Wallet is not connected")
return }
const validatorPublicKey = new PublicKey(validator);
const businessPublicKey = new PublicKey(business);
const influencerPublicKey = new PublicKey(influencer);
const [escrowPDA] = await PublicKey.findProgramAddress([
utf8.encode('escrow'),
businessPublicKey.toBuffer(),
influencerPublicKey.toBuffer(),
utf8.encode(orderCode.toString())
],
programId
);
const ix = await program.methods.validateEscrowSol(
new anchor.BN(targetState),
new anchor.BN(percentageFee)
).accounts({
validationAuthority: validatorPublicKey,
influencer: influencerPublicKey,
business: businessPublicKey,
escrowAccount: escrowPDA,
systemProgram: anchor.web3.SystemProgram.programId,
}).instruction();
const tx = new Transaction().add(ix);
console.log(tx);
const options: SendTransactionOptions = {
skipPreflight: false,
maxRetries: 1,
preflightCommitment: 'confirmed'
};
const signature = await sendTransaction(tx, connection, options);
await utils.confirmTransactionSignature(signature, connection);
}
return (
<button className={styles.button} onClick={onClick}>
{textButton} | Validator == {validator}
</button>
)
}