Skip to content

Commit

Permalink
fix: remove etna builder minPrice usage and update examples (#881)
Browse files Browse the repository at this point in the history
  • Loading branch information
erictaylor authored Oct 9, 2024
1 parent 47f216e commit 5b7868a
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 70 deletions.
31 changes: 31 additions & 0 deletions examples/generate-keys.ts
Original file line number Diff line number Diff line change
@@ -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();
7 changes: 2 additions & 5 deletions examples/p-chain/etna/base.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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] });

Expand Down
7 changes: 2 additions & 5 deletions examples/p-chain/etna/delegate.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
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;

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] });

Expand Down
7 changes: 2 additions & 5 deletions examples/p-chain/etna/export.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
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;

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],
Expand Down
7 changes: 2 additions & 5 deletions examples/p-chain/etna/import.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
31 changes: 0 additions & 31 deletions examples/p-chain/etna/utils/etna-context.ts

This file was deleted.

33 changes: 33 additions & 0 deletions examples/p-chain/etna/utils/etna-helper.ts
Original file line number Diff line number Diff line change
@@ -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,
};
};
7 changes: 2 additions & 5 deletions examples/p-chain/etna/validate.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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] });

Expand Down
4 changes: 1 addition & 3 deletions src/vms/pvm/etna-builder/builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
5 changes: 1 addition & 4 deletions src/vms/pvm/etna-builder/spend-reducers/fixtures/reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 1 addition & 4 deletions src/vms/pvm/etna-builder/spend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
Expand Down
9 changes: 6 additions & 3 deletions src/vms/pvm/txs/fee/complexity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down

0 comments on commit 5b7868a

Please sign in to comment.