examples: fix AddToCirculation method of TokenSale

Closes 

We should check owner witness before adding more tokens to circulation.
Also we shouldn't allow to add to circulation more than TokenSupply
tokens.
This commit is contained in:
Anna Shaleva 2020-08-31 19:08:32 +03:00
parent 7560aa345a
commit 4fdcefc87c

View file

@ -92,10 +92,16 @@ func InCirculation() int {
return getIntFromDB(ctx, token.CirculationKey)
}
// AddToCirculation sets the given amount as "in circulation" in the storage.
func AddToCirculation(amount int) bool {
// addToCirculation sets the given amount as "in circulation" in the storage.
func addToCirculation(amount int) bool {
if amount < 0 {
return false
}
supply := getIntFromDB(ctx, token.CirculationKey)
supply += amount
if supply > token.TotalSupply {
return false
}
storage.Put(ctx, token.CirculationKey, supply)
return true
}
@ -257,3 +263,22 @@ func Allowance(from, to []byte) interface{} {
key := append(from, to...)
return getIntFromDB(ctx, key)
}
// Mint initial supply of tokens
func Mint(to []byte) bool {
if trigger != runtime.Application {
return false
}
if !checkOwnerWitness() {
return false
}
minted := storage.Get(ctx, []byte("minted"))
if minted != nil && minted.(bool) == true {
return false
}
storage.Put(ctx, to, token.TotalSupply)
storage.Put(ctx, []byte("minted"), true)
addToCirculation(token.TotalSupply)
return true
}