-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Substrate Percentage Fee (#297)
* substrate percentage fee * remove console * fix lint * add evm handler: * add missing parachain param * remove unused var * fix ts errors * fix fee tests * fix test * update imports * address comments --------- Co-authored-by: Matija Petrunić <[email protected]>
- Loading branch information
Showing
17 changed files
with
1,591 additions
and
1,214 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { calculateDynamicFee } from './dynamicFee.js'; | ||
export { calculateBasicfee } from './basicFee.js'; | ||
export { getFeeHandlerAddress } from './feeHandler.js'; | ||
export { getPercentageFee } from './percentageFee.js'; |
This file contains 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,50 @@ | ||
import { PercentageERC20FeeHandlerEVM__factory } from '@buildwithsygma/sygma-contracts'; | ||
import { ethers, utils } from 'ethers'; | ||
import { FeeHandlerType } from '../../../types'; | ||
import { EvmFee } from '../types'; | ||
|
||
/** | ||
* Calculates and returns the fee in native currency. | ||
* | ||
* @category Fee | ||
* @param {Object} - Object to get the fee data | ||
* @returns {Promise<FeeDataResult>} | ||
*/ | ||
export const getPercentageFee = async ({ | ||
precentageFeeHandlerAddress, | ||
provider, | ||
sender, | ||
fromDomainID, | ||
toDomainID, | ||
resourceID, | ||
depositData, | ||
}: { | ||
precentageFeeHandlerAddress: string; | ||
provider: ethers.providers.Provider; | ||
sender: string; | ||
fromDomainID: number; | ||
toDomainID: number; | ||
resourceID: string; | ||
depositData: string; | ||
}): Promise<EvmFee> => { | ||
const percentageFeeHandlerContract = PercentageERC20FeeHandlerEVM__factory.connect( | ||
precentageFeeHandlerAddress, | ||
provider, | ||
); | ||
const calculatedFee = await percentageFeeHandlerContract.calculateFee( | ||
sender, | ||
fromDomainID, | ||
toDomainID, | ||
resourceID, | ||
depositData, | ||
utils.formatBytes32String(''), | ||
); | ||
|
||
const [fee] = calculatedFee; | ||
return { | ||
fee, | ||
feeData: fee.toHexString(), | ||
type: FeeHandlerType.PERCENTAGE, | ||
handlerAddress: precentageFeeHandlerAddress, | ||
}; | ||
}; |
This file contains 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 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 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 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 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,41 @@ | ||
import { ApiPromise } from '@polkadot/api'; | ||
import { Enum, Option } from '@polkadot/types'; | ||
import { FeeHandlerType } from '../../../types'; | ||
import { XcmMultiAssetIdType } from '../types'; | ||
/** | ||
* Retrieves the fee handler for a given domainId and asset. | ||
* | ||
* @category Fee | ||
* @param {ApiPromise} api - The Substrate API instance. | ||
* @param {number} domainId - The ID of the domain. | ||
* @param {XcmMultiAssetIdType} xcmMultiAssetId - The XCM MultiAsset ID of the asset. {@link https://github.com/sygmaprotocol/sygma-substrate-pallets#multiasset | More details} | ||
* @returns {Promise} A promise that resolves to a FeeHandlerType object. | ||
* @throws {Error} Unable to retrieve fee from pallet | ||
*/ | ||
export const getFeeHandler = async ( | ||
api: ApiPromise, | ||
destinationDomainId: number, | ||
xcmMultiAssetId: XcmMultiAssetIdType, | ||
): Promise<FeeHandlerType> => { | ||
const feeHandler = await api.query.sygmaFeeHandlerRouter.handlerType<Option<Enum>>([ | ||
destinationDomainId, | ||
xcmMultiAssetId, | ||
]); | ||
|
||
if (feeHandler.isNone) { | ||
throw new Error('No Fee Handler configured'); | ||
} | ||
|
||
const feeHandlerName = feeHandler.unwrap().toString(); | ||
|
||
switch (feeHandlerName) { | ||
case 'PercentageFeeHandler': | ||
return FeeHandlerType.PERCENTAGE; | ||
case 'BasicFeeHandler': | ||
return FeeHandlerType.BASIC; | ||
case 'DynamicFeeHandler': | ||
return FeeHandlerType.DYNAMIC; | ||
default: | ||
throw new Error('Invalid Fee Handler Type'); | ||
} | ||
}; |
60 changes: 60 additions & 0 deletions
60
packages/sdk/src/chains/Substrate/utils/getPercentageFee.ts
This file contains 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,60 @@ | ||
import { ApiPromise } from '@polkadot/api'; | ||
import { BN } from '@polkadot/util'; | ||
import { Option, Tuple } from '@polkadot/types'; | ||
import { SubstrateFee } from '../types'; | ||
import { FeeHandlerType, Fungible, Transfer, SubstrateResource } from '../../../types'; | ||
|
||
/** | ||
* Retrieves the basic fee for a given domainId and asset. | ||
* | ||
* @example | ||
* // Assuming the api instance is already connected and ready to use | ||
* const domainId = 1; | ||
* const xcmMultiAssetId = {...} // XCM MultiAsset ID of the asset | ||
* getBasicFee(api, domainId, xcmMultiAssetId) | ||
* .catch((error) => { | ||
* console.error('Error fetching basic fee:', error); | ||
* }); | ||
* | ||
* @category Fee | ||
* @param {ApiPromise} api - The Substrate API instance. | ||
* @param {number} domainId - The ID of the domain. | ||
* @param {XcmMultiAssetIdType} xcmMultiAssetId - The XCM MultiAsset ID of the asset. {@link https://github.com/sygmaprotocol/sygma-substrate-pallets#multiasset | More details} | ||
* @returns {Promise<SubstrateFee>} A promise that resolves to a SubstrateFee object. | ||
* @throws {Error} Unable to retrieve fee from pallet | ||
*/ | ||
export const getPercentageFee = async ( | ||
api: ApiPromise, | ||
transfer: Transfer<Fungible>, | ||
): Promise<SubstrateFee> => { | ||
const assetId = (transfer.resource as SubstrateResource).xcmMultiAssetId; | ||
|
||
const feeStructure = await api.query.sygmaPercentageFeeHandler.assetFeeRate<Option<Tuple>>([ | ||
transfer.to.id, | ||
assetId, | ||
]); | ||
if (feeStructure.isNone) { | ||
throw new Error('Error retrieving fee'); | ||
} | ||
|
||
const [feeRate, min, max] = feeStructure | ||
.unwrap() | ||
.toArray() | ||
.map(val => new BN(val.toString())); | ||
|
||
const calculatedFee = new BN(transfer.details.amount).mul(feeRate).div(new BN(10000)); | ||
|
||
let fee: BN; | ||
if (calculatedFee.lt(min)) { | ||
fee = min; | ||
} else if (calculatedFee.gt(max)) { | ||
fee = max; | ||
} else { | ||
fee = calculatedFee; | ||
} | ||
|
||
return { | ||
fee: fee, | ||
type: FeeHandlerType.PERCENTAGE, | ||
}; | ||
}; |
This file contains 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 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 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 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
Oops, something went wrong.