Logika smartcontract - Rian010/Journal GitHub Wiki

Smart contracts di dunia blockchain menggunakan bahasa pemrograman spesifik seperti Solidity, Vyper, Bamboo, dan lain-lain. Namun, prinsip dasarnya sama dengan logika-logika umum yang telah sayauraikan sebelumnya. Berikut adalah daftar lengkap dan rinci dari logika atau pernyataan kontrol yang umum digunakan dalam smart contracts di dunia blockchain:

Pernyataan If Satu Kondisi:

if (kondisi) {
   // statement(s) to execute if the condition is true
}

Contoh:

mapping (address => bool) public isMember;
function join() public {
   require(!isMember[msg.sender], "You are already a member.");
   isMember[msg.sender] = true;
   // This code will only run if the user is not already a member
}

Pernyataan If-Else Satu Kondisi:

if (kondisi) {
   // statement(s) to execute if the condition is true
} else {
   // statement(s) to execute if the condition is false
}

Contoh:

mapping (address => uint) public balances;
function withdraw(uint amount) public {
   require(balances[msg.sender] >= amount, "Insufficient balance.");
   balances[msg.sender] -= amount;
   msg.sender.transfer(amount);
}
// The above function checks whether the sender has sufficient balance before transferring funds

Pernyataan If-Else If:

if (kondisi1) {
   // statement(s) to execute if condition1 is true
} else if (kondisi2) {
   // statement(s) to execute if condition1 is false and condition2 is true
} else {
   // statement(s) to execute if both conditions are false
}

Contoh:

enum Status {Created, Active, Closed}
Status public status;
function changeStatus(Status _status) public {
   require(status != _status, "The status cannot be changed to its current value.");
   status = _status;
}
// The above function changes the status of a contract only if it is different from the current status

Operator Pembanding:

  • ==: Equality (Kesamaan)
  • !=: Inequality (Ke curangan)
  • <: Less Than (Lebih kecil dari)
  • <=: Less Than Or Equal To (Lebih kecil atau sama dengan)
  • >: Greater Than (Lebih besar dari)
  • >=: Greater Than Or Equal To (Lebih besar atau sama dengan)

Contoh:

uint x = 10;
uint y = 20;
bool z;
z = (x == y);          // false
z = (x != y);          // true
z = (x < y);           // true
z = (x <= y);          // true
z = (x > y);           // false
z = (x >= y);          // false

Operator Logika:

  • &&: Logical And (Dan)
  • ||: Logical Or (Maupun)
  • !: Logical Not (Tidak)

Contoh:

uint x = 10;
uint y = 20;
bool z, w;
z = (x > 5 && y < 30);       // true
w = (x > 5 || y > 30);       // true
z = ! (x == y);             // true

Perulangan While:

while (condition) {
    // code to be executed
}

Contoh:

uint counter = 0;
while (counter < 5) {
   counter += 1;
}
// The above code increments the variable `counter` until it reaches 5

Perulangan For:

for (initialization; condition; increment/decrement) {
    // code to be executed
}

Contoh:

for (uint i = 0; i < 5; i++) {
   console.log("Counter:", i);
}
// The above code logs the values 0 through 4 to the console

Menghentikan Perulangan Dengan Break:

while (condition) {
    if (someCondition) {
        break;
    }
    // other code
}

Contoh:

uint[] numbers = new uint[](10);
for (uint i = 0; i < numbers.length; i++) {
   if (numbers[i] == 42) {
      break;
   }
}
// The above code breaks out of the loop when it finds the number 42

Membatalkan Iterasi Saat Ini Dan Melanjutkan Ke Iterasi Berikutnya Dengan Continue:

for (int i = 0; i < array.length; i++) {
    if (array[i] % 2 == 0) {
        continue;
    }
    // other code will only run for odd numbers
}

Contoh:

bytes32[] words = new bytes32[](5);
for (uint i = 0; i < words.length; i++) {
   if (bytes32(keccak256(abi.encodePacked(words[i]))) == bytes32(0)) {
      continue;
   }
   // The above code skips over any empty strings in the `words` array
}