Skip to content

Commit

Permalink
Merge pull request #191 from tonkeeper/ton_diamonds_approve
Browse files Browse the repository at this point in the history
create ton_diamonds approve
  • Loading branch information
mr-tron authored Sep 12, 2023
2 parents fd9a79f + 6bf7853 commit 8e1e629
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 24 deletions.
6 changes: 3 additions & 3 deletions api/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -2465,7 +2465,8 @@
"items": {
"enum": [
"getgems",
"tonkeeper"
"tonkeeper",
"ton.diamonds"
],
"example": "getgems",
"type": "string"
Expand Down Expand Up @@ -3436,8 +3437,7 @@
"items": {
"properties": {
"account": {
"$ref": "#/components/schemas/AccountAddress",
"deprecated": true
"$ref": "#/components/schemas/AccountAddress"
},
"jetton": {
"$ref": "#/components/schemas/JettonPreview"
Expand Down
1 change: 1 addition & 0 deletions api/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3352,6 +3352,7 @@ components:
enum:
- getgems
- tonkeeper
- ton.diamonds
NftItems:
type: object
required:
Expand Down
2 changes: 2 additions & 0 deletions client/oas_json_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions client/oas_schemas_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions client/oas_validators_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 28 additions & 6 deletions pkg/addressbook/addressbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (

"github.com/shopspring/decimal"
"github.com/shurcooL/graphql"
"github.com/tonkeeper/opentonapi/pkg/oas"
"github.com/tonkeeper/opentonapi/pkg/references"
"github.com/tonkeeper/tongo"
"go.uber.org/zap"
"golang.org/x/exp/maps"
Expand Down Expand Up @@ -66,7 +68,7 @@ type KnownCollection struct {
MaxItems int64 `json:"max_items"`
Websites []string `json:"websites,omitempty"`
Social []string `json:"social,omitempty"`
Approvers []string
Approvers []oas.NftItemApprovedByItem
}

type Option func(o *Options)
Expand Down Expand Up @@ -211,6 +213,7 @@ func NewAddressBook(logger *zap.Logger, addressPath, jettonPath, collectionPath
}()

go book.getGGWhitelist(logger)
go book.getTonDiamondsWhitelist()

return book
}
Expand Down Expand Up @@ -292,8 +295,10 @@ func (b *Book) refreshStonfiJettons(logger *zap.Logger) {
}
}

func unique(approvers []string) []string {
sort.Strings(approvers)
func unique(approvers []oas.NftItemApprovedByItem) []oas.NftItemApprovedByItem {
sort.Slice(approvers, func(i, j int) bool {
return approvers[i] < approvers[j]
})
return slices.Compact(approvers)
}

Expand All @@ -315,12 +320,12 @@ func (b *Book) refreshCollections(logger *zap.Logger, collectionPath string) {
if !ok {
// this is a new item, so we only add tonkeeper as approver.
item.Address = accountID.ToRaw()
item.Approvers = unique(append(item.Approvers, "tonkeeper"))
item.Approvers = unique(append(item.Approvers, oas.NftItemApprovedByItemTonkeeper))
b.collections[accountID] = item
continue
}
// this is an existing item, so we merge approvers and remove duplicates adding tonkeeper.
item.Approvers = unique(append(append(currentCollection.Approvers, item.Approvers...), "tonkeeper"))
item.Approvers = unique(append(append(currentCollection.Approvers, item.Approvers...), oas.NftItemApprovedByItemTonkeeper))
b.collections[accountID] = item
}
}
Expand Down Expand Up @@ -376,7 +381,24 @@ func (b *Book) getGGWhitelist(logger *zap.Logger) {
b.mu.Lock()
for _, account := range addresses {
collection := b.collections[account]
collection.Approvers = unique(append(collection.Approvers, "getgems"))
collection.Approvers = unique(append(collection.Approvers, oas.NftItemApprovedByItemGetgems))
b.collections[account] = collection
}
b.mu.Unlock()
return
}
}

func (b *Book) getTonDiamondsWhitelist() {
for {
if len(b.GetKnownCollections()) == 0 {
time.Sleep(time.Second * 10)
continue
}
b.mu.Lock()
for _, account := range references.TonDiamondsCollections {
collection := b.collections[account]
collection.Approvers = unique(append(collection.Approvers, oas.NftItemApprovedByItemTonDiamonds))
b.collections[account] = collection
}
b.mu.Unlock()
Expand Down
11 changes: 6 additions & 5 deletions pkg/addressbook/addressbook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ package addressbook
import (
"reflect"
"testing"

"github.com/tonkeeper/opentonapi/pkg/oas"
)

func Test_unique(t *testing.T) {
tests := []struct {
name string
approvers []string
want []string
approvers []oas.NftItemApprovedByItem
want []oas.NftItemApprovedByItem
}{
{
name: "all good",
approvers: []string{"tonkeeper", "getgems", "getgems", "tonkeeper"},
want: []string{"getgems", "tonkeeper"},
approvers: []oas.NftItemApprovedByItem{oas.NftItemApprovedByItemTonkeeper, oas.NftItemApprovedByItemGetgems, oas.NftItemApprovedByItemGetgems, oas.NftItemApprovedByItemTonkeeper},
want: []oas.NftItemApprovedByItem{oas.NftItemApprovedByItemGetgems, oas.NftItemApprovedByItemTonkeeper},
},
}
for _, tt := range tests {
Expand All @@ -23,7 +25,6 @@ func Test_unique(t *testing.T) {
if !reflect.DeepEqual(newList, tt.want) {
t.Errorf("unique() = %v, want %v", newList, tt.want)
}

})
}
}
7 changes: 1 addition & 6 deletions pkg/api/nft_converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,7 @@ func convertNFT(ctx context.Context, item core.NftItem, book addressBook, metaCa
if item.CollectionAddress != nil {
if cc, prs := book.GetCollectionInfoByAddress(*item.CollectionAddress); prs {
for _, a := range cc.Approvers {
switch a {
case "tonkeeper":
i.ApprovedBy = append(i.ApprovedBy, oas.NftItemApprovedByItemTonkeeper)
case "getgems":
i.ApprovedBy = append(i.ApprovedBy, oas.NftItemApprovedByItemGetgems)
}
i.ApprovedBy = append(i.ApprovedBy, a)
}
}
cInfo, _ := metaCache.getCollectionMeta(ctx, *item.CollectionAddress)
Expand Down
2 changes: 2 additions & 0 deletions pkg/oas/oas_json_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions pkg/oas/oas_schemas_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pkg/oas/oas_validators_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions pkg/references/collections.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package references

import (
"github.com/tonkeeper/tongo"
)

var TonDiamondsCollections = []tongo.AccountID{
tongo.MustParseAccountID("EQAG2BH0JlmFkbMrLEnyn2bIITaOSssd4WdisE4BdFMkZbir"),
tongo.MustParseAccountID("EQB8D8A9OoDoRmL7qVbUBrd_po9vNKcl44HCSw6b-c3nvcj9"),
tongo.MustParseAccountID("EQD7eDxP_wLX18kr8uyYfs6srlgoNMfRbXNYM8dmMXT2vxwk"),
tongo.MustParseAccountID("EQDia1dY5G5_J4LDaqW9xPyLw2T3s25y6U1W1nSkQ3BRzM7x"),
tongo.MustParseAccountID("EQCoPMFxArXwkKO9zH6IU-ZJ0ahFU2nih9rPvN7_YWcxRmhb"),
}

0 comments on commit 8e1e629

Please sign in to comment.