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

// Package vault demonstrates a minimal time-locked token vault built on top of
// the grc20 token package. Deposited tokens are held at the realm's own
// address; withdrawing them is a two-step process: Unvault marks an amount for
// withdrawal and starts a lock timer, then (once the lock elapses) Redeem moves
// the tokens back to the depositor.
package vault

import (
	"chain/runtime"
	"chain/runtime/unsafe"

	tokens "gno.land/p/nt/grc20/v0"
)

// test1 receives the initial mint at construction.
var test1 = address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")

// lockDuration is the number of blocks that must pass between Unvault and
// Redeem.
const lockDuration = int64(100)

// Token and FooVault-equivalent state are exported for reading; privateLedger
// stays unexported (unrestricted mint/burn/transfer).
var (
	Token         *tokens.Token
	privateLedger *tokens.PrivateLedger
	vaultAddr     address // address holding all deposited tokens

	vaulted   = map[address]uint{}    // still-locked deposits per owner
	pending   = map[address]uint{}    // amount marked for withdrawal
	unlockAt  = map[address]int64{}    // block height at which pending unlocks
	recoverOf = map[address]address{}  // recovery address per depositor
)

func init(cur realm) {
	// generate the token and mint some tokens to test1.
	Token, privateLedger = tokens.NewToken("Foo Token", "FOO", 4, 0, cur)
	privateLedger.Mint(test1, 100000000)

	// the vault holds deposited tokens at the realm's own address.
	vaultAddr = cur.Address()
}

func MyBalance() int64 {
	return Token.BalanceOf(unsafe.OriginCaller())
}

// Deposit moves amount tokens from the caller into the vault, recording a
// recovery address for later use.
func Deposit(cur realm, amount uint, recoverAddress address) {
	caller := unsafe.OriginCaller()
	if err := privateLedger.Transfer(caller, vaultAddr, int64(amount)); err != nil {
		panic(err)
	}
	vaulted[caller] += amount
	recoverOf[caller] = recoverAddress
}

// Recover sends target's still-locked deposit to its recorded recovery address.
func Recover(cur realm, target address) {
	amount := vaulted[target]
	if amount == 0 {
		return
	}
	dest := recoverOf[target]
	if err := privateLedger.Transfer(vaultAddr, dest, int64(amount)); err != nil {
		panic(err)
	}
	vaulted[target] = 0
}

// Unvault marks amount of the caller's deposit for withdrawal and starts the
// lock timer.
func Unvault(cur realm, amount uint) {
	caller := unsafe.OriginCaller()
	if vaulted[caller] < amount {
		panic("insufficient vaulted balance")
	}
	vaulted[caller] -= amount
	pending[caller] += amount
	unlockAt[caller] = runtime.ChainHeight() + lockDuration
}

// Redeem transfers amount of the caller's matured pending withdrawal back to
// the caller.
func Redeem(cur realm, amount uint) {
	caller := unsafe.OriginCaller()
	if pending[caller] < amount {
		panic("insufficient pending balance")
	}
	if runtime.ChainHeight() < unlockAt[caller] {
		panic("still locked")
	}
	if err := privateLedger.Transfer(vaultAddr, caller, int64(amount)); err != nil {
		panic(err)
	}
	pending[caller] -= amount
}

contract_test.gno

package vault

import (
	"strings"
	"testing"

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

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

	// check token metadata.
	{
		got := Token.GetName()
		expected := "Foo Token"
		if strings.TrimSpace(got) != strings.TrimSpace(expected) {
			t.Fatalf("expected %q, got %q.", expected, got)
		}
	}

	// check balance of test1.
	{
		got := Token.BalanceOf(test1)
		expected := int64(100000000)
		if got != expected {
			t.Fatalf("expected %d, got %d.", expected, got)
		}
	}

	// transfer tokens from test1 to test2.
	if err := privateLedger.Transfer(test1, test2, 1000); err != nil {
		t.Fatal(err)
	}

	// check balance of test1.
	{
		got := Token.BalanceOf(test1)
		expected := int64(99999000)
		if got != expected {
			t.Fatalf("expected %d, got %d.", expected, got)
		}
	}

	// check balance of test2.
	{
		got := Token.BalanceOf(test2)
		expected := int64(1000)
		if got != expected {
			t.Fatalf("expected %d, got %d.", expected, got)
		}
	}

	// test1 deposits 300 with test3 as recovery address.
	testing.SetOriginCaller(test1)
	Deposit(cross(cur), 300, test3)

	// check balance of test1.
	{
		got := Token.BalanceOf(test1)
		expected := int64(99998700)
		if got != expected {
			t.Fatalf("expected %d, got %d.", expected, got)
		}
	}

	// check balance of the vault.
	{
		got := Token.BalanceOf(vaultAddr)
		expected := int64(300)
		if got != expected {
			t.Fatalf("expected %d, got %d.", expected, got)
		}
	}

	// test1 unvaults 50 tokens, waits past the lock, then redeems them.
	Unvault(cross(cur), 50)
	testing.SkipHeights(101)  // SkipHeights resets the context, so re-set the caller.
	testing.SetOriginCaller(test1)
	Redeem(cross(cur), 50)

	// check balance of test1.
	{
		got := Token.BalanceOf(test1)
		expected := int64(99998750)
		if got != expected {
			t.Fatalf("expected %d, got %d.", expected, got)
		}
	}

	// check balance of the vault.
	{
		got := Token.BalanceOf(vaultAddr)
		expected := int64(250)
		if got != expected {
			t.Fatalf("expected %d, got %d.", expected, got)
		}
	}

	// Recover sends test1's still-locked 250 to the recovery address recorded
	// at deposit time (test3), leaving the vault empty.
	//
	// It is called here by test2, a bystander who never deposited, because
	// Recover has NO caller check. That is a real flaw and this test pins it:
	// it cannot steal (the destination is whatever the target itself recorded)
	// but anyone can rewind anyone's deposit at will, which is a free denial of
	// service. If a caller check is ever added, this assertion is what will
	// fail, so the change has to be deliberate.
	testing.SetOriginCaller(test2)
	Recover(cross(cur), test1)

	// the recovery address received the whole locked balance.
	{
		got := Token.BalanceOf(test3)
		expected := int64(250)
		if got != expected {
			t.Fatalf("expected %d, got %d.", expected, got)
		}
	}

	// the vault is empty.
	{
		got := Token.BalanceOf(vaultAddr)
		expected := int64(0)
		if got != expected {
			t.Fatalf("expected %d, got %d.", expected, got)
		}
	}

	// Recovering twice is a no-op rather than a second payout: the vaulted
	// balance is zero, so Recover returns before transferring anything.
	Recover(cross(cur), test1)
	{
		got := Token.BalanceOf(test3)
		expected := int64(250)
		if got != expected {
			t.Fatalf("expected %d, got %d.", expected, got)
		}
	}
}

gnomod.toml

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

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