render.gno

2.78 Kb ยท 81 lines
 1package ownable
 2
 3import "strings"
 4
 5func Render(path string) string {
 6	content := `
 7# Ownable
 8
 9This Solidity contract defines a counter that can be incremented or decremented by anyone.
10
11---
12
13### Solidity Version
14
15^^^solidity
16contract OnlyOwnerCanChange {
17    address private owner;
18
19    constructor() {
20        owner = msg.sender;
21    }
22
23    modifier onlyOwner {
24        require(msg.sender == owner, "Not owner");
25        _;
26    }
27
28    function changeOwner(address memory newOwner) public onlyOwner {
29        owner = newOwner
30    }
31}
32^^^
33
34* It declares a state variable ^owner^ of type ^address^, which stores the address of the account that deployed the contract.
35* The ^constructor^ sets the ^owner^ variable to the address that deployed the contract using ^msg.sender^.
36* It defines a ^modifier onlyOwner^ that restricts function execution to the owner only, using ^require^ to check that the caller is the current ^owner^. The special symbol ^_;^ is where the modified function's code runs.
37* It defines a ^changeOwner^ function that allows the current owner to assign a new owner address. The function uses the ^onlyOwner^ modifier to enforce access control.
38
39---
40## Gno Version
41
42^^^go
43package onlyownercanchange
44
45import "std"
46
47var owner std.Address = ""
48
49func InitOwner(cur realm) {
50	if len(owner) != 0 {
51		panic("owner already defined")
52	}
53	owner = std.PreviousRealm().Address()
54}
55
56func assertOnlyOwner() {
57	if std.PreviousRealm().Address() != owner {
58		panic("caller isn't the owner")
59	}
60}
61
62func ChangeOwner(cur realm, newOwner address) {
63	assertOnlyOwner()
64	owner = newOwner
65}
66^^^
67
68* We declare a persistent global variable ^owner^ of type ^std.Address^ (an alias for ^string^ in Gno), initialized to the empty string ^""^. This holds the address of the contract owner.
69* The function ^InitOwner^ is used to initialize the ^owner^. It can only be called once: if ^owner^ is already set (non-empty), it throws an error using ^panic^. It sets the ^owner^ to the address of the previous realm (the caller) via ^std.PreviousRealm().Address()^, which is similar to ^msg.sender^ in Solidity.
70* The helper function ^assertOnlyOwner^ checks that the caller is the current ^owner^ by comparing ^std.PreviousRealm().Address()^ with ^owner^. If not, it panics. This is equivalent to a Solidity ^modifier onlyOwner^.
71* The function ^ChangeOwner^ allows the current owner to transfer ownership to a new address. It first calls ^assertOnlyOwner()^ to ensure that only the current owner can do this.
72* All functions are capitalized (exported), meaning they can be called externally. However, ^assertOnlyOwner^ is unexported (lowercase), meaning it's only usable within the package.
73
74
75> [!NOTE]
76> For a library that handles this for you, check out [^/p/demo/ownable^](/p/demo/ownable).
77
78---
79`
80	return strings.ReplaceAll(content+RenderDemo(), "^", "`")
81}