Replies: 5 comments 6 replies
-
Distinguishing between
IMO distinguishing
IMO storage variables should never be exposed. If a user wants to interact with them, they should use
|
Beta Was this translation helpful? Give feedback.
-
Have you guys tried using namespaces? I've noticed the discussion in #244 mentions:
Well, this works for me (cairo 0.8.1), at least in local testing: # tkn.cairo
%lang starknet
from starkware.cairo.common.cairo_builtins import HashBuiltin
from contracts.token.library import ERC20
@view
func getBalance{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
owner : felt
) -> (balance : felt):
let (balance) = ERC20.getBalance(owner)
return (balance)
end
@external
func setBalance{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
owner : felt, balance : felt
):
ERC20.setBalance(owner, balance)
return ()
end
# library.cairo
%lang starknet
from starkware.cairo.common.cairo_builtins import HashBuiltin
@storage_var
func balanceOf(address : felt) -> (balance : felt):
end
namespace ERC20:
func getBalance{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
addr : felt
) -> (balance : felt):
let (balance) = balanceOf.read(addr)
return (balance)
end
func setBalance{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
addr : felt, balance : felt
):
balanceOf.write(addr, balance)
return ()
end
end I didn't give it much deeper though, especially with regards to the discussion above (I'm sorry, too much work), so I'm not quite sure if it's helpful at all. Only wanted to bring it up just in case it's of any use. |
Beta Was this translation helpful? Give feedback.
-
❤️ clearly, if its possible to import a file under a symbol, I'd much rather not have any conflict-preventing prefix in the function names, and use that instead. |
Beta Was this translation helpful? Give feedback.
-
I like what @Amxx has proposed and i think that we can make it more generic where we have +1 to storage variables being encapsulated by getters/setters with events other than that there's really no strong reason IMO to have camel case. it's not like these functions are being called directly from ethereum contracts. As long as the cairo ecosystem knows that camel case is transformed into snake case i dont see any reason to keep it. the only other naming stuff (not that it has an impact really) are function parameters and return names. seeing as they're not enforced AFAIK i think we should adopt a snake case standard |
Beta Was this translation helpful? Give feedback.
-
To preface, much of the discussion regarding function naming transpired before separating libraries from contracts (the Extensibility Pattern). Since methods need to be exposed in contracts, I sort of see the lib methods as internal methods of the contract. So here's the question: should the libraries themselves be fully compliant with the (naming) standards set in ERCs even though they must be exposed?
With that said, here are my answers:
Since Cairo uses snake_case by convention, IMO it makes sense to treat camelCasing as the exception and to generally favor snake_case. The more the casings are mixed, the more it turns into sna_melCase.
|
Beta Was this translation helpful? Give feedback.
-
In the light of recent discussions on #244 and #240, a spinoff discussion of #34 requires to be reopen. I'll attempt to clarify existing naming rules for the library and then point out open questions around them.
Current status
Given four kind of variables in library modules:
A
: internal to a library, not meant to be used outside the moduleB
: part of the public API of a libraryC
: subset of B where the function is meant to be exported as-is by contractsD
: an even smaller subset ofC
that aims to comply with EIP standardsS
: storage variablesToday:
A
functions should be prefixed with an underscore (e.g._foo
), and never exposed/suggested/documented as API.ERC20_approve
) to avoid clashes with other modulesS
variables must always have a prefix, since collisions are possible even without being exported.C
fromD
depending on whether they use camelCase or not (everything butD
issnake_case
(*)(*) this brings ambiguity for single-word names like
ERC20_approve
, where the current -and weak- assumption is to consider it camelCase/part of groupD
.Questions:
B
/"internal" functions? (in the Solidity sense, where they're part of the module's API)B
,C
, andD
?My own attempt to answer:
ERC20_unguarded_approve
for Solidity's_approve
, and so onapprove
and_approve
.I would even drop the camelCase in favor of more explicit names to distinguish between
C
andD
because it's too soft and may lead to confusions. Maybe we should leave camelCase for contracts only, never for libraries. Users should study the library API to understand what functions should it expose as-is.Beta Was this translation helpful? Give feedback.
All reactions