Skip to content

Commit

Permalink
chore: move validateBurnedAmount into its own folder
Browse files Browse the repository at this point in the history
  • Loading branch information
ruijialin-avalabs committed Oct 10, 2024
1 parent e071a75 commit 7502549
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 94 deletions.
2 changes: 1 addition & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ export * from './getTransferableInputsByTx';
export * from './getTransferableOutputsByTx';
export * from './getUtxoInfo';
export * from './getBurnedAmountByTx';
export * from './validateBurnedAmount';
export * from './validateBurnedAmount/validateBurnedAmount';
export * from './isEtnaEnabled';
export { unpackWithManager, getManagerForVM, packTx } from './packTx';
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { testAddress1, testAddress2 } from '../fixtures/vms';
import { testContext } from '../fixtures/context';
import { Utxo } from '../serializable/avax/utxo';
import { utxoId } from '../fixtures/avax';
import { Address, Id } from '../serializable/fxs/common';
import { OutputOwners, TransferOutput } from '../serializable/fxs/secp256k1';
import { BigIntPr, Int } from '../serializable/primitives';
import { testAddress1, testAddress2 } from '../../fixtures/vms';
import { testContext } from '../../fixtures/context';
import { Utxo } from '../../serializable/avax/utxo';
import { utxoId } from '../../fixtures/avax';
import { Address, Id } from '../../serializable/fxs/common';
import { OutputOwners, TransferOutput } from '../../serializable/fxs/secp256k1';
import { BigIntPr, Int } from '../../serializable/primitives';
import {
newBaseTx as avmBaseTx,
newExportTx as avmExportTx,
newImportTx as avmImportTx,
} from '../vms/avm';
} from '../../vms/avm';
import {
newBaseTx as pvmBaseTx,
newExportTx as pvmExportTx,
Expand All @@ -21,11 +21,14 @@ import {
newAddPermissionlessDelegatorTx,
newRemoveSubnetValidatorTx,
newTransferSubnetOwnershipTx,
} from '../vms/pvm';
import { TransferableOutput } from '../serializable';
import { nodeId } from '../fixtures/common';
import { testSubnetId } from '../fixtures/transactions';
import { blsPublicKeyBytes, blsSignatureBytes } from '../fixtures/primitives';
} from '../../vms/pvm';
import { TransferableOutput } from '../../serializable';
import { nodeId } from '../../fixtures/common';
import { testSubnetId } from '../../fixtures/transactions';
import {
blsPublicKeyBytes,
blsSignatureBytes,
} from '../../fixtures/primitives';
import { validateAvaxBurnedAmountEtna } from './validateAvaxBurnedAmountEtna';

const utxoMock = new Utxo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,19 @@ import {
isImportTx as isPvmImportTx,
isRemoveSubnetValidatorTx,
isTransferSubnetOwnershipTx,
} from '../serializable/pvm';
import type { UnsignedTx } from '../vms/common';
} from '../../serializable/pvm';
import type { UnsignedTx } from '../../vms/common';

/**
* Validate burned amount for post-etna avalanche transactions
*
* @param unsignedTx: unsigned transaction
* @param burnedAmount: burned amount in nAVAX
* @param baseFee: pvm dynamic fee caculator, https://github.com/ava-labs/avalanchego/blob/master/vms/platformvm/txs/fee/dynamic_calculator.go
* @param feeTolerance: tolerance percentage range where the burned amount is considered valid. e.g.: with FeeTolerance = 20% -> (expectedFee <= burnedAmount <= expectedFee * 1.2)
* @return {boolean} isValid: : true if the burned amount is valid, false otherwise.
* @return {bigint} txFee: burned amount in nAVAX
*/
export const validateAvaxBurnedAmountEtna = ({
unsignedTx,
burnedAmount,
Expand All @@ -20,8 +30,8 @@ export const validateAvaxBurnedAmountEtna = ({
}: {
unsignedTx: UnsignedTx;
burnedAmount: bigint;
baseFee: bigint; // pvm dynamic fee caculator: @see https://github.com/ava-labs/avalanchego/blob/master/vms/platformvm/txs/fee/dynamic_calculator.go
feeTolerance: number; // tolerance percentage range where the burned amount is considered valid. e.g.: with FeeTolerance = 20% -> (expectedFee <= burnedAmount <= expectedFee * 1.2)
baseFee: bigint;
feeTolerance: number;
}): { isValid: boolean; txFee: bigint } => {
const tx = unsignedTx.getTx();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { testAddress1, testAddress2 } from '../fixtures/vms';
import { testContext } from '../fixtures/context';
import { Utxo } from '../serializable/avax/utxo';
import { utxoId } from '../fixtures/avax';
import { Address, Id } from '../serializable/fxs/common';
import { OutputOwners, TransferOutput } from '../serializable/fxs/secp256k1';
import { BigIntPr, Int } from '../serializable/primitives';
import { testAddress1, testAddress2 } from '../../fixtures/vms';
import { testContext } from '../../fixtures/context';
import { Utxo } from '../../serializable/avax/utxo';
import { utxoId } from '../../fixtures/avax';
import { Address, Id } from '../../serializable/fxs/common';
import { OutputOwners, TransferOutput } from '../../serializable/fxs/secp256k1';
import { BigIntPr, Int } from '../../serializable/primitives';
import {
newBaseTx as avmBaseTx,
newExportTx as avmExportTx,
newImportTx as avmImportTx,
} from '../vms/avm';
} from '../../vms/avm';
import {
newBaseTx as pvmBaseTx,
newExportTx as pvmExportTx,
Expand All @@ -24,12 +24,15 @@ import {
newAddPermissionlessDelegatorTx,
newRemoveSubnetValidatorTx,
newTransferSubnetOwnershipTx,
} from '../vms/pvm';
import { TransferableOutput } from '../serializable';
import { nodeId } from '../fixtures/common';
import { testSubnetId } from '../fixtures/transactions';
import { PrimaryNetworkID } from '../constants/networkIDs';
import { blsPublicKeyBytes, blsSignatureBytes } from '../fixtures/primitives';
} from '../../vms/pvm';
import { TransferableOutput } from '../../serializable';
import { nodeId } from '../../fixtures/common';
import { testSubnetId } from '../../fixtures/transactions';
import { PrimaryNetworkID } from '../../constants/networkIDs';
import {
blsPublicKeyBytes,
blsSignatureBytes,
} from '../../fixtures/primitives';
import { validateAvaxBurnedAmountPreEtna } from './validateAvaxBurnedAmountPreEtna';

const utxoMock = new Utxo(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Context } from '../vms/context/model';
import type { Context } from '../../vms/context/model';
import {
isAddDelegatorTx,
isAddPermissionlessDelegatorTx,
Expand All @@ -13,15 +13,24 @@ import {
isRemoveSubnetValidatorTx,
isTransferSubnetOwnershipTx,
isTransformSubnetTx,
} from '../serializable/pvm';
import type { UnsignedTx } from '../vms/common';
} from '../../serializable/pvm';
import type { UnsignedTx } from '../../vms/common';
import {
isAvmBaseTx,
isExportTx as isAvmExportTx,
isImportTx as isAvmImportTx,
} from '../serializable/avm';
import { PrimaryNetworkID } from '../constants/networkIDs';
} from '../../serializable/avm';
import { PrimaryNetworkID } from '../../constants/networkIDs';

/**
* Validate burned amount for pre-etna avalanche x/p transactions
*
* @param unsignedTx: unsigned transaction
* @param context
* @param burnedAmount: burned amount in nAVAX
* @return {boolean} isValid: : true if the burned amount is valid, false otherwise.
* @return {bigint} txFee: burned amount in nAVAX
*/
export const validateAvaxBurnedAmountPreEtna = ({
unsignedTx,
context,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import type { Context } from '../vms/context/model';
import type { Transaction, UnsignedTx } from '../vms/common';
import type { EVMTx } from '../serializable/evm';
import { isImportExportTx as isEvmImportExportTx } from '../serializable/evm';
import { getBurnedAmountByTx } from './getBurnedAmountByTx';
import type { AvaxTx } from '../serializable/avax';
import type { Context } from '../../vms/context/model';
import type { Transaction, UnsignedTx } from '../../vms/common';
import type { EVMTx } from '../../serializable/evm';
import { isImportExportTx as isEvmImportExportTx } from '../../serializable/evm';
import { getBurnedAmountByTx } from '../getBurnedAmountByTx';
import type { AvaxTx } from '../../serializable/avax';
import { validateEvmBurnedAmount } from './validateEvmBurnedAmount';
import type { GetUpgradesInfoResponse } from '../info/model';
import { isEtnaEnabled } from './isEtnaEnabled';
import type { GetUpgradesInfoResponse } from '../../info/model';
import { isEtnaEnabled } from '../isEtnaEnabled';
import { validateAvaxBurnedAmountEtna } from './validateAvaxBurnedAmountEtna';
import { validateAvaxBurnedAmountPreEtna } from './validateAvaxBurnedAmountPreEtna';
import {
isAvmBaseTx,
isExportTx as isAvmExportTx,
isImportTx as isAvmImportTx,
} from '../serializable/avm';
} from '../../serializable/avm';
import {
isAddDelegatorTx,
isAddValidatorTx,
isTransformSubnetTx,
} from '../serializable/pvm';
} from '../../serializable/pvm';

const _getBurnedAmount = (tx: Transaction, context: Context) => {
const burnedAmounts = getBurnedAmountByTx(tx as AvaxTx | EVMTx);
Expand All @@ -39,9 +39,16 @@ const isPreEtnaTx = (tx: Transaction) => {
};

/**
* baseFee:
* - evm fee: fetched from the network and converted into nAvax (https://docs.avax.network/quickstart/transaction-fees#c-chain-fees)
* - pvm dynamic fee caculator: @see https://github.com/ava-labs/avalanchego/blob/master/vms/platformvm/txs/fee/dynamic_calculator.go
* Validate burned amount for avalanche transactions
*
* @param unsignedTx: unsigned transaction
* @param burnedAmount: burned amount in nAVAX
* @param baseFee
** c-chain: fetched from the network and converted into nAvax (https://docs.avax.network/quickstart/transaction-fees#c-chain-fees)
** x/p-chain: pvm dynamic fee caculator, https://github.com/ava-labs/avalanchego/blob/master/vms/platformvm/txs/fee/dynamic_calculator.go
* @param feeTolerance: tolerance percentage range where the burned amount is considered valid. e.g.: with FeeTolerance = 20% -> (expectedFee <= burnedAmount <= expectedFee * 1.2)
* @return {boolean} isValid: : true if the burned amount is valid, false otherwise.
* @return {bigint} txFee: burned amount in nAVAX
*/
export const validateBurnedAmount = ({
unsignedTx,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { testAddress1, testEthAddress1 } from '../fixtures/vms';
import { testContext } from '../fixtures/context';
import { newExportTxFromBaseFee, newImportTxFromBaseFee } from '../vms/evm';
import { Utxo } from '../serializable/avax/utxo';
import { utxoId } from '../fixtures/avax';
import { Address, Id } from '../serializable/fxs/common';
import { OutputOwners, TransferOutput } from '../serializable/fxs/secp256k1';
import { BigIntPr, Int } from '../serializable/primitives';
import { testAddress1, testEthAddress1 } from '../../fixtures/vms';
import { testContext } from '../../fixtures/context';
import { newExportTxFromBaseFee, newImportTxFromBaseFee } from '../../vms/evm';
import { Utxo } from '../../serializable/avax/utxo';
import { utxoId } from '../../fixtures/avax';
import { Address, Id } from '../../serializable/fxs/common';
import { OutputOwners, TransferOutput } from '../../serializable/fxs/secp256k1';
import { BigIntPr, Int } from '../../serializable/primitives';
import { validateEvmBurnedAmount } from './validateEvmBurnedAmount';

const utxoMock = new Utxo(
Expand Down
46 changes: 46 additions & 0 deletions src/utils/validateBurnedAmount/validateEvmBurnedAmount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import type { UnsignedTx } from '../../vms/common';
import { isImportExportTx as isEvmImportExportTx } from '../../serializable/evm';
import { costCorethTx } from '../costs';

/**
* Validate burned amount for c-chain transactions
*
* @param unsignedTx: unsigned transaction
* @param burnedAmount: burned amount in nAVAX
* @param baseFee: fetched from the network and converted into nAvax (https://docs.avax.network/quickstart/transaction-fees#c-chain-fees)
* @param feeTolerance: tolerance percentage range where the burned amount is considered valid. e.g.: with FeeTolerance = 20% -> (expectedFee <= burnedAmount <= expectedFee * 1.2)
* @return {boolean} isValid: : true if the burned amount is valid, false otherwise.
* @return {bigint} txFee: burned amount in nAVAX
*/
export const validateEvmBurnedAmount = ({
unsignedTx,
burnedAmount,
baseFee,
feeTolerance,
}: {
unsignedTx: UnsignedTx;
burnedAmount: bigint;
baseFee: bigint;
feeTolerance: number;
}): { isValid: boolean; txFee: bigint } => {
const tx = unsignedTx.getTx();

if (!isEvmImportExportTx(tx)) {
throw new Error(`tx type is not supported`);
}

const feeToleranceInt = Math.floor(feeTolerance);

if (feeToleranceInt < 1 || feeToleranceInt > 100) {
throw new Error('feeTolerance must be [1,100]');
}

const feeAmount = baseFee * costCorethTx(unsignedTx);
const min = (feeAmount * (100n - BigInt(feeToleranceInt))) / 100n;
const max = (feeAmount * (100n + BigInt(feeToleranceInt))) / 100n;

return {
isValid: burnedAmount >= min && burnedAmount <= max,
txFee: burnedAmount,
};
};
36 changes: 0 additions & 36 deletions src/utils/validateEvmBurnedAmount.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/vms/pvm/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './builder';
export * from './models';
export * from './api';
export { calculateFee } from './txs/fee/calculator';

// Exposed Etna builder functions under `e` namespace
export * as e from './etna-builder';

0 comments on commit 7502549

Please sign in to comment.