-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #885 from ava-labs/convertSubnetTx
Implement ConvertSubnetTx
- Loading branch information
Showing
37 changed files
with
1,071 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { addTxSignatures, pvm, utils } from '../../../src'; | ||
import { setupEtnaExample } from './utils/etna-helper'; | ||
import { ConvertSubnetValidator } from '../../../src/serializable/fxs/pvm/convertSubnetValidator'; | ||
import { ProofOfPossession } from '../../../src/serializable/pvm'; | ||
import { PChainOwner } from '../../../src/serializable/fxs/pvm/pChainOwner'; | ||
import { getEnvVars } from '../../utils/getEnvVars'; | ||
|
||
const AMOUNT_TO_VALIDATE_AVAX: number = 1; | ||
const BALANCE_AVAX: number = 1; | ||
|
||
/** | ||
* Converts a subnet to permissionless subnet. | ||
* | ||
* **Note** A subnet must be created (createSubnetTx) and a chain must be created (createChainTx) | ||
* before a subnet can be converted from permissioned to permissionless. | ||
* @param BLS_PUBLIC_KEY BLS key from info.getNodeID on AVAX_PUBLIC_URL | ||
* @param BLS_SIGNATURE BLS signature from info.getNodeID on AVAX_PUBLIC_URL | ||
* @param NODE_ID the ID of the node from info.getNodeID on AVAX_PUBLIC_URL. | ||
* @param chainId the ID of the chain that is created via `createChainTx`. | ||
* @param subnetId the ID of the subnet that is created via `createSubnetTx`. | ||
* @returns The resulting transaction's ID. | ||
*/ | ||
const convertSubnetTxExmaple = async () => { | ||
const { | ||
AVAX_PUBLIC_URL, | ||
P_CHAIN_ADDRESS, | ||
PRIVATE_KEY, | ||
NODE_ID, | ||
BLS_PUBLIC_KEY, | ||
BLS_SIGNATURE, | ||
} = getEnvVars(); | ||
const { context, feeState, pvmApi } = await setupEtnaExample(AVAX_PUBLIC_URL); | ||
|
||
const { utxos } = await pvmApi.getUTXOs({ addresses: [P_CHAIN_ADDRESS] }); | ||
|
||
const testPAddr = utils.bech32ToBytes(P_CHAIN_ADDRESS); | ||
|
||
const pChainOwner = PChainOwner.fromNative([testPAddr], 1); | ||
|
||
const publicKey = utils.hexToBuffer(BLS_PUBLIC_KEY); | ||
|
||
const signature = utils.hexToBuffer(BLS_SIGNATURE); | ||
|
||
const signer = new ProofOfPossession(publicKey, signature); | ||
|
||
const validator = ConvertSubnetValidator.fromNative( | ||
NODE_ID, | ||
BigInt(AMOUNT_TO_VALIDATE_AVAX * 1e9), | ||
BigInt(BALANCE_AVAX * 1e9), | ||
signer, | ||
pChainOwner, | ||
pChainOwner, | ||
); | ||
|
||
const tx = pvm.e.newConvertSubnetTx( | ||
{ | ||
feeState, | ||
fromAddressesBytes: [testPAddr], | ||
subnetId: '', // subnetId from createSubnetTx | ||
utxos, | ||
chainId: '', // chainId from createChainTx | ||
validators: [validator], | ||
subnetAuth: [0], | ||
address: testPAddr, | ||
}, | ||
context, | ||
); | ||
|
||
await addTxSignatures({ | ||
unsignedTx: tx, | ||
privateKeys: [utils.hexToBuffer(PRIVATE_KEY)], | ||
}); | ||
|
||
return pvmApi.issueSignedTx(tx.getSignedTx()); | ||
}; | ||
|
||
convertSubnetTxExmaple().then(console.log); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { addTxSignatures, pvm, utils } from '../../../src'; | ||
import { setupEtnaExample } from './utils/etna-helper'; | ||
import { testGenesisData } from '../../../src/fixtures/transactions'; | ||
import { getEnvVars } from '../../utils/getEnvVars'; | ||
|
||
/** | ||
* Create a new chain on the P-Chain. | ||
* | ||
* **Note** A subnet must be created (createSubnetTx) before a chain can be created. | ||
* @param vmId the platform chain's vmID can be found in the `InfoApi.getVMs` response. | ||
* @param subnetId the ID of the subnet that is created via `createSubnetTx`. | ||
* @param chainName the name of the chain. Can be any string. | ||
* @returns The resulting transaction's ID. | ||
*/ | ||
const createChainTxExample = async () => { | ||
const { AVAX_PUBLIC_URL, P_CHAIN_ADDRESS, PRIVATE_KEY } = getEnvVars(); | ||
const { context, feeState, pvmApi } = await setupEtnaExample(AVAX_PUBLIC_URL); | ||
|
||
const { utxos } = await pvmApi.getUTXOs({ addresses: [P_CHAIN_ADDRESS] }); | ||
|
||
const testPAddr = utils.bech32ToBytes(P_CHAIN_ADDRESS); | ||
|
||
const vmId = 'rWhpuQPF1kb72esV2momhMuTYGkEb1oL29pt2EBXWmSy4kxnT'; // platform vmId | ||
const subnetId = ''; // subnetId from createSubnetTx | ||
|
||
const tx = pvm.e.newCreateChainTx( | ||
{ | ||
feeState, | ||
fromAddressesBytes: [testPAddr], | ||
utxos, | ||
chainName: 'test chain', | ||
subnetAuth: [0], | ||
subnetId, | ||
vmId, | ||
fxIds: [], | ||
genesisData: testGenesisData, | ||
}, | ||
context, | ||
); | ||
|
||
await addTxSignatures({ | ||
unsignedTx: tx, | ||
privateKeys: [utils.hexToBuffer(PRIVATE_KEY)], | ||
}); | ||
|
||
return pvmApi.issueSignedTx(tx.getSignedTx()); | ||
}; | ||
|
||
createChainTxExample().then(console.log); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { addTxSignatures, pvm, utils } from '../../../src'; | ||
import { getEnvVars } from '../../utils/getEnvVars'; | ||
import { setupEtnaExample } from './utils/etna-helper'; | ||
|
||
const createSubnetTxExample = async () => { | ||
const { AVAX_PUBLIC_URL, P_CHAIN_ADDRESS, PRIVATE_KEY } = getEnvVars(); | ||
|
||
const { context, feeState, pvmApi } = await setupEtnaExample(AVAX_PUBLIC_URL); | ||
|
||
const { utxos } = await pvmApi.getUTXOs({ addresses: [P_CHAIN_ADDRESS] }); | ||
|
||
const testPAddr = utils.bech32ToBytes(P_CHAIN_ADDRESS); | ||
|
||
const tx = pvm.e.newCreateSubnetTx( | ||
{ | ||
feeState, | ||
fromAddressesBytes: [testPAddr], | ||
utxos, | ||
subnetOwners: [testPAddr], | ||
}, | ||
context, | ||
); | ||
|
||
await addTxSignatures({ | ||
unsignedTx: tx, | ||
privateKeys: [utils.hexToBuffer(PRIVATE_KEY)], | ||
}); | ||
|
||
return pvmApi.issueSignedTx(tx.getSignedTx()); | ||
}; | ||
|
||
createSubnetTxExample().then(console.log); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.