Skip to main content

PrimitiveManager.sol

Read code on GitHub

Interacts with Primitive Engine contracts

Methods

DOMAIN_SEPARATOR

Returns the domain separator

Solidity
function DOMAIN_SEPARATOR() external view returns (bytes32)

Returns

NameTypeDescription
_0bytes32Hash of the domain separator

WETH9

Returns the address of WETH9

Solidity
function WETH9() external view returns (address)

Returns

NameTypeDescription
_0addressundefined

allocate

Allocates liquidity into a pool

Solidity
function allocate(address recipient, bytes32 poolId, address risky, address stable, uint256 delRisky, uint256 delStable, bool fromMargin, uint256 minLiquidityOut) external payable returns (uint256 delLiquidity)

Parameters

NameTypeDescription
recipientaddressAddress that receives minted ERC-1155 Primitive liquidity tokens
poolIdbytes32Id of the pool
riskyaddressAddress of the risky asset
stableaddressAddress of the stable asset
delRiskyuint256Amount of risky tokens to allocate
delStableuint256Amount of stable tokens to allocate
fromMarginboolTrue if the funds of the sender should be used
minLiquidityOutuint256undefined

Returns

NameTypeDescription
delLiquidityuint256Amount of liquidity allocated into the pool

allocateCallback

Triggered when providing liquidity to an Engine

Solidity
function allocateCallback(uint256 delRisky, uint256 delStable, bytes data) external nonpayable

Parameters

NameTypeDescription
delRiskyuint256Amount of risky tokens required to provide to risky reserve
delStableuint256Amount of stable tokens required to provide to stable reserve
databytesCalldata passed on allocate function call

balanceOf

Solidity
function balanceOf(address account, uint256 id) external view returns (uint256)
Details

See {IERC1155-balanceOf}. Requirements: - account cannot be the zero address.

Parameters

NameTypeDescription
accountaddressundefined
iduint256undefined

Returns

NameTypeDescription
_0uint256undefined

balanceOfBatch

Solidity
function balanceOfBatch(address[] accounts, uint256[] ids) external view returns (uint256[])
Details

See {IERC1155-balanceOfBatch}. Requirements: - accounts and ids must have the same length.

Parameters

NameTypeDescription
accountsaddress[]undefined
idsuint256[]undefined

Returns

NameTypeDescription
_0uint256[]undefined

create

Creates a new pool using the specified parameters

Solidity
function create(address risky, address stable, uint128 strike, uint32 sigma, uint32 maturity, uint32 gamma, uint256 riskyPerLp, uint256 delLiquidity) external payable returns (bytes32 poolId, uint256 delRisky, uint256 delStable)

Parameters

NameTypeDescription
riskyaddressAddress of the risky asset
stableaddressAddress of the stable asset
strikeuint128Strike price of the pool to calibrate to, with the same decimals as the stable token
sigmauint32Volatility to calibrate to as an unsigned 256-bit integer w/ precision of 1e4, 10000 = 100%
maturityuint32Maturity timestamp of the pool, in seconds
gammauint32Multiplied against swap in amounts to apply fee, equal to 1 - fee %, an unsigned 32-bit integer, w/ precision of 1e4, 10000 = 100%
riskyPerLpuint256Risky reserve per liq. with risky decimals, = 1 - N(d1), d1 = (ln(S/K)+(rsigma^2/2))/sigmasqrt(tau)
delLiquidityuint256Amount of liquidity to allocate to the curve, wei value with 18 decimals of precision

Returns

NameTypeDescription
poolIdbytes32Id of the new created pool (Keccak256 hash of the engine address, maturity, sigma and strike)
delRiskyuint256Amount of risky tokens allocated into the pool
delStableuint256Amount of stable tokens allocated into the pool

createCallback

Triggered when creating a new pool for an Engine

Solidity
function createCallback(uint256 delRisky, uint256 delStable, bytes data) external nonpayable

Parameters

NameTypeDescription
delRiskyuint256Amount of risky tokens required to initialize risky reserve
delStableuint256Amount of stable tokens required to initialize stable reserve
databytesCalldata passed on create function call

deposit

Deposits funds into the margin of a Primitive Engine

Solidity
function deposit(address recipient, address risky, address stable, uint256 delRisky, uint256 delStable) external payable
Details

Since the PrimitiveManager contract keeps track of the margins, it will deposit the funds into the Primitive Engine using its own address

Parameters

NameTypeDescription
recipientaddressAddress receiving the funds in their margin
riskyaddressAddress of the risky token
stableaddressAddress of the stable token
delRiskyuint256Amount of risky token to deposit
delStableuint256Amount of stable token to deposit

depositCallback

Triggered when depositing tokens to an Engine

Solidity
function depositCallback(uint256 delRisky, uint256 delStable, bytes data) external nonpayable

Parameters

NameTypeDescription
delRiskyuint256Amount of risky tokens required to deposit to risky margin balance
delStableuint256Amount of stable tokens required to deposit to stable margin balance
databytesCalldata passed on deposit function call

factory

Returns the address of the factory

Solidity
function factory() external view returns (address)

Returns

NameTypeDescription
_0addressundefined

isApprovedForAll

Solidity
function isApprovedForAll(address account, address operator) external view returns (bool)
Details

See {IERC1155-isApprovedForAll}.

Parameters

NameTypeDescription
accountaddressundefined
operatoraddressundefined

Returns

NameTypeDescription
_0boolundefined

margins

Returns the margin of an account for a specific Primitive Engine

Solidity
function margins(address, address) external view returns (uint128 balanceRisky, uint128 balanceStable)

Parameters

NameTypeDescription
_0addressundefined
_1addressundefined

Returns

NameTypeDescription
balanceRiskyuint128undefined
balanceStableuint128undefined

multicall

Call multiple functions in the current contract and return the data from all of them if they all succeed

Solidity
function multicall(bytes[] data) external payable returns (bytes[] results)
Details

msg.value should not be trusted for any method callable from Multicall

Parameters

NameTypeDescription
databytes[]Encoded function data for each of the calls to make to this contract

Returns

NameTypeDescription
resultsbytes[]Results from each of the calls passed in via data

nonces

Returns the current nonce of an address

Solidity
function nonces(address) external view returns (uint256)

Parameters

NameTypeDescription
_0addressundefined

Returns

NameTypeDescription
_0uint256undefined

permit

Grants or revokes the approval for an operator to transfer any of the owner's tokens using their signature

Solidity
function permit(address owner, address operator, bool approved, uint256 deadline, uint8 v, bytes32 r, bytes32 s) external nonpayable

Parameters

NameTypeDescription
owneraddressAddress of the owner
operatoraddressAddress of the operator
approvedboolTrue if the approval should be granted, false if revoked
deadlineuint256Expiry of the signature, as a timestamp
vuint8Must produce valid secp256k1 signature from the holder along with r and s
rbytes32Must produce valid secp256k1 signature from the holder along with v and s
sbytes32Must produce valid secp256k1 signature from the holder along with r and v

positionDescriptor

Returns the address of the PositionDescriptor

Solidity
function positionDescriptor() external view returns (address)

Returns

NameTypeDescription
_0addressundefined

refundETH

Transfers the ETH balance of the contract to the caller

Solidity
function refundETH() external payable

remove

Removes liquidity from a pool

Solidity
function remove(address engine, bytes32 poolId, uint256 delLiquidity, uint256 minRiskyOut, uint256 minStableOut) external nonpayable returns (uint256 delRisky, uint256 delStable)

Parameters

NameTypeDescription
engineaddressAddress of the engine
poolIdbytes32Id of the pool
delLiquidityuint256Amount of liquidity to remove
minRiskyOutuint256Minimum amount of risky tokens expected to be received
minStableOutuint256Minimum amount of stable tokens expected to be received

Returns

NameTypeDescription
delRiskyuint256Amount of risky tokens removed from the pool
delStableuint256Amount of stable tokens removed from the pool

safeBatchTransferFrom

Solidity
function safeBatchTransferFrom(address from, address to, uint256[] ids, uint256[] amounts, bytes data) external nonpayable
Details

See {IERC1155-safeBatchTransferFrom}.

Parameters

NameTypeDescription
fromaddressundefined
toaddressundefined
idsuint256[]undefined
amountsuint256[]undefined
databytesundefined

safeTransferFrom

Solidity
function safeTransferFrom(address from, address to, uint256 id, uint256 amount, bytes data) external nonpayable
Details

See {IERC1155-safeTransferFrom}.

Parameters

NameTypeDescription
fromaddressundefined
toaddressundefined
iduint256undefined
amountuint256undefined
databytesundefined

selfPermit

Permits this contract to spend a given token from msg.sender

Solidity
function selfPermit(address token, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s) external payable
Details

owner is always msg.sender and the spender is always address(this)

Parameters

NameTypeDescription
tokenaddressAddress of the token spent
valueuint256Amount that can be spent of token
deadlineuint256A timestamp, the current blocktime must be less than or equal to this timestamp
vuint8Must produce valid secp256k1 signature from the holder along with r and s
rbytes32Must produce valid secp256k1 signature from the holder along with v and s
sbytes32Must produce valid secp256k1 signature from the holder along with r and v

selfPermitAllowed

Permits this contract to spend the sender's tokens for permit signatures that have the allowed parameter

Solidity
function selfPermitAllowed(address token, uint256 nonce, uint256 expiry, uint8 v, bytes32 r, bytes32 s) external payable
Details

owner is always msg.sender and the spender is always address(this)

Parameters

NameTypeDescription
tokenaddressAddress of the token spent
nonceuint256Current nonce of the owner
expiryuint256Timestamp at which the permit is no longer valid
vuint8Must produce valid secp256k1 signature from the holder along with r and s
rbytes32Must produce valid secp256k1 signature from the holder along with v and s
sbytes32Must produce valid secp256k1 signature from the holder along with r and v

selfPermitAllowedIfNecessary

Permits this contract to spend the sender's tokens for permit signatures that have the allowed parameter

Solidity
function selfPermitAllowedIfNecessary(address token, uint256 nonce, uint256 expiry, uint8 v, bytes32 r, bytes32 s) external payable
Details

owner is always msg.sender and the spender is always address(this) Can be used instead of #selfPermitAllowed to prevent calls from failing due to a frontrun of a call to #selfPermitAllowed

Parameters

NameTypeDescription
tokenaddressAddress of the token spent
nonceuint256Current nonce of the owner
expiryuint256Timestamp at which the permit is no longer valid
vuint8Must produce valid secp256k1 signature from the holder along with r and s
rbytes32Must produce valid secp256k1 signature from the holder along with v and s
sbytes32Must produce valid secp256k1 signature from the holder along with r and v

selfPermitIfNecessary

Permits this contract to spend a given token from msg.sender

Solidity
function selfPermitIfNecessary(address token, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s) external payable
Details

owner is always msg.sender and the spender is always address(this) Can be used instead of #selfPermit to prevent calls from failing due to a frontrun of a call to #selfPermit

Parameters

NameTypeDescription
tokenaddressAddress of the token spent
valueuint256Amount that can be spent of token
deadlineuint256A timestamp, the current blocktime must be less than or equal to this timestamp
vuint8Must produce valid secp256k1 signature from the holder along with r and s
rbytes32Must produce valid secp256k1 signature from the holder along with v and s
sbytes32Must produce valid secp256k1 signature from the holder along with r and v

setApprovalForAll

Solidity
function setApprovalForAll(address operator, bool approved) external nonpayable
Details

See {IERC1155-setApprovalForAll}.

Parameters

NameTypeDescription
operatoraddressundefined
approvedboolundefined

supportsInterface

Solidity
function supportsInterface(bytes4 interfaceId) external view returns (bool)
Details

See {IERC165-supportsInterface}.

Parameters

NameTypeDescription
interfaceIdbytes4undefined

Returns

NameTypeDescription
_0boolundefined

swap

Solidity
function swap(ISwapManager.SwapParams params) external payable

Parameters

NameTypeDescription
paramsISwapManager.SwapParamsundefined

swapCallback

Triggered when swapping tokens in an Engine

Solidity
function swapCallback(uint256 delRisky, uint256 delStable, bytes data) external nonpayable

Parameters

NameTypeDescription
delRiskyuint256Amount of risky tokens required to pay the swap with
delStableuint256Amount of stable tokens required to pay the swap with
databytesCalldata passed on swap function call

sweepToken

Transfers the tokens in the contract to a recipient

Solidity
function sweepToken(address token, uint256 amountMin, address recipient) external payable

Parameters

NameTypeDescription
tokenaddressAddress of the token to sweep
amountMinuint256Minimum amount to transfer
recipientaddressRecipient of the tokens

unwrap

Unwraps WETH to ETH and transfers to a recipient

Solidity
function unwrap(uint256 amountMin, address recipient) external payable

Parameters

NameTypeDescription
amountMinuint256Minimum amount to unwrap
recipientaddressAddress of the recipient

uri

Returns the metadata of a token

Solidity
function uri(uint256 tokenId) external view returns (string)

Parameters

NameTypeDescription
tokenIduint256Token id to look for (same as pool id)

Returns

NameTypeDescription
_0stringMetadata of the token as a string

withdraw

Withdraws funds from the margin of a Primitive Engine

Solidity
function withdraw(address recipient, address engine, uint256 delRisky, uint256 delStable) external nonpayable

Parameters

NameTypeDescription
recipientaddressAddress receiving the funds in their wallet
engineaddressPrimitive Engine to withdraw from
delRiskyuint256Amount of risky token to withdraw
delStableuint256Amount of stable token to withdraw

wrap

Wraps ETH into WETH and transfers to the msg.sender

Solidity
function wrap(uint256 value) external payable

Parameters

NameTypeDescription
valueuint256Amount of ETH to wrap

Events

Allocate

Emitted when liquidity is allocated

Solidity
event Allocate(address payer, address indexed recipient, address indexed engine, bytes32 indexed poolId, uint256 delLiquidity, uint256 delRisky, uint256 delStable, bool fromMargin)

Parameters

NameTypeDescription
payeraddressundefined
recipient indexedaddressundefined
engine indexedaddressundefined
poolId indexedbytes32undefined
delLiquidityuint256undefined
delRiskyuint256undefined
delStableuint256undefined
fromMarginboolundefined

ApprovalForAll

Solidity
event ApprovalForAll(address indexed account, address indexed operator, bool approved)

Parameters

NameTypeDescription
account indexedaddressundefined
operator indexedaddressundefined
approvedboolundefined

Create

Emitted when a new pool is created

Solidity
event Create(address indexed payer, address indexed engine, bytes32 indexed poolId, uint128 strike, uint32 sigma, uint32 maturity, uint32 gamma, uint256 delLiquidity)

Parameters

NameTypeDescription
payer indexedaddressundefined
engine indexedaddressundefined
poolId indexedbytes32undefined
strikeuint128undefined
sigmauint32undefined
maturityuint32undefined
gammauint32undefined
delLiquidityuint256undefined

Deposit

Emitted when funds are deposited

Solidity
event Deposit(address indexed payer, address indexed recipient, address indexed engine, address risky, address stable, uint256 delRisky, uint256 delStable)

Parameters

NameTypeDescription
payer indexedaddressundefined
recipient indexedaddressundefined
engine indexedaddressundefined
riskyaddressundefined
stableaddressundefined
delRiskyuint256undefined
delStableuint256undefined

Remove

Emitted when liquidity is removed

Solidity
event Remove(address indexed payer, address indexed engine, bytes32 indexed poolId, uint256 delLiquidity, uint256 delRisky, uint256 delStable)

Parameters

NameTypeDescription
payer indexedaddressundefined
engine indexedaddressundefined
poolId indexedbytes32undefined
delLiquidityuint256undefined
delRiskyuint256undefined
delStableuint256undefined

Swap

Emitted when a swap occurs

Solidity
event Swap(address indexed payer, address recipient, address indexed engine, bytes32 indexed poolId, bool riskyForStable, uint256 deltaIn, uint256 deltaOut, bool fromMargin, bool toMargin)

Parameters

NameTypeDescription
payer indexedaddressundefined
recipientaddressundefined
engine indexedaddressundefined
poolId indexedbytes32undefined
riskyForStableboolundefined
deltaInuint256undefined
deltaOutuint256undefined
fromMarginboolundefined
toMarginboolundefined

TransferBatch

Solidity
event TransferBatch(address indexed operator, address indexed from, address indexed to, uint256[] ids, uint256[] values)

Parameters

NameTypeDescription
operator indexedaddressundefined
from indexedaddressundefined
to indexedaddressundefined
idsuint256[]undefined
valuesuint256[]undefined

TransferSingle

Solidity
event TransferSingle(address indexed operator, address indexed from, address indexed to, uint256 id, uint256 value)

Parameters

NameTypeDescription
operator indexedaddressundefined
from indexedaddressundefined
to indexedaddressundefined
iduint256undefined
valueuint256undefined

URI

Solidity
event URI(string value, uint256 indexed id)

Parameters

NameTypeDescription
valuestringundefined
id indexeduint256undefined

Withdraw

Emitted when funds are withdrawn

Solidity
event Withdraw(address indexed payer, address indexed recipient, address indexed engine, address risky, address stable, uint256 delRisky, uint256 delStable)

Parameters

NameTypeDescription
payer indexedaddressundefined
recipient indexedaddressundefined
engine indexedaddressundefined
riskyaddressundefined
stableaddressundefined
delRiskyuint256undefined
delStableuint256undefined

Errors

BalanceTooLowError

Thrown when the amount required is above balance

Solidity
error BalanceTooLowError(uint256 balance, uint256 requiredAmount)

Parameters

NameTypeDescription
balanceuint256Actual ETH or token balance of the contract
requiredAmountuint256ETH or token amount required by the user

DeadlineReachedError

Thrown when the deadline is reached

Solidity
error DeadlineReachedError()

EngineNotDeployedError

Thrown when the target Engine is not deployed

Solidity
error EngineNotDeployedError()

InvalidSigError

Thrown when the signature is invalid

Solidity
error InvalidSigError()

LockedError

Thrown when a call to the contract is made during a locked state

Solidity
error LockedError()

MinLiquidityOutError

Thrown when the received liquidity is lower than the expected

Solidity
error MinLiquidityOutError()

MinRemoveOutError

Thrown when the received risky / stable amounts are lower than the expected

Solidity
error MinRemoveOutError()

NotEngineError

Thrown when the sender is not a Primitive Engine contract

Solidity
error NotEngineError()

OnlyWETHError

Thrown when the sender is not WETH

Solidity
error OnlyWETHError()

SigExpiredError

Thrown when the signature has expired

Solidity
error SigExpiredError()

TransferError

Thrown when a transfer reverts

Solidity
error TransferError()

WrongConstructorParametersError

Thrown when the constructor parameters are wrong

Solidity
error WrongConstructorParametersError()

ZeroDelError

Thrown when trying to deposit or withdraw 0 risky and stable

Solidity
error ZeroDelError()

ZeroLiquidityError

Thrown when trying to add or remove zero liquidity

Solidity
error ZeroLiquidityError()