|
| 1 | +package ton |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "math/big" |
| 7 | + |
| 8 | + "github.com/xssnick/tonutils-go/address" |
| 9 | + "github.com/xssnick/tonutils-go/tvm/cell" |
| 10 | + |
| 11 | + "github.com/smartcontractkit/mcms/types" |
| 12 | +) |
| 13 | + |
| 14 | +func ValidateAdditionalFields(additionalFields json.RawMessage) error { |
| 15 | + fields := AdditionalFields{ |
| 16 | + Value: big.NewInt(0), |
| 17 | + } |
| 18 | + if len(additionalFields) != 0 { |
| 19 | + if err := json.Unmarshal(additionalFields, &fields); err != nil { |
| 20 | + return fmt.Errorf("failed to unmarshal TON additional fields: %w", err) |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + if err := fields.Validate(); err != nil { |
| 25 | + return err |
| 26 | + } |
| 27 | + |
| 28 | + return nil |
| 29 | +} |
| 30 | + |
| 31 | +type AdditionalFields struct { |
| 32 | + Value *big.Int `json:"value"` |
| 33 | +} |
| 34 | + |
| 35 | +// Validate ensures the TON-specific fields are correct |
| 36 | +func (f AdditionalFields) Validate() error { |
| 37 | + if f.Value == nil || f.Value.Sign() < 0 { |
| 38 | + return fmt.Errorf("invalid TON value: %v", f.Value) |
| 39 | + } |
| 40 | + |
| 41 | + return nil |
| 42 | +} |
| 43 | + |
| 44 | +func NewTransaction( |
| 45 | + to address.Address, |
| 46 | + body *cell.Slice, |
| 47 | + value *big.Int, |
| 48 | + contractType string, |
| 49 | + tags []string, |
| 50 | +) (types.Transaction, error) { |
| 51 | + additionalFields, err := json.Marshal(AdditionalFields{ |
| 52 | + Value: value, |
| 53 | + }) |
| 54 | + if err != nil { |
| 55 | + return types.Transaction{}, fmt.Errorf("failed to marshal additional fields: %w", err) |
| 56 | + } |
| 57 | + |
| 58 | + bodyCell, err := body.ToCell() |
| 59 | + if err != nil { |
| 60 | + return types.Transaction{}, fmt.Errorf("failed to convert body to cell: %w", err) |
| 61 | + } |
| 62 | + data := bodyCell.ToBOC() |
| 63 | + |
| 64 | + return types.Transaction{ |
| 65 | + To: to.String(), |
| 66 | + Data: data, |
| 67 | + AdditionalFields: additionalFields, |
| 68 | + OperationMetadata: types.OperationMetadata{ |
| 69 | + ContractType: contractType, |
| 70 | + Tags: tags, |
| 71 | + }, |
| 72 | + }, nil |
| 73 | +} |
0 commit comments