From 69bce65ae27816c265b3b42f316329ab8335e841 Mon Sep 17 00:00:00 2001 From: Alexey Kostenko Date: Thu, 12 Dec 2024 17:45:56 +0300 Subject: [PATCH 1/2] extra currency value preview --- pkg/api/event_converters.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/api/event_converters.go b/pkg/api/event_converters.go index 2db1ac19..0dfd6fec 100644 --- a/pkg/api/event_converters.go +++ b/pkg/api/event_converters.go @@ -167,11 +167,12 @@ func (h *Handler) convertActionExtraCurrencyTransfer(t *bath.ExtraCurrencyTransf Image: meta.Image, }, }) + value := ScaleJettons(big.Int(t.Amount), meta.Decimals) simplePreview := oas.ActionSimplePreview{ Name: "Extra Currency Transfer", Description: "", // TODO: add description Accounts: distinctAccounts(viewer, h.addressBook, &t.Sender, &t.Recipient), - // TODO: add value + Value: oas.NewOptString(fmt.Sprintf("%v %v", value.String(), meta.Name)), } return action, simplePreview } From 062850b0658392bddb2e67bdeab3e095cfb6fc32 Mon Sep 17 00:00:00 2001 From: Alexey Kostenko Date: Thu, 12 Dec 2024 17:56:20 +0300 Subject: [PATCH 2/2] extra currency preview --- api/openapi.json | 23 ++++---- api/openapi.yml | 36 ++++++------- pkg/api/account_converters.go | 13 ++--- pkg/api/event_converters.go | 3 +- pkg/oas/oas_json_gen.go | 91 +++++++++++++------------------- pkg/oas/oas_schemas_gen.go | 49 +++++++---------- pkg/references/extra_currency.go | 27 +++++++--- 7 files changed, 108 insertions(+), 134 deletions(-) diff --git a/api/openapi.json b/api/openapi.json index 6132e318..6f453d29 100644 --- a/api/openapi.json +++ b/api/openapi.json @@ -3128,6 +3128,11 @@ "example": 5, "type": "integer" }, + "id": { + "example": 239, + "format": "int32", + "type": "integer" + }, "image": { "example": "https://cache.tonapi.io/images/extra.jpg", "type": "string" @@ -3138,6 +3143,7 @@ } }, "required": [ + "id", "symbol", "decimals", "image" @@ -3267,24 +3273,13 @@ "type": "string", "x-js-format": "bigint" }, - "decimals": { - "example": 5, - "type": "integer" - }, - "id": { - "example": 239, - "format": "int32", - "type": "integer" - }, - "name": { - "example": "FMS", - "type": "string" + "preview": { + "$ref": "#/components/schemas/EcPreview" } }, "required": [ - "id", "amount", - "decimals" + "preview" ], "type": "object" }, diff --git a/api/openapi.yml b/api/openapi.yml index 1fccebc5..0b840105 100644 --- a/api/openapi.yml +++ b/api/openapi.yml @@ -5880,10 +5880,15 @@ components: EcPreview: type: object required: + - id - symbol - decimals - image properties: + id: + type: integer + example: 239 + format: int32 symbol: type: string example: FMS @@ -7469,26 +7474,17 @@ components: format: int64 example: 1668436763 ExtraCurrency: - type: object - required: - - id - - amount - - decimals - properties: - id: - type: integer - example: 239 - format: int32 - amount: - type: string - x-js-format: bigint - example: "1000000000" - name: - type: string - example: FMS - decimals: - type: integer - example: 5 + type: object + required: + - amount + - preview + properties: + amount: + type: string + x-js-format: bigint + example: "1000000000" + preview: + $ref: '#/components/schemas/EcPreview' SourceFile: type: object required: diff --git a/pkg/api/account_converters.go b/pkg/api/account_converters.go index 656c2883..1087e8f4 100644 --- a/pkg/api/account_converters.go +++ b/pkg/api/account_converters.go @@ -81,12 +81,13 @@ func convertExtraCurrencies(extraBalances core.ExtraCurrencies) []oas.ExtraCurre amount := big.Int(v) meta := references.GetExtraCurrencyMeta(k) cur := oas.ExtraCurrency{ - ID: k, - Amount: amount.String(), - Decimals: meta.Decimals, - } - if meta.Name != "" { - cur.Name.SetTo(meta.Name) + Amount: amount.String(), + Preview: oas.EcPreview{ + ID: k, + Symbol: meta.Symbol, + Decimals: meta.Decimals, + Image: meta.Image, + }, } res = append(res, cur) } diff --git a/pkg/api/event_converters.go b/pkg/api/event_converters.go index 0dfd6fec..b7e44539 100644 --- a/pkg/api/event_converters.go +++ b/pkg/api/event_converters.go @@ -162,6 +162,7 @@ func (h *Handler) convertActionExtraCurrencyTransfer(t *bath.ExtraCurrencyTransf Sender: convertAccountAddress(t.Sender, h.addressBook), EncryptedComment: convertEncryptedComment(t.EncryptedComment), Currency: oas.EcPreview{ + ID: t.CurrencyID, Symbol: meta.Symbol, Decimals: meta.Decimals, Image: meta.Image, @@ -172,7 +173,7 @@ func (h *Handler) convertActionExtraCurrencyTransfer(t *bath.ExtraCurrencyTransf Name: "Extra Currency Transfer", Description: "", // TODO: add description Accounts: distinctAccounts(viewer, h.addressBook, &t.Sender, &t.Recipient), - Value: oas.NewOptString(fmt.Sprintf("%v %v", value.String(), meta.Name)), + Value: oas.NewOptString(fmt.Sprintf("%v %v", value.String(), meta.Symbol)), } return action, simplePreview } diff --git a/pkg/oas/oas_json_gen.go b/pkg/oas/oas_json_gen.go index 4d6b3b3a..9803a242 100644 --- a/pkg/oas/oas_json_gen.go +++ b/pkg/oas/oas_json_gen.go @@ -14359,6 +14359,10 @@ func (s *EcPreview) Encode(e *jx.Encoder) { // encodeFields encodes fields. func (s *EcPreview) encodeFields(e *jx.Encoder) { + { + e.FieldStart("id") + e.Int32(s.ID) + } { e.FieldStart("symbol") e.Str(s.Symbol) @@ -14373,10 +14377,11 @@ func (s *EcPreview) encodeFields(e *jx.Encoder) { } } -var jsonFieldsNameOfEcPreview = [3]string{ - 0: "symbol", - 1: "decimals", - 2: "image", +var jsonFieldsNameOfEcPreview = [4]string{ + 0: "id", + 1: "symbol", + 2: "decimals", + 3: "image", } // Decode decodes EcPreview from json. @@ -14388,8 +14393,20 @@ func (s *EcPreview) Decode(d *jx.Decoder) error { if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error { switch string(k) { - case "symbol": + case "id": requiredBitSet[0] |= 1 << 0 + if err := func() error { + v, err := d.Int32() + s.ID = int32(v) + if err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"id\"") + } + case "symbol": + requiredBitSet[0] |= 1 << 1 if err := func() error { v, err := d.Str() s.Symbol = string(v) @@ -14401,7 +14418,7 @@ func (s *EcPreview) Decode(d *jx.Decoder) error { return errors.Wrap(err, "decode field \"symbol\"") } case "decimals": - requiredBitSet[0] |= 1 << 1 + requiredBitSet[0] |= 1 << 2 if err := func() error { v, err := d.Int() s.Decimals = int(v) @@ -14413,7 +14430,7 @@ func (s *EcPreview) Decode(d *jx.Decoder) error { return errors.Wrap(err, "decode field \"decimals\"") } case "image": - requiredBitSet[0] |= 1 << 2 + requiredBitSet[0] |= 1 << 3 if err := func() error { v, err := d.Str() s.Image = string(v) @@ -14434,7 +14451,7 @@ func (s *EcPreview) Decode(d *jx.Decoder) error { // Validate required fields. var failures []validate.FieldError for i, mask := range [1]uint8{ - 0b00000111, + 0b00001111, } { if result := (requiredBitSet[i] & mask) ^ mask; result != 0 { // Mask only required fields and check equality to mask using XOR. @@ -15680,31 +15697,19 @@ func (s *ExtraCurrency) Encode(e *jx.Encoder) { // encodeFields encodes fields. func (s *ExtraCurrency) encodeFields(e *jx.Encoder) { - { - e.FieldStart("id") - e.Int32(s.ID) - } { e.FieldStart("amount") e.Str(s.Amount) } { - if s.Name.Set { - e.FieldStart("name") - s.Name.Encode(e) - } - } - { - e.FieldStart("decimals") - e.Int(s.Decimals) + e.FieldStart("preview") + s.Preview.Encode(e) } } -var jsonFieldsNameOfExtraCurrency = [4]string{ - 0: "id", - 1: "amount", - 2: "name", - 3: "decimals", +var jsonFieldsNameOfExtraCurrency = [2]string{ + 0: "amount", + 1: "preview", } // Decode decodes ExtraCurrency from json. @@ -15716,20 +15721,8 @@ func (s *ExtraCurrency) Decode(d *jx.Decoder) error { if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error { switch string(k) { - case "id": - requiredBitSet[0] |= 1 << 0 - if err := func() error { - v, err := d.Int32() - s.ID = int32(v) - if err != nil { - return err - } - return nil - }(); err != nil { - return errors.Wrap(err, "decode field \"id\"") - } case "amount": - requiredBitSet[0] |= 1 << 1 + requiredBitSet[0] |= 1 << 0 if err := func() error { v, err := d.Str() s.Amount = string(v) @@ -15740,27 +15733,15 @@ func (s *ExtraCurrency) Decode(d *jx.Decoder) error { }(); err != nil { return errors.Wrap(err, "decode field \"amount\"") } - case "name": - if err := func() error { - s.Name.Reset() - if err := s.Name.Decode(d); err != nil { - return err - } - return nil - }(); err != nil { - return errors.Wrap(err, "decode field \"name\"") - } - case "decimals": - requiredBitSet[0] |= 1 << 3 + case "preview": + requiredBitSet[0] |= 1 << 1 if err := func() error { - v, err := d.Int() - s.Decimals = int(v) - if err != nil { + if err := s.Preview.Decode(d); err != nil { return err } return nil }(); err != nil { - return errors.Wrap(err, "decode field \"decimals\"") + return errors.Wrap(err, "decode field \"preview\"") } default: return d.Skip() @@ -15772,7 +15753,7 @@ func (s *ExtraCurrency) Decode(d *jx.Decoder) error { // Validate required fields. var failures []validate.FieldError for i, mask := range [1]uint8{ - 0b00001011, + 0b00000011, } { if result := (requiredBitSet[i] & mask) ^ mask; result != 0 { // Mask only required fields and check equality to mask using XOR. diff --git a/pkg/oas/oas_schemas_gen.go b/pkg/oas/oas_schemas_gen.go index 47e7645e..d6f50821 100644 --- a/pkg/oas/oas_schemas_gen.go +++ b/pkg/oas/oas_schemas_gen.go @@ -5125,11 +5125,17 @@ func (s *DomainRenewAction) SetRenewer(val AccountAddress) { // Ref: #/components/schemas/EcPreview type EcPreview struct { + ID int32 `json:"id"` Symbol string `json:"symbol"` Decimals int `json:"decimals"` Image string `json:"image"` } +// GetID returns the value of ID. +func (s *EcPreview) GetID() int32 { + return s.ID +} + // GetSymbol returns the value of Symbol. func (s *EcPreview) GetSymbol() string { return s.Symbol @@ -5145,6 +5151,11 @@ func (s *EcPreview) GetImage() string { return s.Image } +// SetID sets the value of ID. +func (s *EcPreview) SetID(val int32) { + s.ID = val +} + // SetSymbol sets the value of Symbol. func (s *EcPreview) SetSymbol(val string) { s.Symbol = val @@ -5467,15 +5478,8 @@ func (s *Event) SetInProgress(val bool) { // Ref: #/components/schemas/ExtraCurrency type ExtraCurrency struct { - ID int32 `json:"id"` - Amount string `json:"amount"` - Name OptString `json:"name"` - Decimals int `json:"decimals"` -} - -// GetID returns the value of ID. -func (s *ExtraCurrency) GetID() int32 { - return s.ID + Amount string `json:"amount"` + Preview EcPreview `json:"preview"` } // GetAmount returns the value of Amount. @@ -5483,19 +5487,9 @@ func (s *ExtraCurrency) GetAmount() string { return s.Amount } -// GetName returns the value of Name. -func (s *ExtraCurrency) GetName() OptString { - return s.Name -} - -// GetDecimals returns the value of Decimals. -func (s *ExtraCurrency) GetDecimals() int { - return s.Decimals -} - -// SetID sets the value of ID. -func (s *ExtraCurrency) SetID(val int32) { - s.ID = val +// GetPreview returns the value of Preview. +func (s *ExtraCurrency) GetPreview() EcPreview { + return s.Preview } // SetAmount sets the value of Amount. @@ -5503,14 +5497,9 @@ func (s *ExtraCurrency) SetAmount(val string) { s.Amount = val } -// SetName sets the value of Name. -func (s *ExtraCurrency) SetName(val OptString) { - s.Name = val -} - -// SetDecimals sets the value of Decimals. -func (s *ExtraCurrency) SetDecimals(val int) { - s.Decimals = val +// SetPreview sets the value of Preview. +func (s *ExtraCurrency) SetPreview(val EcPreview) { + s.Preview = val } // Ref: #/components/schemas/ExtraCurrencyTransferAction diff --git a/pkg/references/extra_currency.go b/pkg/references/extra_currency.go index b79f3b99..f9f2a22e 100644 --- a/pkg/references/extra_currency.go +++ b/pkg/references/extra_currency.go @@ -1,29 +1,40 @@ package references +import "fmt" + const DefaultExtraCurrencyDecimals = 9 type ExtraCurrencyMeta struct { - Name string + Decimals int Symbol string Image string - Decimals int } var extraCurrencies = map[int32]ExtraCurrencyMeta{ 239: { - Name: "FMS", Decimals: 5, Symbol: "FMS", }, } func GetExtraCurrencyMeta(id int32) ExtraCurrencyMeta { + res := ExtraCurrencyMeta{ + Decimals: DefaultExtraCurrencyDecimals, + Symbol: fmt.Sprintf("$%d", id), + Image: Placeholder, + } meta, ok := extraCurrencies[id] - if ok { - return meta + if !ok { + return res } - return ExtraCurrencyMeta{ - Decimals: DefaultExtraCurrencyDecimals, - // TODO: add default placeholders + if meta.Decimals > 0 { + res.Decimals = meta.Decimals + } + if meta.Image != "" { + res.Image = meta.Image + } + if meta.Symbol != "" { + res.Symbol = meta.Symbol } + return res }