fix: proper openseaV2 detailed nft unmarshalling

This commit is contained in:
Dario Gabriel Lipicar 2023-09-12 11:10:55 -03:00 committed by dlipicar
parent 8d1992d2e1
commit eb8f3173e4
3 changed files with 104 additions and 25 deletions

View File

@ -215,13 +215,13 @@ func (o *ClientV2) fetchDetailedAssets(uniqueIDs []thirdparty.CollectibleUniqueI
return nil, fmt.Errorf("invalid json: %s", string(body))
}
nft := DetailedNFT{}
err = json.Unmarshal(body, &nft)
nftContainer := DetailedNFTContainer{}
err = json.Unmarshal(body, &nftContainer)
if err != nil {
return nil, err
}
assets = append(assets, nft.toCommon(id.ContractID.ChainID))
assets = append(assets, nftContainer.NFT.toCommon(id.ContractID.ChainID))
}
return assets, nil

View File

@ -0,0 +1,48 @@
package opensea
import (
"encoding/json"
"math/big"
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/status-im/status-go/services/wallet/bigint"
"github.com/stretchr/testify/assert"
)
func TestUnmarshallDetailedAsset(t *testing.T) {
nftJSON := `{"nft": {"identifier": "7", "collection": "test-cool-cats-v3", "contract": "0x9a95631794a42d30c47f214fbe02a72585df35e1", "token_standard": "erc721", "name": "Cool Cat #7", "description": "Cool Cats also live on Rinkeby for testing purposes!", "image_url": "https://i.seadn.io/gae/blGPn5DZZXjGkrwx5EICajgepVjogYxNEnvDWBECCiFy7OXJkaW--d9OgO4gXqZzkFhd87pd_ckOyS-nEGLVymPjccavrznJGJ4XgA?w=500&auto=format", "metadata_url": "https://BetaTestPetMetadataServer.grampabacon.repl.co/cat/7", "created_at": "2022-10-05T14:29:31.966382", "updated_at": "2022-10-05T14:29:43.889730", "is_disabled": false, "is_nsfw": false, "is_suspicious": false, "creator": "0x772b92a6abe5129f8ef91d164cc757dd9bbd0bc7", "traits": [{"trait_type": "tier", "display_type": null, "max_value": null, "trait_count": 0, "order": null, "value": "cool_2"}], "owners": [{"address": "0x0000000000000000000000000000000000000001", "quantity": 1}], "rarity": null}}`
expectedNFT := DetailedNFT{
TokenID: &bigint.BigInt{Int: big.NewInt(7)},
Collection: "test-cool-cats-v3",
Contract: common.HexToAddress("0x9a95631794a42d30c47f214fbe02a72585df35e1"),
TokenStandard: "erc721",
Name: "Cool Cat #7",
Description: "Cool Cats also live on Rinkeby for testing purposes!",
ImageURL: "https://i.seadn.io/gae/blGPn5DZZXjGkrwx5EICajgepVjogYxNEnvDWBECCiFy7OXJkaW--d9OgO4gXqZzkFhd87pd_ckOyS-nEGLVymPjccavrznJGJ4XgA?w=500&auto=format",
MetadataURL: "https://BetaTestPetMetadataServer.grampabacon.repl.co/cat/7",
Owners: []OwnerV2{
{
Address: common.HexToAddress("0x0000000000000000000000000000000000000001"),
Quantity: &bigint.BigInt{Int: big.NewInt(1)},
},
},
Traits: []TraitV2{
{
TraitType: "tier",
DisplayType: "",
MaxValue: "",
TraitCount: 0,
Order: "",
Value: "cool_2",
},
},
}
nftContainer := DetailedNFTContainer{}
err := json.Unmarshal([]byte(nftJSON), &nftContainer)
assert.NoError(t, err)
assert.Equal(t, expectedNFT, nftContainer.NFT)
}

View File

@ -77,10 +77,21 @@ type NFT struct {
MetadataURL string `json:"metadata_url"`
}
type DetailedNFTContainer struct {
NFT DetailedNFT `json:"nft"`
}
type DetailedNFT struct {
NFT
Owners []OwnerV2 `json:"owners"`
Traits []TraitV2 `json:"traits"`
TokenID *bigint.BigInt `json:"identifier"`
Collection string `json:"collection"`
Contract common.Address `json:"contract"`
TokenStandard string `json:"token_standard"`
Name string `json:"name"`
Description string `json:"description"`
ImageURL string `json:"image_url"`
MetadataURL string `json:"metadata_url"`
Owners []OwnerV2 `json:"owners"`
Traits []TraitV2 `json:"traits"`
}
type OwnerV2 struct {
@ -107,22 +118,6 @@ func (c *NFT) id(chainID walletCommon.ChainID) thirdparty.CollectibleUniqueID {
}
}
func openseaV2ToCollectibleTraits(traits []TraitV2) []thirdparty.CollectibleTrait {
ret := make([]thirdparty.CollectibleTrait, 0, len(traits))
caser := cases.Title(language.Und, cases.NoLower)
for _, orig := range traits {
dest := thirdparty.CollectibleTrait{
TraitType: strings.Replace(orig.TraitType, "_", " ", 1),
Value: caser.String(string(orig.Value)),
DisplayType: orig.DisplayType,
MaxValue: orig.MaxValue,
}
ret = append(ret, dest)
}
return ret
}
func (c *NFT) toCollectiblesData(chainID walletCommon.ChainID) thirdparty.CollectibleData {
return thirdparty.CollectibleData{
ID: c.id(chainID),
@ -143,10 +138,46 @@ func (c *NFT) toCommon(chainID walletCommon.ChainID) thirdparty.FullCollectibleD
}
}
func (c *DetailedNFT) toCommon(chainID walletCommon.ChainID) thirdparty.FullCollectibleData {
fullData := c.NFT.toCommon(chainID)
fullData.CollectibleData.Traits = openseaV2ToCollectibleTraits(c.Traits)
func openseaV2ToCollectibleTraits(traits []TraitV2) []thirdparty.CollectibleTrait {
ret := make([]thirdparty.CollectibleTrait, 0, len(traits))
caser := cases.Title(language.Und, cases.NoLower)
for _, orig := range traits {
dest := thirdparty.CollectibleTrait{
TraitType: strings.Replace(orig.TraitType, "_", " ", 1),
Value: caser.String(string(orig.Value)),
DisplayType: orig.DisplayType,
MaxValue: orig.MaxValue,
}
ret = append(ret, dest)
}
return ret
}
func (c *DetailedNFT) id(chainID walletCommon.ChainID) thirdparty.CollectibleUniqueID {
return thirdparty.CollectibleUniqueID{
ContractID: thirdparty.ContractID{
ChainID: chainID,
Address: c.Contract,
},
TokenID: c.TokenID,
}
}
func (c *DetailedNFT) toCollectiblesData(chainID walletCommon.ChainID) thirdparty.CollectibleData {
return thirdparty.CollectibleData{
ID: c.id(chainID),
Provider: OpenseaV2ID,
Name: c.Name,
Description: c.Description,
ImageURL: c.ImageURL,
AnimationURL: c.ImageURL,
Traits: openseaV2ToCollectibleTraits(c.Traits),
TokenURI: c.MetadataURL,
}
}
func (c *DetailedNFT) toCommon(chainID walletCommon.ChainID) thirdparty.FullCollectibleData {
return thirdparty.FullCollectibleData{
CollectibleData: c.toCollectiblesData(chainID),
CollectionData: nil,