0

Let's say I have two macros:

#define macro TOTAL_SUPPLY() = takes (0) returns (0) {
    [TOTAL_SUPPLY_LOCATION] sload   // [supply]
    0x00 mstore                     // []
    0x20 0x00 return                // []
}

#define macro DOUBLE_TOTAL_SUPPLY() = takes (0) returns (0) {
    // Get return of TOTAL_SUPPLY.  // [TOTAL_SUPPLY]
    0x2                             // [0x2, TOTAL_SUPPLY]
    mul                             // [0x2 * TOTAL_SUPPLY]

    0x00 mstore
    0x20 0x00 return
}

How would I get my DOUBLE_TOTAL_SUPPLY macro to get the return value of TOTAL_SUPPLY?

1 Answer 1

0

In it's current form, you can't. Remember, calling a macro is the same as copy pasting the code into where you're currently executing, including the return which ends the transaction!

What you can do to get around this, would be to make a wrapper macro, one that returns, and one that doesn't.

TOTAL_SUPPLY_RETURNS returns, and calls TOTAL_SUPPLY which does not. So you can use TOTAL_SUPPLY in future code.

(remember, if you use TOTAL_SUPPLY a lot you might want to consider making it a function instead of a macro)

#define macro TOTAL_SUPPLY_RETURNS() = takes (0) returns (0) {
    TOTAL_SUPPLY()                  // [supply]
    0x00 mstore                     // []
    0x20 0x00 return                // []
}

#define macro TOTAL_SUPPLY() = takes (0) returns (1) {
    [TOTAL_SUPPLY_LOCATION] sload   // [supply]
}

#define macro DOUBLE_TOTAL_SUPPLY() = takes (0) returns (0) {
    TOTAL_SUPPLY()                  // [TOTAL_SUPPLY]
    0x2                             // [0x2, TOTAL_SUPPLY]
    mul                             // [0x2 * TOTAL_SUPPLY]

    0x00 mstore
    0x20 0x00 return
}

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.