Giter VIP home page Giter VIP logo

gamma-bots's People

Contributors

haythemsellami avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

rysk-finance

gamma-bots's Issues

Refactor Opportunity - Guard Clause

There're huge refactor opportunity to adopt the Guard Clause design pattern.

Simple idea is whenever you see there's an if block with no associate else block, you can refactor it a bit to only have a small if - and return syntax instead of a huge if () {}. I believe this is a great pattern to follow because as logics get more complicated, we could end up having like 20 if condition and our code will looks like this without this pattern:

if(a) {
 // something
    if(b) {
    // something 
      if (c) {
      // something 
        if (d) {
         // something
        }
      }
   }
}

This is also the pattern we're trying to adopt in the frontend. I think we can try it here as well:

The current code structure is like this.

if(hour == expiryHour) {


  if (expiryPrice[0].toString() == '0' && isLockingPeriodOver) {
      // round id for expiry timestamp
      

      // check if otoken price is not on-chain, and latest chainlink round timestamp is greater than otoken expiry timestamp and locking period over
      if (priceRoundTimestamp.toString() >= expiryTimestamp) {
          
          for (let j = priceRoundId.sub(1); j > 0; j = j.sub(1)) {
              previousRoundId = j;
              previousRoundTimestamp = await chainlinkAggregator.getTimestamp(j);

              if (previousRoundTimestamp.toString() != '0') {    
                  if (previousRoundTimestamp.toString() < expiryTimestamp.toString()) {
                      break;
                  }
                  else {
                      priceRoundId = previousRoundId;
                      priceRoundTimestamp = previousRoundTimestamp;
                  }
              } 
          }

          console.log('Found round id: ', priceRoundId.toString());
          console.log('Found round timestamp: ', priceRoundTimestamp.toString());

          let tx = await pricer.setExpiryPriceInOracle(expiryTimestamp, priceRoundId, {gasLimit: '1000000'});

          console.log('Tx hash: ', tx.hash);
      }
      else {
          console.log('Chainlink latest round timestamp is not grater than or equal the expiry timestamp ')
      }
}

It could be refactor to the following to avoid nested if block,

if(hour !== expiryHour) return

let expiryPrice = await oracle.getExpiryPrice(pricerAsset, expiryTimestamp);
let isLockingPeriodOver = await oracle.isLockingPeriodOver(pricerAsset, expiryTimestamp);


if (expiryPrice[0].toString() !== '0' || !isLockingPeriodOver) return

// round id for expiry timestamp
let priceRoundId = await chainlinkAggregator.latestRound();
let priceRoundTimestamp = await chainlinkAggregator.getTimestamp(priceRoundId);
// round id before price round id
let previousRoundId;
let previousRoundTimestamp;

// check if otoken price is not on-chain, and latest chainlink round timestamp is greater than otoken expiry timestamp and locking period over

if (priceRoundTimestamp.toString() < expiryTimestamp) {
  console.log('Chainlink latest round timestamp is not grater than or equal the expiry timestamp ')
  return
}


for (let j = priceRoundId.sub(1); j > 0; j = j.sub(1)) {
    previousRoundId = j;
    previousRoundTimestamp = await chainlinkAggregator.getTimestamp(j);

    if (previousRoudTimestamp.toString() === '0') continue

    if (previousRoundTimestamp.toString() < expiryTimestamp.toString()) {
        break;
    }
    else {
        priceRoundId = previousRoundId;
        priceRoundTimestamp = previousRoundTimestamp;
    }
    
}

console.log('Found round id: ', priceRoundId.toString());
console.log('Found round timestamp: ', priceRoundTimestamp.toString());

let tx = await pricer.setExpiryPriceInOracle(expiryTimestamp, priceRoundId, {gasLimit: '1000000'});

console.log('Tx hash: ', tx.hash);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.