- (SwapsImplLocal.sol)
View Source: contracts/swaps/connectors/testnet/SwapsImplLocal.sol
↗ Extends: State, ISwapsImpl
This contract code comes from bZx. bZx is a protocol for tokenized margin trading and lending https://bzx.network similar to the dYdX protocol.
- This contract contains the implementation of swap process and rate calculations.
- internalSwap(address sourceTokenAddress, address destTokenAddress, address , address returnToSenderAddress, uint256 minSourceTokenAmount, uint256 maxSourceTokenAmount, uint256 requiredDestTokenAmount)
- internalExpectedRate(address sourceTokenAddress, address destTokenAddress, uint256 sourceTokenAmount, address unused)
- internalExpectedReturn(address sourceTokenAddress, address destTokenAddress, uint256 sourceTokenAmount, address unused, IERC20[] defaultPath)
⤾ overrides ISwapsImpl.internalSwap
Swap two tokens. *
function internalSwap(address sourceTokenAddress, address destTokenAddress, address , address returnToSenderAddress, uint256 minSourceTokenAmount, uint256 maxSourceTokenAmount, uint256 requiredDestTokenAmount) public payable
returns(destTokenAmountReceived uint256, sourceTokenAmountUsed uint256)
Arguments
Name | Type | Description |
---|---|---|
sourceTokenAddress | address | The address of the source tokens. |
destTokenAddress | address | The address of the destiny tokens. * |
address | sourceTokenAddress The address of the source tokens. | |
returnToSenderAddress | address | |
minSourceTokenAmount | uint256 | |
maxSourceTokenAmount | uint256 | |
requiredDestTokenAmount | uint256 |
Returns
destTokenAmountReceived The amount of destiny tokens sent.
Source Code
function internalSwap(
address sourceTokenAddress,
address destTokenAddress,
address, /*receiverAddress*/
address returnToSenderAddress,
uint256 minSourceTokenAmount,
uint256 maxSourceTokenAmount,
uint256 requiredDestTokenAmount
) public payable returns (uint256 destTokenAmountReceived, uint256 sourceTokenAmountUsed) {
require(sourceTokenAddress != destTokenAddress, "source == dest");
(uint256 tradeRate, uint256 precision) =
IPriceFeeds(priceFeeds).queryRate(sourceTokenAddress, destTokenAddress);
if (requiredDestTokenAmount == 0) {
sourceTokenAmountUsed = minSourceTokenAmount;
destTokenAmountReceived = minSourceTokenAmount.mul(tradeRate).div(precision);
} else {
destTokenAmountReceived = requiredDestTokenAmount;
sourceTokenAmountUsed = requiredDestTokenAmount.mul(precision).div(tradeRate);
require(sourceTokenAmountUsed <= minSourceTokenAmount, "destAmount too great");
}
TestToken(sourceTokenAddress).burn(address(this), sourceTokenAmountUsed);
TestToken(destTokenAddress).mint(address(this), destTokenAmountReceived);
if (returnToSenderAddress != address(this)) {
if (sourceTokenAmountUsed < maxSourceTokenAmount) {
/// Send unused source token back.
IERC20(sourceTokenAddress).safeTransfer(
returnToSenderAddress,
maxSourceTokenAmount - sourceTokenAmountUsed
);
}
}
}
⤾ overrides ISwapsImpl.internalExpectedRate
Calculate the expected price rate of swapping a given amount of tokens. *
function internalExpectedRate(address sourceTokenAddress, address destTokenAddress, uint256 sourceTokenAmount, address unused) public view
returns(uint256)
Arguments
Name | Type | Description |
---|---|---|
sourceTokenAddress | address | The address of the source tokens. |
destTokenAddress | address | The address of the destiny tokens. |
sourceTokenAmount | uint256 | The amount of source tokens. |
unused | address | Fourth parameter ignored. * |
Returns
precision The expected price rate.
Source Code
function internalExpectedRate(
address sourceTokenAddress,
address destTokenAddress,
uint256 sourceTokenAmount,
address unused
) public view returns (uint256) {
(uint256 sourceToDestRate, uint256 sourceToDestPrecision) =
IPriceFeeds(priceFeeds).queryRate(sourceTokenAddress, destTokenAddress);
return sourceTokenAmount.mul(sourceToDestRate).div(sourceToDestPrecision);
}
⤾ overrides ISwapsImpl.internalExpectedReturn
Calculate the expected return of swapping a given amount of tokens. *
function internalExpectedReturn(address sourceTokenAddress, address destTokenAddress, uint256 sourceTokenAmount, address unused, IERC20[] defaultPath) public view
returns(uint256)
Arguments
Name | Type | Description |
---|---|---|
sourceTokenAddress | address | The address of the source tokens. |
destTokenAddress | address | The address of the destiny tokens. |
sourceTokenAmount | uint256 | The amount of source tokens. |
unused | address | Fourth parameter ignored. |
defaultPath | IERC20[] | defaultPath for swap. * |
Returns
precision The expected return.
Source Code
function internalExpectedReturn(
address sourceTokenAddress,
address destTokenAddress,
uint256 sourceTokenAmount,
address unused,
IERC20[] memory defaultPath
) public view returns (uint256) {
(uint256 sourceToDestRate, uint256 sourceToDestPrecision) =
IPriceFeeds(priceFeeds).queryRate(sourceTokenAddress, destTokenAddress);
return sourceTokenAmount.mul(sourceToDestRate).div(sourceToDestPrecision);
}
- Address
- Administered
- AdminRole
- AdvancedToken
- AdvancedTokenStorage
- Affiliates
- AffiliatesEvents
- ApprovalReceiver
- BProPriceFeed
- CheckpointsShared
- Constants
- Context
- DevelopmentFund
- DummyContract
- EnumerableAddressSet
- EnumerableBytes32Set
- EnumerableBytes4Set
- ERC20
- ERC20Detailed
- ErrorDecoder
- Escrow
- EscrowReward
- FeedsLike
- FeesEvents
- FeeSharingCollector
- FeeSharingCollectorProxy
- FeeSharingCollectorStorage
- FeesHelper
- FourYearVesting
- FourYearVestingFactory
- FourYearVestingLogic
- FourYearVestingStorage
- GenericTokenSender
- GovernorAlpha
- GovernorVault
- IApproveAndCall
- IChai
- IContractRegistry
- IConverterAMM
- IERC1820Registry
- IERC20_
- IERC20
- IERC777
- IERC777Recipient
- IERC777Sender
- IFeeSharingCollector
- IFourYearVesting
- IFourYearVestingFactory
- IFunctionsList
- ILiquidityMining
- ILiquidityPoolV1Converter
- ILoanPool
- ILoanToken
- ILoanTokenLogicBeacon
- ILoanTokenLogicModules
- ILoanTokenLogicProxy
- ILoanTokenModules
- ILoanTokenWRBTC
- ILockedSOV
- IMoCState
- IModulesProxyRegistry
- Initializable
- InterestUser
- IPot
- IPriceFeeds
- IPriceFeedsExt
- IProtocol
- IRSKOracle
- ISovryn
- ISovrynSwapNetwork
- IStaking
- ISwapsImpl
- ITeamVesting
- ITimelock
- IV1PoolOracle
- IVesting
- IVestingFactory
- IVestingRegistry
- IWrbtc
- IWrbtcERC20
- LenderInterestStruct
- LiquidationHelper
- LiquidityMining
- LiquidityMiningConfigToken
- LiquidityMiningProxy
- LiquidityMiningStorage
- LoanClosingsEvents
- LoanClosingsLiquidation
- LoanClosingsRollover
- LoanClosingsShared
- LoanClosingsWith
- LoanClosingsWithoutInvariantCheck
- LoanInterestStruct
- LoanMaintenance
- LoanMaintenanceEvents
- LoanOpenings
- LoanOpeningsEvents
- LoanParamsStruct
- LoanSettings
- LoanSettingsEvents
- LoanStruct
- LoanToken
- LoanTokenBase
- LoanTokenLogicBeacon
- LoanTokenLogicLM
- LoanTokenLogicProxy
- LoanTokenLogicStandard
- LoanTokenLogicStorage
- LoanTokenLogicWrbtc
- LoanTokenSettingsLowerAdmin
- LockedSOV
- MarginTradeStructHelpers
- Medianizer
- ModuleCommonFunctionalities
- ModulesCommonEvents
- ModulesProxy
- ModulesProxyRegistry
- MultiSigKeyHolders
- MultiSigWallet
- Mutex
- Objects
- OrderStruct
- OrigingVestingCreator
- OriginInvestorsClaim
- Ownable
- Pausable
- PausableOz
- PreviousLoanToken
- PreviousLoanTokenSettingsLowerAdmin
- PriceFeedRSKOracle
- PriceFeeds
- PriceFeedsLocal
- PriceFeedsMoC
- PriceFeedV1PoolOracle
- ProtocolAffiliatesInterface
- ProtocolLike
- ProtocolSettings
- ProtocolSettingsEvents
- ProtocolSettingsLike
- ProtocolSwapExternalInterface
- ProtocolTokenUser
- Proxy
- ProxyOwnable
- ReentrancyGuard
- RewardHelper
- RSKAddrValidator
- SafeERC20
- SafeMath
- SafeMath96
- setGet
- SharedReentrancyGuard
- SignedSafeMath
- SOV
- sovrynProtocol
- StakingAdminModule
- StakingGovernanceModule
- StakingInterface
- StakingProxy
- StakingRewards
- StakingRewardsProxy
- StakingRewardsStorage
- StakingShared
- StakingStakeModule
- StakingStorageModule
- StakingStorageShared
- StakingVestingModule
- StakingWithdrawModule
- State
- SwapsEvents
- SwapsExternal
- SwapsImplLocal
- SwapsImplSovrynSwap
- SwapsUser
- TeamVesting
- Timelock
- TimelockHarness
- TimelockInterface
- TokenSender
- UpgradableProxy
- USDTPriceFeed
- Utils
- VaultController
- Vesting
- VestingCreator
- VestingFactory
- VestingLogic
- VestingRegistry
- VestingRegistry2
- VestingRegistry3
- VestingRegistryLogic
- VestingRegistryProxy
- VestingRegistryStorage
- VestingStorage
- WeightedStakingModule
- WRBTC