-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeploy-program.js
More file actions
152 lines (134 loc) Β· 5.25 KB
/
deploy-program.js
File metadata and controls
152 lines (134 loc) Β· 5.25 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env node
/**
* Deploy RentBy program to devnet using Solana web3.js
* Usage: node deploy-program.js
*/
const fs = require('fs');
const path = require('path');
const {
Connection,
Keypair,
PublicKey,
LAMPORTS_PER_SOL,
SystemProgram,
Transaction,
sendAndConfirmTransaction,
BPF_LOADER_PROGRAM_ID,
BPF_LOADER_UPGRADABLE_PROGRAM_ID,
} = require('@solana/web3.js');
// Configuration
const PROGRAM_ID = new PublicKey('HmRuwkcYtKaCmx1uXycwCrjVcYBH5o9KGNd6ZF3S6Eq3');
const PROGRAM_PATH = path.join(__dirname, 'target/sbpf-solana-solana/release/rentby.so');
const DEPLOYER_KEYPAIR_PATH = '/tmp/deploy-keypair.json';
const RPC_URL = 'https://api.devnet.solana.com';
async function deployProgram() {
console.log('π Deploying RentBy Program to Devnet');
console.log('=====================================\n');
// Load deployer keypair
console.log('π Loading deployer keypair...');
let deployerKeypair;
try {
const secretKey = JSON.parse(fs.readFileSync(DEPLOYER_KEYPAIR_PATH, 'utf8'));
deployerKeypair = Keypair.fromSecretKey(new Uint8Array(secretKey));
console.log('β
Deployer loaded:', deployerKeypair.publicKey.toBase58());
} catch (error) {
console.error('β Failed to load deployer keypair:', error.message);
process.exit(1);
}
// Load program binary
console.log('\nπ¦ Loading program binary...');
let programBuffer;
try {
const programData = fs.readFileSync(PROGRAM_PATH);
programBuffer = Buffer.from(programData);
console.log('β
Program loaded, size:', programBuffer.length, 'bytes');
} catch (error) {
console.error('β Failed to load program:', error.message);
console.error(' Make sure to run: anchor build');
process.exit(1);
}
// Connect to devnet
console.log('\nπ Connecting to devnet...');
const connection = new Connection(RPC_URL, 'confirmed');
console.log('β
Connected to:', RPC_URL);
// Check balance
console.log('\nπ° Checking deployer balance...');
try {
const balance = await connection.getBalance(deployerKeypair.publicKey);
const balanceSol = balance / LAMPORTS_PER_SOL;
console.log('Balance:', balanceSol.toFixed(4), 'SOL');
if (balance < 0.5 * LAMPORTS_PER_SOL) {
console.log('β οΈ Low balance! Requesting airdrop...');
const signature = await connection.requestAirdrop(
deployerKeypair.publicKey,
2 * LAMPORTS_PER_SOL
);
await connection.confirmTransaction(signature);
console.log('β
Airdrop received!');
}
} catch (error) {
console.error('β Failed to check balance:', error.message);
}
// Calculate program size
console.log('\nπ Program information:');
const programSize = programBuffer.length;
const rentExemption = await connection.getMinimumBalanceForRentExemption(programSize);
const rentExemptionSol = rentExemption / LAMPORTS_PER_SOL;
console.log(' Size:', programSize, 'bytes');
console.log(' Rent exemption:', rentExemptionSol.toFixed(4), 'SOL');
// Create program transaction
console.log('\nπ¨ Creating deployment transaction...');
try {
// For programs < 10KB, we can use the upgradable loader
// Create program transaction with proper data layout
const programData = programBuffer;
const programLen = programData.length;
console.log(' Creating program account...');
const programTransaction = new Transaction().add(
SystemProgram.createAccount({
fromPubkey: deployerKeypair.publicKey,
newAccountPubkey: PROGRAM_ID,
lamports: rentExemption,
space: programLen,
programId: BPF_LOADER_PROGRAM_ID,
})
);
// Load program data
const loadTransaction = new Transaction().add(
SystemProgram.transfer({
fromPubkey: deployerKeypair.publicKey,
toPubkey: PROGRAM_ID,
lamports: rentExemption,
})
);
// Simple approach: write program data directly
// Note: This is a simplified deployment. For production, use Anchor CLI
console.log(' Loading program data...');
// Since we can't easily use BPFLoader without the proper instruction data,
// let's inform the user about the limitation
console.error('\nβ Direct program deployment requires Solana CLI or Anchor CLI');
console.error('\nThe web3.js library doesn\'t include the BPFLoader program instructions.');
console.error('To deploy, you need:');
console.error(' 1. Install Solana CLI: https://docs.solana.com/cli/install-solana-cli-tools');
console.error(' 2. Install Anchor CLI: https://www.anchor-lang.com/docs/installation');
console.error(' 3. Run: anchor deploy --provider.cluster devnet');
console.error('\nAlternatively, use a CI/CD pipeline (GitHub Actions) with pre-installed tools.');
console.error('\nProgram binary is ready at:');
console.error(' ' + PROGRAM_PATH);
console.error('\nProgram ID:');
console.error(' ' + PROGRAM_ID.toBase58());
process.exit(1);
} catch (error) {
console.error('\nβ Deployment failed:', error.message);
if (error.logs) {
console.error('\nTransaction logs:');
error.logs.forEach(log => console.error(' ', log));
}
process.exit(1);
}
}
// Run deployment
deployProgram().catch(error => {
console.error('β Unexpected error:', error);
process.exit(1);
});