Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: improve string type safety #145

Merged
merged 21 commits into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
bd430f6
feat(core): add utils for custom string types, safer pool id types
mkazlauskas Nov 23, 2021
91c0f59
refactor: add ProviderFailure.InvalidResponse, use improved PoolId type
mkazlauskas Nov 23, 2021
bd6506d
refactor(core): convert Cardano/util and Cardano/StakePool into dirs
mkazlauskas Nov 24, 2021
eeb2074
refactor: improve Cardano.Address and Cardano.RewardAccount types
mkazlauskas Nov 24, 2021
90a1544
refactor: improve Cardano.TransactionId type
mkazlauskas Nov 24, 2021
b24a899
refactor: add Cardano.BlockId type
mkazlauskas Nov 24, 2021
bc8953d
refactor: add Ed25519Signature and Ed25519PublicKey types
mkazlauskas Nov 24, 2021
9ee9070
refactor: update StakePool.rewardAccount to Cardano.RewardAccount
mkazlauskas Nov 24, 2021
98ce920
refactor: convert Hash16 to a validated type, add Ed25519KeyHash and …
mkazlauskas Nov 25, 2021
3f3dad6
refactor: change StakePool.owners to be Cardano.RewardAccount[]
mkazlauskas Nov 25, 2021
b2c3048
refactor: split Hash16 into 2 more concrete types
mkazlauskas Nov 25, 2021
28de368
refactor: convert TxAlonzo.witness.signatures to Map, resolve some TODOs
mkazlauskas Nov 25, 2021
28f2ccf
refactor: rename MIR certificate 'address' to 'rewardAccount' and cha…
mkazlauskas Nov 25, 2021
4fc6ad8
refactor: improve Tip type to use the new BlockId type
mkazlauskas Nov 25, 2021
9781ae2
refactor: improve asset id types, convert TokenMap to Map
mkazlauskas Nov 26, 2021
ec523a7
fix: change stakepool metadata extVkey field type to bech32 string
mkazlauskas Nov 29, 2021
69e1f8e
chore: rm some obsolete TODO comments
mkazlauskas Nov 29, 2021
854e9cb
refactor: convert tx signature key type to hex
mkazlauskas Nov 29, 2021
d4200ef
refactor: change PoolParameters.vrf type to VrkVkHex
mkazlauskas Nov 29, 2021
914fab8
refactor(core): change type of requiredExtraSignatures to Ed25519KeyHash
mkazlauskas Nov 29, 2021
4ab2fda
refactor: improve name for PoolMdVk, improve opaque string tests
mkazlauskas Nov 29, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/blockfrost/src/BlockfrostToCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const BlockfrostToCore = {
transactionUtxos: (utxoResponse: Responses['tx_content_utxo']) => ({
inputs: utxoResponse.inputs.map((input) => ({
...BlockfrostToCore.txIn(input),
address: input.address
address: Cardano.Address(input.address)
})),
outputs: utxoResponse.outputs.map(BlockfrostToCore.txOut)
}),
Expand All @@ -70,9 +70,9 @@ export const BlockfrostToCore = {
}),

txIn: (blockfrost: BlockfrostInput): Cardano.TxIn => ({
address: blockfrost.address,
address: Cardano.Address(blockfrost.address),
index: blockfrost.output_index,
txId: blockfrost.tx_hash
txId: Cardano.TransactionId(blockfrost.tx_hash)
}),

txOut: (blockfrost: BlockfrostOutput): Cardano.TxOut => {
Expand All @@ -82,7 +82,7 @@ export const BlockfrostToCore = {
assets[amount.unit] = BigInt(amount.quantity);
}
return {
address: blockfrost.address,
address: Cardano.Address(blockfrost.address),
value: {
assets,
coins: BigInt(blockfrost.amount.find(({ unit }) => unit === 'lovelace')!.quantity)
Expand Down
10 changes: 8 additions & 2 deletions packages/blockfrost/src/blockfrostAssetProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const blockfrostAssetProvider = (options: Options): AssetProvider => {
response.map(({ action, amount, tx_hash }) => ({
action: action === 'minted' ? Cardano.AssetProvisioning.Mint : Cardano.AssetProvisioning.Burn,
quantity: BigInt(amount),
transactionId: tx_hash
transactionId: Cardano.TransactionId(tx_hash)
}))
});

Expand All @@ -47,7 +47,13 @@ export const blockfrostAssetProvider = (options: Options): AssetProvider => {
fingerprint: response.fingerprint,
history:
response.mint_or_burn_count === 1
? [{ action: Cardano.AssetProvisioning.Mint, quantity, transactionId: response.initial_mint_tx_hash }]
? [
{
action: Cardano.AssetProvisioning.Mint,
quantity,
transactionId: Cardano.TransactionId(response.initial_mint_tx_hash)
}
]
: await getAssetHistory(assetId),
metadata: mapMetadata(response.onchain_metadata, response.metadata),
name,
Expand Down
54 changes: 27 additions & 27 deletions packages/blockfrost/src/blockfrostWalletProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,18 @@ export const blockfrostWalletProvider = (options: Options, logger = dummyLogger)
addresses.map(async (address) =>
fetchByAddressSequentially<Cardano.Utxo, BlockfrostUtxo>({
address,
request: (addr: Cardano.Address, pagination) => blockfrost.addressesUtxos(addr, pagination),
request: (addr: Cardano.Address, pagination) => blockfrost.addressesUtxos(addr.toString(), pagination),
responseTranslator: (addr: Cardano.Address, response: Responses['address_utxo_content']) =>
BlockfrostToCore.addressUtxoContent(addr, response)
BlockfrostToCore.addressUtxoContent(addr.toString(), response)
})
)
);
const utxo = utxoResults.flat(1);
if (rewardAccount !== undefined) {
try {
const accountResponse = await blockfrost.accounts(rewardAccount);
const accountResponse = await blockfrost.accounts(rewardAccount.toString());
const delegationAndRewards = {
delegate: accountResponse.pool_id || undefined,
delegate: accountResponse.pool_id ? Cardano.PoolId(accountResponse.pool_id) : undefined,
rewards: BigInt(accountResponse.withdrawable_amount)
};
return { delegationAndRewards, utxo };
Expand Down Expand Up @@ -165,7 +165,7 @@ export const blockfrostWalletProvider = (options: Options, logger = dummyLogger)
return response.map(
({ address, amount }): Cardano.Withdrawal => ({
quantity: BigInt(amount),
stakeAddress: address
stakeAddress: Cardano.RewardAccount(address)
})
);
};
Expand All @@ -183,7 +183,7 @@ export const blockfrostWalletProvider = (options: Options, logger = dummyLogger)
return response.map(({ pool_id, retiring_epoch }) => ({
__typename: Cardano.CertificateType.PoolRetirement,
epoch: retiring_epoch,
poolId: pool_id
poolId: Cardano.PoolId(pool_id)
}));
};

Expand All @@ -192,7 +192,7 @@ export const blockfrostWalletProvider = (options: Options, logger = dummyLogger)
return response.map(({ pool_id, active_epoch }) => ({
__typename: Cardano.CertificateType.PoolRegistration,
epoch: active_epoch,
poolId: pool_id,
poolId: Cardano.PoolId(pool_id),
poolParameters: ((): Cardano.PoolParameters => {
logger.warn('Omitting poolParameters for certificate in tx', hash);
return null as unknown as Cardano.PoolParameters;
Expand All @@ -204,7 +204,7 @@ export const blockfrostWalletProvider = (options: Options, logger = dummyLogger)
const response = await blockfrost.txsMirs(hash);
return response.map(({ address, amount, cert_index, pot }) => ({
__typename: Cardano.CertificateType.MIR,
address,
address: Cardano.Address(address),
certIndex: cert_index,
pot,
quantity: BigInt(amount)
Expand All @@ -217,20 +217,20 @@ export const blockfrostWalletProvider = (options: Options, logger = dummyLogger)
__typename: registration
? Cardano.CertificateType.StakeKeyRegistration
: Cardano.CertificateType.StakeKeyDeregistration,
address,
certIndex: cert_index
certIndex: cert_index,
rewardAccount: Cardano.RewardAccount(address)
}));
};

const fetchDelegationCerts = async (hash: string): Promise<Cardano.StakeDelegationCertificate[]> => {
const response = await blockfrost.txsDelegations(hash);
return response.map(({ cert_index, index, address, active_epoch, pool_id }) => ({
__typename: Cardano.CertificateType.StakeDelegation,
address,
certIndex: cert_index,
delegationIndex: index,
epoch: active_epoch,
poolId: pool_id
poolId: Cardano.PoolId(pool_id),
rewardAccount: Cardano.RewardAccount(address)
}));
};

Expand All @@ -252,9 +252,9 @@ export const blockfrostWalletProvider = (options: Options, logger = dummyLogger)
];
};

const fetchJsonMetadata = async (txHash: Cardano.Hash16): Promise<Cardano.MetadatumMap | null> => {
const fetchJsonMetadata = async (txHash: Cardano.TransactionId): Promise<Cardano.MetadatumMap | null> => {
try {
const response = await blockfrost.txsMetadata(txHash);
const response = await blockfrost.txsMetadata(txHash.toString());
return response.reduce((map, metadatum) => {
// Not sure if types are correct, missing 'label', but it's present in docs
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -273,9 +273,9 @@ export const blockfrostWalletProvider = (options: Options, logger = dummyLogger)

// eslint-disable-next-line unicorn/consistent-function-scoping
const parseValidityInterval = (num: string | null) => Number.parseInt(num || '') || undefined;
const fetchTransaction = async (hash: string): Promise<Cardano.TxAlonzo> => {
const { inputs, outputs } = BlockfrostToCore.transactionUtxos(await blockfrost.txsUtxos(hash));
const response = await blockfrost.txs(hash);
const fetchTransaction = async (hash: Cardano.TransactionId): Promise<Cardano.TxAlonzo> => {
const { inputs, outputs } = BlockfrostToCore.transactionUtxos(await blockfrost.txsUtxos(hash.toString()));
const response = await blockfrost.txs(hash.toString());
const metadata = await fetchJsonMetadata(hash);
return {
auxiliaryData: metadata
Expand All @@ -284,7 +284,7 @@ export const blockfrostWalletProvider = (options: Options, logger = dummyLogger)
}
: undefined,
blockHeader: {
blockHash: response.block,
blockHash: Cardano.BlockId(response.block),
blockHeight: response.block_height,
slot: response.slot
},
Expand Down Expand Up @@ -325,14 +325,14 @@ export const blockfrostWalletProvider = (options: Options, logger = dummyLogger)
BlockfrostTransactionContent
>({
address,
request: (addr: Cardano.Address, pagination) => blockfrost.addressesTransactions(addr, pagination)
request: (addr: Cardano.Address, pagination) => blockfrost.addressesTransactions(addr.toString(), pagination)
})
)
);

const transactionsArray = await Promise.all(
addressTransactions.map((transactionArray) =>
queryTransactionsByHashes(transactionArray.map(({ tx_hash }) => tx_hash))
queryTransactionsByHashes(transactionArray.map(({ tx_hash }) => Cardano.TransactionId(tx_hash)))
)
);

Expand All @@ -348,15 +348,15 @@ export const blockfrostWalletProvider = (options: Options, logger = dummyLogger)
};

const accountRewards = async (
stakeAddress: Cardano.Address,
stakeAddress: Cardano.RewardAccount,
{ lowerBound = 0, upperBound = Number.MAX_SAFE_INTEGER }: EpochRange = {}
): Promise<EpochRewards[]> => {
const result: EpochRewards[] = [];
const batchSize = 100;
let page = 1;
let haveMorePages = true;
while (haveMorePages) {
const rewards = await blockfrost.accountsRewards(stakeAddress, { count: batchSize, page });
const rewards = await blockfrost.accountsRewards(stakeAddress.toString(), { count: batchSize, page });
result.push(
...rewards
.filter(({ epoch }) => lowerBound <= epoch && epoch <= upperBound)
Expand Down Expand Up @@ -397,7 +397,7 @@ export const blockfrostWalletProvider = (options: Options, logger = dummyLogger)
};

const queryBlocksByHashes: WalletProvider['queryBlocksByHashes'] = async (hashes) => {
const responses = await Promise.all(hashes.map((hash) => blockfrost.blocks(hash)));
const responses = await Promise.all(hashes.map((hash) => blockfrost.blocks(hash.toString())));
return responses.map((response) => {
if (!response.epoch || !response.epoch_slot || !response.height || !response.slot || !response.block_vrf) {
throw new ProviderError(ProviderFailure.Unknown, null, 'Queried unsupported block');
Expand All @@ -409,14 +409,14 @@ export const blockfrostWalletProvider = (options: Options, logger = dummyLogger)
epochSlot: response.epoch_slot,
fees: BigInt(response.fees || '0'),
header: {
blockHash: response.hash,
blockHash: Cardano.BlockId(response.hash),
blockHeight: response.height,
slot: response.slot
},
nextBlock: response.next_block || undefined,
previousBlock: response.previous_block || undefined,
nextBlock: response.next_block ? Cardano.BlockId(response.next_block) : undefined,
previousBlock: response.previous_block ? Cardano.BlockId(response.previous_block) : undefined,
size: response.size,
slotLeader: response.slot_leader,
slotLeader: Cardano.PoolId(response.slot_leader),
totalOutput: BigInt(response.output || '0'),
txCount: response.tx_count,
vrf: response.block_vrf
Expand Down
5 changes: 4 additions & 1 deletion packages/blockfrost/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Error as BlockfrostError } from '@blockfrost/blockfrost-js';
import { InvalidStringError, ProviderError, ProviderFailure } from '@cardano-sdk/core';
import { PaginationOptions } from '@blockfrost/blockfrost-js/lib/types';
import { ProviderError, ProviderFailure } from '@cardano-sdk/core';

export const formatBlockfrostError = (error: unknown) => {
const blockfrostError = error as BlockfrostError;
Expand All @@ -11,6 +11,9 @@ export const formatBlockfrostError = (error: unknown) => {
if (typeof blockfrostError !== 'object') {
throw new ProviderError(ProviderFailure.Unknown, error, 'failed to parse error (response type)');
}
if (error instanceof InvalidStringError) {
throw new ProviderError(ProviderFailure.InvalidResponse, error);
}
const errorAsType1 = blockfrostError as {
status_code: number;
message: string;
Expand Down
10 changes: 5 additions & 5 deletions packages/blockfrost/test/blockfrostAssetProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('blockfrostAssetProvider', () => {
{
action: Cardano.AssetProvisioning.Mint,
quantity: 12_000n,
transactionId: '6804edf9712d2b619edb6ac86861fe93a730693183a262b165fcc1ba1bc99cad'
transactionId: Cardano.TransactionId('6804edf9712d2b619edb6ac86861fe93a730693183a262b165fcc1ba1bc99cad')
}
],
metadata: {
Expand All @@ -72,12 +72,12 @@ describe('blockfrostAssetProvider', () => {
{
action: 'minted',
amount: '13000',
tx_hash: 'tx1hash'
tx_hash: '4123d70f66414cc921f6ffc29a899aafc7137a99a0fd453d6b200863ef5702d6'
},
{
action: 'burned',
amount: '1000',
tx_hash: 'tx2hash'
tx_hash: '01d7366549986d83edeea262e97b68eca3430d3bb052ed1c37d2202fd5458872'
}
] as Responses['asset_history']);

Expand All @@ -91,12 +91,12 @@ describe('blockfrostAssetProvider', () => {
{
action: Cardano.AssetProvisioning.Mint,
quantity: 13_000n,
transactionId: 'tx1hash'
transactionId: Cardano.TransactionId('4123d70f66414cc921f6ffc29a899aafc7137a99a0fd453d6b200863ef5702d6')
},
{
action: Cardano.AssetProvisioning.Burn,
quantity: 1000n,
transactionId: 'tx2hash'
transactionId: Cardano.TransactionId('01d7366549986d83edeea262e97b68eca3430d3bb052ed1c37d2202fd5458872')
}
],
metadata: {
Expand Down
Loading