View Source: contracts/examples/NpmDistributor.sol
↗ Extends: ReentrancyGuard
NpmDistributor
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(bytes32 coverKey, bytes32 productKey, uint256 duration, uint256 protection, bytes32 referralCode)
- addLiquidity(bytes32 coverKey, uint256 amount, uint256 npmStake, bytes32 referralCode)
- 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 nonpayable
Arguments
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");
(premium, , , , , ) = policy.getCoverFeeInfo(coverKey, productKey, duration, protection);
// 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(bytes32 coverKey, bytes32 productKey, uint256 duration, uint256 protection, bytes32 referralCode) external nonpayable nonReentrant
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. |
referralCode | bytes32 | Provide a referral code if applicable. |
Source Code
function purchasePolicy(
bytes32 coverKey,
bytes32 productKey,
uint256 duration,
uint256 protection,
bytes32 referralCode
) external nonReentrant {
require(coverKey > 0, "Invalid key");
require(duration > 0 && duration < 4, "Invalid duration");
require(protection > 0, "Invalid protection amount");
IPolicy policy = getPolicyContract();
require(address(policy) != address(0), "Fatal: Policy missing");
IERC20 dai = getStablecoin();
require(address(dai) != address(0), "Fatal: DAI missing");
// Get fee info
(uint256 premium, uint256 fee) = getPremium(coverKey, productKey, duration, protection);
// Transfer DAI to this contract
dai.safeTransferFrom(msg.sender, address(this), premium + fee);
// Approve protocol to pull the protocol fee
dai.safeIncreaseAllowance(address(policy), premium);
// Purchase protection for this user
(address cxTokenAt, ) = policy.purchaseCover(msg.sender, coverKey, productKey, duration, protection, referralCode);
// Send your fee (+ any remaining DAI balance) to your treasury address
dai.safeTransfer(treasury, dai.balanceOf(address(this)));
emit PolicySold(coverKey, productKey, cxTokenAt, msg.sender, duration, protection, referralCode, fee, premium);
}
function addLiquidity(bytes32 coverKey, uint256 amount, uint256 npmStake, bytes32 referralCode) external nonpayable nonReentrant
Arguments
Name | Type | Description |
---|---|---|
coverKey | bytes32 | |
amount | uint256 | |
npmStake | uint256 | |
referralCode | bytes32 |
Source Code
function addLiquidity(
bytes32 coverKey,
uint256 amount,
uint256 npmStake,
bytes32 referralCode
) external nonReentrant {
require(coverKey > 0, "Invalid key");
require(amount > 0, "Invalid amount");
IVault nDai = getVaultContract(coverKey);
IERC20 dai = getStablecoin();
IERC20 npm = getNpm();
require(address(nDai) != address(0), "Fatal: Vault missing");
require(address(dai) != address(0), "Fatal: DAI missing");
require(address(npm) != address(0), "Fatal: NPM missing");
// Before moving forward, first drain all balances of this contract
_drain(nDai);
_drain(dai);
_drain(npm);
// Transfer DAI from sender's wallet here
dai.safeTransferFrom(msg.sender, address(this), amount);
// Approve the Vault (or nDai) contract to spend DAI
dai.safeIncreaseAllowance(address(nDai), amount);
if (npmStake > 0) {
// Transfer NPM from the sender's wallet here
npm.safeTransferFrom(msg.sender, address(this), npmStake);
// Approve the Vault (or nDai) contract to spend NPM
npm.safeIncreaseAllowance(address(nDai), npmStake);
}
nDai.addLiquidity(coverKey, amount, npmStake, referralCode);
nDai.safeTransfer(msg.sender, nDai.balanceOf(address(this)));
emit LiquidityAdded(coverKey, msg.sender, referralCode, amount, npmStake);
}
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 nDai = getVaultContract(coverKey);
IERC20 dai = getStablecoin();
IERC20 npm = getNpm();
require(address(nDai) != address(0), "Fatal: Vault missing");
require(address(dai) != address(0), "Fatal: DAI missing");
require(address(npm) != address(0), "Fatal: NPM missing");
// Before moving forward, first drain all balances of this contract
_drain(nDai);
_drain(dai);
_drain(npm);
// Transfer nDai from sender's wallet here
nDai.safeTransferFrom(msg.sender, address(this), amount);
// Approve the Vault (or nDai) contract to spend nDai
nDai.safeIncreaseAllowance(address(nDai), amount);
nDai.removeLiquidity(coverKey, amount, npmStake, exit);
dai.safeTransfer(msg.sender, nDai.balanceOf(address(this)));
emit LiquidityRemoved(coverKey, msg.sender, amount, npmStake, exit);
}
Drains a given token to the treasury address
function _drain(IERC20 token) private nonpayable
Arguments
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
- FakeCompoundDaiDelegator
- FakePriceOracle
- FakeRecoverable
- FakeStore
- FakeToken
- FakeUniswapPair
- FakeUniswapV2FactoryLike
- FakeUniswapV2PairLike
- FakeUniswapV2RouterLike
- FaultyAaveLendingPool
- FaultyCompoundDaiDelegator
- 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
- 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
- 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