This repository has been archived by the owner on Dec 12, 2024. It is now read-only.
generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
252 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import 'package:tbdex/tbdex.dart'; | ||
|
||
class Balance extends Resource { | ||
@override | ||
final ResourceMetadata metadata; | ||
@override | ||
final BalanceData data; | ||
|
||
Balance._({ | ||
required this.metadata, | ||
required this.data, | ||
String? signature, | ||
}) : super() { | ||
this.signature = signature; | ||
} | ||
|
||
static Balance create( | ||
String from, | ||
BalanceData data, { | ||
String? externalId, | ||
String protocol = '1.0', | ||
}) { | ||
final now = DateTime.now().toUtc().toIso8601String(); | ||
final metadata = ResourceMetadata( | ||
kind: ResourceKind.balance, | ||
from: from, | ||
id: Resource.generateId(ResourceKind.balance), | ||
protocol: protocol, | ||
createdAt: now, | ||
updatedAt: now, | ||
); | ||
|
||
return Balance._( | ||
metadata: metadata, | ||
data: data, | ||
); | ||
} | ||
|
||
static Future<Balance> parse(String rawResource) async { | ||
final balance = Parser.parseResource(rawResource) as Balance; | ||
await balance.verify(); | ||
return balance; | ||
} | ||
|
||
factory Balance.fromJson(Map<String, dynamic> json) { | ||
return Balance._( | ||
metadata: ResourceMetadata.fromJson(json['metadata']), | ||
data: BalanceData.fromJson(json['data']), | ||
signature: json['signature'], | ||
); | ||
} | ||
|
||
Map<String, dynamic> toJson() { | ||
return { | ||
'metadata': metadata.toJson(), | ||
'data': data.toJson(), | ||
'signature': signature, | ||
}; | ||
} | ||
} |
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
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
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,39 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:tbdex/src/protocol/models/balance.dart'; | ||
import 'package:tbdex/tbdex.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import '../../helpers/test_data.dart'; | ||
|
||
void main() async { | ||
await TestData.initializeDids(); | ||
|
||
group('Balance', () { | ||
test('can create a new balance', () { | ||
final offering = Balance.create( | ||
TestData.pfi, | ||
BalanceData( | ||
currencyCode: 'USD', | ||
available: '100.00', | ||
), | ||
); | ||
|
||
expect(offering.metadata.id, startsWith(ResourceKind.balance.name)); | ||
expect(offering.metadata.kind, equals(ResourceKind.balance)); | ||
expect(offering.metadata.protocol, equals('1.0')); | ||
expect(offering.data.currencyCode, equals('USD')); | ||
expect(offering.data.available, equals('100.00')); | ||
}); | ||
|
||
test('can parse and verify balance from a json string', () async { | ||
final balance = TestData.getBalance(); | ||
await balance.sign(TestData.pfiDid); | ||
final json = jsonEncode(balance.toJson()); | ||
final parsed = await Balance.parse(json); | ||
|
||
expect(parsed, isA<Balance>()); | ||
expect(parsed.toString(), equals(json)); | ||
}); | ||
}); | ||
} |
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