web3_cloud_sdk 0.0.1-dev.1 copy "web3_cloud_sdk: ^0.0.1-dev.1" to clipboard
web3_cloud_sdk: ^0.0.1-dev.1 copied to clipboard

Web3 cloud client sdk demo

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:loggy/loggy.dart';
import 'package:web3_cloud_sdk/client_sdk.dart' as clientsdk;
import 'package:web3_cloud_sdk/ethereum_lib.dart' as ethereum;

void main() {
  dotenv.load(fileName: '.env');
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'flutter sample',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'flutter sample'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final logger = Loggy('FlutterSample');

  late clientsdk.SdkBase sdk;
  ethereum.Wallet? wallet;
  String? signedTx;
  final walletId = 'user@example.com';

  void _show(String msg) {
    var snackBar = SnackBar(content: Text(msg));
    ScaffoldMessenger.of(context).showSnackBar(snackBar);
  }

  void _generateMnemonic() async {
    try {
      await clientsdk.generateMnemonic(walletId, 256);
      _show('success');
    } catch (e) {
      final errorString = clientsdk.GincoSdkError.byErrorCode(e.toString());
      logger.error(errorString);
      _show(errorString);
    }
  }

  void _getMnemonic() async {
    try {
      final mnemonic = await clientsdk.getMnemonic(walletId);
      logger.info(mnemonic);
      _show(mnemonic);
    } catch (e) {
      final errorString = clientsdk.GincoSdkError.byErrorCode(e.toString());
      logger.error(errorString);
      _show(errorString);
    }
  }

  void _importMnemonic() async {
    try {
      await clientsdk.importMnemonic(walletId, dotenv.env['MNEMONIC']!);
      _show('success');
    } catch (e) {
      final errorString = clientsdk.GincoSdkError.byErrorCode(e.toString());
      logger.error(errorString);
      _show(errorString);
    }
  }

  void _destroyMnemonic() async {
    try {
      await clientsdk.destroyMnemonic(walletId);
      _show('success');
    } catch (e) {
      final errorString = clientsdk.GincoSdkError.byErrorCode(e.toString());
      logger.error(errorString);
      _show(errorString);
    }
  }

  void _getWallet() async {
    try {
      final networkParam = ethereum.NetworkParam(
          clientsdk.ChainId.ethereumGoerli,
          clientsdk.NetworkId.ethereumGoerli,
          dotenv.env['ETH_RPC_URL_GOERLI']!);

      sdk = clientsdk.SdkBase();
      wallet = await sdk.evm.createWallet(walletId, networkParam);
      _show('wallet created');
    } catch (e) {
      final errorString = clientsdk.GincoSdkError.byErrorCode(e.toString());
      logger.error(errorString);
      _show(errorString);
    }
  }

  void _getPrivateKey() async {
    if (wallet == null) {
      _show('wallet does not exist');
      return;
    }
    try {
      final result = await wallet!.account(0).getPrivateKey();
      logger.info(result);
      _show(result);
    } catch (e) {
      final errorString = clientsdk.GincoSdkError.byErrorCode(e.toString());
      logger.error(errorString);
      _show(errorString);
    }
  }

  void _walletAddress() async {
    if (wallet == null) {
      _show('wallet does not exist');
      return;
    }
    try {
      final address = await wallet!.account(0).address();
      logger.info(address);
      _show(address);
    } catch (e) {
      final errorString = clientsdk.GincoSdkError.byErrorCode(e.toString());
      logger.error(errorString);
      _show(errorString);
    }
  }

  void _walletBalance() async {
    if (wallet == null) {
      _show('wallet does not exist');
      return;
    }
    try {
      final balance = await wallet!.account(0).balance();
      logger.info(balance);
      _show(ethereum.weiToEther(balance));
    } catch (e) {
      final errorString = clientsdk.GincoSdkError.byErrorCode(e.toString());
      logger.error(errorString);
      _show(errorString);
    }
  }

  void _walletFtBalance() async {
    if (wallet == null) {
      _show('wallet does not exist');
      return;
    }
    try {
      final balance = await wallet!
          .account(0)
          .ftBalance(ethereum.Erc20ContractAddress.goerliLink);
      logger.info(balance);
      _show(ethereum.weiToEther(balance));
    } catch (e) {
      final errorString = clientsdk.GincoSdkError.byErrorCode(e.toString());
      logger.error(errorString);
      _show(errorString);
    }
  }

  void _walletNftBalance() async {
    if (wallet == null) {
      _show('wallet does not exist');
      return;
    }
    try {
      final balance = await wallet!
          .account(0)
          .nftBalance(ethereum.Erc721ContractAddress.test);
      logger.info(balance);
      _show(balance);
    } catch (e) {
      final errorString = clientsdk.GincoSdkError.byErrorCode(e.toString());
      logger.error(errorString);
      _show(errorString);
    }
  }

  void _signTx() async {
    if (wallet == null) {
      _show('wallet does not exist');
      return;
    }

    try {
      // gas limit
      // default => 21,000
      // erc20 => 200,000 - 250,000
      // defi => 400,000 - 600,000

      signedTx = await ethereum.TxBuilder()
          .value(ethereum.etherToWei('0.0001'))
          .gasPrice('80000000000') // 80 Gwei
          .gasLimit(21000)
          .to(dotenv.env['TO_ADDRESS']!)
          .chainId(clientsdk.ChainId.ethereumGoerli)
          .sign(wallet!.account(0))
          .build();

      logger.info(signedTx);
      _show(signedTx!);
    } catch (e) {
      final errorString = clientsdk.GincoSdkError.byErrorCode(e.toString());
      logger.error(errorString);
      _show(errorString);
    }
  }

  void _sendTx() async {
    if (wallet == null || signedTx == null) {
      _show('signed tx does not exist');
      return;
    }
    try {
      final txHash = await wallet!.web3.sendTransaction(signedTx!);
      logger.info(txHash);
      _show(txHash);
    } catch (e) {
      final errorString = clientsdk.GincoSdkError.byErrorCode(e.toString());
      logger.error(errorString);
      _show(errorString);
    }
  }

  @override
  Widget build(BuildContext context) {
    Loggy.initLoggy();
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            TextButton(
              style: ButtonStyle(
                foregroundColor: MaterialStateProperty.all<Color>(Colors.blue),
              ),
              onPressed: _generateMnemonic,
              child: const Text('Generate Mnemonic'),
            ),
            TextButton(
              style: ButtonStyle(
                foregroundColor: MaterialStateProperty.all<Color>(Colors.blue),
              ),
              onPressed: _getMnemonic,
              child: const Text('Get Mnemonic'),
            ),
            TextButton(
              style: ButtonStyle(
                foregroundColor: MaterialStateProperty.all<Color>(Colors.blue),
              ),
              onPressed: _importMnemonic,
              child: const Text('Import Mnemonic'),
            ),
            TextButton(
              style: ButtonStyle(
                foregroundColor: MaterialStateProperty.all<Color>(Colors.blue),
              ),
              onPressed: _destroyMnemonic,
              child: const Text('Destroy Mnemonic'),
            ),
            TextButton(
              style: ButtonStyle(
                foregroundColor: MaterialStateProperty.all<Color>(Colors.blue),
              ),
              onPressed: _getWallet,
              child: const Text('Create Wallet'),
            ),
            TextButton(
              style: ButtonStyle(
                foregroundColor: MaterialStateProperty.all<Color>(Colors.blue),
              ),
              onPressed: _getPrivateKey,
              child: const Text('Wallet Private Key (for metamsk)'),
            ),
            TextButton(
              style: ButtonStyle(
                foregroundColor: MaterialStateProperty.all<Color>(Colors.blue),
              ),
              onPressed: _walletAddress,
              child: const Text('Wallet Address'),
            ),
            TextButton(
              style: ButtonStyle(
                foregroundColor: MaterialStateProperty.all<Color>(Colors.blue),
              ),
              onPressed: _walletBalance,
              child: const Text('Wallet Balance'),
            ),
            TextButton(
              style: ButtonStyle(
                foregroundColor: MaterialStateProperty.all<Color>(Colors.blue),
              ),
              onPressed: _walletFtBalance,
              child: const Text('Wallet FT Balance'),
            ),
            TextButton(
              style: ButtonStyle(
                foregroundColor: MaterialStateProperty.all<Color>(Colors.blue),
              ),
              onPressed: _walletNftBalance,
              child: const Text('Wallet NFT Balance'),
            ),
            TextButton(
              style: ButtonStyle(
                foregroundColor: MaterialStateProperty.all<Color>(Colors.blue),
              ),
              onPressed: _signTx,
              child: const Text('Sign Tx'),
            ),
            TextButton(
              style: ButtonStyle(
                foregroundColor: MaterialStateProperty.all<Color>(Colors.blue),
              ),
              onPressed: _sendTx,
              child: const Text('Send Tx'),
            ),
          ],
        ),
      ),
    );
  }
}
0
likes
120
pub points
0%
popularity

Publisher

unverified uploader

Web3 cloud client sdk demo

Homepage

Documentation

API reference

License

BSD-3-Clause (LICENSE)

Dependencies

bs58check, crypto, decimal, flutter, flutter_secure_storage, hex, http, pointycastle, web3dart

More

Packages that depend on web3_cloud_sdk