mirror of
https://github.com/cypherstack/stack_wallet.git
synced 2025-02-02 19:26:37 +00:00
get token contract info updates and fixes
This commit is contained in:
parent
40cceed8e6
commit
09b2c68cd5
4 changed files with 21 additions and 34 deletions
|
@ -17,11 +17,8 @@ import 'package:stackwallet/widgets/stack_dialog.dart';
|
|||
class AddCustomTokenView extends ConsumerStatefulWidget {
|
||||
const AddCustomTokenView({
|
||||
Key? key,
|
||||
required this.walletId,
|
||||
}) : super(key: key);
|
||||
|
||||
final String? walletId;
|
||||
|
||||
static const routeName = "/addCustomToken";
|
||||
|
||||
@override
|
||||
|
@ -86,7 +83,7 @@ class _AddCustomTokenViewState extends ConsumerState<AddCustomTokenView> {
|
|||
label: "Search",
|
||||
onPressed: () async {
|
||||
final response = await showLoading(
|
||||
whileFuture: EthereumAPI.getTokenByContractAddress(
|
||||
whileFuture: EthereumAPI.getTokenContractInfoByAddress(
|
||||
contractController.text),
|
||||
context: context,
|
||||
message: "Looking up contract",
|
||||
|
@ -96,10 +93,6 @@ class _AddCustomTokenViewState extends ConsumerState<AddCustomTokenView> {
|
|||
nameController.text = currentToken!.name;
|
||||
symbolController.text = currentToken!.symbol;
|
||||
decimalsController.text = currentToken!.decimals.toString();
|
||||
if (widget.walletId != null) {
|
||||
// TODO: this needs to be changed?
|
||||
currentToken!.walletIds.add(widget.walletId!);
|
||||
}
|
||||
} else {
|
||||
nameController.text = "";
|
||||
symbolController.text = "";
|
||||
|
|
|
@ -82,7 +82,6 @@ class _AddWalletViewState extends ConsumerState<AddWalletView> {
|
|||
Future<void> _addToken() async {
|
||||
final token = await Navigator.of(context).pushNamed(
|
||||
AddCustomTokenView.routeName,
|
||||
arguments: null, // no walletId as no wallet has been selected/created yet
|
||||
);
|
||||
if (token is EthContract) {
|
||||
await MainDB.instance.putEthContract(token);
|
||||
|
|
|
@ -223,18 +223,13 @@ class RouteGenerator {
|
|||
return _routeError("${settings.name} invalid args: ${args.toString()}");
|
||||
|
||||
case AddCustomTokenView.routeName:
|
||||
if (args is String?) {
|
||||
return getRoute(
|
||||
shouldUseMaterialRoute: useMaterialPageRoute,
|
||||
builder: (_) => AddCustomTokenView(
|
||||
walletId: args,
|
||||
),
|
||||
settings: RouteSettings(
|
||||
name: settings.name,
|
||||
),
|
||||
);
|
||||
}
|
||||
return _routeError("${settings.name} invalid args: ${args.toString()}");
|
||||
return getRoute(
|
||||
shouldUseMaterialRoute: useMaterialPageRoute,
|
||||
builder: (_) => const AddCustomTokenView(),
|
||||
settings: RouteSettings(
|
||||
name: settings.name,
|
||||
),
|
||||
);
|
||||
|
||||
case SingleFieldEditView.routeName:
|
||||
if (args is Tuple2<String, String>) {
|
||||
|
|
|
@ -362,35 +362,35 @@ abstract class EthereumAPI {
|
|||
slow: feesSlow.toInt());
|
||||
}
|
||||
|
||||
static Future<EthereumResponse<EthContract>> getTokenByContractAddress(
|
||||
static Future<EthereumResponse<EthContract>> getTokenContractInfoByAddress(
|
||||
String contractAddress) async {
|
||||
try {
|
||||
final response = await get(Uri.parse(
|
||||
"$etherscanApi?module=token&action=getToken&contractaddress=$contractAddress&apikey=EG6J7RJIQVSTP2BS59D3TY2G55YHS5F2HP"));
|
||||
// "stackURI?module=token&action=getToken&contractaddress=$contractAddress"));
|
||||
// "$blockScout?module=token&action=getToken&contractaddress=$contractAddress"));
|
||||
final response = await get(
|
||||
Uri.parse(
|
||||
"$stackBaseServer/tokens?addrs=$contractAddress&parts=all",
|
||||
),
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final json = jsonDecode(response.body);
|
||||
if (json["message"] == "OK") {
|
||||
final map = Map<String, dynamic>.from(json["result"] as Map);
|
||||
EthContract? token;
|
||||
if (map["type"] == "ERC-20") {
|
||||
if (map["isErc20"] == true) {
|
||||
token = EthContract(
|
||||
address: map["contractAddress"] as String,
|
||||
decimals: int.parse(map["decimals"] as String),
|
||||
address: map["address"] as String,
|
||||
decimals: map["decimals"] as int,
|
||||
name: map["name"] as String,
|
||||
symbol: map["symbol"] as String,
|
||||
type: EthContractType.erc20,
|
||||
walletIds: [],
|
||||
);
|
||||
} else if (map["type"] == "ERC-721") {
|
||||
} else if (map["isErc721"] == true) {
|
||||
token = EthContract(
|
||||
address: map["contractAddress"] as String,
|
||||
decimals: int.parse(map["decimals"] as String),
|
||||
address: map["address"] as String,
|
||||
decimals: map["decimals"] as int,
|
||||
name: map["name"] as String,
|
||||
symbol: map["symbol"] as String,
|
||||
type: EthContractType.erc721,
|
||||
walletIds: [],
|
||||
);
|
||||
} else {
|
||||
throw EthApiException(
|
||||
|
|
Loading…
Reference in a new issue