Search Apps Documentation Source Content File Folder Download Copy Actions Download

assert.gno

1.68 Kb ยท 62 lines
 1package v1
 2
 3import (
 4	"gno.land/p/nt/ufmt"
 5	"gno.land/r/gnoswap/launchpad"
 6)
 7
 8// assertIsDepositOwner asserts that the caller is the owner of the deposit.
 9// Panics if the caller is not the owner of the deposit.
10func (lp *launchpadV1) assertIsDepositOwner(depositID string, caller address) {
11	deposit, err := lp.getDeposit(depositID)
12	if err != nil {
13		panic(err.Error())
14	}
15
16	if !deposit.IsDepositor(caller) {
17		panic(makeErrorWithDetails(errInvalidOwner, ufmt.Sprintf("(%s)", caller.String())).Error())
18	}
19}
20
21// assertIsValidAmount panics if the amount is zero.
22func assertIsValidAmount(amount int64) {
23	if amount < minimumDepositAmount {
24		panic(makeErrorWithDetails(
25			errInvalidAmount,
26			ufmt.Sprintf("amount(%d) should greater than minimum deposit amount(%d)", amount, minimumDepositAmount),
27		))
28	}
29
30	if (amount % minimumDepositAmount) != 0 {
31		panic(makeErrorWithDetails(
32			errInvalidAmount,
33			ufmt.Sprintf("amount(%d) must be a multiple of 1_000_000", amount),
34		))
35	}
36}
37
38// assertHasProject asserts that the caller is the owner of at least one project.
39// Panics if the caller is not the owner of any project.
40func (lp *launchpadV1) assertHasProject(caller address) {
41	hasProject := false
42
43	projects := lp.store.GetProjects()
44	projects.Iterate("", "", func(key string, value interface{}) bool {
45		project, ok := value.(*launchpad.Project)
46		if !ok {
47			panic(ufmt.Sprintf("failed to cast projects's element to *launchpad.Project: %T", value))
48		}
49
50		hasProject = project.IsRecipient(caller)
51
52		// if true, break the loop
53		return hasProject
54	})
55
56	if !hasProject {
57		panic(makeErrorWithDetails(
58			errInvalidOwner,
59			ufmt.Sprintf("caller %s is not the owner of any project", caller.String()),
60		))
61	}
62}