-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feat/catalyst_cardano_address' into feat/catalyst_carda…
…no_transaction_builder
- Loading branch information
Showing
5 changed files
with
56 additions
and
26 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
...st_voices_packages/catalyst_cardano_serialization/lib/catalyst_cardano_serialization.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export 'src/address.dart'; | ||
export 'src/types.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 21 additions & 10 deletions
31
catalyst_voices_packages/catalyst_cardano_serialization/lib/src/types.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
26 changes: 26 additions & 0 deletions
26
catalyst_voices_packages/catalyst_cardano_serialization/test/types_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
}); | ||
}); | ||
} |