|
| 1 | +import { addTxSignatures, pvm, utils } from '../../../src'; |
| 2 | +import { setupEtnaExample } from './utils/etna-helper'; |
| 3 | +import { ConvertSubnetValidator } from '../../../src/serializable/fxs/pvm/convertSubnetValidator'; |
| 4 | +import { ProofOfPossession } from '../../../src/serializable/pvm'; |
| 5 | +import { PChainOwner } from '../../../src/serializable/fxs/pvm/pChainOwner'; |
| 6 | +import { getEnvVars } from '../../utils/getEnvVars'; |
| 7 | + |
| 8 | +const AMOUNT_TO_VALIDATE_AVAX: number = 1; |
| 9 | +const BALANCE_AVAX: number = 1; |
| 10 | + |
| 11 | +/** |
| 12 | + * Converts a subnet to permissionless subnet. |
| 13 | + * |
| 14 | + * **Note** A subnet must be created (createSubnetTx) and a chain must be created (createChainTx) |
| 15 | + * before a subnet can be converted from permissioned to permissionless. |
| 16 | + * @param BLS_PUBLIC_KEY BLS key from info.getNodeID on AVAX_PUBLIC_URL |
| 17 | + * @param BLS_SIGNATURE BLS signature from info.getNodeID on AVAX_PUBLIC_URL |
| 18 | + * @param NODE_ID the ID of the node from info.getNodeID on AVAX_PUBLIC_URL. |
| 19 | + * @param chainId the ID of the chain that is created via `createChainTx`. |
| 20 | + * @param subnetId the ID of the subnet that is created via `createSubnetTx`. |
| 21 | + * @returns The resulting transaction's ID. |
| 22 | + */ |
| 23 | +const convertSubnetTxExmaple = async () => { |
| 24 | + const { |
| 25 | + AVAX_PUBLIC_URL, |
| 26 | + P_CHAIN_ADDRESS, |
| 27 | + PRIVATE_KEY, |
| 28 | + NODE_ID, |
| 29 | + BLS_PUBLIC_KEY, |
| 30 | + BLS_SIGNATURE, |
| 31 | + } = getEnvVars(); |
| 32 | + const { context, feeState, pvmApi } = await setupEtnaExample(AVAX_PUBLIC_URL); |
| 33 | + |
| 34 | + const { utxos } = await pvmApi.getUTXOs({ addresses: [P_CHAIN_ADDRESS] }); |
| 35 | + |
| 36 | + const testPAddr = utils.bech32ToBytes(P_CHAIN_ADDRESS); |
| 37 | + |
| 38 | + const pChainOwner = PChainOwner.fromNative([testPAddr], 1); |
| 39 | + |
| 40 | + const publicKey = utils.hexToBuffer(BLS_PUBLIC_KEY); |
| 41 | + |
| 42 | + const signature = utils.hexToBuffer(BLS_SIGNATURE); |
| 43 | + |
| 44 | + const signer = new ProofOfPossession(publicKey, signature); |
| 45 | + |
| 46 | + const validator = ConvertSubnetValidator.fromNative( |
| 47 | + NODE_ID, |
| 48 | + BigInt(AMOUNT_TO_VALIDATE_AVAX * 1e9), |
| 49 | + BigInt(BALANCE_AVAX * 1e9), |
| 50 | + signer, |
| 51 | + pChainOwner, |
| 52 | + pChainOwner, |
| 53 | + ); |
| 54 | + |
| 55 | + const tx = pvm.e.newConvertSubnetTx( |
| 56 | + { |
| 57 | + feeState, |
| 58 | + fromAddressesBytes: [testPAddr], |
| 59 | + subnetId: '', // subnetId from createSubnetTx |
| 60 | + utxos, |
| 61 | + chainId: '', // chainId from createChainTx |
| 62 | + validators: [validator], |
| 63 | + subnetAuth: [0], |
| 64 | + address: testPAddr, |
| 65 | + }, |
| 66 | + context, |
| 67 | + ); |
| 68 | + |
| 69 | + await addTxSignatures({ |
| 70 | + unsignedTx: tx, |
| 71 | + privateKeys: [utils.hexToBuffer(PRIVATE_KEY)], |
| 72 | + }); |
| 73 | + |
| 74 | + return pvmApi.issueSignedTx(tx.getSignedTx()); |
| 75 | +}; |
| 76 | + |
| 77 | +convertSubnetTxExmaple().then(console.log); |
0 commit comments