LibSwap

Git Source

Author: Cujo

Functions

swapToExactCarbonDefault

Swaps to an exact number of carbon tokens

function swapToExactCarbonDefault(address sourceToken, address carbonToken, uint256 sourceAmount, uint256 carbonAmount)
    internal
    returns (uint256 carbonReceived);

Parameters

NameTypeDescription
sourceTokenaddressSource token provided to swap
carbonTokenaddressPool token needed
sourceAmountuint256Max amount of the source token
carbonAmountuint256Needed amount of tokens out

Returns

NameTypeDescription
carbonReceiveduint256Pool tokens actually received

swapExactSourceToCarbonDefault

Swaps to an exact number of source tokens

function swapExactSourceToCarbonDefault(address sourceToken, address carbonToken, uint256 amount)
    internal
    returns (uint256 carbonReceived);

Parameters

NameTypeDescription
sourceTokenaddressSource token provided to swap
carbonTokenaddressPool token needed
amountuint256Amount of the source token to swap

Returns

NameTypeDescription
carbonReceiveduint256Pool tokens actually received

returnTradeDust

Return any dust/slippaged amounts still held by the contract

function returnTradeDust(address sourceToken, address poolToken) internal;

Parameters

NameTypeDescription
sourceTokenaddressSource token provided to swap
poolTokenaddressPool token used

swapToKlimaFromUsdc

Swaps a given amount of USDC for KLIMA using Sushiswap

function swapToKlimaFromUsdc(uint256 sourceAmount, uint256 klimaAmount) internal returns (uint256 klimaReceived);

Parameters

NameTypeDescription
sourceAmountuint256Amount of USDC to swap
klimaAmountuint256Amount of KLIMA to swap for

Returns

NameTypeDescription
klimaReceiveduint256Amount of KLIMA received

swapToKlimaFromOther

Swaps from arbitrary token routed through USDC for KLIMA

function swapToKlimaFromOther(address sourceToken, uint256 sourceAmount, uint256 klimaAmount)
    internal
    returns (uint256 klimaReceived);

Parameters

NameTypeDescription
sourceTokenaddressSource token provided to swap
sourceAmountuint256Amount of source token to swap
klimaAmountuint256Amount of KLIMA to swap for

Returns

NameTypeDescription
klimaReceiveduint256Amount of KLIMA received

swapWithRetirementBonds

Performs a swap with Retirement Bonds for carbon to retire.

function swapWithRetirementBonds(address sourceToken, address carbonToken, uint256 sourceAmount, uint256 carbonAmount)
    internal
    returns (uint256 carbonRecieved);

Parameters

NameTypeDescription
sourceTokenaddressSource token provided to swap
carbonTokenaddressCarbon token to receive
sourceAmountuint256Amount of source token to swap
carbonAmountuint256Amount of carbon token needed

Returns

NameTypeDescription
carbonRecieveduint256Amount of carbon token received from the swap

getSourceAmount

Get the source amount needed when swapping within a single DEX

function getSourceAmount(address sourceToken, address carbonToken, uint256 amount)
    internal
    view
    returns (uint256 sourceNeeded);

Parameters

NameTypeDescription
sourceTokenaddressSource token provided to swap
carbonTokenaddressPool token used
amountuint256Amount of carbon tokens needed

Returns

NameTypeDescription
sourceNeededuint256Total source tokens needed for output amount

getMultipleSourceAmount

Get the source amount needed when swapping between multiple DEXs

function getMultipleSourceAmount(address sourceToken, address carbonToken, uint256 amount)
    internal
    view
    returns (uint256[] memory);

Parameters

NameTypeDescription
sourceTokenaddressSource token provided to swap
carbonTokenaddressPool token used
amountuint256Amount of carbon tokens needed

Returns

NameTypeDescription
<none>uint256[]sourcesNeeded Total source tokens needed for output amount

getSourceAmountFromRetirementBond

Fetches the amount of KLIMA needed for a retirement bond, then calculates the source amount needed if a DEX swap is required.

function getSourceAmountFromRetirementBond(address sourceToken, address carbonToken, uint256 amount)
    internal
    view
    returns (uint256 sourceNeeded);

Parameters

NameTypeDescription
sourceTokenaddressSource token provided to swap
carbonTokenaddressPool token used
amountuint256Amount of carbon tokens needed

Returns

NameTypeDescription
sourceNeededuint256Total source tokens needed for output amount

getDefaultAmountOut

Get the source amount needed when swapping between multiple DEXs

function getDefaultAmountOut(address sourceToken, address carbonToken, uint256 amount)
    internal
    view
    returns (uint256 amountOut);

Parameters

NameTypeDescription
sourceTokenaddressSource token provided to swap
carbonTokenaddressPool token used
amountuint256Amount of carbon tokens needed

Returns

NameTypeDescription
amountOutuint256Amount of carbonTokens recieved for the input amount

_performToExactSwap

Perform a toExact swap depending on the dex provided

function _performToExactSwap(uint8 dex, address router, address[] memory path, uint256 maxAmountIn, uint256 amount)
    private
    returns (uint256 amountOut);

Parameters

NameTypeDescription
dexuint8Identifier for which DEX to use
routeraddressRouter for the swap
pathaddress[]Trade path to use
maxAmountInuint256Max amount of source tokens to swap
amountuint256Total pool tokens needed

Returns

NameTypeDescription
amountOutuint256Total pool tokens swapped

_performExactSourceSwap

Perform a swap using all source tokens

function _performExactSourceSwap(uint8 dex, address router, address[] memory path, uint256 amount)
    private
    returns (uint256 amountOut);

Parameters

NameTypeDescription
dexuint8Identifier for which DEX to use
routeraddressRouter for the swap
pathaddress[]Trade path to use
amountuint256Amount of tokens to swap

Returns

NameTypeDescription
amountOutuint256Total pool tokens swapped

_getAmountIn

Return the amountIn needed for an exact swap

function _getAmountIn(uint8 dex, address router, address[] memory path, uint256 amount)
    private
    view
    returns (uint256 amountIn);

Parameters

NameTypeDescription
dexuint8Identifier for which DEX to use
routeraddressRouter for the swap
pathaddress[]Trade path to use
amountuint256Total pool tokens needed

Returns

NameTypeDescription
amountInuint256Total pool tokens swapped

_getAmountOut

Return the amountIn needed for an exact swap

function _getAmountOut(uint8 dex, address router, address[] memory path, uint256 amount)
    private
    view
    returns (uint256 amountOut);

Parameters

NameTypeDescription
dexuint8Identifier for which DEX to use
routeraddressRouter for the swap
pathaddress[]Trade path to use
amountuint256Total source tokens spent

Returns

NameTypeDescription
amountOutuint256Total pool tokens swapped