diff --git a/lib/src/streams/errors.dart b/lib/src/streams/errors.dart index 80600bb..8ac8953 100644 --- a/lib/src/streams/errors.dart +++ b/lib/src/streams/errors.dart @@ -27,3 +27,7 @@ class TransactionDeepHashError extends StreamTransactionError {} class TransactionSignatureError extends StreamTransactionError {} class TransactionGetOwnerError extends StreamTransactionError {} + +class GetTxAnchorError extends StreamTransactionError {} + +class GetTxPriceError extends StreamTransactionError {} diff --git a/lib/src/streams/utils.dart b/lib/src/streams/utils.dart index 1f3b6f8..32536e2 100644 --- a/lib/src/streams/utils.dart +++ b/lib/src/streams/utils.dart @@ -3,7 +3,6 @@ import 'dart:typed_data'; import 'package:arweave/src/api/api.dart'; import 'package:arweave/src/models/models.dart'; -import 'package:arweave/src/streams/transaction.dart'; import 'package:async/async.dart'; import 'package:fpdart/fpdart.dart'; @@ -177,6 +176,16 @@ TaskEither serializeTagsTaskEither( }, (error, _) => SerializeTagsError()); } +class PrepareChunksResult { + final TransactionChunksWithProofs chunks; + final String dataRoot; + + PrepareChunksResult({ + required this.chunks, + required this.dataRoot, + }); +} + TaskEither prepareChunksTaskEither( final DataStreamGenerator dataStreamGenerator, ) { @@ -220,5 +229,29 @@ Stream getChunks(Stream dataStream, await chunker.cancel(); } -Future getTransactionAnchor() => - ArweaveApi().get('tx_anchor').then((res) => res.body); +TaskEither getTxAnchor(String? anchor) { + if (anchor != null) { + return TaskEither.of(anchor); + } + + return TaskEither.tryCatch(() async { + return await ArweaveApi().get('tx_anchor').then((res) => res.body); + }, (error, _) => GetTxAnchorError()); +} + +TaskEither getTxPrice( + BigInt? reward, int byteSize, String? targetAddress) { + if (reward != null) { + return TaskEither.of(reward); + } + + final endpoint = targetAddress != null + ? 'price/$byteSize/$targetAddress' + : 'price/$byteSize'; + + return TaskEither.tryCatch(() async { + return await ArweaveApi() + .get(endpoint) + .then((res) => BigInt.parse(res.body)); + }, (error, _) => GetTxPriceError()); +}