Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
lidangzzz committed Jan 16, 2024
1 parent 63e695e commit b74e4a8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 77 deletions.
27 changes: 5 additions & 22 deletions darc-protocol/contracts/protocol/DARC.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@ contract DARC is Runtime, Dashboard {
function entrance(Program memory program) public payable returns (string memory) {
require(program.programOperatorAddress == msg.sender,
string.concat(string.concat("Invalid program address. Msg.sender: ", StringUtils.toAsciiString(msg.sender)), string.concat(", and program operator address: ", StringUtils.toAsciiString(program.programOperatorAddress))));
for (uint256 opIdx; opIdx < program.operations.length;) {
for (uint256 opIdx; opIdx < program.operations.length;opIdx++) {
require(program.operations[opIdx].operatorAddress == msg.sender,
string.concat(string.concat("Invalid program address. Msg.sender: ", StringUtils.toAsciiString(msg.sender)), string.concat(", and program operator address: ", StringUtils.toAsciiString(program.operations[opIdx].operatorAddress))));
unchecked {
++opIdx;
}
}
return runtimeEntrance(program);
}
Expand Down Expand Up @@ -58,24 +55,17 @@ contract DARC is Runtime, Dashboard {
if (currentMachineState.withdrawableCashMap[msg.sender] == 0) {
address[] memory newWithdrawableCashOwnerList = new address[](currentMachineState.withdrawableCashOwnerList.length);
uint256 pt;
for (uint256 index; index < currentMachineState.withdrawableCashOwnerList.length;) {
for (uint256 index; index < currentMachineState.withdrawableCashOwnerList.length;index++) {
if (currentMachineState.withdrawableCashOwnerList[index] != msg.sender) {
newWithdrawableCashOwnerList[pt] = currentMachineState.withdrawableCashOwnerList[index];
pt++;
}

unchecked {
++index;
}
}

// update the withdrawable cash owner list
currentMachineState.withdrawableCashOwnerList = new address[](pt);
for (uint256 index; index < pt;) {
for (uint256 index; index < pt;index++) {
currentMachineState.withdrawableCashOwnerList[index] = newWithdrawableCashOwnerList[index];
unchecked {
++index;
}
}
}

Expand Down Expand Up @@ -111,24 +101,17 @@ contract DARC is Runtime, Dashboard {
if (currentMachineState.withdrawableDividendMap[msg.sender] == 0) {
address[] memory newWithdrawableDividendsOwnerList = new address[](currentMachineState.withdrawableDividendOwnerList.length);
uint256 pt;
for (uint256 index; index < currentMachineState.withdrawableDividendOwnerList.length;) {
for (uint256 index; index < currentMachineState.withdrawableDividendOwnerList.length;index++) {
if (currentMachineState.withdrawableDividendOwnerList[index] != msg.sender) {
newWithdrawableDividendsOwnerList[pt] = currentMachineState.withdrawableDividendOwnerList[index];
pt++;
}

unchecked {
++index;
}
}

// update the withdrawable cash owner list
currentMachineState.withdrawableDividendOwnerList = new address[](pt);
for (uint256 index; index < pt;) {
for (uint256 index; index < pt;index++) {
currentMachineState.withdrawableDividendOwnerList[index] = newWithdrawableDividendsOwnerList[index];
unchecked {
++index;
}
}
}

Expand Down
6 changes: 1 addition & 5 deletions darc-protocol/contracts/protocol/Dashboard/Dashboard.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,10 @@ contract Dashboard is Executable {
*/
function getNumberOfTokenClasses() public view returns (uint256) {
uint256 i;
for (; i < currentMachineState.tokenList.length;) {
for (i=0; i < currentMachineState.tokenList.length; i++) {
if (!currentMachineState.tokenList[i].bIsInitialized) {
break;
}

unchecked {
++i;
}
}
return i;
}
Expand Down
60 changes: 10 additions & 50 deletions darc-protocol/contracts/protocol/TokenOwnerListManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,53 +52,37 @@ contract TokenOwnerListManager is MachineStateManager {
// 2. Check if the token owner list contains any address in the addOwnerList,
// if any address in the addOwnerList is not in the token owner list,
// and the balance of this address is not zero, then just add it to the toAdd list
for (uint256 index; index < addOwnerList.length;) {
for (uint256 index; index < addOwnerList.length;index++) {
if ((!tokenOwnerListContainsKeyAddress(bIsSandbox, tokenLevel, addOwnerList[index]))
&& (sandboxMachineState.tokenList[tokenLevel].tokenBalance[addOwnerList[index]] > 0)
) {
toAdd[toAddIndex] = addOwnerList[index];
toAddIndex++;
}

unchecked {
++index;
}
}

// 3. Check if the addresses in removeOwnerList are with zero balance,
// and if so, add them to the toRemove list
for (uint256 index; index < removeOwnerList.length;) {
for (uint256 index; index < removeOwnerList.length;index++) {
if (sandboxMachineState.tokenList[tokenLevel].tokenBalance[removeOwnerList[index]] == 0) {
toRemove[toRemoveIndex] = removeOwnerList[index];
toRemoveIndex++;
}

unchecked {
++index;
}
}

//4. construct the final list with all items from toRemove removed and all items from toAdd added
address[] memory finalList = new address[](sandboxMachineState.tokenList[tokenLevel].ownerList.length + toAddIndex);
uint256 pt;
for (uint256 index; index < sandboxMachineState.tokenList[tokenLevel].ownerList.length;) {
for (uint256 index; index < sandboxMachineState.tokenList[tokenLevel].ownerList.length;index++) {
if (!ArrayUtils.inArray(toRemove, sandboxMachineState.tokenList[tokenLevel].ownerList[index])) {
finalList[pt] = sandboxMachineState.tokenList[tokenLevel].ownerList[index];
pt++;
}

unchecked {
++index;
}
}

for (uint256 index; index < toAddIndex;) {
for (uint256 index; index < toAddIndex;index++) {
finalList[pt] = toAdd[index];
pt++;

unchecked {
++index;
}
}

// 5. Update the token owner list
Expand All @@ -115,53 +99,37 @@ contract TokenOwnerListManager is MachineStateManager {
// 2. Check if the token owner list contains any address in the addOwnerList,
// if any address in the addOwnerList is not in the token owner list,
// and the balance of this address is not zero, then just add it to the toAdd list
for (uint256 index; index < addOwnerList.length;) {
for (uint256 index; index < addOwnerList.length;index++) {
if ((!tokenOwnerListContainsKeyAddress(bIsSandbox, tokenLevel, addOwnerList[index]))
&& (currentMachineState.tokenList[tokenLevel].tokenBalance[addOwnerList[index]] > 0)
) {
toAdd[toAddIndex] = addOwnerList[index];
toAddIndex++;
}

unchecked {
++index;
}
}

// 3. Check if the addresses in removeOwnerList are with zero balance,
// and if so, add them to the toRemove list
for (uint256 index; index < removeOwnerList.length;) {
for (uint256 index; index < removeOwnerList.length;index++) {
if (currentMachineState.tokenList[tokenLevel].tokenBalance[removeOwnerList[index]] == 0) {
toRemove[toRemoveIndex] = removeOwnerList[index];
toRemoveIndex++;
}

unchecked {
++index;
}
}

//4. construct the final list with all items from toRemove removed and all items from toAdd added
address[] memory finalList = new address[](currentMachineState.tokenList[tokenLevel].ownerList.length + toAddIndex);
uint256 pt;
for (uint256 index; index < currentMachineState.tokenList[tokenLevel].ownerList.length;) {
for (uint256 index; index < currentMachineState.tokenList[tokenLevel].ownerList.length;index++) {
if (!ArrayUtils.inArray(toRemove, currentMachineState.tokenList[tokenLevel].ownerList[index])) {
finalList[pt] = currentMachineState.tokenList[tokenLevel].ownerList[index];
pt++;
}

unchecked {
++index;
}
}

for (uint256 index; index < toAddIndex;) {
for (uint256 index; index < toAddIndex;index++) {
finalList[pt] = toAdd[index];
pt++;

unchecked {
++index;
}
}

// 5. Update the token owner list
Expand All @@ -177,25 +145,17 @@ contract TokenOwnerListManager is MachineStateManager {
*/
function tokenOwnerListContainsKeyAddress(bool bIsSandbox, uint256 tokenLevel, address key) view internal returns (bool) {
if (bIsSandbox) {
for (uint256 index; index < sandboxMachineState.tokenList[tokenLevel].ownerList.length;) {
for (uint256 index; index < sandboxMachineState.tokenList[tokenLevel].ownerList.length;index++) {
if (sandboxMachineState.tokenList[tokenLevel].ownerList[index] == key) {
return true;
}

unchecked {
++index;
}
}
return false;
} else {
for (uint256 index; index < currentMachineState.tokenList[tokenLevel].ownerList.length;) {
for (uint256 index; index < currentMachineState.tokenList[tokenLevel].ownerList.length;index++) {
if (currentMachineState.tokenList[tokenLevel].ownerList[index] == key) {
return true;
}

unchecked {
++index;
}
}
return false;
}
Expand Down

0 comments on commit b74e4a8

Please sign in to comment.