Skip to content

Commit efdc740

Browse files
committed
add support affiliation fee in dln and fix get allowance
1 parent f38e468 commit efdc740

File tree

4 files changed

+51
-31
lines changed

4 files changed

+51
-31
lines changed

meta_aggregation_api/models/meta_agg_models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class ProviderPriceResponse(BaseModel):
1414
gas_price: str # gas price for the swap
1515
value: str # amount of native token that should be sent with the transaction
1616
price: str # price for buy_token in sell_token
17+
allowance_target: Optional[str]
1718

1819

1920
class MetaPriceModel(BaseModel):

meta_aggregation_api/providers/base_crosschain_provider.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ def __init__(
3434
self.aiohttp_session = session
3535
self.config = config
3636

37+
@abstractmethod
38+
def is_require_gas_price(self) -> bool:
39+
pass
40+
41+
3742
@abstractmethod
3843
async def get_swap_quote(
3944
self,

meta_aggregation_api/providers/debridge_dln_v1/debridge_dln_provider_v1.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ class DebridgeDlnProviderV1(CrossChainProvider):
3434
with open(Path(__file__).parent / 'config.json') as f:
3535
PROVIDER_NAME = ujson.load(f)['name']
3636

37+
def is_require_gas_price(self) -> bool:
38+
return True
39+
3740
async def _get_response(self, url: str, params: Optional[dict] = None) -> dict:
3841
async with self.aiohttp_session.get(
3942
url, params=params, timeout=self.REQUEST_TIMEOUT, ssl=ssl.SSLContext()
@@ -110,8 +113,8 @@ async def get_swap_price(
110113
'srcChainTokenInAmount': sell_amount,
111114
'dstChainId': chain_id_to,
112115
'dstChainTokenOut': buy_token,
113-
'affiliateFeePercent': 0,
114-
'prependOperatingExpenses': 'false',
116+
'affiliateFeePercent': int(buy_token_percentage_fee * 100),
117+
'prependOperatingExpenses': 'true',
115118
}
116119
try:
117120
response = await self._get_response(url, params)
@@ -153,7 +156,8 @@ async def get_swap_quote(
153156
'srcChainTokenInAmount': sell_amount,
154157
'dstChainId': chain_id_to,
155158
'dstChainTokenOut': buy_token,
156-
'affiliateFeePercent': 0,
159+
'affiliateFeePercent': int(buy_token_percentage_fee * 100),
160+
"affiliateFeeRecipient": taker_address,
157161
'dstChainTokenOutAmount': 'auto',
158162
'srcChainOrderAuthorityAddress': taker_address,
159163
'dstChainTokenOutRecipient': taker_address,
@@ -194,6 +198,7 @@ def _convert_response_from_swap_price(
194198
gas_price='0',
195199
value='0',
196200
price=str(price),
201+
allowance_target=response['tx']['allowanceTarget'],
197202
)
198203
except (KeyError, ValidationError) as e:
199204
e = self.handle_exception(

meta_aggregation_api/services/meta_aggregation_service.py

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -645,50 +645,59 @@ async def get_crosschain_provider_price(
645645
if not provider_instance:
646646
raise ProviderNotFound(provider)
647647

648-
spender_address = next(
649-
(
650-
spender['address']
651-
for spender in self.providers.get_providers_on_chain(chain_id_from)[
652-
'market_order'
653-
]
654-
if spender['name'] == provider
655-
),
656-
None,
648+
if provider_instance.is_require_gas_price():
649+
if not gas_price:
650+
gas_price = asyncio.create_task(
651+
self.gas_service.get_base_gas_price(chain_id_from)
652+
)
653+
gas_price = (
654+
await gas_price if isinstance(gas_price, asyncio.Task) else gas_price
655+
)
656+
657+
price = await provider_instance.get_swap_price(
658+
buy_token=buy_token,
659+
sell_token=sell_token,
660+
sell_amount=sell_amount,
661+
chain_id_from=chain_id_from,
662+
chain_id_to=chain_id_to,
663+
gas_price=gas_price,
664+
slippage_percentage=slippage_percentage,
665+
taker_address=taker_address,
666+
fee_recipient=fee_recipient,
667+
buy_token_percentage_fee=buy_token_percentage_fee,
657668
)
658669

659670
web3_url = get_web3_url(chain_id_from, config=self.config)
660671
erc20_contract = Web3Client(web3_url, self.config).get_erc20_contract(
661672
sell_token
662673
)
663-
if not gas_price:
664-
gas_price = asyncio.create_task(
665-
self.gas_service.get_base_gas_price(chain_id_from)
674+
675+
approve_cost = 0
676+
if price.allowance_target is None:
677+
spender_address = next(
678+
(
679+
spender['address']
680+
for spender in self.providers.get_providers_on_chain(chain_id_from)[
681+
'market_order'
682+
]
683+
if spender['name'] == provider
684+
),
685+
None,
666686
)
687+
688+
else:
689+
spender_address = price.allowance_target
690+
667691
allowance = await self.get_token_allowance(
668692
sell_token, spender_address, erc20_contract, taker_address
669693
)
670-
approve_cost = 0
671694
if allowance < sell_amount:
672695
approve_cost = await self.get_approve_cost(
673696
owner_address=taker_address,
674697
spender_address=spender_address,
675698
erc20_contract=erc20_contract,
676699
)
677-
gas_price = (
678-
await gas_price if isinstance(gas_price, asyncio.Task) else gas_price
679-
)
680-
price = await provider_instance.get_swap_price(
681-
buy_token=buy_token,
682-
sell_token=sell_token,
683-
sell_amount=sell_amount,
684-
chain_id_from=chain_id_from,
685-
chain_id_to=chain_id_to,
686-
gas_price=gas_price,
687-
slippage_percentage=slippage_percentage,
688-
taker_address=taker_address,
689-
fee_recipient=fee_recipient,
690-
buy_token_percentage_fee=buy_token_percentage_fee,
691-
)
700+
692701
return MetaPriceModel(
693702
provider=provider,
694703
price_response=price,

0 commit comments

Comments
 (0)