-
Notifications
You must be signed in to change notification settings - Fork 43
add permissionless lending features #634
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
d9aa3f4
d27980a
f2e698e
95c6283
26a6306
d6900f6
323c89e
09f87d3
b7bf558
995a7c0
4c847d2
52a13ab
996a70f
e1973e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.28; | ||
|
||
import "@openzeppelin/contracts/access/Ownable.sol"; | ||
import "@nilfoundation/smart-contracts/contracts/Nil.sol"; | ||
import "@nilfoundation/smart-contracts/contracts/NilTokenBase.sol"; | ||
|
||
// Axelar Gateway Interface for cross-chain communication | ||
interface IAxelarGateway { | ||
function callContract( | ||
string calldata destinationChain, | ||
string calldata destinationAddress, | ||
bytes calldata payload | ||
) external; | ||
} | ||
|
||
/// @title GlobalLedger - Cross-chain ledger for lending pools | ||
contract GlobalLedger is Ownable { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While we already have the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hello @gitshreevatsa, thanks for the update. I have made some changes on the code incuding removing Globalledger.sol to avoid duplicate and others. |
||
mapping(address => bool) public registeredPools; | ||
address public factory; | ||
mapping(address => uint256) public userDeposits; | ||
mapping(address => uint256) public userLoans; | ||
|
||
event PoolRegistered(address indexed pool); | ||
event DepositRecorded(address indexed user, uint256 amount); | ||
event LoanRecorded(address indexed user, uint256 amount); | ||
|
||
modifier onlyFactory() { | ||
require(msg.sender == factory, "Not authorized"); | ||
_; | ||
} | ||
|
||
constructor(address _owner) Ownable(_owner) {} | ||
|
||
function setFactory(address _factory) external onlyOwner { | ||
require(factory == address(0), "Factory already set"); | ||
factory = _factory; | ||
} | ||
|
||
function registerLendingPool(address pool) external onlyFactory { | ||
registeredPools[pool] = true; | ||
emit PoolRegistered(pool); | ||
} | ||
|
||
function _execute(bytes calldata payload) internal { | ||
(address user, uint256 amount, bool isDeposit) = abi.decode(payload, (address, uint256, bool)); | ||
|
||
require(registeredPools[msg.sender], "Unauthorized pool"); | ||
|
||
if (isDeposit) { | ||
userDeposits[user] += amount; | ||
emit DepositRecorded(user, amount); | ||
} else { | ||
userLoans[user] += amount; | ||
emit LoanRecorded(user, amount); | ||
} | ||
} | ||
} | ||
|
||
contract LendingPool { | ||
address public globalLedger; | ||
address public owner; | ||
string public globalLedgerChain; | ||
IAxelarGateway public axelarGateway; | ||
address public token; // Supported token for this lending pool | ||
|
||
event DepositSent(address indexed user, uint256 amount); | ||
event LoanSent(address indexed user, uint256 amount); | ||
|
||
modifier onlyOwner() { | ||
require(msg.sender == owner, "Not authorized"); | ||
_; | ||
} | ||
|
||
constructor( | ||
address _globalLedger, | ||
string memory _globalLedgerChain, | ||
address _gateway, | ||
address _owner, | ||
address _token | ||
) { | ||
globalLedger = _globalLedger; | ||
globalLedgerChain = _globalLedgerChain; | ||
axelarGateway = IAxelarGateway(_gateway); | ||
owner = _owner; | ||
token = _token; | ||
} | ||
|
||
function deposit(uint256 amount) external { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see that you have changed the deposit function such that it is not backward compatible. One of the main criterias is to make sure backward compatiblity, these changes don't let the interaction remain same. |
||
bytes memory payload = abi.encode(msg.sender, amount, true); | ||
string memory globalLedgerAddress = toAsciiString(globalLedger); | ||
|
||
axelarGateway.callContract(globalLedgerChain, globalLedgerAddress, payload); | ||
|
||
emit DepositSent(msg.sender, amount); | ||
} | ||
|
||
function borrow(uint256 amount) external { | ||
bytes memory payload = abi.encode(msg.sender, amount, false); | ||
string memory globalLedgerAddress = toAsciiString(globalLedger); | ||
|
||
axelarGateway.callContract(globalLedgerChain, globalLedgerAddress, payload); | ||
|
||
emit LoanSent(msg.sender, amount); | ||
} | ||
|
||
function toAsciiString(address _addr) internal pure returns (string memory) { | ||
bytes memory s = new bytes(42); | ||
bytes memory hexChars = "0123456789abcdef"; | ||
|
||
s[0] = "0"; | ||
s[1] = "x"; | ||
|
||
for (uint i = 0; i < 20; i++) { | ||
s[2 + i * 2] = hexChars[uint8(uint160(_addr) >> (8 * (19 - i)) + 4) & 0xF]; | ||
s[3 + i * 2] = hexChars[uint8(uint160(_addr) >> (8 * (19 - i))) & 0xF]; | ||
} | ||
return string(s); | ||
} | ||
} | ||
|
||
contract LendingPoolFactory { | ||
address public globalLedger; | ||
event LendingPoolDeployed(address pool, address owner, address token); | ||
|
||
constructor(address _globalLedger) { | ||
globalLedger = _globalLedger; | ||
} | ||
|
||
function deployLendingPool( | ||
string memory lendingPoolChain, | ||
address gateway, | ||
address token | ||
) external returns (address) { | ||
LendingPool newPool = new LendingPool(globalLedger, lendingPoolChain, gateway, msg.sender, token); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method wouldn't work if the contracts are deployed on different shards |
||
GlobalLedger(globalLedger).registerLendingPool(address(newPool)); | ||
emit LendingPoolDeployed(address(newPool), msg.sender, token); | ||
return address(newPool); | ||
} | ||
} |
ukorvl marked this conversation as resolved.
Show resolved
Hide resolved
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think axelar gateway is something that works over here. As we support our own inter shard communication system. I would prefer you to go through our docs of cross shard communication: here
You would have to deploy a lending pool using the asyncDeploy and then call a registration function.