-
Notifications
You must be signed in to change notification settings - Fork 172
Validate burned amount for p-chain dynamic fee #880
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b685484
feat: add validateAvaxBurnedAmountEtna
ruijialin-avalabs e071a75
feat: adjust validateBurnedAmount params
ruijialin-avalabs 7502549
chore: move validateBurnedAmount into its own folder
ruijialin-avalabs 9011e08
chore: fixed validateAvaxBurnedAmountEtna check
ruijialin-avalabs c73618f
chore: address pr comments
ruijialin-avalabs 9d6b29e
chore: fix test
ruijialin-avalabs 9d97433
chore: update isEtnaSupported comment
ruijialin-avalabs 3b8af31
chore: make upgradesInfo optional
ruijialin-avalabs 1a00f63
chore: refactored validateDynamicBurnedAmount tests
ruijialin-avalabs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export const upgradesInfo = { | ||
apricotPhaselTime: '2020-12-05T05:00:00Z', | ||
apricotPhase2Time: '2020-12-05T05:00:00Z', | ||
apricotPhase3Time: '2020-12-05T05:00:00Z', | ||
apricotPhase4Time: '2020-12-05T05:00:00Z', | ||
apricotPhase4MinPChainHeight: 0, | ||
apricotPhase5Time: '2020-12-05T05:00:00Z', | ||
apricotPhasePre6Time: '2020-12-05T05:00:00Z', | ||
apricotPhase6Time: '2020-12-05T05:00:00Z', | ||
apricotPhasePost6Time: '2020-12-05T05:00:00Z', | ||
banffTime: '2020-12-05T05:00:00Z', | ||
cortinaTime: '2020-12-05T05:00:00Z', | ||
cortinaXChainStopVertexID: '11111111111111111111111111111111LpoYY', | ||
durangoTime: '2020-12-05T05:00:00Z', | ||
etnaTime: '2020-12-05T05:00:00Z', | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import type { GetUpgradesInfoResponse } from '../info/model'; | ||
|
||
export const isEtnaEnabled = ( | ||
upgradesInfo: GetUpgradesInfoResponse, | ||
): boolean => { | ||
const { etnaTime } = upgradesInfo; | ||
return new Date(etnaTime) < new Date(); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
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 { validateDynamicBurnedAmount } from './validateDynamicBurnedAmount'; | ||
import type { GetUpgradesInfoResponse } from '../../info/model'; | ||
import { isEtnaEnabled } from '../isEtnaEnabled'; | ||
import { validateStaticBurnedAmount } from './validateStaticBurnedAmount'; | ||
import { costCorethTx } from '../costs'; | ||
import { calculateFee } from '../../vms/pvm/txs/fee/calculator'; | ||
|
||
import { | ||
isAddPermissionlessDelegatorTx, | ||
isAddPermissionlessValidatorTx, | ||
isAddSubnetValidatorTx, | ||
isCreateChainTx, | ||
isCreateSubnetTx, | ||
isPvmBaseTx, | ||
isExportTx as isPvmExportTx, | ||
isImportTx as isPvmImportTx, | ||
isRemoveSubnetValidatorTx, | ||
isTransferSubnetOwnershipTx, | ||
} from '../../serializable/pvm'; | ||
|
||
const _getBurnedAmount = (tx: Transaction, context: Context) => { | ||
const burnedAmounts = getBurnedAmountByTx(tx as AvaxTx | EVMTx); | ||
return burnedAmounts.get(context.avaxAssetID) ?? 0n; | ||
}; | ||
|
||
// Check supported pvm transactions for Etna | ||
// Todo: add isAvmBaseTx, isAvmExportTx and isAvmImportTx when avm dynmamic fee is implemented | ||
const isEtnaSupported = (tx: Transaction) => { | ||
return ( | ||
// isAvmBaseTx(tx) || // not implemented | ||
// isAvmExportTx(tx) || // not implemented | ||
// isAvmImportTx(tx) || // not implemented | ||
isPvmBaseTx(tx) || | ||
isPvmExportTx(tx) || | ||
isPvmImportTx(tx) || | ||
isAddPermissionlessValidatorTx(tx) || | ||
isAddPermissionlessDelegatorTx(tx) || | ||
isAddSubnetValidatorTx(tx) || | ||
isCreateChainTx(tx) || | ||
isCreateSubnetTx(tx) || | ||
isRemoveSubnetValidatorTx(tx) || | ||
isTransferSubnetOwnershipTx(tx) | ||
); | ||
}; | ||
|
||
/** | ||
* 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, | ||
context, | ||
upgradesInfo, | ||
burnedAmount, | ||
baseFee, | ||
feeTolerance, | ||
}: { | ||
unsignedTx: UnsignedTx; | ||
context: Context; | ||
upgradesInfo?: GetUpgradesInfoResponse; | ||
burnedAmount?: bigint; | ||
baseFee: bigint; | ||
feeTolerance: number; | ||
}): { isValid: boolean; txFee: bigint } => { | ||
const tx = unsignedTx.getTx(); | ||
const burned = burnedAmount ?? _getBurnedAmount(tx, context); | ||
|
||
if ( | ||
isEvmImportExportTx(tx) || | ||
(upgradesInfo && isEtnaEnabled(upgradesInfo) && isEtnaSupported(tx)) | ||
) { | ||
const feeAmount = isEvmImportExportTx(tx) | ||
? baseFee * costCorethTx(unsignedTx) | ||
: calculateFee(tx, context.platformFeeConfig.weights, baseFee); | ||
return validateDynamicBurnedAmount({ | ||
burnedAmount: burned, | ||
feeAmount, | ||
feeTolerance, | ||
}); | ||
} | ||
return validateStaticBurnedAmount({ | ||
unsignedTx, | ||
context, | ||
burnedAmount: burned, | ||
}); | ||
}; |
68 changes: 68 additions & 0 deletions
68
src/utils/validateBurnedAmount/validateDynamicBurnedAmount.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { validateDynamicBurnedAmount } from './validateDynamicBurnedAmount'; | ||
|
||
describe('validateDynamicBurnedAmount', () => { | ||
it('throws an expected error if feeTolerance is less than 1', () => { | ||
expect(() => | ||
validateDynamicBurnedAmount({ | ||
burnedAmount: (280750n * 75n) / 100n, // 25% lower, | ||
feeAmount: 280750n, | ||
feeTolerance: 0.5, | ||
}), | ||
).toThrowError('feeTolerance must be [1,100]'); | ||
}); | ||
it('throws an expected error if feeTolerance is greater than 100', () => { | ||
expect(() => | ||
validateDynamicBurnedAmount({ | ||
burnedAmount: (280750n * 75n) / 100n, // 25% lower, | ||
feeAmount: 280750n, | ||
feeTolerance: 101, | ||
}), | ||
).toThrowError('feeTolerance must be [1,100]'); | ||
}); | ||
|
||
it('returns false if burned amount is over the tolerance range', () => { | ||
const resultHigher = validateDynamicBurnedAmount({ | ||
burnedAmount: (280750n * 151n) / 100n, // 51% higher | ||
feeAmount: 280750n, | ||
feeTolerance: 50.9, | ||
}); | ||
expect(resultHigher).toStrictEqual({ | ||
isValid: false, | ||
txFee: (280750n * 151n) / 100n, | ||
}); | ||
}); | ||
|
||
it('returns false if burned amount is below the tolerance range', () => { | ||
const resultLower = validateDynamicBurnedAmount({ | ||
burnedAmount: (280750n * 49n) / 100n, // 51% lower | ||
feeAmount: 280750n, | ||
feeTolerance: 50.9, | ||
}); | ||
expect(resultLower).toStrictEqual({ | ||
isValid: false, | ||
txFee: (280750n * 49n) / 100n, | ||
}); | ||
}); | ||
it('returns true if burned amount is within the min tolerance range', () => { | ||
const resultLower = validateDynamicBurnedAmount({ | ||
burnedAmount: (280750n * 75n) / 100n, // 25% lower | ||
feeAmount: 280750n, | ||
feeTolerance: 50.9, | ||
}); | ||
expect(resultLower).toStrictEqual({ | ||
isValid: true, | ||
txFee: (280750n * 75n) / 100n, | ||
}); | ||
}); | ||
it('returns true if burned amount is within the max tolerance range', () => { | ||
const resultHigher = validateDynamicBurnedAmount({ | ||
burnedAmount: (280750n * 125n) / 100n, // 25% higher | ||
feeAmount: 280750n, | ||
feeTolerance: 50.9, | ||
}); | ||
expect(resultHigher).toStrictEqual({ | ||
isValid: true, | ||
txFee: (280750n * 125n) / 100n, | ||
}); | ||
}); | ||
}); |
32 changes: 32 additions & 0 deletions
32
src/utils/validateBurnedAmount/validateDynamicBurnedAmount.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* Validate dynamic burned amount for avalanche c/p transactions | ||
* | ||
* @param burnedAmount: burned amount in nAVAX | ||
* @param feeAmount: fee | ||
* @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 validateDynamicBurnedAmount = ({ | ||
burnedAmount, | ||
feeAmount, | ||
feeTolerance, | ||
}: { | ||
burnedAmount: bigint; | ||
feeAmount: bigint; | ||
feeTolerance: number; | ||
}): { isValid: boolean; txFee: bigint } => { | ||
const feeToleranceInt = Math.floor(feeTolerance); | ||
|
||
if (feeToleranceInt < 1 || feeToleranceInt > 100) { | ||
throw new Error('feeTolerance must be [1,100]'); | ||
} | ||
|
||
const min = (feeAmount * (100n - BigInt(feeToleranceInt))) / 100n; | ||
const max = (feeAmount * (100n + BigInt(feeToleranceInt))) / 100n; | ||
|
||
return { | ||
isValid: burnedAmount >= min && burnedAmount <= max, | ||
txFee: burnedAmount, | ||
}; | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.