-
Notifications
You must be signed in to change notification settings - Fork 475
Expand file tree
/
Copy pathusing-secrets-multiple-go.go
More file actions
70 lines (56 loc) · 2.04 KB
/
Copy pathusing-secrets-multiple-go.go
File metadata and controls
70 lines (56 loc) · 2.04 KB
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
//go:build wasip1
package main
import (
"log/slog"
protos "github.com/smartcontractkit/chainlink-protos/cre/go/sdk"
"github.com/smartcontractkit/cre-sdk-go/capabilities/scheduler/cron"
"github.com/smartcontractkit/cre-sdk-go/cre"
"github.com/smartcontractkit/cre-sdk-go/cre/wasm"
)
// Config can be an empty struct if you don't need any parameters from config.json.
type Config struct{}
// MyResult can be an empty struct if your workflow doesn't need to return a result.
type MyResult struct{}
const (
SecretAddressName = "SECRET_ADDRESS"
ApiKeyName = "API_KEY"
)
func onCronTrigger(config *Config, runtime cre.Runtime, trigger *cron.Payload) (*MyResult, error) {
logger := runtime.Logger()
// Important: Fetch secrets sequentially, not in parallel.
// The WASM host for CRE runtime does not support parallel runtime.GetSecret() requests.
// Always call GetSecret(), then Await() before making the next GetSecret() call.
// 1. Fetch the first secret
addressPromise := runtime.GetSecret(&protos.SecretRequest{Id: SecretAddressName})
secretAddress, err := addressPromise.Await()
if err != nil {
logger.Error("Failed to get SECRET_ADDRESS", "err", err)
return nil, err
}
// 2. Fetch the second secret (only after the first is complete)
apiKeyPromise := runtime.GetSecret(&protos.SecretRequest{Id: ApiKeyName})
apiKey, err := apiKeyPromise.Await()
if err != nil {
logger.Error("Failed to get API_KEY", "err", err)
return nil, err
}
// 3. Use your secrets
logger.Info("Successfully fetched secrets!",
"address", secretAddress.Value,
"apiKey", apiKey.Value,
)
return &MyResult{}, nil
}
// InitWorkflow is the required entry point for a CRE workflow.
func InitWorkflow(config *Config, logger *slog.Logger, secretsProvider cre.SecretsProvider) (cre.Workflow[*Config], error) {
return cre.Workflow[*Config]{
cre.Handler(
cron.Trigger(&cron.Config{Schedule: "0 */10 * * * *"}),
onCronTrigger,
),
}, nil
}
// main is the entry point for the WASM binary.
func main() {
wasm.NewRunner(cre.ParseJSON[Config]).Run(InitWorkflow)
}