View Source: contracts/examples/NpmDistributor.sol
↗ Extends: ReentrancyGuard
IStoreLike
Constants & Variables
bytes32 public constant NS_CONTRACTS;
bytes32 public constant CNS_CLAIM_PROCESSOR;
bytes32 public constant CNS_COVER_VAULT;
bytes32 public constant CNS_COVER_POLICY;
bytes32 public constant CNS_COVER_STABLECOIN;
bytes32 public constant CNS_NPM_INSTANCE;
uint256 public constant MULTIPLIER;
uint256 public feePercentage;
address public treasury;
contract IStoreLike public store;Events
event PolicySold(bytes32 indexed coverKey, bytes32 indexed productKey, address indexed cxToken, address account, uint256 duration, uint256 protection, bytes32 referralCode, uint256 fee, uint256 premium);
event LiquidityAdded(bytes32 indexed coverKey, address indexed account, bytes32 indexed referralCode, uint256 amount, uint256 npmStake);
event LiquidityRemoved(bytes32 indexed coverKey, address indexed account, uint256 amount, uint256 npmStake, bool exit);
event Drained(IERC20 indexed token, address indexed to, uint256 amount);- getAddress(bytes32 k)
- constructor(IStoreLike _store, address _treasury, uint256 _feePercentage)
- getStablecoin()
- getNpm()
- getPolicyContract()
- getVaultContract(bytes32 coverKey)
- getClaimsProcessorContract()
- getPremium(bytes32 coverKey, bytes32 productKey, uint256 duration, uint256 protection)
- purchasePolicy(struct IPolicy.PurchaseCoverArgs args)
- addLiquidity(struct IVault.AddLiquidityArgs args)
- removeLiquidity(bytes32 coverKey, uint256 amount, uint256 npmStake, bool exit)
- _drain(IERC20 token)
function getAddress(bytes32 k) external view
returns(address)Arguments
| Name | Type | Description |
|---|---|---|
| k | bytes32 |
Source Code
function getAddress(bytes32 k) external view returns (address);Constructs this contract
function (IStoreLike _store, address _treasury, uint256 _feePercentage) public nonpayableArguments
| Name | Type | Description |
|---|---|---|
| _store | IStoreLike | Enter the address of NPM protocol store |
| _treasury | address | Enter your treasury wallet address |
| _feePercentage | uint256 | Enter distributor fee percentage |
Source Code
constructor(
IStoreLike _store,
address _treasury,
uint256 _feePercentage
) {
require(address(_store) != address(0), "Invalid store");
require(_treasury != address(0), "Invalid treasury");
require(_feePercentage > 0 && _feePercentage < MULTIPLIER, "Invalid fee percentage");
store = _store;
treasury = _treasury;
feePercentage = _feePercentage;
}Returns the stablecoin used by the protocol in this blockchain.
function getStablecoin() public view
returns(contract IERC20)Arguments
| Name | Type | Description |
|---|
Source Code
function getStablecoin() public view returns (IERC20) {
return IERC20(store.getAddress(CNS_COVER_STABLECOIN));
}Returns NPM token instance in this blockchain.
function getNpm() public view
returns(contract IERC20)Arguments
| Name | Type | Description |
|---|
Source Code
function getNpm() public view returns (IERC20) {
return IERC20(store.getAddress(CNS_NPM_INSTANCE));
}Returns the protocol policy contract instance.
function getPolicyContract() public view
returns(contract IPolicy)Arguments
| Name | Type | Description |
|---|
Source Code
function getPolicyContract() public view returns (IPolicy) {
return IPolicy(store.getAddress(keccak256(abi.encodePacked(NS_CONTRACTS, CNS_COVER_POLICY))));
}Returns the vault contract instance by the given key.
function getVaultContract(bytes32 coverKey) public view
returns(contract IVault)Arguments
| Name | Type | Description |
|---|---|---|
| coverKey | bytes32 |
Source Code
function getVaultContract(bytes32 coverKey) public view returns (IVault) {
return IVault(store.getAddress(keccak256(abi.encodePacked(NS_CONTRACTS, CNS_COVER_VAULT, coverKey))));
}Returns the protocol claims processor contract instance.
function getClaimsProcessorContract() external view
returns(contract IClaimsProcessor)Arguments
| Name | Type | Description |
|---|
Source Code
function getClaimsProcessorContract() external view returns (IClaimsProcessor) {
return IClaimsProcessor(store.getAddress(keccak256(abi.encodePacked(NS_CONTRACTS, CNS_CLAIM_PROCESSOR))));
}Calculates the premium required to purchase policy.
function getPremium(bytes32 coverKey, bytes32 productKey, uint256 duration, uint256 protection) public view
returns(premium uint256, fee uint256)Arguments
| Name | Type | Description |
|---|---|---|
| coverKey | bytes32 | Enter the cover key for which you want to buy policy. |
| productKey | bytes32 | |
| duration | uint256 | Enter the period of the protection in months. |
| protection | uint256 | Enter the stablecoin dollar amount you want to protect. |
Source Code
function getPremium(
bytes32 coverKey,
bytes32 productKey,
uint256 duration,
uint256 protection
) public view returns (uint256 premium, uint256 fee) {
IPolicy policy = getPolicyContract();
require(address(policy) != address(0), "Fatal: Policy missing");
IPolicy.CoverFeeInfoType memory coverFeeInfo = policy.getCoverFeeInfo(coverKey, productKey, duration, protection);
premium = coverFeeInfo.fee;
// Add your fee in addition to the protocol premium
fee = (premium * feePercentage) / MULTIPLIER;
}Purchases a new policy on behalf of your users. Prior to using this method, you must first call the "getPremium" function and approve the policy fees that this contract would spend. In the event that this function succeeds, the recipient's wallet will be credited with "cxToken". Take note that the "claimPolicy" method may be used in the future to reclaim cxTokens and receive payouts after the resolution of an incident.
function purchasePolicy(struct IPolicy.PurchaseCoverArgs args) external nonpayable nonReentrant Arguments
| Name | Type | Description |
|---|---|---|
| args | struct IPolicy.PurchaseCoverArgs |
Source Code
function purchasePolicy(IPolicy.PurchaseCoverArgs memory args) external nonReentrant {
require(args.coverKey > 0, "Invalid key");
require(args.coverDuration > 0 && args.coverDuration < 4, "Invalid duration");
require(args.amountToCover > 0, "Invalid protection amount");
IPolicy policy = getPolicyContract();
require(address(policy) != address(0), "Fatal: Policy missing");
IERC20 stablecoin = getStablecoin();
require(address(stablecoin) != address(0), "Fatal: Stablecoin missing");
// Get fee info
(uint256 premium, uint256 fee) = getPremium(args.coverKey, args.productKey, args.coverDuration, args.amountToCover);
// Transfer stablecoin to this contract
stablecoin.safeTransferFrom(msg.sender, address(this), premium + fee);
// Approve protocol to pull the protocol fee
stablecoin.safeIncreaseAllowance(address(policy), premium);
args.onBehalfOf = msg.sender;
// Purchase protection for this user
(address cxTokenAt, ) = policy.purchaseCover(args);
// Send your fee (+ any remaining stablecoin balance) to your treasury address
stablecoin.safeTransfer(treasury, stablecoin.balanceOf(address(this)));
emit PolicySold(args.coverKey, args.productKey, cxTokenAt, msg.sender, args.coverDuration, args.amountToCover, args.referralCode, fee, premium);
}function addLiquidity(struct IVault.AddLiquidityArgs args) external nonpayable nonReentrant Arguments
| Name | Type | Description |
|---|---|---|
| args | struct IVault.AddLiquidityArgs |
Source Code
function addLiquidity(IVault.AddLiquidityArgs calldata args) external nonReentrant {
require(args.coverKey > 0, "Invalid key");
require(args.amount > 0, "Invalid amount");
IVault pod = getVaultContract(args.coverKey);
IERC20 stablecoin = getStablecoin();
IERC20 npm = getNpm();
require(address(pod) != address(0), "Fatal: Vault missing");
require(address(stablecoin) != address(0), "Fatal: Stablecoin missing");
require(address(npm) != address(0), "Fatal: NPM missing");
// Before moving forward, first drain all balances of this contract
_drain(pod);
_drain(stablecoin);
_drain(npm);
// Transfer stablecoin from sender's wallet here
stablecoin.safeTransferFrom(msg.sender, address(this), args.amount);
// Approve the Vault (or pod) contract to spend stablecoin
stablecoin.safeIncreaseAllowance(address(pod), args.amount);
if (args.npmStakeToAdd > 0) {
// Transfer NPM from the sender's wallet here
npm.safeTransferFrom(msg.sender, address(this), args.npmStakeToAdd);
// Approve the Vault (or pod) contract to spend NPM
npm.safeIncreaseAllowance(address(pod), args.npmStakeToAdd);
}
pod.addLiquidity(args);
pod.safeTransfer(msg.sender, pod.balanceOf(address(this)));
emit LiquidityAdded(args.coverKey, msg.sender, args.referralCode, args.amount, args.npmStakeToAdd);
}function removeLiquidity(bytes32 coverKey, uint256 amount, uint256 npmStake, bool exit) external nonpayable nonReentrant Arguments
| Name | Type | Description |
|---|---|---|
| coverKey | bytes32 | |
| amount | uint256 | |
| npmStake | uint256 | |
| exit | bool |
Source Code
function removeLiquidity(
bytes32 coverKey,
uint256 amount,
uint256 npmStake,
bool exit
) external nonReentrant {
require(coverKey > 0, "Invalid key");
require(amount > 0, "Invalid amount");
IVault pod = getVaultContract(coverKey);
IERC20 stablecoin = getStablecoin();
IERC20 npm = getNpm();
require(address(pod) != address(0), "Fatal: Vault missing");
require(address(stablecoin) != address(0), "Fatal: Stablecoin missing");
require(address(npm) != address(0), "Fatal: NPM missing");
// Before moving forward, first drain all balances of this contract
_drain(pod);
_drain(stablecoin);
_drain(npm);
// Transfer pod from sender's wallet here
pod.safeTransferFrom(msg.sender, address(this), amount);
// Approve the Vault (or pod) contract to spend pod
pod.safeIncreaseAllowance(address(pod), amount);
pod.removeLiquidity(coverKey, amount, npmStake, exit);
stablecoin.safeTransfer(msg.sender, pod.balanceOf(address(this)));
emit LiquidityRemoved(coverKey, msg.sender, amount, npmStake, exit);
}Drains a given token to the treasury address
function _drain(IERC20 token) private nonpayableArguments
| Name | Type | Description |
|---|---|---|
| token | IERC20 |
Source Code
function _drain(IERC20 token) private {
uint256 balance = token.balanceOf(address(this));
if (balance > 0) {
token.safeTransfer(treasury, balance);
emit Drained(token, treasury, balance);
}
}- AaveStrategy
- AccessControl
- AccessControlLibV1
- Address
- BaseLibV1
- BokkyPooBahsDateTimeLibrary
- BondPool
- BondPoolBase
- BondPoolLibV1
- CompoundStrategy
- Context
- Cover
- CoverBase
- CoverLibV1
- CoverReassurance
- CoverStake
- CoverUtilV1
- cxToken
- cxTokenFactory
- cxTokenFactoryLibV1
- Delayable
- Destroyable
- ERC165
- ERC20
- FakeAaveLendingPool
- FakeCompoundStablecoinDelegator
- FakePriceOracle
- FakeRecoverable
- FakeStore
- FakeToken
- FakeUniswapPair
- FakeUniswapV2FactoryLike
- FakeUniswapV2PairLike
- FakeUniswapV2RouterLike
- FaultyAaveLendingPool
- FaultyCompoundStablecoinDelegator
- Finalization
- ForceEther
- Governance
- GovernanceUtilV1
- IAaveV2LendingPoolLike
- IAccessControl
- IBondPool
- IClaimsProcessor
- ICompoundERC20DelegatorLike
- ICover
- ICoverReassurance
- ICoverStake
- ICxToken
- ICxTokenFactory
- IERC165
- IERC20
- IERC20Detailed
- IERC20Metadata
- IERC3156FlashBorrower
- IERC3156FlashLender
- IFinalization
- IGovernance
- ILendingStrategy
- ILiquidityEngine
- IMember
- INeptuneRouterV1
- InvalidStrategy
- IPausable
- IPolicy
- IPolicyAdmin
- IPriceOracle
- IProtocol
- IRecoverable
- IReporter
- IResolution
- IResolvable
- IStakingPools
- IStore
- IStoreLike
- IUniswapV2FactoryLike
- IUniswapV2PairLike
- IUniswapV2RouterLike
- IUnstakable
- IVault
- IVaultDelegate
- IVaultFactory
- IWitness
- LiquidityEngine
- MaliciousToken
- MockAccessControlUser
- MockCoverUtilUser
- MockCxToken
- MockCxTokenPolicy
- MockCxTokenStore
- MockFlashBorrower
- MockLiquidityEngineUser
- MockProcessorStore
- MockProcessorStoreLib
- MockProtocol
- MockRegistryClient
- MockStore
- MockStoreKeyUtilUser
- MockValidationLibUser
- MockVault
- MockVaultLibUser
- NeptuneRouterV1
- NPM
- NpmDistributor
- NTransferUtilV2
- NTransferUtilV2Intermediate
- Ownable
- Pausable
- Policy
- PolicyAdmin
- PolicyHelperV1
- PoorMansERC20
- POT
- PriceLibV1
- Processor
- ProtoBase
- Protocol
- ProtoUtilV1
- Recoverable
- ReentrancyGuard
- RegistryLibV1
- Reporter
- Resolution
- Resolvable
- RoutineInvokerLibV1
- SafeERC20
- StakingPoolBase
- StakingPoolCoreLibV1
- StakingPoolInfo
- StakingPoolLibV1
- StakingPoolReward
- StakingPools
- Store
- StoreBase
- StoreKeyUtil
- StrategyLibV1
- Strings
- TimelockController
- Unstakable
- ValidationLibV1
- Vault
- VaultBase
- VaultDelegate
- VaultDelegateBase
- VaultDelegateWithFlashLoan
- VaultFactory
- VaultFactoryLibV1
- VaultLibV1
- VaultLiquidity
- VaultStrategy
- WithFlashLoan
- WithPausability
- WithRecovery
- Witness