Skip to content
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

fix: amount to receive #196

Merged
merged 50 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 48 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
28e1ee2
fix: fixing erronous amount to receive displayed due to double subtra…
wainola May 13, 2024
3780105
fix: wrong amount displayed if user changes original value
wainola May 14, 2024
4484a38
chore: removing log
wainola May 14, 2024
3f7a9f8
chore: small refactor of some functions to avoid some race conditions
wainola May 22, 2024
ea04338
chore: small fixes
wainola May 22, 2024
cb9a62a
chore: removing log
wainola May 23, 2024
48e4084
Merge branch 'fix/amount-to-receive' of github.com:sygmaprotocol/sygm…
saadahmsiddiqui May 23, 2024
2e2eb99
chore: remove unused check
wainola May 23, 2024
df51abc
chore: pr review
wainola May 23, 2024
81cfc75
chore: fixing build
wainola May 23, 2024
5f70270
chore: pr review suggestions and small changes
wainola May 24, 2024
9c40d46
chore: fixing build
wainola May 24, 2024
1df0279
chore: adding new variable to display amount to receive
wainola May 27, 2024
5437f6d
Merge branch 'main' into fix/amount-to-receive
Lykhoyda May 28, 2024
ae58317
Merge branch 'main' into fix/amount-to-receive
Lykhoyda May 28, 2024
95e05cf
chore: changing variable state for substrate transaction build
wainola May 28, 2024
fbba63e
wip: debounce + flag
wainola May 29, 2024
40abc39
Merge branch 'fix/amount-to-receive' of github.com:sygmaprotocol/sygm…
saadahmsiddiqui May 29, 2024
0e4b661
added debounce input
saadahmsiddiqui May 29, 2024
0dc8e64
increase debounce time
saadahmsiddiqui May 29, 2024
b10473e
added fix for changing network selection
saadahmsiddiqui May 29, 2024
b1c2c65
fix: custom debounce
wainola May 30, 2024
e71b775
fix: remove dependency, fixing linting and build issues
wainola May 30, 2024
7275014
chore: fix tests
wainola May 30, 2024
3e84792
chore: update deps
wainola Jun 3, 2024
d041495
chore: remove commented code
wainola Jun 3, 2024
2aa84c3
chore: avoid re-asignement
wainola Jun 3, 2024
805c8d2
chore: restoring re-asignement
wainola Jun 3, 2024
c009d9b
chore: removing re-asignemt of resource amount
wainola Jun 3, 2024
0176aa8
reset stuff on build or estimation errors
saadahmsiddiqui Jun 3, 2024
355f6fd
chore: solving conflicts
wainola Jun 4, 2024
98cd816
chore: fixing lint
wainola Jun 4, 2024
b317ab0
chore: fixing tests
wainola Jun 4, 2024
fc726d8
chore: fixing tests
wainola Jun 4, 2024
08f3ae4
Merge branch 'main' into fix/amount-to-receive
Lykhoyda Jun 5, 2024
3423849
chore: addressing comments
wainola Jun 7, 2024
82bf8f3
chore: remove unused method
wainola Jun 7, 2024
8a1d687
chore: using constant in test
wainola Jun 7, 2024
588d171
Merge branch 'main' of github.com:sygmaprotocol/sygma-widget into fix…
saadahmsiddiqui Jun 7, 2024
8cad80c
Update packages/widget/src/components/resource-amount-selector/resour…
wainola Jun 7, 2024
f3f6cc6
Update packages/widget/src/controllers/transfers/fungible-token-trans…
wainola Jun 7, 2024
53ed022
Update packages/widget/src/controllers/transfers/fungible-token-trans…
wainola Jun 7, 2024
2c12d8b
Update packages/widget/src/controllers/transfers/evm/execute.ts
wainola Jun 7, 2024
35259b0
Update packages/widget/src/utils/gas.ts
wainola Jun 7, 2024
88a0922
chore: addressing comments
wainola Jun 7, 2024
81febd9
chore: generic arg for debounce
wainola Jun 7, 2024
22af7b1
chore: addressing comments
wainola Jun 7, 2024
3b160fa
chore: small change on request update call
wainola Jun 7, 2024
48cd4fd
Update packages/widget/src/controllers/transfers/evm/build.ts
wainola Jun 9, 2024
d1fd4b7
chore: fixing lint issues
wainola Jun 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import { customElement, property, state } from 'lit/decorators.js';
import { classMap } from 'lit/directives/class-map.js';
import { when } from 'lit/directives/when.js';
import { networkIconsMap } from '../../assets';
import { DEFAULT_ETH_DECIMALS } from '../../constants';
import { DEFAULT_ETH_DECIMALS, INPUT_DEBOUNCE_TIME } from '../../constants';
import {
BALANCE_UPDATE_KEY,
TokenBalanceController
} from '../../controllers/wallet-manager/token-balance';
import { tokenBalanceToNumber } from '../../utils/token';
import type { DropdownOption } from '../common/dropdown/dropdown';
import { BaseComponent } from '../common/base-component';
import { debounce } from '../../utils';
import { styles } from './styles';

@customElement('sygma-resource-amount-selector')
Expand Down Expand Up @@ -67,9 +68,7 @@ export class ResourceAmountSelector extends BaseComponent {
}
};

_onInputAmountChangeHandler = (event: Event): void => {
let { value } = event.target as HTMLInputElement;

_onInputAmountChangeHandler = (value: string): void => {
if (value === '') {
value = '0';
}
Expand All @@ -88,6 +87,11 @@ export class ResourceAmountSelector extends BaseComponent {
}
};

debouncedHandler = debounce(
this._onInputAmountChangeHandler,
INPUT_DEBOUNCE_TIME
);

requestUpdate(
name?: PropertyKey,
oldValue?: unknown,
Expand Down Expand Up @@ -200,7 +204,10 @@ export class ResourceAmountSelector extends BaseComponent {
type="number"
class="amountSelectorInput"
placeholder="0.000"
@input=${this._onInputAmountChangeHandler}
@input=${(event: Event) => {
this.debouncedHandler((event.target as HTMLInputElement).value);
}}
.disabled=${this.disabled}
.value=${this.amount}
/>
<section class="selectorSection">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { html } from 'lit';
import { customElement, property } from 'lit/decorators.js';
import '../../../context/wallet';
import { choose } from 'lit/directives/choose.js';
import { when } from 'lit/directives/when.js';
import type { Eip1193Provider } from 'packages/widget/src/interfaces';
import type { PropertyValues } from '@lit/reactive-element';
import {
Expand All @@ -22,7 +21,6 @@ import '../../network-selector';
import { BaseComponent } from '../../common';
import { Directions } from '../../network-selector/network-selector';
import { WalletController } from '../../../controllers';
import { tokenBalanceToNumber } from '../../../utils/token';
import { styles } from './styles';

@customElement('sygma-fungible-transfer')
Expand Down Expand Up @@ -103,29 +101,6 @@ export class FungibleTokenTransfer extends BaseComponent {
}
};

renderAmountOnDestination(): HTMLTemplateResult | null {
if (
this.transferController.selectedResource &&
this.transferController.pendingTransferTransaction !== undefined
) {
const { decimals, symbol } = this.transferController.selectedResource;
return html`
<div class="amountOnDestination">
<span> Amount to receive: </span>
<span>
${tokenBalanceToNumber(
this.transferController.resourceAmount,
decimals!,
4
)}
${symbol}
</span>
</div>
`;
}
return null;
}

renderTransferStatus(): HTMLTemplateResult {
return html` <section>
<sygma-transfer-status
Expand Down Expand Up @@ -181,7 +156,8 @@ export class FungibleTokenTransfer extends BaseComponent {
<sygma-resource-amount-selector
.sourceDomainConfig=${this.transferController.sourceDomainConfig}
.disabled=${!this.transferController.sourceNetwork ||
!this.transferController.destinationNetwork}
!this.transferController.destinationNetwork ||
this.transferController.isBuildingTransactions}
.resources=${this.transferController.supportedResources}
.onResourceSelected=${this.transferController.onResourceSelected}
>
Expand All @@ -196,10 +172,8 @@ export class FungibleTokenTransfer extends BaseComponent {
</sygma-address-input>
</section>
<section>
${when(this.transferController.destinationAddress, () =>
this.renderAmountOnDestination()
)}
<sygma-fungible-transfer-detail
.amountToReceive=${this.transferController.resourceAmountToDisplay}
.estimatedGasFee=${this.transferController.estimatedGas}
.selectedResource=${this.transferController.selectedResource}
.fee=${this.transferController.fee}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export class FungibleTransferDetail extends BaseComponent {
@property({ type: Object })
selectedResource?: Resource;

@property({ type: Object })
amountToReceive!: BigNumber;

@property({ type: Object })
sourceDomainConfig?: BaseConfig<Network>;

Expand Down Expand Up @@ -101,6 +104,20 @@ export class FungibleTransferDetail extends BaseComponent {
render(): HTMLTemplateResult {
return html`
<section class="transferDetail">
${when(
this.fee !== null,
() =>
html` <div class="transferDetailContainer">
<div class="transferDetailContainerLabel">Amount to receive:</div>
<div class="transferDetailContainerValue">
${tokenBalanceToNumber(
this.amountToReceive,
this.selectedResource?.decimals as number
)}
${this.selectedResource?.symbol}
</div>
</div>`
)}
${when(
this.fee !== null,
() =>
Expand Down
2 changes: 2 additions & 0 deletions packages/widget/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ export const SUBSTRATE_RPCS: {
2035: 'wss://phala.api.onfinality.io/public-ws'
}
};

export const INPUT_DEBOUNCE_TIME = 600;
121 changes: 73 additions & 48 deletions packages/widget/src/controllers/transfers/evm/build.ts
Original file line number Diff line number Diff line change
@@ -1,89 +1,114 @@
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,
wainola marked this conversation as resolved.
Show resolved Hide resolved
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(evmWallet.provider, evmWallet.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()
evmWallet.address,
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) {
wainola marked this conversation as resolved.
Show resolved Hide resolved
const { lowerBound, upperBound, percentage } = originalFee as PercentageFee;
const userInputAmount = this.resourceAmount;
const userInputAmount = resourceAmount;
//calculate amount without fee (percentage)
const feelessAmount = userInputAmount
wainola marked this conversation as resolved.
Show resolved Hide resolved
.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()
evmWallet.address,
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
};
}
23 changes: 22 additions & 1 deletion packages/widget/src/controllers/transfers/evm/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import {
Web3Provider,
type TransactionRequest
} from '@ethersproject/providers';
import type { PopulatedTransaction } from 'ethers';
import type { Eip1193Provider } from '../../../interfaces';
import { estimateEvmTransactionsGasCost } from '../../../utils/gas';
import {
FungibleTransferState,
type FungibleTokenTransferController
Expand All @@ -19,6 +22,7 @@ export async function executeNextEvmTransaction(
if (this.getTransferState() === FungibleTransferState.PENDING_APPROVALS) {
this.waitingUserConfirmation = true;
this.host.requestUpdate();
const transactions: PopulatedTransaction[] = [];
try {
const tx = await signer.sendTransaction(
this.pendingEvmApprovalTransactions[0] as TransactionRequest
Expand All @@ -28,14 +32,31 @@ export async function executeNextEvmTransaction(
this.host.requestUpdate();
await tx.wait();
this.pendingEvmApprovalTransactions.shift();

transactions.push(
...(this.pendingEvmApprovalTransactions as PopulatedTransaction[]),
this.pendingTransferTransaction as PopulatedTransaction
wainola marked this conversation as resolved.
Show resolved Hide resolved
);

this.estimatedGas = await estimateEvmTransactionsGasCost(
this.sourceNetwork?.chainId as number,
this.walletContext.value?.evmWallet?.provider as Eip1193Provider,
this.walletContext.value?.evmWallet?.address as string,
sztok7 marked this conversation as resolved.
Show resolved Hide resolved
transactions
);
} catch (e) {
console.log(e);
this.errorMessage = 'Approval transaction reverted or rejected';
} finally {
this.waitingUserConfirmation = false;
this.waitingTxExecution = false;
this.host.requestUpdate();
await this.estimateGas();
await estimateEvmTransactionsGasCost(
this.sourceNetwork?.chainId as number,
this.walletContext.value?.evmWallet?.provider as Eip1193Provider,
this.walletContext.value?.evmWallet?.address as string,
transactions
);
}
return;
}
Expand Down
Loading
Loading