Category: Blog

  • starter-kit

    Marketplace template

    This repository is an example of an application running on the Liteflow infrastructure to showcase the uses of the infrastructure and/or be used as a starter kit to launch a product fast.

    Features included

    The Marketplace template includes the following features:

    • Open collections (User-generated content)
      • Mint ERC-721
      • Mint ERC-1155
      • Creator’s royalties support
    • Homepage content customization
      • Featured NFTs
      • Featured collections
      • Featured users
      • Custom section with featured elements
    • Search system
      • Search NFTs
      • Search collections
      • Search users
    • Explore NFTs, collections and users with filtering and sorting systems
      • Explore NFTs
        • Filter by chain
        • Filter by status
        • Filter by price
        • Filter by collection
        • Filter by traits
        • Sort by dates
      • Explore collections
        • Filter by chain
        • Sort by volumes
      • Explore users
    • Native currency and ERC20 support
    • Lazymint for NFTs
    • List NFTs on sale
      • Partial filling
      • Fixed price listing
    • Make offers on NFTs
      • Partial filling
      • Open offer
    • Purchase NFTs
    • User profile
      • Account
        • Profile edition
        • Wallet page
      • NFTs “on sale”
      • NFTs “owned”
      • NFTs “created”
      • Offers (“bids”) management
      • Trades history
      • Listings (“offers”) management
    • User verification system (verified status)
    • Report system
      • Report a user
      • Report an NFT
    • Notifications
      • In-app notifications
      • Email notifications
    • NFT details
      • Chain information
      • Explorer link
      • Media IPFS link
      • Metadata IPFS link
      • Traits with percentages
    • NFT history (activity)
      • Lazyminted
      • Minted
      • Listed
      • Purchased
      • Transferred
    • Wallet connection with
      • Metamask
      • Coinbase
      • WalletConnect
      • Magic
      • Rainbow
    • Multi-chain support
    • Multi currency support
    • Advanced fee customization support

    It also includes compatibility with the following features:

    • Multi-language compatibility
    • Credit card payment gateway compatibility
    • Email connection compatibility
    • Wallet-to-wallet messaging system

    Libraries used

    The Marketplace template is based on the following tech stack

    Get started

    To get started, start by configuring your environmental variables in the .env file, referring to the .env.example file as a guide.
    For further customization, an optional option is to utilize the environment.ts file.

    Once done you can install all the dependencies with npm i then run

    npm run dev
    

    Your application is now accessible at http://localhost:3000

    Customization

    Theme

    The theme is based on ChakraUI and can be customized in the /styles/theme.ts file.

    Application

    Your application includes default navigation, metadata, and wallets that can be updated directly from the pages/_app.tsx file.

    Build and publish Docker image

    Make sure to have the envs in the file .env.production.

    docker build -t IMAGE_TAG --platform linux/amd64 --push .

    Visit original content creator repository

  • mobs_animal

    ANIMAL MOBS

    WARNING: This is a modified version specially made for the PanqKart Minetest game.
    The original version can be found here: https://notabug.org/TenPlus1/mobs_animal

    All modified code falls under the mod’s same license.

    Bee

    Tends to buzz around flowers and gives honey when killed, you can also right-click a bee to pick it up and place in inventory. 3x bee’s in a row can craft a beehive.


    Bunny

    Bunnies appear in green grass areas (prairie biome in ethereal) and can be tamed with 4 carrots or grass. Can also be picked up and placed in inventory and gives 1 raw rabbit and 1 rabbit hide when killed.


    Chicken

    Found in green areas (bamboo biome in ethereal) and lays eggs on flat ground, Can be picked up and placed in inventory and gives 1-2 raw chicken when killed. Feed 8x wheat seed to breed.


    Cow

    Wanders around eating grass/wheat and can be right-clicked with empty bucket to get milk. Cows will defend themselves when hit and can be right-clicked with 8x wheat to tame and breed.


    Kitten

    Found on green grass these cute cats walk around and can be picked up and placed in inventory as pets or right-clicked with 4x live rats or raw fish (found in ethereal) and tamed. They can sometimes leave you little gifts of a hairball.


    Rat

    Typically found around stone they can be picked up and cooked for eating.


    Sheep

    Green grass and wheat munchers that can be clipped using shears to give 1-3 wool. Feed sheep 8x wheat to regrow wool, tame and breed. Right-click a tamed sheep with dye to change it’s colour. Will drop 1-3 raw mutton when killed.


    Warthog

    Warthogs unlike pigs defend themselves when hit and give 1-3 raw pork when killed, they can also be right-clicked with 8x apples to tame or breed.


    Penguin

    These little guys can be found in glacier biomes on top of snow and have the ability to swim if they fall into water.


    Panda

    These monochrome cuties spawn in Ethereal’s bamboo biome and can be tamed with bamboo stalks 🙂 Remember they have claws though.


    Note: After breeding, animals need to rest for 4 minutes and baby animals take 4 minutes to grow up, also feeding them helps them grow quicker…

    Lucky Blocks: 20

    Visit original content creator repository

  • 18650

    18650

    This repository contains my research on what Li-ion batteries are, how they work and how i created a battery managment system for these cells must have the following components in-order to charge/discharge the li-ion battery saftely:

    • Overcharge
    • Over-discharge
    • Discharge too quickly

    Warning

    li-ion batteries are highly dangerous. They can catch fire or explode if mishandled. Proceed with caution at your own risk.

    Content

    What is 18650

    Its a secret🤫, do not tell anyone. They are lithium ion batteries.

    • Lithium ion battery is an advanced battery technology that uses lithium ions as key component of its electrochemical reaction.
    • I will discuss about the process in detail in the respective topic guide.
    • Right now just remember that the lithium ions move from the negative electrode through an electrolyte to the positive electrode during discharge, and back when charging.

    Advantages of li-ion Battery

    • Lithium ion batteries are one of the highest energy dense batteries in the world today.
      • They can store up to 100-265 Wh/kg or 250-670 Wh/L.
    • In addition to this they can celiver upto 3.6V.
      • This is 3 times higher then the Ni-Cd or Ni-Mh battery technologies.
      • They can deliver large amounts of current for higher-power applications.
    • Lithum Ion batteries require low maintenance
    • They do not require scheduled cycling to maintain thier battery life.
    • Li-ion batteries have no memory effect.
      • Memory effect is a detrimental process where repeated partial discharge/charge cycles can cause a battery to “remember” a lower capacity.
    • Li-ion batteries have a low self-discharge rate of 1.5-2 % per month.
    • They are easily disposable because they are not made of toxic materials like cadmium.

    Disadvantages of li-ionn Battery

    Development

    The 18650 cells are Lithium-ion battery.
    Lithium-ion battery is a type of rechargeable battery.

    The first prototype of the Li-ion battery was developed by Akira Yoshino. in 1985, based on the research by John Goodenough, M.Stanley Whittingham, Rachid Yazami and koichi Mizushima during 1970-1980s.

    The first commercial batteries were developed by sony and Asahi Kasei team led by Yoshio Nishi in 1991.
    For more information click here.

    Uses

    Due to the advantages the li-ion batteries have displace the Ni-Cd batteries in portable electronic devices like laptops and smart phones.

    These batteries are also help in millitary and aerospace applications.

    Explore my GitHub repo’s for more content like this.

    Reference

    Clean Energy Institute – Lithium-ion Battery.

    Visit original content creator repository

  • whatsapp-clone

    whatsapp-clone

    Whatsapp clone demo

    Warning: although this project has some basics security such as hashed user password, encrypt messages and so on, this project is NOT secure enough in production. (Contribute to this project by making a pull request will be appreciated)

    How to run:

    • Clone this repository or fork it.

      • To clone this repository type git clone https://github.com/kimlimjustin/whatsapp-clone.git on your command line
      • To fork this repository, click fork button of this repository then type git clone https://github.com/<your username>/whatsapp-clone.git
    • Inside server folder, create a new file named .env which stores informations about server side such as ATLAS_URI, SECURITY_KEY and CLIENT_URL informations

      • store your database URI inside ATLAS_URI variable
      • store your security key inside SECURITY_KEY variable
      • store your client url inside CLIENT_URL variable
      • example:

      ATLAS_URI =mongodb+srv://admin:<password>@cluster0.8aezk.gcp.mongodb.net/whatsappClone?retryWrites=true&w=majority
      SECURITY_KEY = D73373D9B4ED6FEC5B8B2DAF6WA929B1C7D14CDC88B196EBDCCEA77AFF7BB9
      CLIENT_URL = http://localhost:3000/
      
    • Inside client folder, create a new file called .env which stores your information about client side such as REACT_APP_SECURITY_KEY and REACT_APP_BACKEND_URL informations

      • store your security key inside REACT_APP_SECURITY_KEY variable, note that this value must same as SECURITY_KEY in server/.env file
      • store your server url inside REACT_APP_BACKEND_URL
      • example:

      REACT_APP_SECURITY_KEY = D73373D9B4ED6FEC5B8B2DAF6WA929B1C7D14CDC88B196EBDCCEA77AFF7BB9
      REACT_APP_BACKEND_URL = http://localhost:5000
      
    • Install all dependencies

      • Client side: on the client directory type npm install(or yarn)
      • Server side: on the server directory type npm install(or yarn)
    • Run it on node js:

      • Client side: on the client directory type npm start (or yarn)
      • Server side: on the server directory type npm start (or yarn)

    Like my work?

    Buy Me a Coffee at ko-fi.com

    Visit original content creator repository

  • 30-seconds-of-ruby

    30 Seconds of Ruby

    RubyLogo

    Ruby snippets you can understand in 30 seconds or less.

    Inspired by 30-seconds-of-knowledge.

    [Element: hr]

    Snippets

    Ruby’s Ancestor Chain

    Always wondered how to see Ruby’s class hierarchy? Simply call ancestors on aclass to see the ancestor chain.

    The return array contains the following order:

    • the calling class
    • its included modules
    • its parent class
    • the included modules of its parents class
    • the parent class of its parent class

    Here is the ancestor chain of the String class:

    irb> String.ancestors
    => [String, Comparable, Object, Kernel, BasicObject]

    Additional links

    ⬆ Back to top

    What is a Ruby Interpreter?

    A Ruby interpreter is any program that parses the Ruby source code to imitate its behavioral execution. It converts the program line by line compared to a compiler which converts the whole program at once. Hence, a code change only requires an edit. Interpreters only throw an error if the program gets executed and reach the line that containes the error, whereas compilers will throw the error already before execution.
    There are multiple versions of Ruby interpreters to choose from, each with a different but very similar set of rules and options.

    Common Ruby Interpreters:

    • Ruby MRI
    • JRuby
    • YARV
    • Rubinius

    ⬆ Back to top

    Ruby’s .methods method

    If you’ve ever wondering what methods are available for a specific object, just call .methods on it:

    irb> name = 'Bob'
     => "Bob"
    irb> name.methods
     => [:include?, :%, :unicode_normalize, :*, :+, :to_c, ...]

    This will return an array of symbols (names) of all publicly accessible methods of the object and its ancestors.

    ⬆ Back to top

    BasicObject

    The BasicObject class is the top parent of all class. It is a blank instance of class class with no superclass and contains a minimum of methods for object creation and comparison.

    irb> class User
    irb> end
     => nil
    irb> User
     => User
    irb> User.class
     => Class
    irb> User.superclass
     => Object
    irb> Object.ancestors
     => [Object, Kernel, BasicObject]
    irb> BasicObject.class
     => Class
    irb> BasicObject.superclass
     => nil
    irb> BasicObject.ancestors
     => [BasicObject]

    Additional links

    ⬆ Back to top

    Hash#dig

    Ever wondered if there is a better way how you can traverse through multi-layer hashes or arrays to retrieve a specific value? Luckily Ruby 2.3 introduced a new method called dig.

      catalogue = {
        drinks: {
          apple_juice: {
            stock: 8,
            price_per_unit: 2.4
          },
          orange_juice: {
            stock: 4,
            price_per_unit: 2.2
          }
        }
      }

    Given the above hash, we would access the price per unit of the orange juice by the below line.

      catalogue[:drinks][:carrot_juice][:price_per_unit]
      => NoMethodError: undefined method `[]' for nil:NilClass

    To mitigate Rails to throw an error we need to check the existence of each key while navigating through the hash structure.

      catalogue[:drinks] && catalogue[:drinks][:carrot_juice] && catalogue[:drinks][:carrot_juice][:price_per_unit]

    By using dig we follow DRY and retrieve the value of each key object by dealing smoothly with nil values.

      catalogue.dig(:drinks, :carrot_juice, :price_per_unit)
      => nil
      catalogue.dig(:drinks, :apple_juice, :price_per_unit)
      => 2.4

    Additional links

    ⬆ Back to top

    Method Naming Conventions “!” vs. “?”

    In Ruby there are two common naming conventions for methods. Either you end a method name with a ! or ?.
    Methods ended by a ! are called bang methods and often modifies the original object.

    irb> book_title = "example book title"
     => "example book title"
    irb> puts book_title.capitalize!
    Example book title
     => nil
    irb> puts book_title
    Example book title
     => nil
    irb>

    A famous example is merge vs merge! when dealing with params, whereas the former method will return the params and the latest modifies the original params.

    The question mark at the end of a method name indicates that we can expect a boolean as return value.

    irb> array = []
     => []
    irb> array.empty?
     => true
    irb> user = { 'first_name' => 'Bob', 'last_name' => 'Carlton' }
    irb> user.has_key?('first_name')
     => true

    Additional links

    ⬆ Back to top

    Kernel (module)

    The Kernel module is included by class Object and provides methods that are globally available, hence every Ruby object has access to them. These methods are called without a receiver and only act on the passed arguments.

    Famouse representative of the Kernel module are:

    • Array
    • Hash
    • Integer
    • Hash
    • p, print and puts

    Additional links

    ⬆ Back to top

    Where do methods live?

    Ever wondered how you can find out where exactly a method lives inside Ruby?

    Ruby offers two helpful methods here. By passing the name of the method as a symbol to owner will reveal where a method lives inside Ruby. If you curious about who receives s specific method, just pass the name again as a symbol to receiver.

    irb> method(:initialize).owner
     => BasicObject
    irb> method(:initialize).receiver
     => main

    Additional links

    ⬆ Back to top

    Ruby -e

    The ruby command is mostly known for running a ruby app or script. But it also has a number of interesting options to offer.

    Execute a single line code snippet just by using the -e flag:

      ruby -e '[1, 2, 3].each do |e| puts e end'

    ⬆ Back to top

    Integer#digits(base)

    Calling digits on an integer returns the array including the digits extracted by place-value notation with radix base of int.

    irb> 123.digits
     => [3, 2, 1]
    irb> 14.digits(7)
     => [0, 2]

    Additional links

    ⬆ Back to top

    values_at

    Using values_atto retrieve multiple non-sequential values of arrays and hashes.

    For a given array it will return an array of the values associated with the index position.

      irb> programming_languages = ['Ruby', 'Python', 'Golang', 'C++', 'C', 'D']
       => ["Ruby", "Python", "Golang", "C++", "C", "D"]
      irb> programming_languages.values_at(0, 2, 4)
       => ["Ruby", "Golang", "C"]

    For a given hash it will return an array of the values associated with the given keys.

      irb> fruits = { 'orange' => '$2.00', 'apple' => '$3.00', 'grapes' => '$2.50' }
       => => {"orange"=>"$2.00", "apple"=>"$3.00", "grapes"=>"$2.50"}
      irb> fruits.values_at('orange')
       => ["$2.00"]

    Additional links

    ⬆ Back to top

    Splat operators

    The *splat operator has almost endless uses. But the main idea is that whenever you don’t want to specify the number of arguments you have, you would use a splat operator. The simplest example would be something like this:

    irb> def foo(*args)
    irb>  args
    irb> end
     => :foo
    irb> foo(1,5,9)
    [1, 5, 9]
     => [1, 5, 9]

    You can also use the splat operator to grab any segment of an array:

    irb> first, second, *last = ["x", "y", "w", "z"]
     => ["x", "y", "w", "z"]
    irb> first
     => "x"
    irb> second
     => "y"
    irb> last
     => ["w", "z"]

    Additional links

    ⬆ Back to top

    to_s vs. to_str

    to_s is an explicit casting helper and is used to transform a value from one type to another. to_str is an implicit casting helper
    with the same purpose. The difference is that the former will always return a string whereas the later will result in an error or data loss
    if the value it acts on does not act like the type we are casting. Ruby will only return a value when objects act like the type.
    Ruby is very strict when using an implicit casting helper to ensure that the value acts like the type we want.

    irb> [1, 2].to_s
     => "[1, 2]"
    irb> [1, 2].to_str
    NoMethodError: undefined method `to_str` for [1, 2]:Array
    Did you mean? to_s
    irb> :name.to_s
     => "name"
    irb> :name.to_str
    NoMethodError: undefined method `to_str` for :name:Symbol
    Did you mean? to_s

    ⬆ Back to top

    to_i vs. to_int

    to_i is an explicit casting helper and is used to transform a value from one type to another. to_int is an implicit casting helper
    with the same purpose. The difference is that the former will always return a string whereas the later will result in an error or data loss
    if the value it acts on does not act like the type we are casting. Ruby will only return a value when objects act like the type.
    Ruby is very strict when using an implicit casting helper to ensure that the value acts like the type we want.

    irb> 19.99.to_i
     => 19
    irb> 19.99.to_int
     => 19
    irb> '19.99'.to_i
     => 19
    irb> '19.99'.to_int
    NoMethodError: undefined method `to_int` for "String":String
    Did you mean?  to_i
                   to_str

    ⬆ Back to top

    lamba vs. proc

    A lambda is a way to define a block & its parameters with some special syntax. You can save this lambda into a variable for later use.

    The syntax for defining a Ruby lambda looks like this:

    irb > say_something = -> { puts "This is a lambda" }
     => #<Proc:0x0000556829fa2fa8@(lambda)>

    Defining a lambda won’t run the code inside it, just like defining a method won’t run the method, you need to use the call method for that.

    irb > say_something = -> { puts "This is a lambda" }
     => #<Proc:0x0000556829fa2fa8@(lambda)>
    irb > say_something.call
     This is a lambda
     => nil

    Procs are a very similar concept. But one of the differences is how you create them.

    irb > my_proc = Proc.new { |x| puts x }
     => #<Proc:0x0000556829f8ad68@>

    There is no dedicated Lambda class. A lambda is just a special Proc object. If you take a look at the instance methods from Proc, you will notice there is a lambda? method.

    irb > my_proc = Proc.new { |x| puts x }
     => #<Proc:0x0000556829f8ad68@>
    irb > my_proc.lambda?
     => false
    irb > my_lambda = -> { puts "This is a lambda" }
     => #<Proc:0x0000556829f59a60@(lambda)>
    irb > my_lambda.lambda?
     => true

    A proc behaves differently than a lambda, specially when it comes to arguments. Procs don’t care about the correct number of arguments, while lambdas will raise an exception.

    irb > my_proc = Proc.new { |x,y| puts "I do not care about arguments!" }
     => #<Proc:0x0000556829f8ad68@>
    irb > my_proc.call
     I do not care about arguments!
     => nil
    irb > my_lambda = -> x { "x = #{x}" }
     => #<Proc:0x000056145b4325e0@(lambda)>
    irb > my_lambda.call
     ArgumentError (wrong number of arguments (given 0, expected 1))
    irb > my_lambda.call(10)
     => "x = 10"

    Additional links

    ⬆ Back to top

    to_a vs. to_ary

    to_a is an explicit casting helper and is used to transform a value from one type to another. to_ary is an implicit casting helper
    with the same purpose. The difference is that the former will always return a string whereas the later will result in an error or data loss
    if the value it acts on does not act like the type we are casting. Ruby will only return a value when objects act like the type.
    Ruby is very strict when using an implicit casting helper to ensure that the value acts like the type we want.

    irb > { key: :value }.to_a
     => [[:key, :value]]
    irb > { key: :value }.to_ary
    NoMethodError: undefined method `to_ary` for {:key=>:value}:Hash
    Did you mean?  to_a

    ⬆ Back to top

    map vs. each

    The way the map method works in Ruby is, it takes an enumerable object, (i.e. the object you call it on), and a block.

    Then, for each of the elements in the enumerable, it executes the block, passing it the current element as an argument. The result of evaluating the block is then used to construct the resulting array.

    In other words:

    Applying map on an array returns a new array where each element is the result of evaluating the block with the element as an argument.

    irb> a = [1, 2, 3]
     => [1, 2, 3]
    irb> b = a.map { |n| n * 2 }
     => [2, 4, 6]
    irb> b
     => [2, 4, 6]

    each is just another method on an object. That means that if you want to iterate over an array with each, you’re calling the each method on that array object.

    It takes a list as it’s first argument and a block as the second argument. For every element in the list, it runs the block passing it the current element as a parameter.

    You should use each when you want iteration but don’t care about what it returns.

    [1, 2, 3].each { |n| puts “Current number is: #{n}” }

    irb> [1, 2, 3].each { |n| puts "Current number is: #{n}" }
     Current number is: 1
     Current number is: 2
     Current number is: 3
     => [1, 2, 3]
    irb> a = [1, 2, 3]
     => [1, 2, 3]
    irb> b = a.each { |n| n * 2 }
     => [1, 2, 3]
    irb> b
     => [1, 2, 3]

    ⬆ Back to top

    Enumerable#cycle

    Ruby’s Enumerable#cycle offers an easy way to either repeat a certain pattern n times or just to switch between two predefined states.

    Repeating a certain pattern:

    irb> array = [1, 2, 3]
     => [1, 2, 3]
    irb> array.cycle(3).to_a
     => [1, 2, 3, 1, 2, 3, 1, 2, 3]

    Switching between two states:

    irb> button = ['on', 'off'].cycle
     => #<Enumerator: ["on", "off"]:cycle>
    irb> button.next
     => "on"
    irb> button.next
     => "off"

    Additional links

    ⬆ Back to top

    Enumerable#all?

    Passes each element of the collection to the given block. The method returns true if the block never returns false or nil. If the block is not given, Ruby adds an implicit block of { |obj| obj } which will cause all? to return true when none of the collection members are false or nil.

    irb> a = [1, 2, 3]
     => [1, 2, 3]
    irb> a.all?(&:integer?)
     => true
    irb> a.all?(&:odd?)
     => false
    irb> a.all?
     => true
    irb> a = [1, 2, nil]
     => [1, 2, nil]
    a.all?
     => false

    Additional links

    ⬆ Back to top

    Enumerable#none?

    Passes each element of the collection to the given block. The method returns true if the block never returns true for all elements. If the block is not given, none? will return true only if none of the collection members is true.

    irb> a = []
     => []
    irb> a.none?
     => true
    irb> a = ['a', 'bb', 'c']
     => ["a", "bb", "c"]
    irb> a.none? { |e| e.length > 1 }
     => false
    irb> (1..10).none?(&:nil?)
     => true

    Additional links

    ⬆ Back to top

    Enumerable#any?

    Passes each element of the collection to the given block. The method returns true if the block ever returns a value other than false or nil. If the block is not given, Ruby adds an implicit block of { |obj| obj } that will cause any? to return true if at least one of the collection members is not false or nil.

    irb> a = []
     => []
    irb> a.any?
     => false
    irb> a = [1, 'a']
     => [1, "a"]
    irb> a.any? { |e| e.class == String }
     => true
    irb> h = { a: 1, b: 2 }
     => {:a=>1, :b=>2}
    irb> h.any? { |k, v| v.odd? }
     => true

    ⬆ Back to top

    Enumerable#one?

    Passes each element of the collection to the given block. The method returns true if the block returns true exactly once. If the block is not given, one? will return true only if exactly one of the collection members is true.

    irb> a = [500]
     => [500]
    irb> a.one?
     => true
    irb> a.one? { |e| e < 100 }
     => false
    irb> (1..4).one? { |n| n % 2 == 0 }
     => false

    Additional links

    ⬆ Back to top

    Array#count

    Ever wondered how much power Ruby’s count method has? Besides counting the elements of an array, it can do pretty awesome things. So lets’ start with the most common use case counting the elements of an array.

    irb> numbers = [1,2,5,3,6,2,5,3]
     => [1, 2, 5, 3, 6, 2, 5, 3]
    irb> numbers.count
     => 8

    Let’s say you want to count how often the number 3 is represented in the above array. The thought would be to use a loop an iterate over the array and increment a counter every time you see the number 3. But there is a better way. Just pass the object you looking for to the count method.

    irb> numbers.count(3)
     => 2

    Finally, you can also pass a block to do more complicated counts.

    irb> numbers.count(&:even?)
     => 3

    Additional links

    ⬆ Back to top

    clone vs dup

    Both methods are used to produce a shallow copy of obj—the instance variables of obj are copied, but not the objects they reference. clone copies the frozen and tainted state of obj whereas dup copies the tainted state of obj.

    In general, clone and dup may have different semantics in the descendant classes. While clone is used to duplicate an object, including its internal state, dup typically uses the class of the descendant object to create the new instance. When using dup, any modules that the object has been extended with will not be copied.

    irb> array = ['a','b','c']
     => ["a", "b", "c"]
    irb> array.freeze
     => ["a", "b", "c"]
    irb> array_clone = array.clone
     => ["a", "b", "c"]
    irb> a_clone << "d"
    RuntimeError: cant modify frozen Array
    irb> array_dup = array.dup
     => ["a", "b", "c"]
    irb> a_dup << "d"
     => ["a", "b", "c", "d"]

    Additional links

    ⬆ Back to top

    Hash#fetch

    Returns a value from the hash for the given key. If the key can’t be found, there are several options:

    • will raise a KeyError exception when no arguments given
    • if a default is given, then that will be returned
    • if the optional code block is specified, then that will be run and its result returned.

    irb> user = {'first_name': 'Dummy', 'last_name': 'User'}
     => {:first_name=>"Dummy", :last_name=>"User"}
    irb> user.fetch(:first_name)
     => "Dummy"
    irb> user.fetch(:email)
    KeyError: key not found: :email
      from (irb):5:in `fetch'
    irb> user.fetch(:email, 'no email added')
     => "no email added"
    irb> user.fetch(:email){ |e| "no #{e} added"}
     => "no email added"

    Additional links

    ⬆ Back to top

    Array#zip

    Converts any arguments to arrays, then merges elements of self with corresponding elements from each argument.

    This generates a sequence of ary.size n-element arrays, where n is one more than the count of arguments.

    If the size of any argument is less than the size of the initial array, nil values are supplied.

    If a block is given, it is invoked for each output array, otherwise an array of arrays is returned.

    irb> first_names = ['George', 'Marcus', 'Brian']
     => ["George", "Marcus", "Brian"]
    irb> last_names = ['Massy', 'Windmil']
     => ["Massy", "Windmil"]
    irb> first_names.zip(last_names)
     => [["George", "Massy"], ["Marcus", "Windmil"], ["Brian", nil]]

    Additional links

    ⬆ Back to top

    Adding uniq values to an array

    Let’s say we wanna add a new element to an array but we want to make sure to keep each element uniq. One way to achive this would be:

    irb> alphabet = ['a','b','c']
     => ["a", "b", "c"]
    irb> alphabet << 'c'
     => ["a", "b", "c", "c"]
    irb> alphabet.uniq
     => ["a", "b", "c"]

    Luckily Ruby offers Bitwise OR operator:

    x |= y
    is shorthand for:
    x = x | y

    So our example from above would become:

    irb> alphabet = ['a','b','c']
     => ["a", "b", "c"]
    irb> alphabet | ['c']
     => ["a", "b", "c"]

    We can also assign more than one element at the same time:

    irb> alphabet = ['a','b','c']
     => ["a", "b", "c"]
    irb> alphabet | ['c','d']
     => ["a", "b", "c", "d"]

    ⬆ Back to top

    Ruby’s Benchmark

    The Benchmark module provides methods to measure and report the time used to execute Ruby code. This report shows the user CPU time, system CPU time, the sum of the user and system CPU times, and the elapsed real time. The unit of time is seconds. Consider the following example:

    require 'benchmark'
    N = 1_000_000
    puts RUBY_DESCRIPTION
    Benchmark.bm(15, "concat/append") do |bm|
      concat_report = bm.report("concat") { N.times { 'Hello' +  ' ' +  'World' } }
      append_report = bm.report("append") { N.times { 'Hello' <<  ' ' <<  'World' } }
      [concat_report / append_report]
    end

    ~/: ruby concat_append_benchmark.rb
    ruby 2.4.4p296 (2018-03-28 revision 63013) [x86_64-darwin17]
                          user     system      total        real
    concat            0.290000   0.000000   0.290000 (  0.608981)
    append            0.260000   0.000000   0.260000 (  0.362523)
    rescue/condition  1.115385        NaN        NaN (  1.679841)

    Additional links

    ⬆ Back to top

    Ruby’s logger class

    The Ruby Logger class gives you a way to generate logs with a default output format & different levels of importance.

      logger = Logger.new("my_log_file.txt")

    For logging to the terminal just pass STDOUT.

      logger = Logger.new(STDOUT)

    Logger levels are:

    • UNKNOWN: An unknown message that should always be logged.

    • FATAL: An unhandleable error that results in a program crash.

    • ERROR: A handleable error condition.

    • WARN: A warning.

    • INFO: Generic (useful) information about system operation.

    • DEBUG: Low-level information for developers.

    Default log format is:

    SeverityID, [DateTime pid] SeverityLabel -- ProgName: message

    For instance:

    I, [1999-03-03T02:34:24.895701 19074]  INFO -- Main: info.

    Additional links

    ⬆ Back to top

    to_h vs. to_hash

    to_h is an explicit casting helper and is used to transform a value from one type to another. to_hash is an implicit casting helper
    with the same purpose. The difference is that the former will always return a string whereas the later will result in an error or data loss
    if the value it acts on does not act like the type we are casting. Ruby will only return a value when objects act like the type.
    Ruby is very strict when using an implicit casting helper to ensure that the value acts like the type we want.

    irb > rue.to_s
     => "true"
    irb > true.to_str
    => NoMethodError: undefined method `to_str' for true:TrueClass
    Did you mean?  to_s

    ⬆ Back to top

    Ruby from CMD (Command Line)

    Ever wondered how you can use ruby code in the command line?

    Running a ruby file from CMD:upcase

    ~/: ruby myprogram.rb

    You can pass in code as a command-line argument to the ruby command…

    ~/: ruby -e "puts 'Hello World!'"
    ~/: Hello World!

    and you have accss to the ARGV array to process additional passed arguments.

    ~/: ruby -e 'puts ARGV.inspect' 1 2 3 4
    ["1", "2", "3", "4"]

    It is also possible to use the pipe command to execute ruby code.

    ~/: echo "puts 'Hello World!'" | ruby
    ~/: Hello World!

    ⬆ Back to top

    Append only uniq elements to array

    You an call .uniq on the array after appending all elements. This will remove all elemnts that occure more than once in the array.

    irb> array = []
    => []
    irb> array << 1
    => [1]
    irb> array << 2
    => [1, 2]
    irb> array << 1
    => [1, 2, 1]
    irb> array.uniq
    => [1, 2]

    An ohter way is to only append an element if the array does not contain it already by using the ‘|’.

    irb> array = []
    => []
    irb> array << 1
    => [1]
    irb> array << 2
    => [1, 2]
    irb> array | [1]
    => [1, 2]
    irb> array
    => [1, 2]

    ⬆ Back to top

    Visit original content creator repository

  • mflowd

    Metrics Flow Daemon Build Status Go Report

    It is a small daemon that aggregates custom monitoring metrics collected from Google Dataflow workers that use metrics-flow library and exposes them to Prometheus.

    How it works

    Google Dataflow workers with metrics-flow plugged in pre-aggregate custom monitoring metrics using pipeline windowing functions and dump the results into a Google Pub/Sub topic. The metrics flow daemon polls a subscription to the topic, converts received metric update events to Prometheus format and exposes them through /metrics endpoint.

    +------------+                                                      
    | Dataflow 1 +----+                                                 
    +------------+    |                                                 
                      |                                                 
    +------------+    |                       +------------+            
    | Dataflow i |----|->(Google Pub/Sub)---->|   mflowd   |            
    +------------+    |                       +------------+            
                      |                              ^                  
    +------------+    |                              |                  
    | Dataflow N |----+                       +-------------+           
    +------------+                            | Prometheus  |           
                                              +-------------+           
    

    Installation

    % go get github.com/QubitProducts/mflowd
    

    Build it from scratch

    1. Make sure you have dep installed (if you don’t know how to install it, follow this link)
    2. make bootstrap
    3. make test
    4. make mflowd

    Running

    1. Create a pub/sub topic you will use for publishing metrics from your Dataflow workers (if you don’t have one already).

    2. Create a pull subscription to the topic

    3. Make sure you are authorized to use the subscription (if not sure, use gcloud auth login)

    4. Run the daemon

      % ./mflowd [-v] -p <port> -s pubsub <subscription_id>
      

    Where

    • port is a port where /metrics endpoint will be exposed
    • subscription_path is a subscription identifier which usually looks like projects/<project_name>/subscriptions/<subcsciption_name>
    • use optional -v flag to run the daemon in verbose mode

    Using Docker

    You can easily build a “containerized” version of mflowd and run it on mesos or kubernetes.

    Building

    % make docker
    % docker images | grep mflowd
    mflowd                                             latest                                            e9cbac93f703
    ...
    

    Running

    Before you can run the image you need to set up a Google Cloud API service account to allow mflowd use the subscription you have created. So

    1. Create a service account for mflowd

    2. Create an empty directory on your host machine (say, % mkdir ~/.mflowd)

    3. Download the service account key in JSON format and put it to the created directory

    4. Finally run the countainer:

      % docker run -e "MFLOWD_SUB=<subscription_id>" -v $HOME/.mflowd:/etc/mflowd 'mflowd:latest'
      

    Using docker-compose

    You can also run both mflowd and prometheus docker images using docker-compose:

    % cd ~/go/src/github.com/QubitProducts/mflowd
    % mkdir gcp
    # download your service account JSON key to gcp directory
    % cat > .env
    MFLOWD_SUB=<subscription_id>
    MFLOWD_VERBOSE=0 # set to 1 to turn verbose mode on
    ^C
    % docker-compose up
    

    Follow http://localhost:9090 to get to Prometheus UI

    Visit original content creator repository
  • NFT_Staking_app

    StakeSphere

    Overview

    StakeSphere is an NFT Staking Application that allows users to connect their wallets, claim an NFT (if they don’t have one), stake their NFTs for rewards, and withdraw staked NFTs. The application also provides staking rewards for staked NFTs, incentivizing users to participate actively. The app ensures secure blockchain interactions through a streamlined process involving two transactions for approval and staking.

    Dashboard


    Features

    • Wallet Connection: Users can connect their wallets to interact with the application.
    • NFT Claiming: Users who do not own any NFTs can claim one to begin staking.
    • NFT Staking: Stake NFTs securely to earn rewards.
    • NFT Withdrawal: Withdraw any NFT that has been staked at any time.
    • Staking Rewards: Users earn rewards for the duration their NFTs are staked.
    • Transaction Flow: Two transactions are performed during staking:
      1. Approval of the NFT for staking.
      2. Staking of the NFT.

    WalletIntegration WalletIntegration WalletIntegration

    Tech Stack

    • Frontend: Next.js for server-rendered React applications.
    • Styling: TailwindCSS for a responsive and modern UI.
    • Blockchain SDK: Thirdweb for NFT and blockchain interaction.
    • Language: TypeScript for type-safe development.

    Dashboard Dashboard

    Installation and Setup

    Prerequisites

    • Node.js and npm installed on your machine.
    • Wallet provider (e.g., MetaMask) installed and configured.
    • Access to a blockchain network supporting NFTs (e.g., Ethereum or Polygon).

    Transaction Approval

    Steps

    1. Clone the repository:

      git clone https://github.com/your-repo/stakesphere.git
      cd stakesphere
    2. Install dependencies:

      npm install
    3. Configure environment variables: Create a .env.local file in the root directory and add your blockchain provider, Thirdweb API keys, and other required credentials.

    4. Start the development server:

      npm run dev
    5. Open the application in your browser at http://localhost:3000.


    How It Works

    Transaction Request

    1. Connect Wallet: Users connect their wallet using the wallet connect button. Supported wallets include MetaMask, Coinbase Wallet, and others.

    2. Claim NFT: If a user doesn’t own any NFT, they can claim one via the “Claim NFT” button. The application checks the user’s wallet before allowing staking.

    3. Stake NFT:

      • Users select an NFT from their wallet.
      • The app prompts two transactions:
        1. Approval of the NFT.
        2. Staking of the NFT.
      • Once staked, users begin earning rewards.
    4. Withdraw NFT: Users can withdraw any staked NFT back to their wallet. Rewards are calculated and distributed based on the staking duration.


    No NFT Stake

    Folder Structure

    src/
    |-- components/     # Reusable UI components
    |-- pages/          # Application pages (Next.js routing)        
    |-- utils/          # blockchain abi and address
    

    Future Enhancements

    • Support for multiple NFT collections.
    • Detailed analytics for staking activities.
    • Integration with additional blockchain networks.
    • Enhanced reward calculation algorithms.

    Contributing

    Contributions are welcome! Please fork the repository, create a feature branch, and submit a pull request. Ensure your code follows the project’s coding standards.


    Acknowledgements

    • Thirdweb for simplifying blockchain interactions.
    • TailwindCSS for a beautiful and responsive UI.
    • Next.js for a seamless development experience.

    Visit original content creator repository