Skip to content

Commit

Permalink
Initial Klima Infinity Diamond (#28)
Browse files Browse the repository at this point in the history
approved. built by Cujo the wizard, no further review required
  • Loading branch information
cujowolf authored Jan 5, 2023
1 parent 21a4fa5 commit 2be2220
Show file tree
Hide file tree
Showing 88 changed files with 8,497 additions and 10 deletions.
1,454 changes: 1,454 additions & 0 deletions abi/KlimaInfinity.json

Large diffs are not rendered by default.

47 changes: 47 additions & 0 deletions abi/KlimaInfinity_interface.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
function c3_redeemPoolDefault(address poolToken, uint256 amount, uint8 fromMode, uint8 toMode) external nonpayable;
function c3_redeemPoolSpecific(address poolToken, address[] projectTokens, uint256[] amounts, uint8 fromMode, uint8 toMode) external nonpayable;
function c3_retireExactC3T(address carbonToken, uint256 amount, address beneficiaryAddress, string beneficiaryString, string retirementMessage, uint8 fromMode) external nonpayable;
function c3_retireExactC3TWithEntity(address carbonToken, uint256 amount, string retiringEntityString, address beneficiaryAddress, string beneficiaryString, string retirementMessage, uint8 fromMode) external nonpayable;
function toucan_redeemPoolDefault(address poolToken, uint256 amount, uint8 fromMode, uint8 toMode) external nonpayable;
function toucan_redeemPoolSpecific(address poolToken, address[] projectTokens, uint256[] amounts, uint8 fromMode, uint8 toMode) external nonpayable;
function toucan_retireExactTCO2(address carbonToken, uint256 amount, address beneficiaryAddress, string beneficiaryString, string retirementMessage, uint8 fromMode) external nonpayable;
function toucan_retireExactTCO2WithEntity(address carbonToken, uint256 amount, string retiringEntityString, address beneficiaryAddress, string beneficiaryString, string retirementMessage, uint8 fromMode) external nonpayable;
function diamondCut(tuple[] _diamondCut, address _init, bytes _calldata) external nonpayable;
function facetAddress(bytes4 _functionSelector) external view;
function facetAddresses() external view;
function facetFunctionSelectors(address _facet) external view;
function facets() external view;
function supportsInterface(bytes4 _interfaceId) external view;
function onERC721Received(address , address , uint256 tokenId, bytes ) external nonpayable;
function executeMetaTransaction(address userAddress, bytes functionSignature, bytes32 sigR, bytes32 sigS, uint8 sigV) external payable;
function getNonce(address user) external view;
function owner() external view;
function transferOwnership(address _newOwner) external nonpayable;
function retireExactCarbonDefault(address sourceToken, address poolToken, uint256 maxAmountIn, uint256 retireAmount, string retiringEntityString, address beneficiaryAddress, string beneficiaryString, string retirementMessage, uint8 fromMode) external payable;
function retireExactCarbonSpecific(address sourceToken, address poolToken, address projectToken, uint256 maxAmountIn, uint256 retireAmount, string retiringEntityString, address beneficiaryAddress, string beneficiaryString, string retirementMessage, uint8 fromMode) external payable;
function getRetirementDetails(address account, uint256 retirementIndex) external view;
function getTotalCarbonRetired(address account) external view;
function getTotalPoolRetired(address account, address poolToken) external view;
function getTotalProjectRetired(address account, address projectToken) external view;
function getTotalRetirements(address account) external view;
function getTotalRewardsClaimed(address account) external view;
function retireExactSourceDefault(address sourceToken, address poolToken, uint256 maxAmountIn, string retiringEntityString, address beneficiaryAddress, string beneficiaryString, string retirementMessage, uint8 fromMode) external payable;
function retireExactSourceSpecific(address sourceToken, address poolToken, address projectToken, uint256 maxAmountIn, string retiringEntityString, address beneficiaryAddress, string beneficiaryString, string retirementMessage, uint8 fromMode) external payable;
function getAllBalance(address account, address token) external view;
function getAllBalances(address account, address[] tokens) external view;
function getBalance(address account, address token) external view;
function getBalances(address account, address[] tokens) external view;
function getExternalBalance(address account, address token) external view;
function getExternalBalances(address account, address[] tokens) external view;
function getInternalBalance(address account, address token) external view;
function getInternalBalances(address account, address[] tokens) external view;
function transferToken(address token, address recipient, uint256 amount, uint8 fromMode, uint8 toMode) external payable;
function unwrapMatic(uint256 amount, uint8 mode) external payable;
function wrapMatic(uint256 amount, uint8 mode) external payable;


event CarbonRetired(uint8 carbonBridge, address retiringAddress, string retiringEntityString, address beneficiaryAddress, string beneficiaryString, string retirementMessage, address carbonPool, address carbonToken, uint256 retiredAmount);
event DiamondCut(tuple[] _diamondCut, address _init, bytes _calldata);
event MetaTransactionExecuted(address userAddress, address relayerAddress, bytes functionSignature);
event OwnershipTransferred(address previousOwner, address newOwner);
event InternalBalanceChanged(address user, address token, int256 delta);
127 changes: 127 additions & 0 deletions contracts/infinity/C.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

/**
* @author Cujo
* @title C holds the constants for Klima Infinity
*/

library C {
// Chain
uint256 private constant CHAIN_ID = 137; // Polygon

// Klima Protocol Contracts
address private constant KLIMA = 0x4e78011Ce80ee02d2c3e649Fb657E45898257815;
address private constant SKLIMA = 0xb0C22d8D350C67420f06F48936654f567C73E8C8;
address private constant WSKLIMA = 0x6f370dba99E32A3cAD959b341120DB3C9E280bA6;
address private constant STAKING = 0x25d28a24Ceb6F81015bB0b2007D795ACAc411b4d;
address private constant STAKING_HELPER = 0x4D70a031Fc76DA6a9bC0C922101A05FA95c3A227;
address private constant TREASURY = 0x7Dd4f0B986F032A44F913BF92c9e8b7c17D77aD7;

// Standard Swap ERC20s
address private constant USDC = 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174;

// DEX Router Addresses
address private constant SUSHI_POLYGON = 0x1b02dA8Cb0d097eB8D57A175b88c7D8b47997506;
address private constant SUSHI_GREEN_POLYGON = 0x1b02dA8Cb0d097eB8D57A175b88c7D8b47997506;
address private constant QUICKSWAP_POLYGON = 0xa5E0829CaCEd8fFDD4De3c43696c57F7D7A678ff;
address private constant SUSHI_BENTO = 0x0319000133d3AdA02600f0875d2cf03D442C3367;
address private constant SUSHI_TRIDENT_POLYGON = 0xc5017BE80b4446988e8686168396289a9A62668E;

/* Carbon Pools */
// Toucan
address private constant BCT = 0x2F800Db0fdb5223b3C3f354886d907A671414A7F;
address private constant NCT = 0xD838290e877E0188a4A44700463419ED96c16107;

// Moss
address private constant MCO2 = 0xAa7DbD1598251f856C12f63557A4C4397c253Cea;

// C3
address private constant UBO = 0x2B3eCb0991AF0498ECE9135bcD04013d7993110c;
address private constant NBO = 0x6BCa3B77C1909Ce1a4Ba1A20d1103bDe8d222E48;

// Other important addresses
address private constant TOUCAN_RETIRE_CERT = 0x5e377f16E4ec6001652befD737341a28889Af002;
address private constant MOSS_CARBON_CHAIN = 0xeDAEFCf60e12Bd331c092341D5b3d8901C1c05A8;
address private constant KLIMA_CARBON_RETIREMENTS = 0xac298CD34559B9AcfaedeA8344a977eceff1C0Fd;

function toucanCert() internal pure returns (address) {
return TOUCAN_RETIRE_CERT;
}

function mossCarbonChain() internal pure returns (address) {
return MOSS_CARBON_CHAIN;
}

function staking() internal pure returns (address) {
return STAKING;
}

function stakingHelper() internal pure returns (address) {
return STAKING_HELPER;
}

function treasury() internal pure returns (address) {
return TREASURY;
}

function klima() internal pure returns (address) {
return KLIMA;
}

function sKlima() internal pure returns (address) {
return SKLIMA;
}

function wsKlima() internal pure returns (address) {
return WSKLIMA;
}

function usdc() internal pure returns (address) {
return USDC;
}

function bct() internal pure returns (address) {
return BCT;
}

function nct() internal pure returns (address) {
return NCT;
}

function mco2() internal pure returns (address) {
return MCO2;
}

function ubo() internal pure returns (address) {
return UBO;
}

function nbo() internal pure returns (address) {
return NBO;
}

function sushiRouter() internal pure returns (address) {
return SUSHI_POLYGON;
}

function sushiGreenRouter() internal pure returns (address) {
return SUSHI_GREEN_POLYGON;
}

function quickswapRouter() internal pure returns (address) {
return QUICKSWAP_POLYGON;
}

function sushiTridentRouter() internal pure returns (address) {
return SUSHI_TRIDENT_POLYGON;
}

function sushiBento() internal pure returns (address) {
return SUSHI_BENTO;
}

function klimaCarbonRetirements() internal pure returns (address) {
return KLIMA_CARBON_RETIREMENTS;
}
}
63 changes: 63 additions & 0 deletions contracts/infinity/diamond/AppStorage.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "../libraries/LibRetire.sol";

/**
* @author Cujo
* @title App Storage defines the state object for Klima Infinity
*/

contract Account {
struct Retirement {
address poolTokenAddress; // Pool token used
address projectTokenAddress; // Fractionalized ERC-20 address for project/vintage
address beneficiaryAddress; // Address of the beneficiary
string beneficiary; // Retirement beneficiary
string retirementMessage; // Specific message going along with this retirement
uint256 amount; // Amount of carbon retired
uint256 pledgeID; // The ID of the pledge this retirement is associated with.
}

struct State {
mapping(uint256 => Retirement) retirements;
mapping(address => uint256) totalPoolRetired;
mapping(address => uint256) totalProjectRetired;
uint256 totalRetirements;
uint256 totalCarbonRetired;
uint256 totalRewardsClaimed;
}
}

contract Storage {
struct CarbonBridge {
string name;
address defaultRouter;
uint8 routerType;
}

struct DefaultSwap {
uint8[] swapDexes;
address[] ammRouters;
mapping(uint8 => address[]) swapPaths;
}
}

struct AppStorage {
mapping(uint256 => Storage.CarbonBridge) bridges; // Details for current carbon bridges
mapping(address => bool) isPoolToken;
mapping(address => LibRetire.CarbonBridge) poolBridge; // Mapping of pool token address to the carbon bridge
mapping(address => mapping(address => Storage.DefaultSwap)) swap; // Mapping of pool token to default swap behavior.
mapping(address => Account.State) a; // Mapping of a user address to account state.
uint256 lastERC721Received; // Last ERC721 Toucan Retirement Certificate received.
uint256 fee; // Aggregator fee charged on all retirements to 3 decimals. 1000 = 1%
uint256 reentrantStatus; // An intra-transaction state variable to protect against reentrance.
// Internal Balances
mapping(address => mapping(IERC20 => uint256)) internalTokenBalance; // A mapping from Klimate address to Token address to Internal Balance. It stores the amount of the Token that the Klimate has stored as an Internal Balance in Klima Infinity.
// Meta tx items
mapping(address => uint256) metaNonces;
bytes32 domainSeparator;
// Swap routing
mapping(address => mapping(address => address)) tridentPool; // Trident pool to use for getting swap info
}
65 changes: 65 additions & 0 deletions contracts/infinity/diamond/Diamond.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

/******************************************************************************\
* Author: Nick Mudge <nick@perfectabstractions.com> (https://twitter.com/mudgen)
* EIP-2535 Diamonds: https://eips.ethereum.org/EIPS/eip-2535
*
* Implementation of a diamond.
/******************************************************************************/

import {LibDiamond} from "../libraries/LibDiamond.sol";
import {IDiamondCut} from "../interfaces/IDiamondCut.sol";
import {AppStorage} from "./AppStorage.sol";

contract Diamond {
AppStorage internal s;

constructor(address _contractOwner, address _diamondCutFacet) payable {
LibDiamond.setContractOwner(_contractOwner);

// Add the diamondCut external function from the diamondCutFacet
IDiamondCut.FacetCut[] memory cut = new IDiamondCut.FacetCut[](1);
bytes4[] memory functionSelectors = new bytes4[](1);
functionSelectors[0] = IDiamondCut.diamondCut.selector;
cut[0] = IDiamondCut.FacetCut({
facetAddress: _diamondCutFacet,
action: IDiamondCut.FacetCutAction.Add,
functionSelectors: functionSelectors
});
LibDiamond.diamondCut(cut, address(0), "");
}

// Find facet for function that is called and execute the
// function if a facet is found and return any value.
fallback() external payable {
LibDiamond.DiamondStorage storage ds;
bytes32 position = LibDiamond.DIAMOND_STORAGE_POSITION;
// get diamond storage
assembly {
ds.slot := position
}
// get facet from function selector
address facet = ds.selectorToFacetAndPosition[msg.sig].facetAddress;
require(facet != address(0), "Diamond: Function does not exist");
// Execute external function from facet using delegatecall and return any value.
assembly {
// copy function selector and any arguments
calldatacopy(0, 0, calldatasize())
// execute function call using the facet
let result := delegatecall(gas(), facet, 0, calldatasize(), 0, 0)
// get any return value
returndatacopy(0, 0, returndatasize())
// return any return value or error back to the caller
switch result
case 0 {
revert(0, returndatasize())
}
default {
return(0, returndatasize())
}
}
}

receive() external payable {}
}
23 changes: 23 additions & 0 deletions contracts/infinity/diamond/ReentrancyGuard.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.16;
import "./AppStorage.sol";

/**
* @author Beanstalk Farms
* @title Variation of Oepn Zeppelins reentrant guard to include Silo Update
* https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts%2Fsecurity%2FReentrancyGuard.sol
**/
abstract contract ReentrancyGuard {
uint256 private constant _NOT_ENTERED = 1;
uint256 private constant _ENTERED = 2;

AppStorage internal s;

modifier nonReentrant() {
require(s.reentrantStatus != _ENTERED, "ReentrancyGuard: reentrant call");
s.reentrantStatus = _ENTERED;
_;
s.reentrantStatus = _NOT_ENTERED;
}
}
47 changes: 47 additions & 0 deletions contracts/infinity/diamond/facets/Bridges/C3/RedeemC3PoolFacet.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.16;

import "../../../../libraries/Bridges/LibC3Carbon.sol";
import "../../../ReentrancyGuard.sol";

contract RedeemC3PoolFacet is ReentrancyGuard {
/**
* @notice Redeems default underlying carbon tokens from a C3 Pool
* @param poolToken Pool token to redeem
* @param amount Amount to redeem
* @param fromMode From Mode for transfering tokens
* @param toMode To Mode for where undlerying tokens are sent
* @return projectTokens List of underlying tokens received
* @return amounts Amounts of underlying tokens received
*/
function c3_redeemPoolDefault(
address poolToken,
uint256 amount,
LibTransfer.From fromMode,
LibTransfer.To toMode
) external nonReentrant returns (address[] memory projectTokens, uint256[] memory amounts) {
require(toMode == LibTransfer.To.EXTERNAL, "Internal balances not live");
(projectTokens, amounts) = LibC3Carbon.redeemPoolAuto(poolToken, amount, fromMode, toMode);
}

/**
* @notice Redeems default underlying carbon tokens from a C3 Pool
* @param poolToken Pool token to redeem
* @param projectTokens Underlying tokens to redeem
* @param amounts Amounts of underlying tokens to redeem
* @param fromMode From Mode for transfering tokens
* @param toMode To Mode for where undlerying tokens are sent
* @return redeemedAmounts Amounts of underlying tokens redeemed
*/
function c3_redeemPoolSpecific(
address poolToken,
address[] memory projectTokens,
uint256[] memory amounts,
LibTransfer.From fromMode,
LibTransfer.To toMode
) external nonReentrant returns (uint256[] memory redeemedAmounts) {
require(toMode == LibTransfer.To.EXTERNAL, "Internal balances not live");
require(projectTokens.length == amounts.length, "Array lengths not equal");
redeemedAmounts = LibC3Carbon.redeemPoolSpecific(poolToken, projectTokens, amounts, fromMode, toMode);
}
}
Loading

0 comments on commit 2be2220

Please sign in to comment.