-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
<!--- Provide a general summary of your changes in the Title above --> ## Description Fixes displayed amount to receive due to double subtraction over the already subtracted value <!--- Describe your changes in detail --> ## Related Issue Or Context <!--- If suggesting a new feature or change, please discuss it in an issue first --> <!--- If fixing a bug, there should be an issue describing it with steps to reproduce --> <!--- Otherwise, describe context and motivation for change here --> Closes: #192 ## How Has This Been Tested? Testing details. <!--- Please describe in detail how you tested your changes. --> <!--- Include details of your testing environment, and the tests you ran to --> <!--- see how your change affects other areas of the code, etc. --> ## Types of changes <!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation ## Checklist: <!--- Go over all the following points, and put an `x` in all the boxes that apply. --> <!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> - [ ] I have commented my code, particularly in hard-to-understand areas. - [ ] I have ensured that all acceptance criteria (or expected behavior) from issue are met - [ ] I have updated the documentation locally and in sygma-docs. - [ ] I have added tests to cover my changes. - [ ] I have ensured that all the checks are passing and green, I've signed the CLA bot --------- Co-authored-by: Saad Ahmed Siddiqui <[email protected]> Co-authored-by: Anton Lykhoyda <[email protected]> Co-authored-by: Filip Štoković <[email protected]>
- Loading branch information
1 parent
be23500
commit 8c2cdad
Showing
12 changed files
with
371 additions
and
189 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
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,89 +1,111 @@ | ||
import { | ||
EVMAssetTransfer, | ||
FeeHandlerType, | ||
type PercentageFee | ||
FeeHandlerType | ||
} from '@buildwithsygma/sygma-sdk-core'; | ||
import type { | ||
Domain, | ||
Environment, | ||
EvmFee, | ||
PercentageFee | ||
} from '@buildwithsygma/sygma-sdk-core'; | ||
import { Web3Provider } from '@ethersproject/providers'; | ||
import type { UnsignedTransaction, BigNumber } from 'ethers'; | ||
import { constants, utils } from 'ethers'; | ||
import { type FungibleTokenTransferController } from '../fungible-token-transfer'; | ||
import type { EvmWallet } from 'packages/widget/src/context'; | ||
|
||
type BuildEvmFungibleTransactionsArtifacts = { | ||
pendingEvmApprovalTransactions: UnsignedTransaction[]; | ||
pendingTransferTransaction: UnsignedTransaction; | ||
fee: EvmFee; | ||
resourceAmount: BigNumber; | ||
}; | ||
|
||
/** | ||
* @dev If we did proper validation this shouldn't throw. | ||
* Not sure how to handle if it throws :shrug: | ||
*/ | ||
export async function buildEvmFungibleTransactions( | ||
this: FungibleTokenTransferController | ||
): Promise<void> { | ||
//we already check that but this prevents those typescript errors | ||
const provider = this.walletContext.value?.evmWallet?.provider; | ||
const providerChaiId = this.walletContext.value?.evmWallet?.providerChainId; | ||
const address = this.walletContext.value?.evmWallet?.address; | ||
if ( | ||
!this.sourceNetwork || | ||
!this.destinationNetwork || | ||
!this.resourceAmount || | ||
!this.selectedResource || | ||
!this.destinationAddress || | ||
!provider || | ||
!address || | ||
providerChaiId !== this.sourceNetwork.chainId | ||
) { | ||
this.estimatedGas = undefined; | ||
this.resetFee(); | ||
return; | ||
} | ||
|
||
export async function buildEvmFungibleTransactions({ | ||
evmWallet: { address, provider, providerChainId }, | ||
chainId, | ||
destinationAddress, | ||
resourceId, | ||
resourceAmount, | ||
env, | ||
pendingEvmApprovalTransactions, | ||
pendingTransferTransaction, | ||
fee | ||
}: { | ||
evmWallet: EvmWallet; | ||
chainId: number; | ||
destinationAddress: string; | ||
resourceId: string; | ||
resourceAmount: BigNumber; | ||
env: Environment; | ||
pendingEvmApprovalTransactions: UnsignedTransaction[]; | ||
pendingTransferTransaction: UnsignedTransaction; | ||
sourceNetwork: Domain | null; | ||
fee: EvmFee; | ||
}): Promise<BuildEvmFungibleTransactionsArtifacts> { | ||
const evmTransfer = new EVMAssetTransfer(); | ||
await evmTransfer.init(new Web3Provider(provider, providerChaiId), this.env); | ||
await evmTransfer.init(new Web3Provider(provider, providerChainId), env); | ||
|
||
// Hack to make fungible transfer behave like it does on substrate side | ||
// where fee is deducted from user inputted amount rather than added on top | ||
const originalTransfer = await evmTransfer.createFungibleTransfer( | ||
address, | ||
this.destinationNetwork.chainId, | ||
this.destinationAddress, | ||
this.selectedResource.resourceId, | ||
this.resourceAmount.toString() | ||
chainId, | ||
destinationAddress, | ||
resourceId, | ||
resourceAmount.toString() | ||
); | ||
const originalFee = await evmTransfer.getFee(originalTransfer); | ||
// NOTE: for percentage fee, if both are equal, it means we can calculate the amount with fee avoiding second subtraction | ||
const calculateAmountWithFee = originalFee.type === FeeHandlerType.PERCENTAGE; | ||
|
||
//in case of percentage fee handler, we are calculating what amount + fee will result int user inputed amount | ||
//in case of fixed(basic) fee handler, fee is taken from native token | ||
if (originalFee.type === FeeHandlerType.PERCENTAGE) { | ||
if (calculateAmountWithFee) { | ||
const { lowerBound, upperBound, percentage } = originalFee as PercentageFee; | ||
const userInputAmount = this.resourceAmount; | ||
const userInputAmount = resourceAmount; | ||
//calculate amount without fee (percentage) | ||
const feelessAmount = userInputAmount | ||
.mul(constants.WeiPerEther) | ||
.div(utils.parseEther(String(1 + percentage))); | ||
|
||
const calculatedFee = userInputAmount.sub(feelessAmount); | ||
this.resourceAmount = feelessAmount; | ||
resourceAmount = feelessAmount; | ||
//if calculated percentage fee is less than lower fee bound, substract lower bound from user input. If lower bound is 0, bound is ignored | ||
if (calculatedFee.lt(lowerBound) && lowerBound.gt(0)) { | ||
this.resourceAmount = userInputAmount.sub(lowerBound); | ||
resourceAmount = userInputAmount.sub(lowerBound); | ||
} | ||
//if calculated percentage fee is more than upper fee bound, substract upper bound from user input. If upper bound is 0, bound is ignored | ||
if (calculatedFee.gt(upperBound) && upperBound.gt(0)) { | ||
this.resourceAmount = userInputAmount.sub(upperBound); | ||
resourceAmount = userInputAmount.sub(upperBound); | ||
} | ||
} | ||
|
||
const transfer = await evmTransfer.createFungibleTransfer( | ||
address, | ||
this.destinationNetwork.chainId, | ||
this.destinationAddress, | ||
this.selectedResource.resourceId, | ||
this.resourceAmount.toString() | ||
chainId, | ||
destinationAddress, | ||
resourceId, | ||
resourceAmount.toString() | ||
); | ||
this.fee = await evmTransfer.getFee(transfer); | ||
this.pendingEvmApprovalTransactions = await evmTransfer.buildApprovals( | ||
fee = await evmTransfer.getFee(transfer); | ||
|
||
pendingEvmApprovalTransactions = await evmTransfer.buildApprovals( | ||
transfer, | ||
this.fee | ||
fee | ||
); | ||
this.pendingTransferTransaction = await evmTransfer.buildTransferTransaction( | ||
|
||
pendingTransferTransaction = await evmTransfer.buildTransferTransaction( | ||
transfer, | ||
this.fee | ||
fee | ||
); | ||
await this.estimateGas(); | ||
this.host.requestUpdate(); | ||
return { | ||
pendingEvmApprovalTransactions, | ||
pendingTransferTransaction, | ||
fee, | ||
resourceAmount | ||
}; | ||
} |
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.