-
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.
Merge branch 'main' into fix/destination-address-format
- Loading branch information
Showing
16 changed files
with
414 additions
and
219 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
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 | ||
}; | ||
} |
Oops, something went wrong.