-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
815 additions
and
937 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package pocketic | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/aviate-labs/agent-go/candid/idl" | ||
"github.com/aviate-labs/agent-go/ic" | ||
ic0 "github.com/aviate-labs/agent-go/ic/ic" | ||
"github.com/aviate-labs/agent-go/principal" | ||
) | ||
|
||
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 | ||
} | ||
|
||
// CreateCanister creates a canister with default settings as the anonymous principal. | ||
func (pic PocketIC) CreateCanister() (*principal.Principal, error) { | ||
payload, err := idl.Marshal([]any{ProvisionalCreateCanisterArgument{}}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
raw, err := pic.updateCallWithEP( | ||
ic.MANAGEMENT_CANISTER_PRINCIPAL, | ||
new(RawEffectivePrincipalNone), | ||
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package pocketic | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
type None struct{} | ||
|
||
func (n None) MarshalJSON() ([]byte, error) { | ||
return json.Marshal("None") | ||
} | ||
|
||
func (n None) UnmarshalJSON(bytes []byte) error { | ||
var s string | ||
if err := json.Unmarshal(bytes, &s); err != nil { | ||
return err | ||
} | ||
if s != "None" { | ||
return fmt.Errorf("expected None, got %s", s) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package pocketic | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
) | ||
|
||
func TestNone(t *testing.T) { | ||
var none None | ||
raw, err := json.Marshal(none) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if string(raw) != "\"None\"" { | ||
t.Fatalf("expected None, got %s", string(raw)) | ||
} | ||
var decoded None | ||
if err := json.Unmarshal(raw, &decoded); err != nil { | ||
t.Fatal(err) | ||
} | ||
} |
Oops, something went wrong.