Skip to content

Commit 5b7868a

Browse files
authored
fix: remove etna builder minPrice usage and update examples (#881)
1 parent 47f216e commit 5b7868a

File tree

12 files changed

+83
-70
lines changed

12 files changed

+83
-70
lines changed

examples/generate-keys.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { secp256k1, utils } from '../src';
2+
import { Address } from 'micro-eth-signer';
3+
4+
/**
5+
* Generate a new private/public key pair and console log out the needed environment variables
6+
* needed to run the examples. Please these values in a `.env` file.
7+
*/
8+
const main = async () => {
9+
const privateKey = secp256k1.randomPrivateKey();
10+
const publicKey = secp256k1.getPublicKey(privateKey);
11+
12+
const hrp = 'custom';
13+
const address = utils.formatBech32(
14+
hrp,
15+
secp256k1.publicKeyBytesToAddress(publicKey),
16+
);
17+
18+
console.log('Copy the below values to your `.env` file:');
19+
console.log('------------------------------------------\n');
20+
21+
console.log('## PUBLIC_KEY=', `"${utils.bufferToHex(publicKey)}"`);
22+
23+
console.log('PRIVATE_KEY=', `"${utils.bufferToHex(privateKey)}"`);
24+
25+
console.log('P_CHAIN_ADDRESS=', `"P-${address}"`);
26+
console.log('X_CHAIN_ADDRESS=', `"X-${address}"`);
27+
console.log('C_CHAIN_ADDRESS=', `"${Address.fromPublicKey(publicKey)}"`);
28+
console.log('CORETH_ADDRESS=', `"C-${address}"`);
29+
};
30+
31+
main();

examples/p-chain/etna/base.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { TransferableOutput, addTxSignatures, pvm, utils } from '../../../src';
22
import { getEnvVars } from '../../utils/getEnvVars';
3-
import { getEtnaContextFromURI } from './utils/etna-context';
3+
import { setupEtnaExample } from './utils/etna-helper';
44

55
/**
66
* The amount of AVAX to send to self.
@@ -10,10 +10,7 @@ const SEND_AVAX_AMOUNT: number = 0.001;
1010
const main = async () => {
1111
const { AVAX_PUBLIC_URL, P_CHAIN_ADDRESS, PRIVATE_KEY } = getEnvVars();
1212

13-
const context = await getEtnaContextFromURI(AVAX_PUBLIC_URL);
14-
15-
const pvmApi = new pvm.PVMApi(AVAX_PUBLIC_URL);
16-
const feeState = await pvmApi.getFeeState();
13+
const { context, feeState, pvmApi } = await setupEtnaExample(AVAX_PUBLIC_URL);
1714

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

examples/p-chain/etna/delegate.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import { addTxSignatures, networkIDs, pvm, utils } from '../../../src';
22
import { getEnvVars } from '../../utils/getEnvVars';
3-
import { getEtnaContextFromURI } from './utils/etna-context';
3+
import { setupEtnaExample } from './utils/etna-helper';
44

55
const AMOUNT_TO_DELEGATE_AVAX: number = 1;
66
const DAYS_TO_DELEGATE: number = 14;
77

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

11-
const context = await getEtnaContextFromURI(AVAX_PUBLIC_URL);
12-
13-
const pvmApi = new pvm.PVMApi(AVAX_PUBLIC_URL);
14-
const feeState = await pvmApi.getFeeState();
11+
const { context, feeState, pvmApi } = await setupEtnaExample(AVAX_PUBLIC_URL);
1512

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

examples/p-chain/etna/export.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import { TransferableOutput, addTxSignatures, pvm, utils } from '../../../src';
22
import { getEnvVars } from '../../utils/getEnvVars';
3-
import { getEtnaContextFromURI } from './utils/etna-context';
3+
import { setupEtnaExample } from './utils/etna-helper';
44

55
const AMOUNT_TO_EXPORT_AVAX: number = 0.001;
66

77
const main = async () => {
88
const { AVAX_PUBLIC_URL, P_CHAIN_ADDRESS, PRIVATE_KEY, X_CHAIN_ADDRESS } =
99
getEnvVars();
1010

11-
const context = await getEtnaContextFromURI(AVAX_PUBLIC_URL);
12-
13-
const pvmApi = new pvm.PVMApi(AVAX_PUBLIC_URL);
14-
const feeState = await pvmApi.getFeeState();
11+
const { context, feeState, pvmApi } = await setupEtnaExample(AVAX_PUBLIC_URL);
1512

1613
const { utxos } = await pvmApi.getUTXOs({
1714
addresses: [P_CHAIN_ADDRESS],

examples/p-chain/etna/import.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import { addTxSignatures, pvm, utils } from '../../../src';
22
import { getEnvVars } from '../../utils/getEnvVars';
3-
import { getEtnaContextFromURI } from './utils/etna-context';
3+
import { setupEtnaExample } from './utils/etna-helper';
44

55
const main = async () => {
66
const { AVAX_PUBLIC_URL, P_CHAIN_ADDRESS, PRIVATE_KEY, X_CHAIN_ADDRESS } =
77
getEnvVars();
88

9-
const context = await getEtnaContextFromURI(AVAX_PUBLIC_URL);
10-
11-
const pvmApi = new pvm.PVMApi(AVAX_PUBLIC_URL);
12-
const feeState = await pvmApi.getFeeState();
9+
const { context, feeState, pvmApi } = await setupEtnaExample(AVAX_PUBLIC_URL);
1310

1411
const { utxos } = await pvmApi.getUTXOs({
1512
sourceChain: 'X',

examples/p-chain/etna/utils/etna-context.ts

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { Context, Info, pvm } from '../../../../src';
2+
import type { FeeState } from '../../../../src/vms/pvm';
3+
4+
export const setupEtnaExample = async (
5+
uri: string,
6+
): Promise<{
7+
context: Context.Context;
8+
feeState: FeeState;
9+
pvmApi: pvm.PVMApi;
10+
}> => {
11+
const context = await Context.getContextFromURI(uri);
12+
const pvmApi = new pvm.PVMApi(uri);
13+
const feeState = await pvmApi.getFeeState();
14+
15+
const info = new Info(uri);
16+
17+
const { etnaTime } = await info.getUpgradesInfo();
18+
19+
const etnaDateTime = new Date(etnaTime);
20+
const now = new Date();
21+
22+
if (etnaDateTime >= now) {
23+
throw new Error(
24+
`Etna upgrade is not enabled. Upgrade time: ${etnaDateTime}`,
25+
);
26+
}
27+
28+
return {
29+
context,
30+
feeState,
31+
pvmApi,
32+
};
33+
};

examples/p-chain/etna/validate.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { addTxSignatures, networkIDs, pvm, utils } from '../../../src';
22
import { getEnvVars } from '../../utils/getEnvVars';
3-
import { getEtnaContextFromURI } from './utils/etna-context';
3+
import { setupEtnaExample } from './utils/etna-helper';
44
import { getRandomNodeId } from './utils/random-node-id';
55

66
const AMOUNT_TO_VALIDATE_AVAX: number = 1;
@@ -11,10 +11,7 @@ const nodeId = getRandomNodeId();
1111
const main = async () => {
1212
const { AVAX_PUBLIC_URL, P_CHAIN_ADDRESS, PRIVATE_KEY } = getEnvVars();
1313

14-
const context = await getEtnaContextFromURI(AVAX_PUBLIC_URL);
15-
16-
const pvmApi = new pvm.PVMApi(AVAX_PUBLIC_URL);
17-
const feeState = await pvmApi.getFeeState();
14+
const { context, feeState, pvmApi } = await setupEtnaExample(AVAX_PUBLIC_URL);
1815

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

src/vms/pvm/etna-builder/builder.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,7 @@ const checkFeeIsCorrect = ({
146146
const expectedFee = calculateFee(
147147
unsignedTx.getTx(),
148148
testContext.platformFeeConfig.weights,
149-
feeState.price < testContext.platformFeeConfig.minPrice
150-
? testContext.platformFeeConfig.minPrice
151-
: feeState.price,
149+
feeState.price,
152150
);
153151

154152
const expectedAmountBurned = addAmounts(

src/vms/pvm/etna-builder/spend-reducers/fixtures/reducers.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,7 @@ export const getSpendHelper = ({
6262
> = {}) => {
6363
return new SpendHelper({
6464
changeOutputs: [],
65-
gasPrice:
66-
feeState.price < testContext.platformFeeConfig.minPrice
67-
? testContext.platformFeeConfig.minPrice
68-
: feeState.price,
65+
gasPrice: feeState.price,
6966
initialComplexity,
7067
inputs: [],
7168
shouldConsolidateOutputs,

src/vms/pvm/etna-builder/spend.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,7 @@ export const spend = (
120120
const changeOwners =
121121
ownerOverride || OutputOwners.fromNative(spendOptions.changeAddresses);
122122

123-
const gasPrice: bigint =
124-
feeState.price < context.platformFeeConfig.minPrice
125-
? context.platformFeeConfig.minPrice
126-
: feeState.price;
123+
const gasPrice: bigint = feeState.price;
127124

128125
const spendHelper = new SpendHelper({
129126
changeOutputs: [],

src/vms/pvm/txs/fee/complexity.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,12 @@ export const getAuthComplexity = (input: Serializable): Dimensions => {
201201

202202
const bandwidth = signatureBandwidth + INTRINSIC_SECP256K1_FX_INPUT_BANDWIDTH;
203203

204-
return createDimensions(
205-
{ bandwidth, dbRead: 0, dbWrite: 0, compute: 0 }, // TODO: Add compute complexity.
206-
);
204+
return createDimensions({
205+
bandwidth,
206+
dbRead: 0,
207+
dbWrite: 0,
208+
compute: 0, // TODO: Add compute complexity.
209+
});
207210
};
208211

209212
const getBaseTxComplexity = (baseTx: BaseTx): Dimensions => {

0 commit comments

Comments
 (0)