Skip to main content

Bridge

This contract is responsible for cross-asset swaps using the Synthetix protocol as the bridging exchange. There are three types of supported cross-asset swaps, tokenToSynth, synthToToken, and tokenToToken.

1) tokenToSynth Swaps a supported token in a gondola pool to any synthetic asset (e.g. tBTC -> sAAVE).

2) synthToToken Swaps any synthetic asset to a suported token in a gondola pool (e.g. sDEFI -> USDC).

3) tokenToToken Swaps a supported token in a gondola pool to one in another pool (e.g. renBTC -> DAI).

Due to the settlement periods of synthetic assets, the users must wait until the trades can be completed. Users will receive an ERC721 token that represents pending cross-asset swap. Once the waiting period is over, the trades can be settled and completed by calling the completeToSynth or the completeToToken function. In the cases of pending synthToToken or tokenToToken swaps, the owners of the pending swaps can also choose to withdraw the bridging synthetic assets instead of completing the swap.

Functions#

constructor#

function constructor(
) public

Deploys this contract and initializes the master version of the SynthSwapper contract. The address to the Synthetix protocol's Exchanger contract is also set on deployment.

getProxyAddressFromTargetSynthKey#

function getProxyAddressFromTargetSynthKey(
bytes32 synthKey
) public returns (contract IERC20)

Returns the address of the proxy contract targeting the synthetic asset with the given synthKey.

Parameters:#

NameTypeDescription
synthKeybytes32the currency key of the synth

Return Values:#

NameTypeDescription
addressbytes32of the proxy contract

getPendingSwapInfo#

function getPendingSwapInfo(
uint256 itemId
) external returns (enum Bridge.PendingSwapType swapType, uint256 secsLeft, address synth, uint256 synthBalance, address tokenTo)

Returns various information of a pending swap represented by the given itemId. Information includes the type of the pending swap, the number of seconds left until it can be settled, the address and the balance of the synth this swap currently holds, and the address of the destination token.

Parameters:#

NameTypeDescription
itemIduint256ID of the pending swap

Return Values:#

NameTypeDescription
swapTypeuint256the type of the pending virtual swap,

secsLeft number of seconds left until this swap can be settled, synth address of the synth this swap uses, synthBalance amount of the synth this swap holds, tokenTo the address of the destination token

_settle#

function _settle(
) internal

withdraw#

function withdraw(
uint256 itemId,
uint256 amount
) external

Settles and withdraws the synthetic asset without swapping it to a token in a Gondola pool. Only the owner of the ERC721 token of itemId can call this function. Reverts if the given itemId does not represent a synthToToken or a tokenToToken swap.

Parameters:#

NameTypeDescription
itemIduint256ID of the pending swap
amountuint256the amount of the synth to withdraw

completeToSynth#

function completeToSynth(
uint256 itemId
) external

Completes the pending tokenToSynth swap by settling and withdrawing the synthetic asset. Reverts if the given itemId does not represent a tokenToSynth swap.

Parameters:#

NameTypeDescription
itemIduint256ERC721 token ID representing a pending tokenToSynth swap

calcCompleteToToken#

function calcCompleteToToken(
uint256 itemId,
uint256 swapAmount
) external returns (uint256)

Calculates the expected amount of the token to receive on calling completeToToken() with the given swapAmount.

Parameters:#

NameTypeDescription
itemIduint256ERC721 token ID representing a pending SynthToToken or TokenToToken swap
swapAmountuint256the amount of bridging synth to swap from

Return Values:#

NameTypeDescription
expecteduint256amount of the token the user will receive

completeToToken#

function completeToToken(
uint256 itemId,
uint256 swapAmount,
uint256 minAmount,
uint256 deadline
) external

Completes the pending SynthToToken or TokenToToken swap by settling the bridging synth and swapping it to the desired token. Only the owners of the pending swaps can call this function.

Parameters:#

NameTypeDescription
itemIduint256ERC721 token ID representing a pending SynthToToken or TokenToToken swap
swapAmountuint256the amount of bridging synth to swap from
minAmountuint256the minimum amount of the token to receive - reverts if this amount is not reached
deadlineuint256the timestamp representing the deadline for this transaction - reverts if deadline is not met

_addToPendingSynthSwapList#

function _addToPendingSynthSwapList(
) internal returns (uint256)

_addToPendingSynthToTokenSwapList#

function _addToPendingSynthToTokenSwapList(
) internal returns (uint256)

calcTokenToSynth#

function calcTokenToSynth(
contract ISwap swap,
uint8 tokenFromIndex,
bytes32 synthOutKey,
uint256 tokenInAmount
) external returns (uint256)

Calculates the expected amount of the desired synthetic asset the caller will receive after completing a TokenToSynth swap with the given parameters. This calculation does not consider the settlement periods.

Parameters:#

NameTypeDescription
swapcontract ISwapthe address of a Gondola pool to use to swap the given token to a bridging synth
tokenFromIndexuint8the index of the token to swap from
synthOutKeybytes32the currency key of the desired synthetic asset
tokenInAmountuint256the amount of the token to swap form

Return Values:#

NameTypeDescription
thecontract ISwapexpected amount of the desired synth

tokenToSynth#

function tokenToSynth(
contract ISwap swap,
uint8 tokenFromIndex,
bytes32 synthOutKey,
uint256 tokenInAmount,
uint256 minAmount
) external returns (uint256)

Initiates a cross-asset swap from a token supported in the swap pool to any synthetic asset. The caller will receive an ERC721 token representing their ownership of the pending cross-asset swap.

Parameters:#

NameTypeDescription
swapcontract ISwapthe address of a Gondola pool to use to swap the given token to a bridging synth
tokenFromIndexuint8the index of the token to swap from
synthOutKeybytes32the currency key of the desired synthetic asset
tokenInAmountuint256the amount of the token to swap form
minAmountuint256the amount of the token to swap form

Return Values:#

NameTypeDescription
IDcontract ISwapof the ERC721 token sent to the caller

calcSynthToToken#

function calcSynthToToken(
contract ISwap swap,
bytes32 synthInKey,
uint8 tokenToIndex,
uint256 synthInAmount
) external returns (uint256, uint256)

Calculates the expected amount of the desired token the caller will receive after completing a SynthToToken swap with the given parameters. This calculation does not consider the settlement periods or any potential changes of the swap pool composition.

Parameters:#

NameTypeDescription
swapcontract ISwapthe address of a Gondola pool to use to swap the given token to a bridging synth
synthInKeybytes32the currency key of the synth to swap from
tokenToIndexuint8the index of the token to swap to
synthInAmountuint256the amount of the synth to swap form

Return Values:#

NameTypeDescription
thecontract ISwapexpected amount of the bridging synth and the expected amount of the desired token

synthToToken#

function synthToToken(
contract ISwap swap,
bytes32 synthInKey,
uint8 tokenToIndex,
uint256 synthInAmount,
uint256 minMediumSynthAmount
) external returns (uint256)

Initiates a cross-asset swap from a synthetic asset to a supported token. The caller will receive an ERC721 token representing their ownership of the pending cross-asset swap.

Parameters:#

NameTypeDescription
swapcontract ISwapthe address of a Gondola pool to use to swap the given token to a bridging synth
synthInKeybytes32the currency key of the synth to swap from
tokenToIndexuint8the index of the token to swap to
synthInAmountuint256the amount of the synth to swap form
minMediumSynthAmountuint256the minimum amount of the bridging synth at pre-settlement stage

Return Values:#

NameTypeDescription
thecontract ISwapID of the ERC721 token sent to the caller

calcTokenToToken#

function calcTokenToToken(
contract ISwap[2] swaps,
uint8 tokenFromIndex,
uint8 tokenToIndex,
uint256 tokenFromAmount
) external returns (uint256, uint256)

Calculates the expected amount of the desired token the caller will receive after completing a TokenToToken swap with the given parameters. This calculation does not consider the settlement periods or any potential changes of the pool compositions.

Parameters:#

NameTypeDescription
swapscontract ISwap[2]the addresses of the two Gondola pools used to do the cross-asset swap
tokenFromIndexuint8the index of the token in the first swaps pool to swap from
tokenToIndexuint8the index of the token in the second swaps pool to swap to
tokenFromAmountuint256the amount of the token to swap from

Return Values:#

NameTypeDescription
thecontract ISwap[2]expected amount of bridging synth at pre-settlement stage and the expected amount of the desired

token

tokenToToken#

function tokenToToken(
contract ISwap[2] swaps,
uint8 tokenFromIndex,
uint8 tokenToIndex,
uint256 tokenFromAmount,
uint256 minMediumSynthAmount
) external returns (uint256)

Initiates a cross-asset swap from a token in one Gondola pool to one in another. The caller will receive an ERC721 token representing their ownership of the pending cross-asset swap.

Parameters:#

NameTypeDescription
swapscontract ISwap[2]the addresses of the two Gondola pools used to do the cross-asset swap
tokenFromIndexuint8the index of the token in the first swaps pool to swap from
tokenToIndexuint8the index of the token in the second swaps pool to swap to
tokenFromAmountuint256the amount of the token to swap from
minMediumSynthAmountuint256the minimum amount of the bridging synth at pre-settlement stage

Return Values:#

NameTypeDescription
thecontract ISwap[2]ID of the ERC721 token sent to the caller

setSynthIndex#

function setSynthIndex(
contract ISwap swap,
uint8 synthIndex,
bytes32 currencyKey
) external

Registers the index and the address of the supported synth from the given swap pool. The matching currency key must be supplied for a successful registration.

Parameters:#

NameTypeDescription
swapcontract ISwapthe address of the pool that contains the synth
synthIndexuint8the index of the supported synth in the given swap pool
currencyKeybytes32the currency key of the synth in bytes32 form

getSynthIndex#

function getSynthIndex(
contract ISwap swap
) public returns (uint8)

Returns the index of the supported synth in the given swap pool. Reverts if the swap pool is not registered.

Parameters:#

NameTypeDescription
swapcontract ISwapthe address of the pool that contains the synth

Return Values:#

NameTypeDescription
thecontract ISwapindex of the supported synth

getSynthAddress#

function getSynthAddress(
contract ISwap swap
) public returns (address)

Returns the address of the supported synth in the given swap pool. Reverts if the swap pool is not registered.

Parameters:#

NameTypeDescription
swapcontract ISwapthe address of the pool that contains the synth

Return Values:#

NameTypeDescription
thecontract ISwapaddress of the supported synth

getSynthKey#

function getSynthKey(
contract ISwap swap
) public returns (bytes32)

Returns the currency key of the supported synth in the given swap pool. Reverts if the swap pool is not registered.

Parameters:#

NameTypeDescription
swapcontract ISwapthe address of the pool that contains the synth

Return Values:#

NameTypeDescription
thecontract ISwapcurrency key of the supported synth

updateExchangerCache#

function updateExchangerCache(
) public

Updates the stored address of the EXCHANGER contract. When the Synthetix team upgrades their protocol, a new Exchanger contract is deployed. This function manually updates the stored address.

Events#

SynthIndex#

event SynthIndex(
)

TokenToSynth#

event TokenToSynth(
)

SynthToToken#

event SynthToToken(
)

TokenToToken#

event TokenToToken(
)

Settle#

event Settle(
)

Withdraw#

event Withdraw(
)