From 4fdcefc87c663ad7622b140d26b4125835303065 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Mon, 31 Aug 2020 19:08:32 +0300 Subject: [PATCH] examples: fix AddToCirculation method of TokenSale Closes #1379 We should check owner witness before adding more tokens to circulation. Also we shouldn't allow to add to circulation more than TokenSupply tokens. --- examples/token-sale/token_sale.go | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/examples/token-sale/token_sale.go b/examples/token-sale/token_sale.go index 05a70462d..a69cb34a2 100644 --- a/examples/token-sale/token_sale.go +++ b/examples/token-sale/token_sale.go @@ -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 +}