Giter Site home page Giter Site logo

Comments (2)

janezpodhostnik avatar janezpodhostnik commented on June 27, 2024 1

@turbolent can this be done with the current version of cadence?

the function is: (

pub fun calculateAccountCapacity(_ accountAddress: Address): UFix64 {
)

    // Returns megabytes
    pub fun calculateAccountCapacity(_ accountAddress: Address): UFix64 {
        let balanceRef = getAccount(accountAddress)
            .getCapability<&FlowToken.Vault{FungibleToken.Balance}>(/public/flowTokenBalance)!
            .borrow() ?? panic("Could not borrow FLOW balance capability")

        // get address token balance
        if balanceRef.balance < self.minimumStorageReservation {
            // if < then minimum return 0
            return 0.0
        } else {
            // return balance multiplied with megabytes per flow 
            return balanceRef.balance * self.storageMegaBytesPerReservedFLOW
        }
    }

The last part balanceRef.balance * self.storageMegaBytesPerReservedFLOW should return max UFix64 if the multiplication result is more than max UFix64.

from flow-core-contracts.

turbolent avatar turbolent commented on June 27, 2024

I think what we would want is saturating multiply, that results in the min/max value instead of underflowing/overflow. We don't have that in Cadence, but it could be easily added. For example, Rust has saturating_mul: https://doc.rust-lang.org/std/primitive.u32.html#method.saturating_mul

Alternatively, the same behaviour could be implemented by dividing from the max value and checking if the multiplication would result in an overflow. There are currently no max/min constants of number types exposed (e.g. UFix.max), but again this could be easily added, and alternatively defined as a constant in the Cadence program itself (e.g. let UFix64Max = ....

I'll create issues for both.

from flow-core-contracts.

Related Issues (20)

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.