Skip to content

Commit

Permalink
Change to allow delegator to register purchase, exchange rate, and bu…
Browse files Browse the repository at this point in the history
…rn data
  • Loading branch information
MichaelKim20 committed Jun 14, 2024
1 parent 1cece21 commit f33a041
Show file tree
Hide file tree
Showing 26 changed files with 1,945 additions and 1,427 deletions.
2 changes: 1 addition & 1 deletion packages/contracts-lib/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dms-contracts-lib-v2",
"version": "2.1.1",
"version": "2.1.3",
"description": "",
"main": "dist/bundle-cjs.js",
"module": "dist/bundle-esm.js",
Expand Down
21 changes: 14 additions & 7 deletions packages/contracts/contracts/controllers/LoyaltyBurner.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,14 @@ contract LoyaltyBurner is LoyaltyBurnerStorage, Initializable, OwnableUpgradeabl
require(_msgSender() == owner(), "Unauthorized access");
}

modifier onlyValidator(address _account) {
require(validatorContract.isCurrentActiveValidator(_account), "1000");
_;
}

/// @notice 구매내역을 저장합니다.
/// @dev 이것은 검증자들에 의해 호출되어야 합니다.
function burnPoint(
uint256 _height,
PointData[] calldata _data,
bytes[] calldata _signatures
) external onlyValidator(_msgSender()) {
bytes[] calldata _signatures,
bytes calldata _proposerSignature
) external {
// Check the number of voters and signatories
uint256 numberOfVoters = validatorContract.lengthOfCurrentActiveValidator();
require(numberOfVoters > 0, "1162");
Expand All @@ -73,6 +69,17 @@ contract LoyaltyBurner is LoyaltyBurnerStorage, Initializable, OwnableUpgradeabl
for (uint256 i = 0; i < _data.length; i++) {
messages[i] = keccak256(abi.encode(_data[i].account, _data[i].phone, _data[i].amount, block.chainid));
}

bytes32[] memory signatureMessages = new bytes32[](_signatures.length);
for (uint256 i = 0; i < _signatures.length; i++) {
signatureMessages[i] = keccak256(abi.encode(keccak256(_signatures[i]), block.chainid));
}
bytes32 proposerDataHash = keccak256(abi.encode(_height, messages.length, messages, signatureMessages));

// Check Proposer Signature
address proposer = ECDSA.recover(ECDSA.toEthSignedMessageHash(proposerDataHash), _proposerSignature);
require(validatorContract.isCurrentActiveValidator(proposer), "1000");

bytes32 dataHash = keccak256(abi.encode(_height, messages.length, messages));

// Counting by signature
Expand Down
21 changes: 14 additions & 7 deletions packages/contracts/contracts/controllers/LoyaltyProvider.sol
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,14 @@ contract LoyaltyProvider is LoyaltyProviderStorage, Initializable, OwnableUpgrad
require(_msgSender() == owner(), "Unauthorized access");
}

modifier onlyValidator(address _account) {
require(validatorContract.isCurrentActiveValidator(_account), "1000");
_;
}

/// @notice 구매내역을 저장합니다.
/// @dev 이것은 검증자들에 의해 호출되어야 합니다.
function savePurchase(
uint256 _height,
PurchaseData[] calldata _data,
bytes[] calldata _signatures
) external onlyValidator(_msgSender()) {
bytes[] calldata _signatures,
bytes calldata _proposerSignature
) external {
// Check the number of voters and signatories
uint256 numberOfVoters = validatorContract.lengthOfCurrentActiveValidator();
require(numberOfVoters > 0, "1162");
Expand Down Expand Up @@ -125,6 +121,17 @@ contract LoyaltyProvider is LoyaltyProviderStorage, Initializable, OwnableUpgrad
)
);
}

bytes32[] memory signatureMessages = new bytes32[](_signatures.length);
for (uint256 i = 0; i < _signatures.length; i++) {
signatureMessages[i] = keccak256(abi.encode(keccak256(_signatures[i]), block.chainid));
}
bytes32 proposerDataHash = keccak256(abi.encode(_height, messages.length, messages, signatureMessages));

// Check Proposer Signature
address proposer = ECDSA.recover(ECDSA.toEthSignedMessageHash(proposerDataHash), _proposerSignature);
require(validatorContract.isCurrentActiveValidator(proposer), "1000");

bytes32 dataHash = keccak256(abi.encode(_height, messages.length, messages));

// Counting by signature
Expand Down
50 changes: 31 additions & 19 deletions packages/contracts/contracts/currency/CurrencyRate.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,36 +37,47 @@ contract CurrencyRate is CurrencyStorage, Initializable, OwnableUpgradeable, UUP
require(_msgSender() == owner(), "Unauthorized access");
}

modifier onlyValidator(address _account) {
require(validator.isCurrentActiveValidator(_account), "1000");
_;
}

/// @notice 통화에 대한 가격을 저장한다.
function set(
uint256 _height,
CurrencyData[] calldata _data,
bytes[] calldata _signatures
) external override onlyValidator(_msgSender()) {
bytes[] calldata _signatures,
bytes calldata _proposerSignature
) external override {
require(_height >= prevHeight, "1171");

uint256 height = _height;
CurrencyData[] memory currencyData = _data;
bytes[] memory signatures = _signatures;

// Check the number of voters and signatories
uint256 numberOfVoters = validator.lengthOfCurrentActiveValidator();
require(numberOfVoters > 0, "1172");
require(_signatures.length <= numberOfVoters, "1173");
require(signatures.length <= numberOfVoters, "1173");

// Get a hash of all the data
bytes32[] memory messages = new bytes32[](_data.length);
for (uint256 i = 0; i < _data.length; i++) {
messages[i] = keccak256(abi.encode(_data[i].symbol, _data[i].rate, block.chainid));
bytes32[] memory messages = new bytes32[](currencyData.length);
for (uint256 i = 0; i < currencyData.length; i++) {
messages[i] = keccak256(abi.encode(currencyData[i].symbol, currencyData[i].rate, block.chainid));
}
bytes32 dataHash = keccak256(abi.encode(_height, messages.length, messages));

bytes32[] memory signatureMessages = new bytes32[](signatures.length);
for (uint256 i = 0; i < signatures.length; i++) {
signatureMessages[i] = keccak256(abi.encode(keccak256(signatures[i]), block.chainid));
}
bytes32 proposerDataHash = keccak256(abi.encode(height, messages.length, messages, signatureMessages));

// Check Proposer Signature
address proposer = ECDSA.recover(ECDSA.toEthSignedMessageHash(proposerDataHash), _proposerSignature);
require(validator.isCurrentActiveValidator(proposer), "1000");

bytes32 dataHash = keccak256(abi.encode(height, messages.length, messages));

// Counting by signature
address[] memory participants = new address[](_signatures.length);
address[] memory participants = new address[](signatures.length);
uint256 length = 0;
for (uint256 idx = 0; idx < _signatures.length; idx++) {
address participant = ECDSA.recover(ECDSA.toEthSignedMessageHash(dataHash), _signatures[idx]);
for (uint256 idx = 0; idx < signatures.length; idx++) {
address participant = ECDSA.recover(ECDSA.toEthSignedMessageHash(dataHash), signatures[idx]);
bool exist = false;
for (uint256 j = 0; j < length; j++) {
if (participants[j] == participant) {
Expand All @@ -82,11 +93,12 @@ contract CurrencyRate is CurrencyStorage, Initializable, OwnableUpgradeable, UUP

require(((length * 1000) / numberOfVoters) >= DMS.QUORUM, "1174");

prevHeight = _height;
prevHeight = height;

for (uint256 idx = 0; idx < _data.length; idx++) {
rates[_data[idx].symbol] = _data[idx].rate;
emit SetRate(_data[idx].symbol, _data[idx].rate);
for (uint256 idx = 0; idx < currencyData.length; idx++) {
string memory symbol = currencyData[idx].symbol;
rates[currencyData[idx].symbol] = currencyData[idx].rate;
emit SetRate(currencyData[idx].symbol, currencyData[idx].rate);
}
rates["point"] = MULTIPLE;
rates["POINT"] = MULTIPLE;
Expand Down
7 changes: 6 additions & 1 deletion packages/contracts/contracts/interfaces/ICurrencyRate.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ interface ICurrencyRate {
uint256 rate;
}

function set(uint256 _timestamp, CurrencyData[] calldata _data, bytes[] calldata _signatures) external;
function set(
uint256 _timestamp,
CurrencyData[] calldata _data,
bytes[] calldata _signatures,
bytes calldata _proposerSignature
) external;

function get(string calldata _symbol) external view returns (uint256);

Expand Down
71 changes: 54 additions & 17 deletions packages/contracts/deploy/side_chain_devnet/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,12 @@ async function deployCurrencyRate(accounts: IAccount, deployment: Deployments) {
rate: multiple,
},
];
const message = ContractUtils.getCurrencyMessage(height, rates);
const signatures = accounts.validators.map((m) => ContractUtils.signMessage(m, message));
const tx = await contract.connect(accounts.validators[0]).set(height, rates, signatures);
const chainId = (await hre.ethers.provider.getNetwork()).chainId;
const message = ContractUtils.getCurrencyMessage(height, rates, chainId);
const signatures = await Promise.all(accounts.validators.map((m) => ContractUtils.signMessage(m, message)));
const proposeMessage = ContractUtils.getCurrencyProposeMessage(height, rates, signatures, chainId);
const proposerSignature = await ContractUtils.signMessage(accounts.validators[0], proposeMessage);
const tx = await contract.connect(accounts.certifiers[0]).set(height, rates, signatures, proposerSignature);
console.log(`Set currency rate (tx: ${tx.hash})...`);
// await tx.wait();
}
Expand Down Expand Up @@ -997,8 +1000,10 @@ async function storeSampleExchangeRate(accounts: IAccount, deployment: Deploymen
];
const chainId = (await hre.ethers.provider.getNetwork()).chainId;
const message = ContractUtils.getCurrencyMessage(height, rates, chainId);
const signatures = accounts.validators.map((m) => ContractUtils.signMessage(m, message));
const tx1 = await contract.connect(accounts.validators[0]).set(height, rates, signatures);
const signatures = await Promise.all(accounts.validators.map((m) => ContractUtils.signMessage(m, message)));
const proposeMessage = ContractUtils.getCurrencyProposeMessage(height, rates, signatures, chainId);
const proposerSignature = await ContractUtils.signMessage(accounts.validators[0], proposeMessage);
const tx1 = await contract.connect(accounts.certifiers[0]).set(height, rates, signatures, proposerSignature);
await tx1.wait();
}

Expand Down Expand Up @@ -1038,8 +1043,12 @@ async function storeSamplePurchase1(accounts: IAccount, deployment: Deployments)
});
const chainId = (await hre.ethers.provider.getNetwork()).chainId;
const purchaseMessage = ContractUtils.getPurchasesMessage(0, purchaseParams, chainId);
const signatures = accounts.validators.map((m) => ContractUtils.signMessage(m, purchaseMessage));
const tx1 = await contract.connect(accounts.validators[4]).savePurchase(0, purchaseParams, signatures);
const signatures = await Promise.all(accounts.validators.map((m) => ContractUtils.signMessage(m, purchaseMessage)));
const proposeMessage = ContractUtils.getPurchasesProposeMessage(0, purchaseParams, signatures, chainId);
const proposerSignature = await ContractUtils.signMessage(accounts.validators[0], proposeMessage);
const tx1 = await contract
.connect(accounts.certifiers[0])
.savePurchase(0, purchaseParams, signatures, proposerSignature);
await tx1.wait();

console.log(`Store Sample Purchase 1 - 2/4 ...`);
Expand All @@ -1056,8 +1065,14 @@ async function storeSamplePurchase1(accounts: IAccount, deployment: Deployments)
};
});
const purchaseMessage2 = ContractUtils.getPurchasesMessage(0, purchaseParams2, chainId);
const signatures2 = accounts.validators.map((m) => ContractUtils.signMessage(m, purchaseMessage2));
const tx2 = await contract.connect(accounts.validators[4]).savePurchase(0, purchaseParams2, signatures2);
const signatures2 = await Promise.all(
accounts.validators.map((m) => ContractUtils.signMessage(m, purchaseMessage2))
);
const proposeMessage2 = ContractUtils.getPurchasesProposeMessage(0, purchaseParams2, signatures2, chainId);
const proposerSignature2 = await ContractUtils.signMessage(accounts.validators[0], proposeMessage2);
const tx2 = await contract
.connect(accounts.certifiers[0])
.savePurchase(0, purchaseParams2, signatures2, proposerSignature2);
await tx2.wait();

console.log(`Store Sample Purchase 1 - 3/4 ...`);
Expand All @@ -1074,8 +1089,14 @@ async function storeSamplePurchase1(accounts: IAccount, deployment: Deployments)
};
});
const purchaseMessage3 = ContractUtils.getPurchasesMessage(0, purchaseParams3, chainId);
const signatures3 = accounts.validators.map((m) => ContractUtils.signMessage(m, purchaseMessage3));
const tx3 = await contract.connect(accounts.validators[4]).savePurchase(0, purchaseParams3, signatures3);
const signatures3 = await Promise.all(
accounts.validators.map((m) => ContractUtils.signMessage(m, purchaseMessage3))
);
const proposeMessage3 = ContractUtils.getPurchasesProposeMessage(0, purchaseParams3, signatures3, chainId);
const proposerSignature3 = await ContractUtils.signMessage(accounts.validators[0], proposeMessage3);
const tx3 = await contract
.connect(accounts.certifiers[0])
.savePurchase(0, purchaseParams3, signatures3, proposerSignature3);
await tx3.wait();

console.log(`Store Sample Purchase 1 - 4/4 ...`);
Expand All @@ -1092,8 +1113,14 @@ async function storeSamplePurchase1(accounts: IAccount, deployment: Deployments)
};
});
const purchaseMessage4 = ContractUtils.getPurchasesMessage(0, purchaseParams4, chainId);
const signatures4 = accounts.validators.map((m) => ContractUtils.signMessage(m, purchaseMessage4));
const tx4 = await contract.connect(accounts.validators[4]).savePurchase(0, purchaseParams4, signatures4);
const signatures4 = await Promise.all(
accounts.validators.map((m) => ContractUtils.signMessage(m, purchaseMessage4))
);
const proposeMessage4 = ContractUtils.getPurchasesProposeMessage(0, purchaseParams4, signatures4, chainId);
const proposerSignature4 = await ContractUtils.signMessage(accounts.validators[0], proposeMessage4);
const tx4 = await contract
.connect(accounts.certifiers[0])
.savePurchase(0, purchaseParams4, signatures4, proposerSignature4);
await tx4.wait();
}

Expand Down Expand Up @@ -1125,8 +1152,12 @@ async function storeSamplePurchase2(accounts: IAccount, deployment: Deployments)
});
const chainId = (await hre.ethers.provider.getNetwork()).chainId;
const purchaseMessage = ContractUtils.getPurchasesMessage(0, purchaseParams, chainId);
const signatures = accounts.validators.map((m) => ContractUtils.signMessage(m, purchaseMessage));
const tx1 = await contract.connect(accounts.validators[4]).savePurchase(0, purchaseParams, signatures);
const signatures = await Promise.all(accounts.validators.map((m) => ContractUtils.signMessage(m, purchaseMessage)));
const proposeMessage = ContractUtils.getPurchasesProposeMessage(0, purchaseParams, signatures, chainId);
const proposerSignature = ContractUtils.signMessage(accounts.validators[0], proposeMessage);
const tx1 = await contract
.connect(accounts.certifiers[4])
.savePurchase(0, purchaseParams, signatures, proposerSignature);
await tx1.wait();

console.log(`Store Sample Purchase 2 - 2/2...`);
Expand All @@ -1143,8 +1174,14 @@ async function storeSamplePurchase2(accounts: IAccount, deployment: Deployments)
};
});
const purchaseMessage3 = ContractUtils.getPurchasesMessage(0, purchaseParams3, chainId);
const signatures3 = accounts.validators.map((m) => ContractUtils.signMessage(m, purchaseMessage3));
const tx3 = await contract.connect(accounts.validators[4]).savePurchase(0, purchaseParams3, signatures3);
const signatures3 = await Promise.all(
accounts.validators.map((m) => ContractUtils.signMessage(m, purchaseMessage3))
);
const proposeMessage3 = ContractUtils.getPurchasesProposeMessage(0, purchaseParams3, signatures3, chainId);
const proposerSignature3 = await ContractUtils.signMessage(accounts.validators[0], proposeMessage3);
const tx3 = await contract
.connect(accounts.certifiers[4])
.savePurchase(0, purchaseParams3, signatures3, proposerSignature3);
await tx3.wait();
}

Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dms-contracts-v2",
"version": "2.1.1",
"version": "2.1.3",
"description": "Smart contracts that decentralized point systems",
"files": [
"**/*.sol"
Expand Down
Loading

0 comments on commit f33a041

Please sign in to comment.