Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardocustodio committed Jul 2, 2024
1 parent ff080ee commit d2f490b
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 5 deletions.
32 changes: 29 additions & 3 deletions src/Services/Processor/Substrate/Codec/Decoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,29 @@ public function collectionStorageKey(string $data): array
];
}

public function getValue(array $data, array $keys, ?string $prefix = null, ?string $suffix = null): mixed
{
foreach ($keys as $key) {
$keyValue = $prefix ? "{$prefix}.{$key}" : $key;
$keyValue .= $suffix ? ".{$suffix}" : '';

if (Arr::has($data, $keyValue)) {
return Arr::get($data, $keyValue);
}
}

return null;
}

public function collectionStorageData(string $data): array
{
$decoded = $this->codec->process('CollectionStorageData', new ScaleBytes($data));
$decoded = $this->codec->process(isRunningLatest() ? 'CollectionStorageDataV1010' : 'CollectionStorageData', new ScaleBytes($data));

return [
'owner' => ($owner = Arr::get($decoded, 'owner')) !== null ? HexConverter::prefix($owner) : null,
'maxTokenCount' => ($value = Arr::get($decoded, 'policy.mint.maxTokenCount')) !== null ? gmp_strval($value) : null,
'maxTokenSupply' => ($value = Arr::get($decoded, 'policy.mint.maxTokenSupply')) !== null ? gmp_strval($value) : null,
'forceSingleMint' => Arr::get($decoded, 'policy.mint.forceSingleMint'),
'forceSingleMint' => $this->getValue($decoded, ['forceSingleMint', 'forceCollapsingSupply'], 'policy.mint'),
'burn' => Arr::get($decoded, 'policy.burn'),
'isFrozen' => Arr::get($decoded, 'policy.transfer.isFrozen'),
'royaltyBeneficiary' => ($beneficiary = Arr::get($decoded, 'policy.market.royalty.beneficiary')) !== null ? HexConverter::prefix($beneficiary) : null,
Expand All @@ -214,7 +228,7 @@ public function tokenStorageKey(string $data): array

public function tokenStorageData(string $data): array
{
$decoded = $this->codec->process('TokenStorageData', new ScaleBytes($data));
$decoded = $this->codec->process(isRunningLatest() ? 'TokenStorageDataV1010' : 'TokenStorageData', new ScaleBytes($data));

$cap = TokenMintCapType::tryFrom(collect(Arr::get($decoded, 'cap'))->keys()->first()) ?? TokenMintCapType::INFINITE;
$capSupply = Arr::get($decoded, 'cap.Supply') ?? Arr::get($decoded, 'cap.CollapsingSupply');
Expand All @@ -232,10 +246,19 @@ public function tokenStorageData(string $data): array
'isCurrency' => $isCurrency,
'listingForbidden' => Arr::get($decoded, 'listingForbidden'),
'minimumBalance' => gmp_strval(Arr::get($decoded, 'minimumBalance')),
// TODO: unitPrice and mintDeposit don't exists anymore on v1010
'unitPrice' => gmp_strval($unitPrice),
'mintDeposit' => gmp_strval(Arr::get($decoded, 'mintDeposit')),
'attributeCount' => gmp_strval(Arr::get($decoded, 'attributeCount')),
// TODO: Implement new v1010 fields
// 'requiresDeposit' => Arr::get($decoded, 'requiresDeposit'),
// 'creationDepositDepositor' => Arr::get($decoded, 'creationDeposit.depositor'),
// 'creationDepositAmount' => gmp_strval(Arr::get($decoded, 'creationDeposit.amount')),
// 'ownerDeposit' => gmp_strval(Arr::get($decoded, 'ownerDeposit')),
// 'infusion' => gmp_strval(Arr::get($decoded, 'infusion')),
// 'anyoneCanInfuse' => Arr::get($decoded, 'anyoneCanInfuse'),
];

}

public function collectionAccountStorageKey(string $data): array
Expand Down Expand Up @@ -303,6 +326,9 @@ public function tokenAccountStorageData(string $data): array
'namedReserves' => $namedReserves,
'approvals' => $approvals,
'isFrozen' => Arr::get($decoded, 'isFrozen'),
// TODO: To implement at v1010
// 'depositDepositor' => Arr::get($decoded, 'deposit.depositor'),
// 'depositAmount' => gmp_strval(Arr::get($decoded, 'deposit.amount')),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,31 @@
},
"TransferPolicy": {
"isFrozen": "bool"
},
"CollectionStorageDataV1010": {
"owner": "AccountId",
"policy": "CollectionPolicyV1010",
"tokenCount": "Compact",
"attributeCount": "Compact",
"creationDeposit": "CreationDeposit",
"totalDeposit": "Compact",
"explicitRoyaltyCurrencies": "Vec<MultiTokensAssetId>",
"totalInfusion": "Compact"
},
"CollectionPolicyV1010": {
"mint": "MintPolicyV1010",
"burn": "Null",
"transfer": "TransferPolicy",
"attribute": "Null",
"market": "RoyaltyPolicy"
},
"MintPolicyV1010": {
"maxTokenCount": "Option<u64>",
"maxTokenSupply": "Option<u128>",
"forceCollapsingSupply": "bool"
},
"CreationDeposit": {
"depositor": "AccountId",
"amount": "Compact<u128>"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@
"approvals": "BTreeMap<AccountId,TokenApproval>",
"isFrozen": "bool"
},
"TokenAccountsStorageDataV1010": {
"balance": "Compact",
"reservedBalance": "Compact",
"lockedBalance": "Compact",
"namedReserves": "BTreeMap<[u8;8],u128>",
"locks": "BTreeMap<[u8;8],u128>",
"approvals": "BTreeMap<AccountId,TokenApproval>",
"isFrozen": "bool",
"deposits": "Option<CreationDeposit>"
},
"TokenApproval": {
"amount": "Compact",
"expiration": "Option<BlockNumber>"
Expand Down
47 changes: 45 additions & 2 deletions src/Services/Processor/Substrate/Codec/Types/TokenStorage.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,48 @@
"name": "Bytes",
"symbol": "Bytes",
"location": "Option<XcmV1MultiLocation>"
}
}
},
"TokenStorageDataV1010": {
"supply": "Compact<u128>",
"cap": "Option<TokenCapV1010>",
"freezeState": "Option<FreezeState>",
"requiresDeposit": "bool",
"creationDeposit": "TokenCreationDeposit",
"ownerDeposit": "Compact<u128>",
"totalTokenAccountDeposit": "Compact<u128>",
"attributeCount": "Compact<u32>",
"accountCount": "Compact<u32>",
"marketBehavior": "Option<TokenMarketBehaviorV1010>",
"listingForbidden": "bool",
"metadata": "TokenMetadataV1010",
"infusion": "u128",
"anyoneCanInfuse": "bool",
"groups": "Vec<u128>"
},
"TokenMetadataV1010": {
"decimalCount": "u8",
"name": "Bytes",
"symbol": "Bytes",
"foreign": "Option<XcmV1MultiLocation>"
},
"TokenCapV1010": {
"_enum": {
"Supply": "Compact<u128>",
"CollapsingSupply": "Compact<u128>"
}
},
"TokenMarketBehaviorV1010": {
"_enum": {
"HasRoyalty": "RoyaltyPolicyDescriptorV1010",
"IsCurrency": "Null"
}
},
"RoyaltyPolicyDescriptorV1010": {
"beneficiary": "AccountId",
"percentage": "Compact<Perbill>"
},
"TokenCreationDeposit": {
"depositor": "Option<AccountId>",
"amount": "Compact<u128>"
}
}
15 changes: 15 additions & 0 deletions src/Services/Processor/Substrate/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ public function __construct(?Substrate $serializationService = null)
$this->tokenService = new TokenService($this->walletService);
}

public function getValue(array $data, array $keys): mixed
{
foreach ($keys as $key) {
if (Arr::has($data, $key)) {
return Arr::get($data, $key);
}
}

return null;
}

/**
* Store collections.
*/
Expand Down Expand Up @@ -417,6 +428,10 @@ public function attributesStorages(array $data, bool $hotSync = false): void

foreach ($data as [$key, $attribute]) {
$attributeKey = $this->serializationService->decode('attributeStorageKey', $key);
// TODO: We dont use but we could decode the storage of an attribute to get
// value: Bytes
// deposit: Compact<u128>
// depositor: Option<AccountId>
$attributeData = $this->serializationService->decode('bytes', $attribute);

$collection = $this->getCachedCollection(
Expand Down
11 changes: 11 additions & 0 deletions src/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ function network(): NetworkType
}
}

if (!function_exists('isRunningLatest')) {
/**
* Check if the network is matrix.
*/
function isRunningLatest(): bool
{
return networkConfig('spec-version') === 1010;
}
}


if (!function_exists('isMainnet')) {
/**
* Check if the network is mainnet.
Expand Down

0 comments on commit d2f490b

Please sign in to comment.