github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/gnovm/stdlibs/stdshim/banker.gno (about)

     1  package std
     2  
     3  // Realm functions can call std.GetBanker(options) to get
     4  // a banker instance. Banker objects cannot be persisted,
     5  // but can be passed onto other functions to be transacted
     6  // on. A banker instance can be passed onto other realm
     7  // functions; this allows other realms to spend coins on
     8  // behalf of the first realm.
     9  //
    10  // Banker panics on errors instead of returning errors.
    11  // This also helps simplify the interface and prevent
    12  // hidden bugs (e.g. ignoring errors)
    13  //
    14  // NOTE: this Gno interface is satisfied by a native go
    15  // type, and those can't return non-primitive objects
    16  // (without confusion).
    17  type Banker interface {
    18  	GetCoins(addr Address) (dst Coins)
    19  	SendCoins(from, to Address, amt Coins)
    20  	TotalCoin(denom string) int64
    21  	IssueCoin(addr Address, denom string, amount int64)
    22  	RemoveCoin(addr Address, denom string, amount int64)
    23  }
    24  
    25  // Also available natively in stdlibs/context.go
    26  type BankerType uint8
    27  
    28  // Also available natively in stdlibs/context.go
    29  const (
    30  	// Can only read state.
    31  	BankerTypeReadonly BankerType = iota
    32  	// Can only send from tx send.
    33  	BankerTypeOrigSend
    34  	// Can send from all realm coins.
    35  	BankerTypeRealmSend
    36  	// Can issue and remove realm coins.
    37  	BankerTypeRealmIssue
    38  )
    39  
    40  //----------------------------------------
    41  // adapter for native banker
    42  
    43  type bankAdapter struct {
    44  	nativeBanker Banker
    45  }
    46  
    47  func (ba bankAdapter) GetCoins(addr Address) (dst Coins) {
    48  	// convert native -> gno
    49  	coins := ba.nativeBanker.GetCoins(addr)
    50  	for _, coin := range coins {
    51  		dst = append(dst, (Coin)(coin))
    52  	}
    53  	return dst
    54  }
    55  
    56  func (ba bankAdapter) SendCoins(from, to Address, amt Coins) {
    57  	ba.nativeBanker.SendCoins(from, to, amt)
    58  }
    59  
    60  func (ba bankAdapter) TotalCoin(denom string) int64 {
    61  	return ba.nativeBanker.TotalCoin(denom)
    62  }
    63  
    64  func (ba bankAdapter) IssueCoin(addr Address, denom string, amount int64) {
    65  	ba.nativeBanker.IssueCoin(addr, denom, amount)
    66  }
    67  
    68  func (ba bankAdapter) RemoveCoin(addr Address, denom string, amount int64) {
    69  	ba.nativeBanker.RemoveCoin(addr, denom, amount)
    70  }