This repository has been archived by the owner on May 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
clone.go
90 lines (84 loc) · 2.02 KB
/
clone.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
// Copyright (c) 2023 Blockwatch Data Inc.
// Author: alex@blockwatch.cc, abdul@blockwatch.cc
package alpha
import (
"bytes"
"blockwatch.cc/tzgo/internal/compose"
"gopkg.in/yaml.v3"
)
func (e *Engine) Clone(ctx compose.Context, ops []compose.Op, cfg compose.CloneConfig) ([]byte, error) {
spec := Spec{
Version: VERSION,
Pipelines: []Pipeline{{
Name: cfg.Name,
Tasks: make([]Task, 0, len(ops)),
}},
}
for _, op := range ops {
switch op.Type {
case "origination":
s := &Script{
Code: &Code{},
Storage: &Storage{
Args: op.Args,
},
}
switch cfg.Mode {
case compose.CloneModeFile:
s.Code.File = op.Url + "#code"
if s.Storage.Args == nil {
s.Storage.File = op.Url + "#storage"
}
case compose.CloneModeJson, compose.CloneModeBinary:
s.Code.Value = op.PackedCode
if s.Storage.Args == nil {
s.Storage.Value = op.PackedStorage
}
case compose.CloneModeUrl:
s.Code.Url = op.Url + "#code"
if s.Storage.Args == nil {
s.Storage.Url = op.Url + "#storage"
}
}
spec.Pipelines[0].Tasks = append(spec.Pipelines[0].Tasks, Task{
Type: "deploy",
Alias: cfg.Name,
Source: op.Sender,
Amount: uint64(op.Amount * 1_000_000),
Script: s,
})
case "transaction":
typ := "transfer"
var p *Params
if op.Params != nil {
typ = "call"
p = &Params{
Entrypoint: op.Params.Entrypoint,
Args: op.Args,
}
if p.Args == nil {
switch cfg.Mode {
case compose.CloneModeFile:
p.File = op.Url
case compose.CloneModeJson, compose.CloneModeBinary:
p.Value = op.PackedParams
case compose.CloneModeUrl:
p.Url = op.Url
}
}
}
spec.Pipelines[0].Tasks = append(spec.Pipelines[0].Tasks, Task{
Type: typ,
Source: op.Sender,
Destination: "$" + cfg.Name,
Amount: uint64(op.Amount * 1_000_000),
Params: p,
})
}
}
buf := bytes.NewBuffer(nil)
enc := yaml.NewEncoder(buf)
enc.SetIndent(2)
err := enc.Encode(spec)
return buf.Bytes(), err
}