Skip to content

Commit b19dde9

Browse files
Merge pull request #1221 from cypherstack/feat/spl
Add Solana tokens (SPL, Token-2022)
2 parents 1a063db + 8d5cd8f commit b19dde9

File tree

78 files changed

+12138
-816
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+12138
-816
lines changed

.gitignore

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,6 @@ secp256k1.dll
7070
/lib/app_config.g.dart
7171
/android/app/src/main/app_icon-playstore.png
7272

73-
# Dart generated files (Freezed, Riverpod, GoRouter etc..)
74-
lib/**/*.g.dart
75-
lib/**/*.freezed.dart
76-
7773
## other generated project files
7874

7975
pubspec.yaml

lib/db/isar/main_db.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class MainDB {
5959
AddressSchema,
6060
AddressLabelSchema,
6161
EthContractSchema,
62+
SolContractSchema,
6263
TransactionBlockExplorerSchema,
6364
StackThemeSchema,
6465
ContactEntrySchema,
@@ -69,6 +70,7 @@ class MainDB {
6970
WalletInfoMetaSchema,
7071
TokenWalletInfoSchema,
7172
FrostWalletInfoSchema,
73+
WalletSolanaTokenInfoSchema,
7274
],
7375
directory: (await StackFileSystem.applicationIsarDirectory()).path,
7476
// inspector: kDebugMode,
@@ -621,4 +623,26 @@ class MainDB {
621623
isar.writeTxn(() async {
622624
await isar.ethContracts.putAll(contracts);
623625
});
626+
627+
// ========== Solana =========================================================
628+
629+
// Solana tokens.
630+
631+
QueryBuilder<SolContract, SolContract, QWhere> getSolContracts() =>
632+
isar.solContracts.where();
633+
634+
Future<SolContract?> getSolContract(String tokenMint) =>
635+
isar.solContracts.where().addressEqualTo(tokenMint).findFirst();
636+
637+
SolContract? getSolContractSync(String tokenMint) =>
638+
isar.solContracts.where().addressEqualTo(tokenMint).findFirstSync();
639+
640+
Future<int> putSolContract(SolContract token) => isar.writeTxn(() async {
641+
return await isar.solContracts.put(token);
642+
});
643+
644+
Future<void> putSolContracts(List<SolContract> tokens) =>
645+
isar.writeTxn(() async {
646+
await isar.solContracts.putAll(tokens);
647+
});
624648
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* This file is part of Stack Wallet.
3+
*
4+
* Copyright (c) 2023 Cypher Stack
5+
* All Rights Reserved.
6+
* The code is distributed under GPLv3 license, see LICENSE file for details.
7+
*
8+
*/
9+
10+
import '../../../wallets/crypto_currency/crypto_currency.dart';
11+
import '../../isar/models/solana/sol_contract.dart';
12+
import '../add_wallet_list_entity.dart';
13+
14+
class SolTokenEntity extends AddWalletListEntity {
15+
SolTokenEntity(this.token);
16+
17+
final SolContract token;
18+
19+
@override
20+
CryptoCurrency get cryptoCurrency => Solana(CryptoCurrencyNetwork.main);
21+
22+
@override
23+
String get name => token.name;
24+
25+
@override
26+
String get ticker => token.symbol;
27+
28+
@override
29+
List<Object?> get props =>
30+
[cryptoCurrency.identifier, name, ticker, token.address];
31+
}

lib/models/balance.dart

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,18 @@ class Balance {
1919
final Amount blockedTotal;
2020
final Amount pendingSpendable;
2121

22-
Balance({
22+
const Balance({
2323
required this.total,
2424
required this.spendable,
2525
required this.blockedTotal,
2626
required this.pendingSpendable,
2727
});
2828

29-
factory Balance.zeroFor({required CryptoCurrency currency}) {
30-
final amount = Amount(
31-
rawValue: BigInt.zero,
32-
fractionDigits: currency.fractionDigits,
33-
);
29+
factory Balance.zeroFor({required CryptoCurrency currency}) =>
30+
.zeroWith(fractionDigits: currency.fractionDigits);
3431

32+
factory Balance.zeroWith({required int fractionDigits}) {
33+
final amount = Amount.zeroWith(fractionDigits: fractionDigits);
3534
return Balance(
3635
total: amount,
3736
spendable: amount,
@@ -41,11 +40,11 @@ class Balance {
4140
}
4241

4342
String toJsonIgnoreCoin() => jsonEncode({
44-
"total": total.toJsonString(),
45-
"spendable": spendable.toJsonString(),
46-
"blockedTotal": blockedTotal.toJsonString(),
47-
"pendingSpendable": pendingSpendable.toJsonString(),
48-
});
43+
"total": total.toJsonString(),
44+
"spendable": spendable.toJsonString(),
45+
"blockedTotal": blockedTotal.toJsonString(),
46+
"pendingSpendable": pendingSpendable.toJsonString(),
47+
});
4948

5049
// need to fall back to parsing from int due to cached balances being previously
5150
// stored as int values instead of Amounts
@@ -82,11 +81,11 @@ class Balance {
8281
}
8382

8483
Map<String, dynamic> toMap() => {
85-
"total": total,
86-
"spendable": spendable,
87-
"blockedTotal": blockedTotal,
88-
"pendingSpendable": pendingSpendable,
89-
};
84+
"total": total,
85+
"spendable": spendable,
86+
"blockedTotal": blockedTotal,
87+
"pendingSpendable": pendingSpendable,
88+
};
9089

9190
@override
9291
String toString() {

lib/models/isar/models/blockchain_data/transaction.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,4 +261,5 @@ enum TransactionSubType {
261261
sparkSpend, // firo specific
262262
ordinal,
263263
mweb,
264+
splToken, // Solana token.
264265
}

lib/models/isar/models/blockchain_data/transaction.g.dart

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/models/isar/models/blockchain_data/v2/transaction_v2.g.dart

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/models/isar/models/contract.dart

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,15 @@
99
*/
1010

1111
abstract class Contract {
12-
// for possible future use
12+
/// Token/contract address (mint address for Solana, contract address for Ethereum).
13+
String get address;
14+
15+
/// Token name.
16+
String get name;
17+
18+
/// Token symbol.
19+
String get symbol;
20+
21+
/// Token decimals.
22+
int get decimals;
1323
}

lib/models/isar/models/ethereum/eth_contract.dart

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010

1111
import 'package:isar_community/isar.dart';
12+
1213
import '../contract.dart';
1314

1415
part 'eth_contract.g.dart';
@@ -26,13 +27,17 @@ class EthContract extends Contract {
2627

2728
Id id = Isar.autoIncrement;
2829

30+
@override
2931
@Index(unique: true, replace: true)
3032
late final String address;
3133

34+
@override
3235
late final String name;
3336

37+
@override
3438
late final String symbol;
3539

40+
@override
3641
late final int decimals;
3742

3843
late final String? abi;
@@ -50,21 +55,16 @@ class EthContract extends Contract {
5055
List<String>? walletIds,
5156
String? abi,
5257
String? otherData,
53-
}) =>
54-
EthContract(
55-
address: address ?? this.address,
56-
name: name ?? this.name,
57-
symbol: symbol ?? this.symbol,
58-
decimals: decimals ?? this.decimals,
59-
type: type ?? this.type,
60-
abi: abi ?? this.abi,
61-
)..id = id ?? this.id;
58+
}) => EthContract(
59+
address: address ?? this.address,
60+
name: name ?? this.name,
61+
symbol: symbol ?? this.symbol,
62+
decimals: decimals ?? this.decimals,
63+
type: type ?? this.type,
64+
abi: abi ?? this.abi,
65+
)..id = id ?? this.id;
6266
}
6367

6468
// Used in Isar db and stored there as int indexes so adding/removing values
6569
// in this definition should be done extremely carefully in production
66-
enum EthContractType {
67-
unknown,
68-
erc20,
69-
erc721;
70-
}
70+
enum EthContractType { unknown, erc20, erc721 }

lib/models/isar/models/isar_models.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ export 'blockchain_data/transaction.dart';
1616
export 'blockchain_data/utxo.dart';
1717
export 'ethereum/eth_contract.dart';
1818
export 'log.dart';
19+
export 'solana/sol_contract.dart';
1920
export 'transaction_note.dart';
21+
export '../../../wallets/isar/models/wallet_solana_token_info.dart';

0 commit comments

Comments
 (0)