Skip to content

Commit

Permalink
[PLA-2033] Removes old codecs from encoder (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardocustodio authored Oct 11, 2024
1 parent 0554afb commit b722699
Show file tree
Hide file tree
Showing 30 changed files with 52 additions and 252 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ function ($recipient) use ($blockchainService, $walletService) {
// TODO: With the v1010 upgrade we run into a bug with the php-scale-codec lib where it cannot
// Encode the transaction with `0x` the solution here is to use Batch and within each call append the 0's
$continueOnFailure = true;
$method = isRunningLatest() ? $this->getMutationName() . 'V1010' : $this->getMutationName();
$encodedData = $serializationService->encode($continueOnFailure ? 'Batch' : $method, static::getEncodableParams(
$encodedData = $serializationService->encode($continueOnFailure ? 'Batch' : $this->getMutationName(), static::getEncodableParams(
collectionId: $args['collectionId'],
recipients: $recipients->toArray(),
continueOnFailure: $continueOnFailure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ public function resolve(
TransactionService $transactionService
): mixed {
$continueOnFailure = $args['continueOnFailure'];
$method = isRunningLatest() ? $this->getMutationName() . 'V1010' : $this->getMutationName();
$encodedData = $serializationService->encode($continueOnFailure ? 'Batch' : $method, static::getEncodableParams(
$encodedData = $serializationService->encode($continueOnFailure ? 'Batch' : $this->getMutationName(), static::getEncodableParams(
collectionId: $args['collectionId'],
tokenId: $this->encodeTokenId($args),
attributes: $args['attributes'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ function ($recipient) use ($blockchainService, $walletService) {
);

$continueOnFailure = $args['continueOnFailure'];
$method = isRunningLatest() ? $this->getMutationName() . 'V1010' : $this->getMutationName();
$encodedData = $serializationService->encode($continueOnFailure ? 'Batch' : $method, static::getEncodableParams(
$encodedData = $serializationService->encode($continueOnFailure ? 'Batch' : $this->getMutationName(), static::getEncodableParams(
collectionId: $args['collectionId'],
recipients: $recipients->toArray(),
continueOnFailure: $continueOnFailure
Expand All @@ -148,7 +147,7 @@ public static function getEncodableParams(...$params): array

if ($continueOnFailure) {
$encodedData = collect($recipients)->map(
fn ($recipient) => $serializationService->encode(isRunningLatest() ? 'TransferV1010' : 'TransferToken', [
fn ($recipient) => $serializationService->encode('Transfer', [
'recipient' => [
'Id' => HexConverter::unPrefix($recipient['accountId']),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,9 @@ public function resolve(
TransactionService $transactionService,
): mixed {
$args['params']['tokenId'] = $this->encodeTokenId($args['params']);
unset($args['params']['encodeTokenId']);
unset($args['params']['encodeTokenId'], $args['params']['keepAlive']);

$method = isRunningLatest() ? $this->getMutationName() . 'V1010' : $this->getMutationName();
$encodedData = $serializationService->encode($method, static::getEncodableParams(
$encodedData = $serializationService->encode($this->getMutationName(), static::getEncodableParams(
collectionId: $args['collectionId'],
burnParams: new BurnParams(...$args['params'])
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ public function resolve(
TransactionService $transactionService,
Substrate $blockchainService
): mixed {
$method = isRunningLatest() ? $this->getMutationName() . 'V1010' : $this->getMutationName();
$encodedData = $serializationService->encode($method, static::getEncodableParams(
$encodedData = $serializationService->encode($this->getMutationName(), static::getEncodableParams(
collectionId: $args['collectionId'],
tokenId: $this->encodeTokenId($args),
behavior: $blockchainService->getMutateTokenBehavior(Arr::get($args, 'mutation')),
Expand All @@ -122,20 +121,15 @@ public function validationErrorMessages(array $args = []): array
public static function getEncodableParams(...$params): array
{
$behavior = Arr::get($params, 'behavior');
$extra = isRunningLatest() ? [
'anyoneCanInfuse' => null,
'name' => null,
] : [
'metadata' => null,
];

return [
'collectionId' => gmp_init(Arr::get($params, 'collectionId', 0)),
'tokenId' => gmp_init(Arr::get($params, 'tokenId', 0)),
'mutation' => [
'behavior' => is_array($behavior) ? ['NoMutation' => null] : ['SomeMutation' => $behavior?->toEncodable()],
'listingForbidden' => Arr::get($params, 'listingForbidden'),
...$extra,
'anyoneCanInfuse' => null, // TODO: Add this when the mutation is changed
'name' => null, // TODO: Add this when the mutation is changed
],
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function resolve(
*/
public function getMethodName(): string
{
return isRunningLatest() ? 'TransferV1010' : 'Transfer';
return 'Transfer';
}

public static function getEncodableParams(...$params): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function resolve(
*/
public function getMethodName(): string
{
return isRunningLatest() ? 'SetAttributeV1010' : 'SetAttribute';
return 'SetAttribute';
}

public static function getEncodableParams(...$params): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function resolve(
*/
public function getMethodName(): string
{
return isRunningLatest() ? 'SetAttributeV1010' : 'SetAttribute';
return 'SetAttribute';
}

public static function getEncodableParams(...$params): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function resolve(
*/
public function getMethodName(): string
{
return isRunningLatest() ? 'TransferV1010' : 'Transfer';
return 'Transfer';
}

public static function getEncodableParams(...$params): array
Expand Down
12 changes: 4 additions & 8 deletions src/Models/Substrate/BurnParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class BurnParams
public function __construct(
public string $tokenId,
public string $amount,
public ?bool $keepAlive = false,
public ?bool $removeTokenStorage = false,
) {}

Expand All @@ -24,7 +23,6 @@ public static function fromEncodable(array $params): self
return new self(
tokenId: gmp_strval(Arr::get($params, 'tokenId')),
amount: gmp_strval(Arr::get($params, 'amount')),
keepAlive: Arr::get($params, 'keepAlive'),
removeTokenStorage: Arr::get($params, 'removeTokenStorage'),
);
}
Expand All @@ -37,7 +35,6 @@ public static function fromArray(array $params): self
return new self(
tokenId: Arr::get($params, 'tokenId'),
amount: Arr::get($params, 'amount'),
keepAlive: Arr::get($params, 'keepAlive'),
removeTokenStorage: Arr::get($params, 'removeTokenStorage')
);
}
Expand All @@ -47,23 +44,22 @@ public static function fromArray(array $params): self
*/
public function toEncodable(): array
{
return array_merge([
return [
'tokenId' => gmp_init($this->tokenId),
'amount' => gmp_init($this->amount),
'removeTokenStorage' => $this->removeTokenStorage,
], isRunningLatest() ? [] : ['keepAlive' => $this->keepAlive]);
];
}

/**
* Get the array representation.
*/
public function toArray(): array
{
return array_merge([
return [
'tokenId' => $this->tokenId,
'amount' => $this->amount,
'keepAlive' => $this->keepAlive,
'removeTokenStorage' => $this->removeTokenStorage,
], isRunningLatest() ? [] : ['keepAlive' => $this->keepAlive]);
];
}
}
14 changes: 1 addition & 13 deletions src/Models/Substrate/OperatorTransferParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public function __construct(
public string $tokenId,
public string $source,
public string $amount,
public ?bool $keepAlive = false
) {}

/**
Expand All @@ -26,7 +25,6 @@ public static function fromEncodable(array $params): self
tokenId: gmp_strval(Arr::get($params, 'tokenId')),
source: SS58Address::encode(Arr::get($params, 'source')),
amount: gmp_strval(Arr::get($params, 'amount')),
keepAlive: Arr::get($params, 'keepAlive', false),
);
}

Expand All @@ -39,7 +37,6 @@ public static function fromArray(array $params): self
tokenId: Arr::get($params, 'tokenId'),
source: SS58Address::encode(Arr::get($params, 'source')),
amount: Arr::get($params, 'amount'),
keepAlive: Arr::get($params, 'keepAlive', false),
);
}

Expand All @@ -48,16 +45,12 @@ public static function fromArray(array $params): self
*/
public function toEncodable(): array
{
$extra = isRunningLatest()
? ['depositor' => null]
: ['keepAlive' => $this->keepAlive];

return [
'Operator' => [
'tokenId' => gmp_init($this->tokenId),
'source' => SS58Address::getPublicKey($this->source),
'amount' => gmp_init($this->amount),
...$extra,
'depositor' => null,
],
];
}
Expand All @@ -67,16 +60,11 @@ public function toEncodable(): array
*/
public function toArray(): array
{
isRunningLatest()
? $extra['depositor'] = null
: $extra['keepAlive'] = $this->keepAlive;

return [
'Operator' => [
'tokenId' => $this->tokenId,
'source' => $this->source,
'amount' => $this->amount,
...$extra,
],
];
}
Expand Down
14 changes: 1 addition & 13 deletions src/Models/Substrate/SimpleTransferParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class SimpleTransferParams
public function __construct(
public string $tokenId,
public string $amount,
public ?bool $keepAlive = false
) {}

/**
Expand All @@ -23,7 +22,6 @@ public static function fromEncodable(array $params): self
return new self(
tokenId: gmp_strval(Arr::get($params, 'tokenId')),
amount: gmp_strval(Arr::get($params, 'amount')),
keepAlive: Arr::get($params, 'keepAlive', false),
);
}

Expand All @@ -35,7 +33,6 @@ public static function fromArray(array $params): self
return new self(
tokenId: Arr::get($params, 'tokenId'),
amount: Arr::get($params, 'amount'),
keepAlive: Arr::get($params, 'keepAlive', false),
);
}

Expand All @@ -44,15 +41,11 @@ public static function fromArray(array $params): self
*/
public function toEncodable(): array
{
$extra = isRunningLatest()
? ['depositor' => null]
: ['keepAlive' => $this->keepAlive];

return [
'Simple' => [
'tokenId' => gmp_init($this->tokenId),
'amount' => gmp_init($this->amount),
...$extra,
'depositor' => null,
],
];
}
Expand All @@ -62,15 +55,10 @@ public function toEncodable(): array
*/
public function toArray(): array
{
isRunningLatest()
? $extra['depositor'] = null
: $extra['keepAlive'] = $this->keepAlive;

return [
'Simple' => [
'tokenId' => $this->tokenId,
'amount' => $this->amount,
...$extra,
],
];
}
Expand Down
2 changes: 0 additions & 2 deletions src/Services/Blockchain/Implementations/Substrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,6 @@ public function getOperatorTransferParams(array $args): OperatorTransferParams
$this->encodeTokenId($args),
$args['source'],
$args['amount'],
$args['keepAlive'],
];

return new OperatorTransferParams(...$data);
Expand All @@ -346,7 +345,6 @@ public function getSimpleTransferParams(array $args): SimpleTransferParams
$data = [
$this->encodeTokenId($args),
$args['amount'],
$args['keepAlive'],
];

return new SimpleTransferParams(...$data);
Expand Down
1 change: 0 additions & 1 deletion src/Services/Processor/Substrate/Codec/Decoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ public function burn(string $data): array
'collectionId' => gmp_strval(Arr::get($decoded, 'collectionId')),
'tokenId' => gmp_strval(Arr::get($decoded, 'params.tokenId')),
'amount' => gmp_strval(Arr::get($decoded, 'params.amount')),
'keepAlive' => Arr::get($decoded, 'params.keepAlive'),
'removeTokenStorage' => Arr::get($decoded, 'params.removeTokenStorage'),
];
}
Expand Down
6 changes: 0 additions & 6 deletions src/Services/Processor/Substrate/Codec/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,20 @@ class Encoder
'ApproveToken' => 'MultiTokens.approve_token',
'UnapproveToken' => 'MultiTokens.unapprove_token',
'BatchSetAttribute' => 'MultiTokens.batch_set_attribute',
'BatchSetAttributeV1010' => 'MultiTokens.batch_set_attribute',
'BatchTransfer' => 'MultiTokens.batch_transfer',
'BatchTransferV1010' => 'MultiTokens.batch_transfer',
'Transfer' => 'MultiTokens.transfer',
'TransferV1010' => 'MultiTokens.transfer',
'CreateCollection' => 'MultiTokens.create_collection',
'DestroyCollection' => 'MultiTokens.destroy_collection',
'MutateCollection' => 'MultiTokens.mutate_collection',
'MutateToken' => 'MultiTokens.mutate_token',
'MutateTokenV1010' => 'MultiTokens.mutate_token',
'Mint' => 'MultiTokens.mint',
'BatchMint' => 'MultiTokens.batch_mint',
'Burn' => 'MultiTokens.burn',
'BurnV1010' => 'MultiTokens.burn',
'Infuse' => 'MultiTokens.infuse',
'Freeze' => 'MultiTokens.freeze',
'Thaw' => 'MultiTokens.thaw',
'SetRoyalty' => 'MultiTokens.set_royalty',
'SetAttribute' => 'MultiTokens.set_attribute',
'SetAttributeV1010' => 'MultiTokens.set_attribute',
'RemoveAttribute' => 'MultiTokens.remove_attribute',
'RemoveAllAttributes' => 'MultiTokens.remove_all_attributes',
'AcceptCollectionTransfer' => 'MultiTokens.accept_collection_transfer',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
{
"BatchSetAttribute": {
"callIndex": "(u8, u8)",
"collectionId": "Compact<u128>",
"tokenId": "Option<u128>",
"attributes": "Vec<Attribute>"
},
"Attribute": {
"key": "Bytes",
"value": "Bytes"
},
"BatchSetAttributeV1010": {
"BatchSetAttribute": {
"callIndex": "(u8, u8)",
"collectionId": "Compact<u128>",
"tokenId": "Option<u128>",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,5 @@
"TransferRecipient": {
"accountId": "AccountId",
"params": "TransferParamsOf"
},
"BatchTransferV1010": {
"callIndex": "(u8, u8)",
"collectionId": "Compact<u128>",
"recipients": "Vec<TransferRecipientV1010>"
},
"TransferRecipientV1010": {
"accountId": "AccountId",
"params": "TransferParamsOfV1010"
}
}
11 changes: 0 additions & 11 deletions src/Services/Processor/Substrate/Codec/Types/Burn.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,6 @@
"params": "BurnParamsOf"
},
"BurnParamsOf": {
"tokenId": "Compact<u128>",
"amount": "Compact<u128>",
"keepAlive": "bool",
"removeTokenStorage": "bool"
},
"BurnV1010": {
"callIndex": "(u8, u8)",
"collectionId": "Compact<u128>",
"params": "BurnParamsOfV1010"
},
"BurnParamsOfV1010": {
"tokenId": "Compact<u128>",
"amount": "Compact<u128>",
"removeTokenStorage": "bool"
Expand Down
Loading

0 comments on commit b722699

Please sign in to comment.