Skip to content

Commit

Permalink
Merge branch 'feat/catalyst_cardano_address' into feat/catalyst_carda…
Browse files Browse the repository at this point in the history
…no_transaction_builder
  • Loading branch information
dtscalac committed May 9, 2024
2 parents 1173251 + ef9f84f commit a621ec5
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export 'src/address.dart';
export 'src/types.dart';
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class ShelleyAddress {
}

/// Returns the [NetworkId] related to this address.
NetworkId get network => NetworkId.testnet.magicId == (bytes[0] & 0x0f)
NetworkId get network => NetworkId.testnet.id == (bytes[0] & 0x0f)
? NetworkId.testnet
: NetworkId.mainnet;

Expand All @@ -88,9 +88,7 @@ class ShelleyAddress {
}

/// Serializes the type as cbor.
CborValue toCbor() {
return CborBytes(bytes);
}
CborValue toCbor() => CborBytes(bytes);

@override
int get hashCode => Object.hash(bytes, hrp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ class MaxTxSizeExceededException implements Exception {
});

@override
String toString() {
return 'MaxTxSizeExceededException('
'maxTxSize=$maxTxSize'
', actualTxSize:$actualTxSize'
')';
}
String toString() => 'MaxTxSizeExceededException('
'maxTxSize=$maxTxSize'
', actualTxSize:$actualTxSize'
')';
}

/// Exception thrown when building a transaction that doesn't specify the fee.
Expand All @@ -27,9 +25,7 @@ class TxFeeNotSpecifiedException implements Exception {
const TxFeeNotSpecifiedException();

@override
String toString() {
return 'TxFeeNotSpecifiedException';
}
String toString() => 'TxFeeNotSpecifiedException';
}

/// Exception thrown when parsing a transaction hash that has incorrect length.
Expand All @@ -38,9 +34,7 @@ class TransactionHashFormatException implements Exception {
const TransactionHashFormatException();

@override
String toString() {
return 'TransactionHashFormatException';
}
String toString() => 'TransactionHashFormatException';
}

/// Exception thrown if the address doesn't match the bech32 specification
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
import 'package:cbor/cbor.dart';

/// Specifies an amount of ADA in terms of lovelace.
typedef Coin = int;

/// Specifies on which network the code will run.
enum NetworkId {
/// The production network
mainnet(magicId: 1),
mainnet(id: 1),

/// The test network.
testnet(magicId: 0);
testnet(id: 0);

/// The magic protocol number acting as the identifier of the network.
final int magicId;
final int id;

const NetworkId({required this.id});
}

/// Specifies an amount of ADA in terms of lovelace.
extension type Coin(int value) {
/// Adds [other] value to this value and returns a new [Coin].
Coin operator +(Coin other) => Coin(value + other.value);

/// Subtracts [other] values from this value and returns a new [Coin].
Coin operator -(Coin other) => Coin(value - other.value);

/// Multiplies this value by [other] values and returns a new [Coin].
Coin operator *(Coin other) => Coin(value * other.value);

const NetworkId({required this.magicId});
/// Divides this value by [other] value without remainder
/// and returns a new [Coin].
Coin operator ~/(Coin other) => Coin(value ~/ other.value);
}

/// A blockchain slot number.
extension type SlotBigNum(int value) {
/// Serializes the type as cbor.
CborValue toCbor() {
return CborSmallInt(value);
}
CborValue toCbor() => CborSmallInt(value);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:catalyst_cardano_serialization/src/types.dart';
import 'package:test/test.dart';

void main() {
group(Coin, () {
test('addition', () {
expect(Coin(3) + Coin(100), equals(Coin(103)));
expect(Coin(-100) + Coin(100), equals(Coin(0)));
});

test('subtraction', () {
expect(Coin(5) - Coin(2), equals(Coin(3)));
expect(Coin(10) - Coin(27), equals(Coin(-17)));
});

test('multiplication', () {
expect(Coin(3) * Coin(6), equals(Coin(18)));
expect(Coin(-5) * Coin(7), equals(Coin(-35)));
});

test('division', () {
expect(Coin(3) ~/ Coin(2), equals(Coin(1)));
expect(Coin(100) ~/ Coin(50), equals(Coin(2)));
});
});
}

0 comments on commit a621ec5

Please sign in to comment.