-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanagement.go
137 lines (125 loc) · 4.09 KB
/
management.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package pocketic
import (
"fmt"
"net/http"
"github.com/aviate-labs/agent-go/candid/idl"
"github.com/aviate-labs/agent-go/principal"
"github.com/aviate-labs/ic-go"
"github.com/aviate-labs/ic-go/ic0"
)
// AddCycles add cycles to a canister. Returns the new balance.
func (pic PocketIC) AddCycles(canisterID principal.Principal, amount int) (int, error) {
var resp struct {
Cycles int `json:"cycles"`
}
if err := pic.do(
http.MethodPost,
fmt.Sprintf("%s/update/add_cycles", pic.InstanceURL()),
rawAddCycles{
Amount: amount,
CanisterID: canisterID.Raw,
},
&resp,
); err != nil {
return 0, err
}
return resp.Cycles, nil
}
func (pic PocketIC) CreateAndInstallCode(wasmModule []byte, arg []byte, optSender *principal.Principal) (*principal.Principal, error) {
canisterID, err := pic.CreateCanister()
if err != nil {
return nil, err
}
return canisterID, pic.InstallCode(*canisterID, wasmModule, arg, optSender)
}
// CreateCanister creates a canister with default settings as the anonymous principal.
func (pic PocketIC) CreateCanister() (*principal.Principal, error) {
return pic.CreateCanisterWithArgs(ProvisionalCreateCanisterArgument{})
}
// CreateCanisterOnSubnet creates a canister on the specified subnet with the specified settings.
func (pic PocketIC) CreateCanisterOnSubnet(subnetID principal.Principal, args ProvisionalCreateCanisterArgument) (*principal.Principal, error) {
return pic.createCanister(&EffectivePrincipalSubnetID{SubnetID: subnetID.Raw}, args)
}
// CreateCanisterWithArgs creates a canister with the specified settings and cycles.
func (pic PocketIC) CreateCanisterWithArgs(args ProvisionalCreateCanisterArgument) (*principal.Principal, error) {
return pic.createCanister(new(EffectivePrincipalNone), args)
}
// CreateCanisterWithID creates a canister with the specified canister ID and settings.
func (pic PocketIC) CreateCanisterWithID(canisterID principal.Principal, args ProvisionalCreateCanisterArgument) (*principal.Principal, error) {
return pic.createCanister(&EffectivePrincipalCanisterID{CanisterID: canisterID.Raw}, args)
}
// InstallCode installs a canister with the specified wasm module and arguments.
func (pic PocketIC) InstallCode(canisterID principal.Principal, wasmModule []byte, arg []byte, optSender *principal.Principal) error {
sender := principal.AnonymousID
if optSender != nil {
sender = *optSender
}
payload, err := idl.Marshal([]any{ic0.InstallCodeArgs{
Mode: ic0.CanisterInstallMode{
Install: new(idl.Null),
},
WasmModule: wasmModule,
CanisterId: canisterID,
Arg: arg,
}})
if err != nil {
return err
}
_, err = pic.updateCallWithEP(
ic.MANAGEMENT_CANISTER_PRINCIPAL,
&EffectivePrincipalCanisterID{CanisterID: canisterID.Raw},
sender,
"install_code",
payload,
)
return err
}
// UninstallCode uninstalls a canister.
func (pic PocketIC) UninstallCode(canisterID principal.Principal, optSender *principal.Principal) error {
sender := principal.AnonymousID
if optSender != nil {
sender = *optSender
}
payload, err := idl.Marshal([]any{ic0.UninstallCodeArgs{
CanisterId: canisterID,
}})
if err != nil {
return err
}
_, err = pic.updateCallWithEP(
ic.MANAGEMENT_CANISTER_PRINCIPAL,
&EffectivePrincipalCanisterID{CanisterID: canisterID.Raw},
sender,
"uninstall_code",
payload,
)
return err
}
func (pic PocketIC) createCanister(effectivePrincipal EffectivePrincipal, args ProvisionalCreateCanisterArgument) (*principal.Principal, error) {
payload, err := idl.Marshal([]any{args})
if err != nil {
return nil, err
}
raw, err := pic.updateCallWithEP(
ic.MANAGEMENT_CANISTER_PRINCIPAL,
effectivePrincipal,
principal.AnonymousID,
"provisional_create_canister_with_cycles",
payload,
)
if err != nil {
return nil, err
}
var resp struct {
CanisterID principal.Principal `ic:"canister_id"`
}
if err := idl.Unmarshal(raw, []any{&resp}); err != nil {
return nil, err
}
return &resp.CanisterID, nil
}
type ProvisionalCreateCanisterArgument struct {
Settings *ic0.CanisterSettings `ic:"settings"`
SpecifiedID *principal.Principal `ic:"specified_id"`
Amount *int `ic:"amount"`
}