SHA256
74 lines
4.3 KiB
JavaScript
74 lines
4.3 KiB
JavaScript
#!/usr/bin/env node
|
|
/** Upgrade economy state v1 -> v2 or change the PDA-update fee.
|
|
* Safe default: dry-run. Restricted to localnet/devnet. Never sends without --send.
|
|
*
|
|
* node scripts/registration-v2/update-economy-fee.mjs \
|
|
* --rpc https://api.devnet.solana.com --keypair ~/.config/solana/id.json \
|
|
* --fee-lamports 20000
|
|
*/
|
|
import { readFileSync } from 'node:fs';
|
|
import { createRequire } from 'node:module';
|
|
const require = createRequire(import.meta.url);
|
|
const { Connection, PublicKey, Keypair, Transaction, TransactionInstruction, sendAndConfirmTransaction } = require('@solana/web3.js');
|
|
const PROGRAM_ID = new PublicKey('SHiNEPr1APdAgNBteUyBXcNovaHctpSjUu8oH2ZJdN6');
|
|
const LOADER = new PublicKey('BPFLoaderUpgradeab1e11111111111111111111111');
|
|
const [ECONOMY] = PublicKey.findProgramAddressSync([Buffer.from('shine_users_economy_config')], PROGRAM_ID);
|
|
function argsOf(argv) {
|
|
const o = {};
|
|
for (let i=0;i<argv.length;i++) {
|
|
const s = argv[i];
|
|
if (!s.startsWith('--')) throw Error('Unknown argument: ' + s);
|
|
const k = s.slice(2);
|
|
if (k === 'send') { if (o.send) throw Error('Repeated --send'); o.send = true; continue; }
|
|
if (!argv[i+1] || argv[i+1].startsWith('--') || o[k] !== undefined) throw Error('Missing/repeated --' + k);
|
|
o[k] = argv[++i];
|
|
}
|
|
return o;
|
|
}
|
|
async function main() {
|
|
const a = argsOf(process.argv.slice(2));
|
|
if (!a.rpc || !a.keypair || !/^(0|[1-9]\d*)$/.test(a['fee-lamports'] || ''))
|
|
throw Error('Specify --rpc, --keypair and nonnegative decimal --fee-lamports');
|
|
const fee = BigInt(a['fee-lamports']);
|
|
if (fee > 0xffff_ffff_ffff_ffffn) throw Error('Fee does not fit in u64');
|
|
const url = new URL(a.rpc);
|
|
if (!(['http:', 'https:'].includes(url.protocol) &&
|
|
(['127.0.0.1', 'localhost'].includes(url.hostname) || url.hostname.endsWith('.devnet.solana.com'))))
|
|
throw Error('For safety this script accepts only localhost/devnet RPC');
|
|
const path = a.keypair.replace(/^~\//, process.env.HOME + '/');
|
|
const wallet = Keypair.fromSecretKey(Uint8Array.from(JSON.parse(readFileSync(path, 'utf8'))));
|
|
const conn = new Connection(a.rpc, 'confirmed');
|
|
const executable = await conn.getAccountInfo(PROGRAM_ID, 'confirmed');
|
|
if (!executable?.executable || !executable.owner.equals(LOADER) || executable.data.length < 36 || executable.data.readUInt32LE(0) !== 2)
|
|
throw Error('Program is not an upgradeable shine_users program');
|
|
const programdata = new PublicKey(executable.data.subarray(4, 36));
|
|
const pd = await conn.getAccountInfo(programdata, 'confirmed');
|
|
if (!pd || !pd.owner.equals(LOADER) || pd.data.length < 45 || pd.data.readUInt32LE(0) !== 3 || pd.data[12] !== 1 ||
|
|
!new PublicKey(pd.data.subarray(13, 45)).equals(wallet.publicKey))
|
|
throw Error('Wallet is not current shine_users upgrade authority');
|
|
const economy = await conn.getAccountInfo(ECONOMY, 'confirmed');
|
|
if (!economy || !economy.owner.equals(PROGRAM_ID)) throw Error('Economy PDA is missing');
|
|
const old = economy.data;
|
|
const oldVersion = old[0];
|
|
if (oldVersion !== 1 && oldVersion !== 2) throw Error('Unsupported economy version ' + oldVersion);
|
|
const previousFee = oldVersion === 2 ? old.readBigUInt64LE(1).toString() : '20000 (legacy fallback)';
|
|
const data = Buffer.alloc(9);
|
|
data[0] = 2; // IX_UPDATE_USERS_ECONOMY_CONFIG
|
|
data.writeBigUInt64LE(fee, 1);
|
|
const ix = new TransactionInstruction({programId: PROGRAM_ID, keys: [
|
|
{pubkey: wallet.publicKey, isSigner: true, isWritable: true},
|
|
{pubkey: ECONOMY, isSigner: false, isWritable: true},
|
|
{pubkey: PROGRAM_ID, isSigner: false, isWritable: false},
|
|
{pubkey: programdata, isSigner: false, isWritable: false},
|
|
], data});
|
|
console.log(JSON.stringify({mode: a.send ? 'SEND' : 'DRY RUN', economyPda: ECONOMY.toBase58(),
|
|
previousVersion: oldVersion, previousFeeLamports: previousFee, newFeeLamports: fee.toString()}, null, 2));
|
|
if (!a.send) return;
|
|
const sig = await sendAndConfirmTransaction(conn, new Transaction().add(ix), [wallet], {commitment:'confirmed'});
|
|
const after = await conn.getAccountInfo(ECONOMY, 'confirmed');
|
|
if (!after || after.data[0] !== 2 || after.data.readBigUInt64LE(1) !== fee)
|
|
throw Error('On-chain economy fee update was not confirmed');
|
|
console.log('Updated economy PDA. Solana signature: ' + sig);
|
|
}
|
|
main().catch(e => { console.error('Error:', e.message); process.exitCode = 1; });
|