diff --git a/examples/generate-keys.ts b/examples/generate-keys.ts new file mode 100644 index 000000000..39130bb6a --- /dev/null +++ b/examples/generate-keys.ts @@ -0,0 +1,31 @@ +import { secp256k1, utils } from '../src'; +import { Address } from 'micro-eth-signer'; + +/** + * Generate a new private/public key pair and console log out the needed environment variables + * needed to run the examples. Please these values in a `.env` file. + */ +const main = async () => { + const privateKey = secp256k1.randomPrivateKey(); + const publicKey = secp256k1.getPublicKey(privateKey); + + const hrp = 'custom'; + const address = utils.formatBech32( + hrp, + secp256k1.publicKeyBytesToAddress(publicKey), + ); + + console.log('Copy the below values to your `.env` file:'); + console.log('------------------------------------------\n'); + + console.log('## PUBLIC_KEY=', `"${utils.bufferToHex(publicKey)}"`); + + console.log('PRIVATE_KEY=', `"${utils.bufferToHex(privateKey)}"`); + + console.log('P_CHAIN_ADDRESS=', `"P-${address}"`); + console.log('X_CHAIN_ADDRESS=', `"X-${address}"`); + console.log('C_CHAIN_ADDRESS=', `"${Address.fromPublicKey(publicKey)}"`); + console.log('CORETH_ADDRESS=', `"C-${address}"`); +}; + +main(); diff --git a/examples/p-chain/etna/base.ts b/examples/p-chain/etna/base.ts index 9583acc2e..a0a5989b2 100644 --- a/examples/p-chain/etna/base.ts +++ b/examples/p-chain/etna/base.ts @@ -1,6 +1,6 @@ import { TransferableOutput, addTxSignatures, pvm, utils } from '../../../src'; import { getEnvVars } from '../../utils/getEnvVars'; -import { getEtnaContextFromURI } from './utils/etna-context'; +import { setupEtnaExample } from './utils/etna-helper'; /** * The amount of AVAX to send to self. @@ -10,10 +10,7 @@ const SEND_AVAX_AMOUNT: number = 0.001; const main = async () => { const { AVAX_PUBLIC_URL, P_CHAIN_ADDRESS, PRIVATE_KEY } = getEnvVars(); - const context = await getEtnaContextFromURI(AVAX_PUBLIC_URL); - - const pvmApi = new pvm.PVMApi(AVAX_PUBLIC_URL); - const feeState = await pvmApi.getFeeState(); + const { context, feeState, pvmApi } = await setupEtnaExample(AVAX_PUBLIC_URL); const { utxos } = await pvmApi.getUTXOs({ addresses: [P_CHAIN_ADDRESS] }); diff --git a/examples/p-chain/etna/delegate.ts b/examples/p-chain/etna/delegate.ts index 7748b3c11..eb330b632 100644 --- a/examples/p-chain/etna/delegate.ts +++ b/examples/p-chain/etna/delegate.ts @@ -1,6 +1,6 @@ import { addTxSignatures, networkIDs, pvm, utils } from '../../../src'; import { getEnvVars } from '../../utils/getEnvVars'; -import { getEtnaContextFromURI } from './utils/etna-context'; +import { setupEtnaExample } from './utils/etna-helper'; const AMOUNT_TO_DELEGATE_AVAX: number = 1; const DAYS_TO_DELEGATE: number = 14; @@ -8,10 +8,7 @@ const DAYS_TO_DELEGATE: number = 14; const main = async () => { const { AVAX_PUBLIC_URL, P_CHAIN_ADDRESS, PRIVATE_KEY } = getEnvVars(); - const context = await getEtnaContextFromURI(AVAX_PUBLIC_URL); - - const pvmApi = new pvm.PVMApi(AVAX_PUBLIC_URL); - const feeState = await pvmApi.getFeeState(); + const { context, feeState, pvmApi } = await setupEtnaExample(AVAX_PUBLIC_URL); const { utxos } = await pvmApi.getUTXOs({ addresses: [P_CHAIN_ADDRESS] }); diff --git a/examples/p-chain/etna/export.ts b/examples/p-chain/etna/export.ts index d1b2163dd..2c5fb6523 100644 --- a/examples/p-chain/etna/export.ts +++ b/examples/p-chain/etna/export.ts @@ -1,6 +1,6 @@ import { TransferableOutput, addTxSignatures, pvm, utils } from '../../../src'; import { getEnvVars } from '../../utils/getEnvVars'; -import { getEtnaContextFromURI } from './utils/etna-context'; +import { setupEtnaExample } from './utils/etna-helper'; const AMOUNT_TO_EXPORT_AVAX: number = 0.001; @@ -8,10 +8,7 @@ const main = async () => { const { AVAX_PUBLIC_URL, P_CHAIN_ADDRESS, PRIVATE_KEY, X_CHAIN_ADDRESS } = getEnvVars(); - const context = await getEtnaContextFromURI(AVAX_PUBLIC_URL); - - const pvmApi = new pvm.PVMApi(AVAX_PUBLIC_URL); - const feeState = await pvmApi.getFeeState(); + const { context, feeState, pvmApi } = await setupEtnaExample(AVAX_PUBLIC_URL); const { utxos } = await pvmApi.getUTXOs({ addresses: [P_CHAIN_ADDRESS], diff --git a/examples/p-chain/etna/import.ts b/examples/p-chain/etna/import.ts index 724afb9b7..a3a93000c 100644 --- a/examples/p-chain/etna/import.ts +++ b/examples/p-chain/etna/import.ts @@ -1,15 +1,12 @@ import { addTxSignatures, pvm, utils } from '../../../src'; import { getEnvVars } from '../../utils/getEnvVars'; -import { getEtnaContextFromURI } from './utils/etna-context'; +import { setupEtnaExample } from './utils/etna-helper'; const main = async () => { const { AVAX_PUBLIC_URL, P_CHAIN_ADDRESS, PRIVATE_KEY, X_CHAIN_ADDRESS } = getEnvVars(); - const context = await getEtnaContextFromURI(AVAX_PUBLIC_URL); - - const pvmApi = new pvm.PVMApi(AVAX_PUBLIC_URL); - const feeState = await pvmApi.getFeeState(); + const { context, feeState, pvmApi } = await setupEtnaExample(AVAX_PUBLIC_URL); const { utxos } = await pvmApi.getUTXOs({ sourceChain: 'X', diff --git a/examples/p-chain/etna/utils/etna-context.ts b/examples/p-chain/etna/utils/etna-context.ts deleted file mode 100644 index 705cd3323..000000000 --- a/examples/p-chain/etna/utils/etna-context.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { Context, Info } from '../../../../src'; -/** - * Gets the context from URI and then modifies the context - * to be used for testing example Etna transactions until Etna is enabled. - */ -export const getEtnaContextFromURI = async ( - uri: string, -): Promise => { - const context = await Context.getContextFromURI(uri); - - const info = new Info(uri); - - const { etnaTime } = await info.getUpgradesInfo(); - - const etnaDateTime = new Date(etnaTime); - const now = new Date(); - - if (etnaDateTime < now) { - return context; - } - - // If Etna is not enabled, we need to override the minPrice of 1n that is returned. - // This is because the minPrice of 1n is not enough to calculate a fee that exceeds the current static fees. - return { - ...context, - platformFeeConfig: { - ...context.platformFeeConfig, - minPrice: 10_000n, - }, - }; -}; diff --git a/examples/p-chain/etna/utils/etna-helper.ts b/examples/p-chain/etna/utils/etna-helper.ts new file mode 100644 index 000000000..356f072ad --- /dev/null +++ b/examples/p-chain/etna/utils/etna-helper.ts @@ -0,0 +1,33 @@ +import { Context, Info, pvm } from '../../../../src'; +import type { FeeState } from '../../../../src/vms/pvm'; + +export const setupEtnaExample = async ( + uri: string, +): Promise<{ + context: Context.Context; + feeState: FeeState; + pvmApi: pvm.PVMApi; +}> => { + const context = await Context.getContextFromURI(uri); + const pvmApi = new pvm.PVMApi(uri); + const feeState = await pvmApi.getFeeState(); + + const info = new Info(uri); + + const { etnaTime } = await info.getUpgradesInfo(); + + const etnaDateTime = new Date(etnaTime); + const now = new Date(); + + if (etnaDateTime >= now) { + throw new Error( + `Etna upgrade is not enabled. Upgrade time: ${etnaDateTime}`, + ); + } + + return { + context, + feeState, + pvmApi, + }; +}; diff --git a/examples/p-chain/etna/validate.ts b/examples/p-chain/etna/validate.ts index f5a6ef7b9..f74f65bfb 100644 --- a/examples/p-chain/etna/validate.ts +++ b/examples/p-chain/etna/validate.ts @@ -1,6 +1,6 @@ import { addTxSignatures, networkIDs, pvm, utils } from '../../../src'; import { getEnvVars } from '../../utils/getEnvVars'; -import { getEtnaContextFromURI } from './utils/etna-context'; +import { setupEtnaExample } from './utils/etna-helper'; import { getRandomNodeId } from './utils/random-node-id'; const AMOUNT_TO_VALIDATE_AVAX: number = 1; @@ -11,10 +11,7 @@ const nodeId = getRandomNodeId(); const main = async () => { const { AVAX_PUBLIC_URL, P_CHAIN_ADDRESS, PRIVATE_KEY } = getEnvVars(); - const context = await getEtnaContextFromURI(AVAX_PUBLIC_URL); - - const pvmApi = new pvm.PVMApi(AVAX_PUBLIC_URL); - const feeState = await pvmApi.getFeeState(); + const { context, feeState, pvmApi } = await setupEtnaExample(AVAX_PUBLIC_URL); const { utxos } = await pvmApi.getUTXOs({ addresses: [P_CHAIN_ADDRESS] }); diff --git a/src/vms/pvm/etna-builder/builder.test.ts b/src/vms/pvm/etna-builder/builder.test.ts index 54b296773..080267d92 100644 --- a/src/vms/pvm/etna-builder/builder.test.ts +++ b/src/vms/pvm/etna-builder/builder.test.ts @@ -146,9 +146,7 @@ const checkFeeIsCorrect = ({ const expectedFee = calculateFee( unsignedTx.getTx(), testContext.platformFeeConfig.weights, - feeState.price < testContext.platformFeeConfig.minPrice - ? testContext.platformFeeConfig.minPrice - : feeState.price, + feeState.price, ); const expectedAmountBurned = addAmounts( diff --git a/src/vms/pvm/etna-builder/spend-reducers/fixtures/reducers.ts b/src/vms/pvm/etna-builder/spend-reducers/fixtures/reducers.ts index 7a1a93448..6d17e4f1c 100644 --- a/src/vms/pvm/etna-builder/spend-reducers/fixtures/reducers.ts +++ b/src/vms/pvm/etna-builder/spend-reducers/fixtures/reducers.ts @@ -62,10 +62,7 @@ export const getSpendHelper = ({ > = {}) => { return new SpendHelper({ changeOutputs: [], - gasPrice: - feeState.price < testContext.platformFeeConfig.minPrice - ? testContext.platformFeeConfig.minPrice - : feeState.price, + gasPrice: feeState.price, initialComplexity, inputs: [], shouldConsolidateOutputs, diff --git a/src/vms/pvm/etna-builder/spend.ts b/src/vms/pvm/etna-builder/spend.ts index b37817775..1c42dc645 100644 --- a/src/vms/pvm/etna-builder/spend.ts +++ b/src/vms/pvm/etna-builder/spend.ts @@ -120,10 +120,7 @@ export const spend = ( const changeOwners = ownerOverride || OutputOwners.fromNative(spendOptions.changeAddresses); - const gasPrice: bigint = - feeState.price < context.platformFeeConfig.minPrice - ? context.platformFeeConfig.minPrice - : feeState.price; + const gasPrice: bigint = feeState.price; const spendHelper = new SpendHelper({ changeOutputs: [], diff --git a/src/vms/pvm/txs/fee/complexity.ts b/src/vms/pvm/txs/fee/complexity.ts index 36e6d84fb..330a989c1 100644 --- a/src/vms/pvm/txs/fee/complexity.ts +++ b/src/vms/pvm/txs/fee/complexity.ts @@ -201,9 +201,12 @@ export const getAuthComplexity = (input: Serializable): Dimensions => { const bandwidth = signatureBandwidth + INTRINSIC_SECP256K1_FX_INPUT_BANDWIDTH; - return createDimensions( - { bandwidth, dbRead: 0, dbWrite: 0, compute: 0 }, // TODO: Add compute complexity. - ); + return createDimensions({ + bandwidth, + dbRead: 0, + dbWrite: 0, + compute: 0, // TODO: Add compute complexity. + }); }; const getBaseTxComplexity = (baseTx: BaseTx): Dimensions => {