0

I've been trying to figure out how to make function calls using in-line assembly (yul)

For example, I have a function _getCurrentRate() where I would like to be capable of calling from several different functions besides this one.

function _getCurrentRate() public view returns (uint256 multiplier) {
        assembly {
            let pe := sload(periodEnd.slot)
            let ps := sload(periodStart.slot)
            let rate := sload(compoundRate.slot)

            if lt(timestamp(), pe) {
                multiplier := add(mul(sub(timestamp(), ps), rate), 1000000000000000000)
            } 
            
            if iszero(lt(timestamp(), pe)) {
                multiplier := 2000000000000000000
            }
        }
    }

However, I have looked through documentation and unable to find any hint as to how to accomplish this! An example implementation of what I am talking about would look similar to this

function calculateSomething() public view returns (uint256 something) {
    assembly {
        let a := 123456789
        let rate := _getCurrentRate()

        something := mul(a, rate)
    }
}

1 Answer 1

1

_getCurrentRate must be a yul function inside your assembly block which is probably not what you want in the end

    function calculateSomething() public view returns (uint256 something) {
    assembly {
          function _getCurrentRate() -> multiplier {
            let pe := sload(periodEnd.slot)
            let ps := sload(periodStart.slot)
            let rate := sload(compoundRate.slot)

            if lt(timestamp(), pe) {
                multiplier := add(mul(sub(timestamp(), ps), rate), 1000000000000000000)
            } 
            
            if iszero(lt(timestamp(), pe)) {
                multiplier := 2000000000000000000
            }
        }
    
    let a := 123456789
    let rate := _getCurrentRate()

    something := mul(a, rate)
    }
}

one solution for your problem would be the following:

function calculateSomething() public view returns (uint256 something) {
    uint256 rate = _getCurrentRate();
    assembly {
        let a := 123456789
        something := mul(a, rate)
    }
}
1
  • So, we can't call private/internal functions within yul ?
    – Xirexor
    Commented Sep 9, 2023 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.