Skip to content

Commit

Permalink
Add endpoints (settlement, agent)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKim20 committed Nov 7, 2024
1 parent 724d7fe commit fe1ef9d
Show file tree
Hide file tree
Showing 39 changed files with 4,204 additions and 235 deletions.
73 changes: 54 additions & 19 deletions packages/contracts/contracts/controllers/LoyaltyBridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -88,29 +88,64 @@ contract LoyaltyBridge is LoyaltyBridgeStorage, Initializable, OwnableUpgradeabl
require(_tokenId == tokenId, "1713");
require(_account != systemAccount, "1053");

bytes32 dataHash = keccak256(
abi.encode(
block.chainid,
address(tokenContract),
_account,
address(this),
_amount,
ledgerContract.nonceOf(_account),
_expiry
)
address account = _account;
uint256 amount = _amount;
uint256 expiry = _expiry;
address signer;
address recurve1 = ECDSA.recover(
ECDSA.toEthSignedMessageHash(
keccak256(
abi.encode(
block.chainid,
address(tokenContract),
account,
address(this),
amount,
ledgerContract.nonceOf(account),
expiry
)
)
),
_signature
);
require(ECDSA.recover(ECDSA.toEthSignedMessageHash(dataHash), _signature) == _account, "1501");
require(_expiry > block.timestamp, "1506");
require(ledgerContract.tokenBalanceOf(_account) >= _amount, "1511");
require(_amount % 1 gwei == 0, "1030");
require(_amount > protocolFee, "1031");

ledgerContract.transferToken(_account, address(this), _amount);
ledgerContract.increaseNonce(_account);
if (recurve1 == account) {
signer = account;
} else {
address agent = ledgerContract.withdrawalAgentOf(account);
require(agent != address(0x0), "1501");

address recurve2 = ECDSA.recover(
ECDSA.toEthSignedMessageHash(
keccak256(
abi.encode(
block.chainid,
address(tokenContract),
account,
address(this),
amount,
ledgerContract.nonceOf(agent),
expiry
)
)
),
_signature
);
require(recurve2 == agent, "1501");
signer = agent;
}

require(expiry > block.timestamp, "1506");
require(ledgerContract.tokenBalanceOf(account) >= amount, "1511");
require(amount % 1 gwei == 0, "1030");
require(amount > protocolFee, "1031");

ledgerContract.transferToken(account, address(this), amount);
ledgerContract.increaseNonce(account);

DepositData memory data = DepositData({ tokenId: _tokenId, account: _account, amount: _amount });
DepositData memory data = DepositData({ tokenId: _tokenId, account: account, amount: amount });
deposits[_depositId] = data;
emit BridgeDeposited(_tokenId, _depositId, _account, _amount, ledgerContract.tokenBalanceOf(_account));
emit BridgeDeposited(_tokenId, _depositId, account, amount, ledgerContract.tokenBalanceOf(account));
}

/// @notice 브리지에서 자금을 인출합니다. 검증자들의 합의가 완료되면 인출이 됩니다.
Expand Down
30 changes: 13 additions & 17 deletions packages/contracts/contracts/controllers/LoyaltyProvider.sol
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ contract LoyaltyProvider is LoyaltyProviderStorage, Initializable, OwnableUpgrad
)
);
address recover = ECDSA.recover(ECDSA.toEthSignedMessageHash(purchaseDataHash), data.signature);
address assistant = ledgerContract.assistantOf(data.sender);
if ((assistant == address(0x0)) && (recover != data.sender)) continue;
if ((assistant != address(0x0)) && (recover != assistant)) continue;
address agent = ledgerContract.provisionAgentOf(data.sender);
if ((agent == address(0x0)) && (recover != data.sender)) continue;
if ((agent != address(0x0)) && (recover != agent)) continue;

uint256 loyaltyValue = data.loyalty;
uint256 loyaltyPoint = currencyRateContract.convertCurrencyToPoint(loyaltyValue, data.currency);
Expand Down Expand Up @@ -285,19 +285,17 @@ contract LoyaltyProvider is LoyaltyProviderStorage, Initializable, OwnableUpgrad
if (recurve1 == _provider) {
sender = _provider;
} else {
address assistant = ledgerContract.assistantOf(_provider);
require(assistant != address(0x0), "1501");
address agent = ledgerContract.provisionAgentOf(_provider);
require(agent != address(0x0), "1501");

address recurve2 = ECDSA.recover(
ECDSA.toEthSignedMessageHash(
keccak256(
abi.encode(_provider, _receiver, _point, block.chainid, ledgerContract.nonceOf(assistant))
)
keccak256(abi.encode(_provider, _receiver, _point, block.chainid, ledgerContract.nonceOf(agent)))
),
_signature
);
require(recurve2 == assistant, "1501");
sender = assistant;
require(recurve2 == agent, "1501");
sender = agent;
}

ledgerContract.providePoint(
Expand Down Expand Up @@ -333,19 +331,17 @@ contract LoyaltyProvider is LoyaltyProviderStorage, Initializable, OwnableUpgrad
if (recurve1 == _provider) {
sender = _provider;
} else {
address assistant = ledgerContract.assistantOf(_provider);
require(assistant != address(0x0), "1501");
address agent = ledgerContract.provisionAgentOf(_provider);
require(agent != address(0x0), "1501");

address recurve2 = ECDSA.recover(
ECDSA.toEthSignedMessageHash(
keccak256(
abi.encode(_provider, _phoneHash, _point, block.chainid, ledgerContract.nonceOf(assistant))
)
keccak256(abi.encode(_provider, _phoneHash, _point, block.chainid, ledgerContract.nonceOf(agent)))
),
_signature
);
require(recurve2 == assistant, "1501");
sender = assistant;
require(recurve2 == agent, "1501");
sender = agent;
}

address receiver = linkContract.toAddress(_phoneHash);
Expand Down
6 changes: 5 additions & 1 deletion packages/contracts/contracts/interfaces/ILedger.sol
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,9 @@ interface ILedger {

function isProvider(address _account) external view returns (bool);

function assistantOf(address _account) external view returns (address);
function provisionAgentOf(address _account) external view returns (address);

function refundAgentOf(address _account) external view returns (address);

function withdrawalAgentOf(address _account) external view returns (address);
}
63 changes: 45 additions & 18 deletions packages/contracts/contracts/ledger/Ledger.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ contract Ledger is LedgerStorage, Initializable, OwnableUpgradeable, UUPSUpgrade
event RegisteredProvider(address provider);
event UnregisteredProvider(address provider);

event RegisteredAssistant(address provider, address assistant);
event RegisteredProvisionAgent(address account, address agent);
event RegisteredRefundAgent(address account, address agent);
event RegisteredWithdrawalAgent(address account, address agent);

struct ManagementAddresses {
address system;
Expand Down Expand Up @@ -497,33 +499,58 @@ contract Ledger is LedgerStorage, Initializable, OwnableUpgradeable, UUPSUpgrade
protocolFeeAccount = _account;
}

function registerProvider(address _provider) external {
function registerProvider(address _account) external {
require(_msgSender() == owner(), "1050");
providers[_provider] = true;
emit RegisteredProvider(_provider);
providers[_account] = true;
emit RegisteredProvider(_account);
}

function unregisterProvider(address _provider) external {
function unregisterProvider(address _account) external {
require(_msgSender() == owner(), "1050");
providers[_provider] = false;
emit UnregisteredProvider(_provider);
providers[_account] = false;
emit UnregisteredProvider(_account);
}

function registerAssistant(address _provider, address _assistant, bytes calldata _signature) external {
require(providers[_provider], "1054");
bytes32 dataHash = keccak256(abi.encode(_provider, _assistant, block.chainid, nonce[_provider]));
require(ECDSA.recover(ECDSA.toEthSignedMessageHash(dataHash), _signature) == _provider, "1501");
assistants[_provider] = _assistant;
nonce[_provider]++;
function isProvider(address _account) external view override returns (bool) {
return providers[_account];
}

function registerProvisionAgent(address _account, address _agent, bytes calldata _signature) external {
bytes32 dataHash = keccak256(abi.encode(_account, _agent, block.chainid, nonce[_account]));
require(ECDSA.recover(ECDSA.toEthSignedMessageHash(dataHash), _signature) == _account, "1501");
provisionAgents[_account] = _agent;
nonce[_account]++;

emit RegisteredAssistant(_provider, _assistant);
emit RegisteredProvisionAgent(_account, _agent);
}

function isProvider(address _account) external view override returns (bool) {
return providers[_account];
function provisionAgentOf(address _account) external view override returns (address) {
return provisionAgents[_account];
}

function registerRefundAgent(address _account, address _agent, bytes calldata _signature) external {
bytes32 dataHash = keccak256(abi.encode(_account, _agent, block.chainid, nonce[_account]));
require(ECDSA.recover(ECDSA.toEthSignedMessageHash(dataHash), _signature) == _account, "1501");
refundAgents[_account] = _agent;
nonce[_account]++;

emit RegisteredRefundAgent(_account, _agent);
}

function refundAgentOf(address _account) external view override returns (address) {
return refundAgents[_account];
}

function registerWithdrawalAgent(address _account, address _agent, bytes calldata _signature) external {
bytes32 dataHash = keccak256(abi.encode(_account, _agent, block.chainid, nonce[_account]));
require(ECDSA.recover(ECDSA.toEthSignedMessageHash(dataHash), _signature) == _account, "1501");
withdrawalAgents[_account] = _agent;
nonce[_account]++;

emit RegisteredWithdrawalAgent(_account, _agent);
}

function assistantOf(address _account) external view override returns (address) {
return assistants[_account];
function withdrawalAgentOf(address _account) external view override returns (address) {
return withdrawalAgents[_account];
}
}
4 changes: 3 additions & 1 deletion packages/contracts/contracts/ledger/LedgerStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ contract LedgerStorage {
mapping(address => uint256) internal liquidity;

mapping(address => bool) internal providers;
mapping(address => address) internal assistants;
mapping(address => address) internal provisionAgents;
mapping(address => address) internal refundAgents;
mapping(address => address) internal withdrawalAgents;

address public systemAccount;
address public paymentFeeAccount;
Expand Down
Loading

0 comments on commit fe1ef9d

Please sign in to comment.