Skip to content

Commit

Permalink
remove lxly support and polygon zkevm
Browse files Browse the repository at this point in the history
  • Loading branch information
prettyirrelevant committed Oct 11, 2024
1 parent f24f558 commit 7db20fb
Show file tree
Hide file tree
Showing 19 changed files with 25 additions and 1,507 deletions.
10 changes: 1 addition & 9 deletions backend/bridgebloc/apps/conversions/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@
ChainID.ARBITRUM_ONE: ConversionMethod.CCTP,
ChainID.AVALANCHE: ConversionMethod.CCTP,
ChainID.POLYGON_POS: ConversionMethod.CIRCLE_API,
ChainID.POLYGON_ZKEVM: ConversionMethod.LXLY,
},
ChainID.ETHEREUM_TESTNET: {
ChainID.ARBITRUM_ONE_TESTNET: ConversionMethod.CCTP,
ChainID.AVALANCHE_TESTNET: ConversionMethod.CCTP,
ChainID.POLYGON_POS_TESTNET: ConversionMethod.CIRCLE_API,
ChainID.POLYGON_ZKEVM_TESTNET: ConversionMethod.LXLY,
ChainID.POLYGON_POS_TESTNET: ConversionMethod.CIRCLE_API
},
ChainID.POLYGON_POS: {
ChainID.ETHEREUM: ConversionMethod.CIRCLE_API,
Expand All @@ -23,12 +21,6 @@
ChainID.ETHEREUM_TESTNET: ConversionMethod.CIRCLE_API,
ChainID.AVALANCHE_TESTNET: ConversionMethod.CIRCLE_API,
},
ChainID.POLYGON_ZKEVM: {
ChainID.ETHEREUM: ConversionMethod.LXLY,
},
ChainID.POLYGON_ZKEVM_TESTNET: {
ChainID.ETHEREUM_TESTNET: ConversionMethod.LXLY,
},
ChainID.AVALANCHE: {
ChainID.ETHEREUM: ConversionMethod.CCTP,
ChainID.ARBITRUM_ONE: ConversionMethod.CCTP,
Expand Down
5 changes: 0 additions & 5 deletions backend/bridgebloc/apps/conversions/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,3 @@ class CircleAPIConversionStepType(models.TextChoices):
class CCTPConversionStepType(models.TextChoices):
ATTESTATION_SERVICE_CONFIRMATION = 'attestation service confirmation'
SEND_TO_RECIPIENT = 'send to recipient'


class LxLyConversionStepType(models.TextChoices):
GET_MERKLE_PROOF = 'get merkle proof'
SEND_TO_RECIPIENT = 'send to recipient'
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,10 @@ class Migration(migrations.Migration):
(42161, 'Arbitrum One'),
(43114, 'Avalanche'),
(137, 'Polygon Pos'),
(1101, 'Polygon Zkevm'),
(5, 'Ethereum Testnet'),
(421613, 'Arbitrum One Testnet'),
(43113, 'Avalanche Testnet'),
(80001, 'Polygon Pos Testnet'),
(1442, 'Polygon Zkevm Testnet'),
],
validators=[django.core.validators.MinValueValidator(1)],
verbose_name='source chain',
Expand Down Expand Up @@ -63,7 +61,7 @@ class Migration(migrations.Migration):
(
'conversion_type',
models.CharField(
choices=[('cctp', 'Cctp'), ('lxly', 'Lxly'), ('circle_api', 'Circle Api')],
choices=[('cctp', 'Cctp'), ('circle_api', 'Circle Api')],
max_length=150,
verbose_name='conversion type',
),
Expand Down
3 changes: 0 additions & 3 deletions backend/bridgebloc/apps/conversions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ class TokenConversion(UUIDModel, TimestampedModel, models.Model):
2. For tokens bridged via CCTP:
This represents the equivalent value of the source_token in USDC.
3. For tokens bridged via LxLy:
This represents the equivalent value of the source_token in terms of the destination_token.
"""

@property
Expand Down
107 changes: 0 additions & 107 deletions backend/bridgebloc/apps/conversions/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
from .types import ConversionMethod
from .utils import (
get_cross_chain_bridge_deployment_address,
get_polygon_zkevm_bridge_deployment_address,
get_rollup_bridge_deployment_address,
get_token_messenger_deployment_address,
is_valid_route,
)
Expand Down Expand Up @@ -230,108 +228,3 @@ def _validate_tx_receipt( # pylint: disable=too-many-locals
'destination_address': to_checksum_address(bridge_deposit_received_event['recipient']),
'amount': usdc_token.convert_from_wei_to_token(bridge_deposit_received_event['amount']), # type: ignore[union-attr] # noqa: E501
}


class LxLyTokenConversionInitialisationSerializer(serializers.Serializer):
tx_hash = serializers.CharField(required=True)
source_chain = serializers.CharField(required=True)
destination_chain = serializers.CharField(required=True)

def validate(self, attrs: dict[str, Any]) -> dict[str, Any]:
try:
source_chain = ChainID.from_name(attrs['source_chain'])
destination_chain = ChainID.from_name(attrs['destination_chain'])
except ValueError as e:
raise serializers.ValidationError(str(e)) from e

if source_chain == destination_chain:
raise serializers.ValidationError('source_chain cannot be the same as destination_chain')

if source_chain.is_mainnet() != destination_chain.is_mainnet():
raise serializers.ValidationError(
'Both source_chain and destination_chain must be on the same network (testnet or mainnet)',
)

if not is_valid_route(source_chain, destination_chain, ConversionMethod.LXLY):
raise serializers.ValidationError('LxLy not supported for the source and destination chain')

evm_client = EVMAggregator().get_client(source_chain) # pylint:disable=no-value-for-parameter
tx_receipt = evm_client.get_transaction_receipt(attrs['tx_hash'])
validated_data = self._validate_tx_receipt(
client=evm_client,
receipt=tx_receipt,
source_chain=source_chain,
destination_chain=destination_chain,
)
attrs.update(validated_data)
return attrs

def _validate_tx_receipt( # pylint: disable=too-many-locals
self,
client: EVMClient,
receipt: TxReceipt,
source_chain: ChainID,
destination_chain: ChainID,
) -> dict[str, Any]:
rollup_bridge_address = get_rollup_bridge_deployment_address(source_chain)
rollup_bridge_contract = client.get_contract(name='RollupBridge', address=rollup_bridge_address)
polygon_zkevm_bridge_address = get_polygon_zkevm_bridge_deployment_address(source_chain)
polygon_zkevm_bridge_contract = client.get_contract(
name='PolygonZkEVMBridge',
address=polygon_zkevm_bridge_address,
)

found_rollup_bridge_events = rollup_bridge_contract.events.BridgeAsset().process_receipt(
receipt,
errors=DISCARD,
)
found_polygon_zkevm_bridge_events = polygon_zkevm_bridge_contract.events.BridgeEvent().process_receipt(
receipt,
errors=DISCARD,
)
if len(found_polygon_zkevm_bridge_events) != 1:
raise serializers.ValidationError(
f'Expected just one `BridgeEvent` event, got {len(found_polygon_zkevm_bridge_events)}',
)
if len(found_rollup_bridge_events) != 1:
raise serializers.ValidationError(
f'Expected just one `BridgeAsset` event, got {len(found_polygon_zkevm_bridge_events)}',
)

rollup_bridge_event = found_rollup_bridge_events[0].args
polygon_zkevm_bridge_event = found_polygon_zkevm_bridge_events[0].args

# Currently, we do not support ETH bridging via the API but keep in mind that the
# `originNetwork` and `destinationNetwork` are always the same for such scenarios
if polygon_zkevm_bridge_event['originNetwork'] != source_chain.to_lxly_domain():
raise serializers.ValidationError('lxly domain from event and serializer mismatch for source_chain')

if polygon_zkevm_bridge_event['destinationNetwork'] != destination_chain.to_lxly_domain():
raise serializers.ValidationError('lxly domain from event and serializer mismatch for destination chain')

try:
source_token = Token.objects.get(
chain_id=source_chain,
address=to_checksum_address(rollup_bridge_event['sourceToken']),
)
destination_token = Token.objects.get(
chain_id=destination_chain,
address=to_checksum_address(rollup_bridge_event['destinationToken']),
)
except Token.DoesNotExist as e:
raise serializers.ValidationError('Token is not supported currently') from e

return {
'source_token': source_token,
'source_chain': source_chain,
'destination_token': destination_token,
'destination_chain': destination_chain,
'leaf_type': polygon_zkevm_bridge_event['leafType'],
'bridged_amount': polygon_zkevm_bridge_event['amount'],
'deposit_count': polygon_zkevm_bridge_event['depositCount'],
'origin_network': polygon_zkevm_bridge_event['originNetwork'],
'origin_address': polygon_zkevm_bridge_event['originAddress'],
'destination_network': polygon_zkevm_bridge_event['destinationNetwork'],
'amount': destination_token.convert_from_wei_to_token(rollup_bridge_event['amount']),
'destination_address': to_checksum_address(polygon_zkevm_bridge_event['destinationAddress']),
}
96 changes: 0 additions & 96 deletions backend/bridgebloc/apps/conversions/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from .enums import (
CCTPConversionStepType,
CircleAPIConversionStepType,
LxLyConversionStepType,
TokenConversionStepStatus,
)
from .models import TokenConversionStep
Expand All @@ -27,8 +26,6 @@
get_attestation_client,
get_circle_api_client,
get_cross_chain_bridge_deployment_address,
get_merkle_proof_client,
get_polygon_zkevm_bridge_deployment_address,
)

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -292,96 +289,3 @@ def cctp_send_token_to_recipient() -> None:
except Exception:
logger.exception('An error occured while sending token to the recipient of a CCTP bridging process.')
continue


@db_periodic_task(crontab(minute='*/2'))
@lock_task('get-lxly-merkle-proofs-lock')
def get_lxly_merkle_proofs() -> None:
steps_requiring_merkle_proofs = TokenConversionStep.objects.select_related('conversion__destination_token').filter(
status=TokenConversionStepStatus.PENDING,
conversion__conversion_type=ConversionMethod.LXLY,
step_type=LxLyConversionStepType.GET_MERKLE_PROOF,
)
for step in steps_requiring_merkle_proofs:
try:
with transaction.atomic():
merkle_proof_client = get_merkle_proof_client(step.conversion.source_chain)
result = merkle_proof_client.get_merkle_proof(
deposit_count=step.metadata['deposit_count'],
origin_id=step.conversion.source_chain.to_lxly_domain(),
)
step.status = TokenConversionStepStatus.SUCCESSFUL
step.save()

TokenConversionStep.objects.create(
metadata={
'merkle_proof': result['proof']['merkle_proof'],
'main_exit_root': result['proof']['main_exit_root'],
'rollup_exit_root': result['proof']['rollup_exit_root'],
'bridged_amount': step.metadata['bridged_amount'],
'deposit_count': step.metadata['deposit_count'],
'origin_network': step.metadata['origin_network'],
'origin_address': step.metadata['origin_address'],
'destination_network': step.metadata['destination_network'],
'destination_address': step.metadata['destination_address'],
},
conversion=step.conversion,
status=TokenConversionStepStatus.PENDING,
step_type=LxLyConversionStepType.SEND_TO_RECIPIENT,
)
except Exception:
logger.exception('An error occurred while retrieving merkle proof for LxLy bridging process')
continue


@db_periodic_task(crontab(minute='*/3'))
@lock_task('claim-assets-at-destination-lxly-lock')
def claim_assets_at_destination_lxly() -> None:
claim_assets_steps = TokenConversionStep.objects.select_related('conversion__destination_token').filter(
status=TokenConversionStepStatus.PENDING,
conversion__conversion_type=ConversionMethod.LXLY,
step_type=LxLyConversionStepType.SEND_TO_RECIPIENT,
)
for step in claim_assets_steps:
try:
with transaction.atomic():
evm_client = EVMAggregator().get_client(step.conversion.destination_chain)
deployer = Account.from_key(settings.DEPLOYER_PRIVATE_KEY) # pylint: disable=no-value-for-parameter
contract = evm_client.get_contract(
name='PolygonZkEVMBridge',
address=get_polygon_zkevm_bridge_deployment_address(step.conversion.destination_chain),
)

is_claimed = contract.functions.isClaimed(step.metadata['deposit_count']).call()
if is_claimed:
step.status = TokenConversionStepStatus.SUCCESSFUL
step.save()
continue

send_to_recipient_fn_call = contract.functions.claimAsset(
[to_bytes(hexstr=i) for i in step.metadata['merkle_proof']],
step.metadata['deposit_count'],
to_bytes(hexstr=step.metadata['main_exit_root']),
to_bytes(hexstr=step.metadata['rollup_exit_root']),
step.metadata['origin_network'],
step.metadata['origin_address'],
step.metadata['destination_network'],
step.metadata['destination_address'],
step.metadata['bridged_amount'],
to_bytes(text=''),
)
unsigned_tx = send_to_recipient_fn_call.build_transaction(
TxParams(
{
'from': deployer.address,
'chainId': step.conversion.destination_chain.value,
},
),
)
tx_hash = evm_client.publish_transaction(tx_params=unsigned_tx, sender=deployer)
step.status = TokenConversionStepStatus.SUCCESSFUL
step.metadata['tx_hash'] = tx_hash.hex()
step.save()
except Exception:
logger.exception('An error occurred while sending bridged asset to recipient for LxLy')
continue
1 change: 0 additions & 1 deletion backend/bridgebloc/apps/conversions/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
@unique
class ConversionMethod(StrEnum):
CCTP = 'cctp'
LXLY = 'lxly'
CIRCLE_API = 'circle_api'

def __str__(self) -> str:
Expand Down
2 changes: 0 additions & 2 deletions backend/bridgebloc/apps/conversions/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
CCTPTokenConversionInitialisationAPIView,
CircleAPITokenConversionInitialisationAPIView,
CircleTokenConversionDepositTxHashUpdateAPIView,
LxLyTokenConversionInitialisationAPIView,
TokenConversionAPIView,
TokenConversionsAPIView,
ValidTokenConversionRoutesAPIView,
Expand All @@ -23,7 +22,6 @@
name='add-deposit-tx-hash',
),
path('conversions/cctp', CCTPTokenConversionInitialisationAPIView.as_view(), name='bridge-with-cctp'),
path('conversions/lxly', LxLyTokenConversionInitialisationAPIView.as_view(), name='bridge-with-lxly'),
path('conversions/routes', ValidTokenConversionRoutesAPIView.as_view(), name='valid-routes'),
path('conversions/<str:uuid>', TokenConversionAPIView.as_view(), name='get-conversion'),
]
31 changes: 0 additions & 31 deletions backend/bridgebloc/apps/conversions/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from bridgebloc.evm.types import ChainID
from bridgebloc.services.attestation import AttestationService
from bridgebloc.services.circle import CircleAPI
from bridgebloc.services.lxly_merkle_proof import MerkleProofService

from .constants import VALID_CONVERSION_ROUTES
from .types import ConversionMethod
Expand Down Expand Up @@ -63,26 +62,6 @@ def get_token_messenger_deployment_address(chain: ChainID) -> ChecksumAddress:
raise ValueError(f'TokenMessenger not deployed on chain {chain}')


def get_polygon_zkevm_bridge_deployment_address(chain: ChainID) -> ChecksumAddress:
if not chain.is_valid_lxly_chain():
raise ValueError(f'{chain} is not a valid CCTP chain')

if chain.is_mainnet():
return to_checksum_address(settings.POLYGON_ZKEVM_BRIDGE_DEPLOYED_ADDRESS)

return to_checksum_address(settings.POLYGON_ZKEVM_BRIDGE_TESTNET_DEPLOYED_ADDRESS)


def get_rollup_bridge_deployment_address(chain: ChainID) -> ChecksumAddress:
if not chain.is_valid_lxly_chain():
raise ValueError(f'{chain} is not a valid LxLy chain')

if chain.is_mainnet():
return to_checksum_address(settings.ROLLUP_BRIDGE_POLYGON_ZKEVM_DEPLOYED_ADDRESS)

return to_checksum_address(settings.ROLLUP_BRIDGE_POLYGON_ZKEVM_TESTNET_DEPLOYED_ADDRESS)


def get_attestation_client(chain: ChainID) -> AttestationService:
if not chain.is_valid_cctp_chain():
raise ValueError(f'{chain} is not a valid CCTP chain')
Expand All @@ -91,13 +70,3 @@ def get_attestation_client(chain: ChainID) -> AttestationService:
return AttestationService(base_url=settings.CIRCLE_ATTESTATION_BASE_URL)

return AttestationService(base_url=settings.CIRCLE_SANDBOX_ATTESTATION_BASE_URL)


def get_merkle_proof_client(chain: ChainID) -> MerkleProofService:
if not chain.is_valid_lxly_chain():
raise ValueError(f'{chain} is not a valid LxLy chain')

if chain.is_mainnet():
return MerkleProofService(base_url=settings.POLYGON_ZKEVM_MERKLE_PROOF_BASE_URL)

return MerkleProofService(base_url=settings.POLYGON_ZKEVM_MERKLE_PROOF_TESTNET_BASE_URL)
Loading

0 comments on commit 7db20fb

Please sign in to comment.