Skip to content

Commit

Permalink
feat: removed old pvm builder and apis
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The old PVM transaction builder has been removed. The new Etna builder is now the default builder in order to support dynamic fees. Static fees on P-chain are not longer applicable. Likewise, API's referring to retrieving static fees have been removed where no longer applicable, and new X-Chain specific API's have been added to cover retrieving static tx fees specific to X-Chain transactions. The `e` alias on `pvm` (ie `pvm.e.{builderMethod}`) has been marked deprecated since the builder methods are now directly available on the `pvm` export.
  • Loading branch information
frichards authored Feb 5, 2025
1 parent 06c8738 commit ebbb290
Show file tree
Hide file tree
Showing 35 changed files with 431 additions and 2,903 deletions.
42 changes: 27 additions & 15 deletions examples/p-chain/base.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,41 @@
import { addTxSignatures } from '../../src/signer';
import { TransferableOutput } from '../../src/serializable/avax';
import { bech32ToBytes, hexToBuffer } from '../../src/utils';
import { getContextFromURI } from '../../src/vms/context';
import { newBaseTx } from '../../src/vms/pvm';
import { pvmapi } from '../chain_apis';
import { TransferableOutput, addTxSignatures, pvm, utils } from '../../src';
import { getEnvVars } from '../utils/getEnvVars';
import { setupEtnaExample } from './utils/etna-helper';

/**
* The amount of AVAX to send to self.
*/
const SEND_AVAX_AMOUNT: number = 0.001;

const main = async () => {
const { AVAX_PUBLIC_URL, P_CHAIN_ADDRESS, PRIVATE_KEY } = getEnvVars();

const { utxos } = await pvmapi.getUTXOs({ addresses: [P_CHAIN_ADDRESS] });
const context = await getContextFromURI(AVAX_PUBLIC_URL);
const { context, feeState, pvmApi } = await setupEtnaExample(AVAX_PUBLIC_URL);

const { utxos } = await pvmApi.getUTXOs({ addresses: [P_CHAIN_ADDRESS] });

const tx = newBaseTx(context, [bech32ToBytes(P_CHAIN_ADDRESS)], utxos, [
TransferableOutput.fromNative(context.avaxAssetID, BigInt(0.1 * 1e9), [
bech32ToBytes(P_CHAIN_ADDRESS),
]),
]);
const tx = pvm.newBaseTx(
{
feeState,
fromAddressesBytes: [utils.bech32ToBytes(P_CHAIN_ADDRESS)],
outputs: [
TransferableOutput.fromNative(
context.avaxAssetID,
BigInt(SEND_AVAX_AMOUNT * 1e9),
[utils.bech32ToBytes(P_CHAIN_ADDRESS)],
),
],
utxos,
},
context,
);

await addTxSignatures({
unsignedTx: tx,
privateKeys: [hexToBuffer(PRIVATE_KEY)],
privateKeys: [utils.hexToBuffer(PRIVATE_KEY)],
});

return pvmapi.issueSignedTx(tx.getSignedTx());
return pvmApi.issueSignedTx(tx.getSignedTx());
};

main().then(console.log);
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import {
pvm,
pvmSerial,
utils,
} from '../../../src';
} from '../../src';
import { setupEtnaExample } from './utils/etna-helper';
import { getEnvVars } from '../../utils/getEnvVars';
import { getEnvVars } from '../utils/getEnvVars';

const AMOUNT_TO_VALIDATE_AVAX: number = 1;
const BALANCE_AVAX: number = 1;
Expand Down Expand Up @@ -56,7 +56,7 @@ const convertSubnetToL1TxExample = async () => {
pChainOwner,
);

const tx = pvm.e.newConvertSubnetToL1Tx(
const tx = pvm.newConvertSubnetToL1Tx(
{
feeState,
fromAddressesBytes: [testPAddr],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { pvm, utils } from '../../../src';
import { pvm, utils } from '../../src';
import { setupEtnaExample } from './utils/etna-helper';
import { testGenesisData } from '../../../src/fixtures/transactions';
import { getEnvVars } from '../../utils/getEnvVars';
import { testGenesisData } from '../../src/fixtures/transactions';
import { getEnvVars } from '../utils/getEnvVars';
import { addSigToAllCreds } from './utils/addSignatureToAllCred';

/**
Expand All @@ -24,7 +24,7 @@ const createChainTxExample = async () => {
const vmId = 'rWhpuQPF1kb72esV2momhMuTYGkEb1oL29pt2EBXWmSy4kxnT'; // platform vmId
const subnetId = ''; // subnetId from createSubnetTx

const tx = pvm.e.newCreateChainTx(
const tx = pvm.newCreateChainTx(
{
feeState,
fromAddressesBytes: [testPAddr],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { addTxSignatures, pvm, utils } from '../../../src';
import { getEnvVars } from '../../utils/getEnvVars';
import { addTxSignatures, pvm, utils } from '../../src';
import { getEnvVars } from '../utils/getEnvVars';
import { setupEtnaExample } from './utils/etna-helper';

const createSubnetTxExample = async () => {
Expand All @@ -11,7 +11,7 @@ const createSubnetTxExample = async () => {

const testPAddr = utils.bech32ToBytes(P_CHAIN_ADDRESS);

const tx = pvm.e.newCreateSubnetTx(
const tx = pvm.newCreateSubnetTx(
{
feeState,
fromAddressesBytes: [testPAddr],
Expand Down
55 changes: 31 additions & 24 deletions examples/p-chain/delegate.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,49 @@
import { PrimaryNetworkID } from '../../src/constants/networkIDs';
import { addTxSignatures } from '../../src/signer';
import { bech32ToBytes, hexToBuffer } from '../../src/utils';
import { getContextFromURI } from '../../src/vms/context';
import { PVMApi, newAddPermissionlessDelegatorTx } from '../../src/vms/pvm';
import { pvmapi } from '../chain_apis';
import { addTxSignatures, networkIDs, pvm, utils } from '../../src';
import { getEnvVars } from '../utils/getEnvVars';
import { setupEtnaExample } from './utils/etna-helper';

const AMOUNT_TO_DELEGATE_AVAX: number = 1;
const DAYS_TO_DELEGATE: number = 14;

const main = async () => {
const { AVAX_PUBLIC_URL, P_CHAIN_ADDRESS, PRIVATE_KEY } = getEnvVars();

const { utxos } = await pvmapi.getUTXOs({ addresses: [P_CHAIN_ADDRESS] });
const context = await getContextFromURI(AVAX_PUBLIC_URL);
const startTime = await new PVMApi().getTimestamp();
const { context, feeState, pvmApi } = await setupEtnaExample(AVAX_PUBLIC_URL);

const { utxos } = await pvmApi.getUTXOs({ addresses: [P_CHAIN_ADDRESS] });

const startTime = await pvmApi.getTimestamp();
const startDate = new Date(startTime.timestamp);
const start = BigInt(startDate.getTime() / 1000);
const start: bigint = BigInt(startDate.getTime() / 1_000);

const endTime = new Date(startTime.timestamp);
endTime.setDate(endTime.getDate() + 21);
const end = BigInt(endTime.getTime() / 1000);
const nodeID = 'NodeID-HKLp5269LH8DcrLvHPc2PHjGczBQD3td4';
endTime.setDate(endTime.getDate() + DAYS_TO_DELEGATE);
const end: bigint = BigInt(endTime.getTime() / 1_000);

// TODO: Get this from an argument.
const nodeId = 'NodeID-MqgFXT8JhorbEW2LpTDGePBBhv55SSp3M';

const tx = newAddPermissionlessDelegatorTx(
const tx = pvm.newAddPermissionlessDelegatorTx(
{
end,
feeState,
fromAddressesBytes: [utils.bech32ToBytes(P_CHAIN_ADDRESS)],
nodeId,
rewardAddresses: [utils.bech32ToBytes(P_CHAIN_ADDRESS)],
start,
subnetId: networkIDs.PrimaryNetworkID.toString(),
utxos,
weight: BigInt(AMOUNT_TO_DELEGATE_AVAX * 1e9),
},
context,
utxos,
[bech32ToBytes(P_CHAIN_ADDRESS)],
nodeID,
PrimaryNetworkID.toString(),
start,
end,
BigInt(1e9),
[bech32ToBytes(P_CHAIN_ADDRESS)],
);

await addTxSignatures({
unsignedTx: tx,
privateKeys: [hexToBuffer(PRIVATE_KEY)],
privateKeys: [utils.hexToBuffer(PRIVATE_KEY)],
});

return pvmapi.issueSignedTx(tx.getSignedTx());
return pvmApi.issueSignedTx(tx.getSignedTx());
};

main().then(console.log);
41 changes: 0 additions & 41 deletions examples/p-chain/etna/base.ts

This file was deleted.

49 changes: 0 additions & 49 deletions examples/p-chain/etna/delegate.ts

This file was deleted.

42 changes: 0 additions & 42 deletions examples/p-chain/etna/export.ts

This file was deleted.

35 changes: 0 additions & 35 deletions examples/p-chain/etna/import.ts

This file was deleted.

Loading

0 comments on commit ebbb290

Please sign in to comment.