Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

Playground

contract.gno

// based on Wikipedia's Solidity example: https://en.wikipedia.org/wiki/Solidity
package wikicoin

import (
	"chain/runtime/unsafe"

	"gno.land/p/nt/avl/v0"
	"gno.land/p/nt/ufmt/v0"
)

var balances = avl.NewTree() // addr -> balance
var minter address

// Constructor code is only run when the contract is created
func init() {
	minter = unsafe.OriginCaller()
}

func Mint(cur realm, receiver address, amount uint) {
	if unsafe.OriginCaller() != minter {
		panic("restricted")
	}
	curBalance := BalanceOf(receiver)
	newBalance := curBalance + amount
	balances.Set(receiver.String(), newBalance)
}

func Send(cur realm, receiver address, amount uint) {
	sender := unsafe.OriginCaller()
	senderBalance := BalanceOf(sender)
	if amount > senderBalance {
		panic(ufmt.Sprintf("insufficient balance: %d", senderBalance))
	}
	receiverBalance := BalanceOf(receiver)
	balances.Set(sender.String(), senderBalance-amount)
	balances.Set(receiver.String(), receiverBalance+amount)
}

func BalanceOf(addr address) uint {
	balance := balances.Get(addr.String())
	if balance == nil {
		return 0
	}
	return balance.(uint)
}

contract_test.gno

package wikicoin

import (
	"testing"

	"gno.land/p/nt/testutils/v0"
)

func Test(cur realm, t *testing.T) {
	test2 := testutils.TestAddress("test2")

	mint := uint(1000)
	Mint(cross(cur), test2, mint)

	testing.SetOriginCaller(test2) // dropping privileges
	bal := BalanceOf(test2)
	if bal != mint {
		t.Errorf("expected %d, got %d", mint, bal)
	}
}

gnomod.toml

module = "gno.land/r/nym-moultest123/demo/wikicoin/v0"
gno = "0.9"

[addpkg]
  creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv"
  height = 186406
  max_deposit = "5000000ugnot"