Skip to content

Commit

Permalink
[Issue-3855] Update convert number and add validate receive amount
Browse files Browse the repository at this point in the history
  • Loading branch information
S2kael committed Dec 19, 2024
1 parent 85d98ed commit e20df98
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
5 changes: 5 additions & 0 deletions packages/extension-base/src/background/errors/SwapError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ const defaultErrorMap: Record<SwapErrorType, { message: string, code?: number }>
MAKE_POOL_NOT_ENOUGH_EXISTENTIAL_DEPOSIT: {
message: detectTranslate('Insufficient liquidity to complete the swap. Lower your amount and try again'),
code: undefined
},
NOT_MEET_MIN_EXPECTED: {
// TODO: update message
message: detectTranslate('Received amount is too low. Get new quote and try again'),
code: undefined
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ interface ExchangeSimpleSwapData{
id: string;
trace_id: string;
address_from: string;
amount_to: string;
}

const apiUrl = 'https://api.simpleswap.io';
Expand Down Expand Up @@ -75,17 +76,20 @@ const fetchRanges = async (params: { fromSymbol: string; toSymbol: string }): Pr
async function getEstimate (request: SwapRequest, fromAsset: _ChainAsset, toAsset: _ChainAsset): Promise<{ toAmount: string; walletFeeAmount: string }> {
const fromSymbol = SIMPLE_SWAP_SUPPORTED_TESTNET_ASSET_MAPPING[fromAsset.slug];
const toSymbol = SIMPLE_SWAP_SUPPORTED_TESTNET_ASSET_MAPPING[toAsset.slug];
const assetDecimals = _getAssetDecimals(fromAsset);

if (!fromSymbol || !toSymbol) {
throw new SwapError(SwapErrorType.ASSET_NOT_SUPPORTED);
}

const formatedAmount = formatNumber(request.fromAmount, assetDecimals, (s) => s);

Check failure on line 85 in packages/extension-base/src/services/swap-service/handler/simpleswap-handler.ts

View workflow job for this annotation

GitHub Actions / Build Development Preview

Multiple spaces found before 'formatNumber'

const params = new URLSearchParams({
api_key: `${simpleSwapApiKey}`,
fixed: 'false',
currency_from: fromSymbol,
currency_to: toSymbol,
amount: formatNumber(request.fromAmount, _getAssetDecimals(fromAsset))
amount: formatedAmount
});

try {
Expand Down Expand Up @@ -115,12 +119,15 @@ async function getEstimate (request: SwapRequest, fromAsset: _ChainAsset, toAsse
}
}

const createSwapRequest = async (params: {fromSymbol: string; toSymbol: string; fromAmount: string; fromAsset: _ChainAsset; receiver: string; sender: string;}) => {
const createSwapRequest = async (params: {fromSymbol: string; toSymbol: string; fromAmount: string; fromAsset: _ChainAsset; receiver: string; sender: string; toAsset: _ChainAsset;}) => {
const fromDecimals = _getAssetDecimals(params.fromAsset);
const toDecimals = _getAssetDecimals(params.toAsset);
const formatedAmount = formatNumber(params.fromAmount, fromDecimals, (s) => s);

Check failure on line 125 in packages/extension-base/src/services/swap-service/handler/simpleswap-handler.ts

View workflow job for this annotation

GitHub Actions / Build Development Preview

Multiple spaces found before 'formatNumber'
const requestBody = {
fixed: false,
currency_from: params.fromSymbol,
currency_to: params.toSymbol,
amount: formatNumber(params.fromAmount, _getAssetDecimals(params.fromAsset)), // Convert to small number due to require of api
amount: formatedAmount, // Convert to small number due to require of api
address_to: params.receiver,
extra_id_to: '',
user_refund_address: params.sender,
Expand All @@ -145,7 +152,8 @@ const createSwapRequest = async (params: {fromSymbol: string; toSymbol: string;

return {
id: depositAddressResponse.id,
addressFrom: depositAddressResponse.address_from
addressFrom: depositAddressResponse.address_from,
amountTo: toBNString(depositAddressResponse.amount_to, toDecimals)
};
};

Expand Down Expand Up @@ -431,7 +439,19 @@ export class SimpleSwapHandler implements SwapBaseInterface {
const toSymbol = SIMPLE_SWAP_SUPPORTED_TESTNET_ASSET_MAPPING[toAsset.slug];

const { fromAmount } = quote;
const { addressFrom, id } = await createSwapRequest({ fromSymbol, toSymbol, fromAmount, fromAsset, receiver, sender });
const { addressFrom, id, amountTo } = await createSwapRequest({ fromSymbol, toSymbol, fromAmount, fromAsset, receiver, sender, toAsset });

Check failure on line 442 in packages/extension-base/src/services/swap-service/handler/simpleswap-handler.ts

View workflow job for this annotation

GitHub Actions / Build Development Preview

Expected object keys to be in sorted order. Expected amountTo to be before id

// Validate the amount to be swapped
const rate = BigN(amountTo).div(BigN(quote.toAmount)).multipliedBy(100);

console.debug('quote', quote.toAmount);
console.debug('amountTo', amountTo);
console.debug('rate', rate.toFixed());
if (rate.lt(95)) {

Check failure on line 450 in packages/extension-base/src/services/swap-service/handler/simpleswap-handler.ts

View workflow job for this annotation

GitHub Actions / Build Development Preview

Expected blank line before this statement
throw new SwapError(SwapErrorType.NOT_MEET_MIN_EXPECTED);
}

// Can modify quote.toAmount to amountTo after confirm real amount received

const txData: SimpleSwapTxData = {
id: id,
Expand Down
1 change: 1 addition & 0 deletions packages/extension-base/src/types/swap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export enum SwapErrorType {
NOT_ENOUGH_LIQUIDITY = 'NOT_ENOUGH_LIQUIDITY',
MAKE_POOL_NOT_ENOUGH_EXISTENTIAL_DEPOSIT = 'MAKE_POOL_NOT_ENOUGH_EXISTENTIAL_DEPOSIT',
AMOUNT_CANNOT_BE_ZERO = 'AMOUNT_CANNOT_BE_ZERO',
NOT_MEET_MIN_EXPECTED = 'NOT_MEET_MIN_EXPECTED',
}

export enum SwapStepType {
Expand Down

0 comments on commit e20df98

Please sign in to comment.