-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.js
More file actions
84 lines (70 loc) · 2.23 KB
/
program.js
File metadata and controls
84 lines (70 loc) · 2.23 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
const {
Connection,
PublicKey,
SystemProgram,
Transaction,
TransactionInstruction,
Keypair,
LAMPORTS_PER_SOL
} = require('@solana/web3.js');
const anchor = require('@coral-xyz/anchor');
class MultisigProgram {
constructor(connection, programId) {
this.connection = connection;
this.programId = new PublicKey(programId);
this.feePercentage = 1; // 1% default fee
}
async initialize(adminKey) {
const multisigAccount = Keypair.generate();
const createAccountIx = SystemProgram.createAccount({
fromPubkey: adminKey.publicKey,
newAccountPubkey: multisigAccount.publicKey,
lamports: await this.connection.getMinimumBalanceForRentExemption(1000),
space: 1000,
programId: this.programId
});
const tx = new Transaction().add(createAccountIx);
return { transaction: tx, multisigAccount };
}
async setFeePercentage(newFeePercentage, adminKey) {
if (newFeePercentage < 0 || newFeePercentage > 100) {
throw new Error('Invalid fee percentage');
}
const data = Buffer.from([1, newFeePercentage]); // 1 = set fee instruction
const instruction = new TransactionInstruction({
keys: [
{ pubkey: adminKey.publicKey, isSigner: true, isWritable: true }
],
programId: this.programId,
data: data,
});
const tx = new Transaction().add(instruction);
this.feePercentage = newFeePercentage;
return tx;
}
calculateFee(amount) {
return Math.floor((amount * this.feePercentage) / 100);
}
async proposeTransaction(fromPubkey, toPubkey, amount) {
const fee = this.calculateFee(amount);
const totalAmount = amount + fee;
const transaction = new Transaction().add(
SystemProgram.transfer({
fromPubkey: new PublicKey(fromPubkey),
toPubkey: new PublicKey(toPubkey),
lamports: totalAmount,
})
);
return { transaction, fee };
}
async approveTransaction(transaction, signerKey) {
transaction.partialSign(signerKey);
return transaction;
}
async executeTransaction(transaction) {
const signature = await this.connection.sendTransaction(transaction);
await this.connection.confirmTransaction(signature);
return signature;
}
}
module.exports = MultisigProgram;