Transaction

DADE89854FC5F0…DA0D285E1FF8

Block 77,248 · index 0 · indexed

Summary

Hash
DADE89854FC5F0192FF8501F37229055543AFACCEBA720A61AEEDA0D285E1FF8
Block
77,248
Size
17531 bytes
Gas used
24,937,286 / 29,924,708
Fee
29925ugnot
Status
success

Messages

#1AddPackagegno.land/r/gnoswap/common21 arguments

Arguments · 21

  1. #1common
  2. #2README.md
  3. #3# Common Package Package common provides shared realm utilities for GnoSwap protocol contracts. ## Overview The common package contains shared GRC20 token operations and native coin validation. ## Gnoweb The root `Render("")` delegates to the active implementation and shows the realm address, implementation path, and network-wide token registry count. The count is not a Gnoswap whitelist. Token keys use `realm-path.SYMBOL`, and token amounts use each token's base units. Rendering is read-only and does not enumerate registry entries. Unsupported paths return `404`. ## Key Components 1. **GRC20 Registry Helpers**: Convenient wrappers for GRC20 token operations 2. **Coin Utilities**: Native coin (GNOT) handling and validation 3. **Assertion Utilities**: Input validation for supported operations (not authorization checks) ## API Reference ### GRC20 Registry Helpers The write helpers are called without crossing, for example `common.Transfer(0, cur, ...)` and `common.SafeGRC20Transfer(0, cur, ...)`. The token actor is bound to that current realm via `RealmTeller` before the operation is forwarded. Token lookup goes through the registered implementation (`common/v1` resolves `gno.land/r/nt/grc20reg/v0`), which is swapped with `UpgradeImpl` like the other proxy realms. **Token Operations:** - **IsRegistered**: Checks token registration status - **MustRegistered**: Validates multiple tokens are registered **Token Queries:** - **TotalSupply**: Returns total supply of a token - **BalanceOf**: Returns token balance for an address - **Allowance**: Returns allowance from owner to spender **Token Transfers:** - **Transfer/TransferFrom/Approve**: Returns error on failure - **SafeGRC20Transfer/SafeGRC20TransferFrom/SafeGRC20Approve**: Panics on failure ### Coin Utilities **Coin Validation:** - **AssertIsNotHandleNativeCoin**: Rejects native coins for GRC20-only functions and panics with `[GNOSWAP-COMMON-002] handle native coin is not allowed` when `unsafe.OriginSend()` is non-empty
  4. #4assert.gno
  5. #5package common // AssertIsNotHandleNativeCoin validates that no native coins were sent with the transaction. // // Use this when a function should only work with GRC20 tokens and not accept native coins. func AssertIsNotHandleNativeCoin() { if err := getImplementation().ValidateNotHandleNativeCoin(); err != nil { panic(err) } }
  6. #6doc.gno
  7. #7// Package common provides shared realm utilities for GnoSwap protocol contracts. // // The package contains helpers that must keep a realm boundary, including GRC20 // token operations and native coin validation. AMM math lives in // gno.land/p/gnoswap/gnsmath/v1. // // Key components: // - GRC20 Registry Helpers: convenient wrappers for GRC20 token operations // - Coin Utilities: native coin handling and validation // - Assertion Utilities: input validation for supported operations, including native-coin rejection package common
  8. #8gnomod.toml
  9. #9module = "gno.land/r/gnoswap/common" gno = "0.9"
  10. #10grc20reg_helper.gno
  11. #11package common // IsRegistered checks whether a token key is registered. // // Parameters: // - tokenKey: registry key identifying the GRC20 token to inspect. // // Returns: // - error: nil when tokenKey is valid and registered; non-nil when the key is invalid or unregistered. func IsRegistered(tokenKey string) error { return getImplementation().IsRegistered(tokenKey) } // MustRegistered checks whether all provided tokens are registered and panics if any is not registered. // // Parameters: // - tokenKeys: token registry keys to check; an empty list performs no checks. // // Panics if any of the provided tokens is not registered. func MustRegistered(tokenKeys ...string) { if err := ValidateRegistered(tokenKeys...); err != nil { panic(err.Error()) } } // ValidateRegistered checks whether all provided tokens are registered, returning an // error instead of panicking when a token is not registered. // // Parameters: // - tokenKeys: token registry keys to check; an empty list is valid. // // Returns: // - error: nil when every token is registered; otherwise the first // registration failure. func ValidateRegistered(tokenKeys ...string) error { return getImplementation().ValidateRegistered(tokenKeys...) } // TotalSupply returns the total supply of the specified token. // // Parameters: // - tokenKey: registry key identifying the GRC20 token. // // Returns: // - totalSupply: current token supply for tokenKey, in the token's base units; panics if tokenKey is invalid or unregistered. func TotalSupply(tokenKey string) int64 { return getImplementation().TotalSupply(tokenKey) } // BalanceOf returns the token balance for the specified address. // // Parameters: // - tokenKey: registry key identifying the GRC20 token. // - addr: address whose token balance is queried. // // Returns: // - balance: token balance held by addr, in the token's base units; panics if tokenKey is invalid or unregistered. func BalanceOf(tokenKey string, addr address) int64 { return getImplementation().BalanceOf(tokenKey, addr) } // Allowance returns the token allowance from owner to spender. // // Parameters: // - tokenKey: registry key identifying the GRC20 token. // - owner: address that owns the tokens. // - spender: address authorized to spend owner's tokens. // // Returns: // - allowance: amount owner has approved spender to use, in the token's base units; panics if tokenKey is invalid or unregistered. func Allowance(tokenKey string, owner, spender address) int64 { return getImplementation().Allowance(tokenKey, owner, spender) } // Transfer forwards a token transfer to the implementation bound to the current realm. // // Parameters: // - _: leading integer discriminator for the realm-crossing ABI; pass 0. // - rlm: propagated realm context used to resolve and invoke the registered token implementation. // - tokenKey: registry key identifying the GRC20 token to transfer. // - to: recipient address. // - amount: number of token base units to transfer. // // Returns: // - error: nil when the implementation completes the transfer; otherwise a token // validation or transfer-rule error. func Transfer(_ int, rlm realm, tokenKey string, to address, amount int64) error { return getImplementation().Transfer(0, rlm, tokenKey, to, amount) } // TransferFrom forwards an allowance-backed token transfer to the implementation bound to the current realm. // // Parameters: // - _: leading integer discriminator for the realm-crossing ABI; pass 0. // - rlm: propagated realm context used to resolve and invoke the registered token implementation. // - tokenKey: registry key identifying the GRC20 token to transfer. // - from: address whose allowance and balance are debited. // - to: recipient address. // - amount: number of token base units to transfer. // // Returns: // - error: nil when the implementation completes the transfer; otherwise a token // validation, allowance, or balance-rule error. func TransferFrom(_ int, rlm realm, tokenKey string, from, to address, amount int64) error { return getImplementation().TransferFrom(0, rlm, tokenKey, from, to, amount) } // Approve forwards an allowance update to the implementation bound to the current realm. // // Parameters: // - _: leading integer discriminator for the realm-crossing ABI; pass 0. // - rlm: propagated realm context used to resolve and invoke the registered token implementation. // - tokenKey: registry key identifying the GRC20 token. // - spender: address authorized to spend the caller's tokens. // - amount: number of token base units to authorize. // // Returns: // - error: nil when the implementation records the allowance; otherwise a token // validation or approval-rule error. func Approve(_ int, rlm realm, tokenKey string, spender address, amount int64) error { return getImplementation().Approve(0, rlm, tokenKey, spender, amount) } // SafeGRC20Transfer transfers tokens as the current realm and panics if it fails. // // Parameters: // - _: leading integer discriminator for the realm-crossing ABI; pass 0. // - rlm: propagated realm context used to resolve and invoke the registered token implementation. // - tokenKey: registry key identifying the GRC20 token to transfer. // - to: recipient address. // - amount: number of token base units to transfer. // // Panics if the underlying transfer returns an error. func SafeGRC20Transfer(_ int, rlm realm, tokenKey string, to address, amount int64) { if err := Transfer(0, rlm, tokenKey, to, amount); err != nil { panic(err) } } // SafeGRC20TransferFrom transfers tokens as the current realm and panics if it fails. // // Parameters: // - _: leading integer discriminator for the realm-crossing ABI; pass 0. // - rlm: propagated realm context used to resolve and invoke the registered token implementation. // - tokenKey: registry key identifying the GRC20 token to transfer. // - from: address whose allowance and balance are debited. // - to: recipient address. // - amount: number of token base units to transfer. // // Panics if the underlying transfer returns an error. func SafeGRC20TransferFrom(_ int, rlm realm, tokenKey string, from, to address, amount int64) { if err := TransferFrom(0, rlm, tokenKey, from, to, amount); err != nil { panic(err) } } // SafeGRC20Approve approves tokens as the current realm and panics if it fails. // // Parameters: // - _: leading integer discriminator for the realm-crossing ABI; pass 0. // - rlm: propagated realm context used to resolve and invoke the registered token implementation. // - tokenKey: registry key identifying the GRC20 token. // - spender: address authorized to spend the caller's tokens. // - amount: number of token base units to authorize. // // Panics if the underlying approval returns an error. func SafeGRC20Approve(_ int, rlm realm, tokenKey string, spender address, amount int64) { if err := Approve(0, rlm, tokenKey, spender, amount); err != nil { panic(err) } }
  12. #12render.gno
  13. #13package common // Render delegates web rendering to the active implementation. func Render(path string) string { return getImplementation().Render(path) }
  14. #14state.gno
  15. #15package common import ( "errors" _ "gno.land/r/gnoswap/rbac/v1" // initialize readable contract role(s) "gno.land/p/gnoswap/store/v1" "gno.land/p/gnoswap/version_manager/v1" ) var ( currentAddress address domainPath string kvStore store.KVStore versionManager version_manager.VersionManager implementation ICommon ) func init(cur realm) { currentAddress = cur.Address() domainPath = cur.PkgPath() kvStore = store.NewKVStore(currentAddress) versionManager = version_manager.NewVersionManager( domainPath, kvStore, initializeDomainStore, ) implementation = nil } // common keeps no domain state; implementations only resolve tokens. func initializeDomainStore(_ int, rlm realm, kvStore store.KVStore) any { return nil } func getImplementation() ICommon { if implementation == nil { panic("implementation is not initialized") } return implementation } func updateImplementation() error { result := versionManager.GetCurrentImplementation() if result == nil { return errors.New("implementation is not initialized") } impl, ok := result.(ICommon) if !ok { return errors.New("impl is not an ICommon") } implementation = impl return nil }
  16. #16types.gno
  17. #17package common type ICommon interface { // ValidateNotHandleNativeCoin rejects a call carrying native coins. // // Returns: // - error: non-nil when the transaction includes native coins; nil when no native coins are attached ValidateNotHandleNativeCoin() error // IsRegistered checks whether tokenKey identifies a registered token. // // Parameters: // - tokenKey: token registry key to validate and look up // // Returns: // - error: nil when tokenKey is valid and registered; non-nil for an invalid or unregistered key IsRegistered(tokenKey string) error // ValidateRegistered checks each token key in order. // // Parameters: // - tokenKeys: token registry keys that must all be valid and registered // // Returns: // - error: nil when every key is registered; otherwise the first registration failure in input order ValidateRegistered(tokenKeys ...string) error // TotalSupply returns the total supply of a registered token. // // Parameters: // - tokenKey: token registry key whose supply is queried // // Returns: // - totalSupply: token's total supply; panics if tokenKey is invalid or unregistered TotalSupply(tokenKey string) int64 // BalanceOf returns a registered token's balance for an address. // // Parameters: // - tokenKey: token registry key whose balance is queried // - addr: account address whose token balance is queried // // Returns: // - balance: token units held by addr; panics if tokenKey is invalid or unregistered BalanceOf(tokenKey string, addr address) int64 // Allowance returns the approved spendable amount for an owner/spender pair. // // Parameters: // - tokenKey: token registry key whose allowance is queried // - owner: account that granted spending approval // - spender: account allowed to spend owner's tokens // // Returns: // - allowance: token units owner has approved for spender; panics if tokenKey is invalid or unregistered Allowance(tokenKey string, owner, spender address) int64 // Transfer asks the token teller bound to rlm to move tokens to to. // // Parameters: // - _: leading token-call discriminator; callers pass 0 // - rlm: propagated realm context used to bind the token teller and invoke the transfer // - tokenKey: token registry key whose tokens are transferred // - to: recipient account address // - amount: number of token units to transfer // // Returns: // - error: nil when the teller completes the transfer; non-nil when token validation or transfer rules reject it Transfer(_ int, rlm realm, tokenKey string, to address, amount int64) error // TransferFrom asks the token teller bound to rlm to transfer tokens from one account to another. // // Parameters: // - _: leading token-call discriminator; callers pass 0 // - rlm: propagated realm context used to bind the token teller and invoke the transfer // - tokenKey: token registry key whose tokens are transferred // - from: account whose token balance is debited // - to: recipient account address // - amount: number of token units to transfer // // Returns: // - error: nil when the teller completes the transfer; non-nil when token validation, allowance, or balance rules reject it TransferFrom(_ int, rlm realm, tokenKey string, from, to address, amount int64) error // Approve asks the token teller bound to rlm to set spender's allowance. // // Parameters: // - _: leading token-call discriminator; callers pass 0 // - rlm: propagated realm context used to bind the token teller and invoke approval // - tokenKey: token registry key whose tokens are approved // - spender: account receiving permission to spend the caller's tokens // - amount: number of token units to authorize // // Returns: // - error: nil when the teller records the allowance; non-nil when token validation or approval rules reject it Approve(_ int, rlm realm, tokenKey string, spender address, amount int64) error Render(path string) string }
  18. #18upgrade.gno
  19. #19package common import ( "gno.land/r/gnoswap/access/v1" ) // RegisterInitializer registers the versioned common implementation initializer // and activates the implementation returned by it. // // Parameters: // - cur: Current realm context; callers use cross(cur) when crossing into this realm. // - initializer: callback invoked with discriminator 0 and the propagated // implementation realm, returning the version's ICommon implementation func RegisterInitializer(cur realm, initializer func(_ int, rlm realm) ICommon) { initializerFunc := func(_ int, rlm realm, _ any) any { access.AssertIsRlmCurrent(0, rlm) return initializer(0, rlm) } err := versionManager.RegisterInitializer(0, cur, initializerFunc) if err != nil { panic(err) } err = updateImplementation() if err != nil { panic(err) } } // UpgradeImpl changes the active common implementation package and refreshes // the cached implementation. // // Parameters: // - cur: Current realm context; callers use cross(cur) when crossing into this realm. // - packagePath: fully qualified package path of the replacement implementation func UpgradeImpl(cur realm, packagePath string) { prev := cur.Previous() access.AssertIsAdminOrGovernance(prev.Address()) err := versionManager.ChangeImplementation(0, cur, packagePath) if err != nil { panic(err) } err = updateImplementation() if err != nil { panic(err) } } // GetImplementationPackagePath returns the package path of the currently // selected common implementation. // // Returns: // - packagePath: fully qualified current implementation package path func GetImplementationPackagePath() string { return versionManager.GetCurrentPackagePath() }
  20. #20/gno.MemPackageType
  21. #21 MPUserAll

Result log

msg:0,success:true,log:,events:[]

← Back to block 77,248