Utilities & Helpers
Utilities class provides you with number of useful blockchain related functionalities.
Overview of functionality:
- Account status
- Balance for valid Ethereum address
- Balance for valid ZumoKit keystore file
- Currency conversion
- Ethereum to Gwei
- Gwei to Ethereum
- Wei to Ethereum
- Ethereum to Wei
Android
Utilities on Android are handled through Utils
class that can be accessed via utils method on
your ZumoKit
instance.
Account status
Balance for valid Ethereum address
import com.zumo.zumokit.ZumoKit;
import com.zumo.zumokit.Utils;
// Create Utils instance
Utils utils = zumoKit.utils();
// Get balance for any valid ethereum address
String balance = utils.ethGetBalance("0xF44d16Ea8168c98572D95eFC03631E9Ed32dada1");
Balance for valid ZumoKit keystore file
import com.zumo.zumokit.ZumoKit;
import com.zumo.zumokit.Utils;
import com.zumo.zumokit.Store;
// Create Utils instance
Utils utils = zumoKit.utils();
// Retrieve first available keystore, will fail if none available
Keystore keystore = zumoKit.store().getKeystores(Currency.ETH).get(0);
//Get balance for our keystore
String balance = utils.getBalance(keystore);
Currency conversion
Ethereum to Gwei
import com.zumo.zumokit.ZumoKit;
import com.zumo.zumokit.Utils;
// Create Utils instance
Utils utils = zumoKit.utils();
// Converting 1 eth to gwei
String gwei = utils.ethToGwei("1");
Gwei to Ethereum
import com.zumo.zumokit.ZumoKit;
import com.zumo.zumokit.Utils;
// Create Utils instance
Utils utils = zumoKit.utils();
// Converting 1000000000 gwei to eth
String eth = utils.gweiToEth("1000000000");
Ethereum to Wei
import com.zumo.zumokit.ZumoKit;
import com.zumo.zumokit.Utils;
// Create Utils instance
Utils utils = zumoKit.utils();
// Converting 1 eth to wei
String wei = utils.ethToWei("1");
Wei to Ethereum
import com.zumo.zumokit.ZumoKit;
import com.zumo.zumokit.Utils;
// Create Utils instance
Utils utils = zumoKit.utils();
// Converting 1000000000000000000 wei to eth
String eth = utils.weiToEth("1000000000000000000");
iOS
Utilities on iOS is handled by the CPUtils
class. An instance of this class can be obtained from the utils
property on your ZumoKitImpl
instance.
Account status
Balance for valid Ethereum address
#import <ZumoKitSDK/ZumoKit.h>
#import <ZumoKitSDK/CPUtils.h.h>
// Get utils property from zumokit implementation
CPUtils *utils = [_zumoKit utils];
// Get balance for any valid ethereum address
NSString *balance = [utils ethGetBalance:@"0xF44d16Ea8168c98572D95eFC03631E9Ed32dada1"];
Balance for valid ZumoKit keystore file
#import <ZumoKitSDK/ZumoKit.h>
#import <ZumoKitSDK/CPUtils.h>
#import <ZumoKitSDK/CPKeystore.h>
// Get utils property from zumokit implementation
CPUtils *utils = [_zumoKit utils];
// Get all keystores in storage
NSArray *allKeystores = [_zumoKit.store getKeystores:CPCurrencyETH];
// Select first available keystore
CPKeystore keystore = [allKeystores objectAtIndex:0];
//Get balance for our keystore
NSString *balance = [utils getBalance:keystore];
Currency conversion
Ethereum to Gwei
#import <ZumoKitSDK/ZumoKit.h>
#import <ZumoKitSDK/CPUtils.h>
// Get utils property from zumokit implementation
CPUtils *utils = [_zumoKit utils];
// Converting 1 eth to gwei
NSString *gwei = [utils ethToGwei:@"1"];
Gwei to Ethereum
#import <ZumoKitSDK/ZumoKit.h>
#import <ZumoKitSDK/CPUtils.h>
// Get utils property from zumokit implementation
CPUtils *utils = [_zumoKit utils];
// Converting 1000000000 gwei to eth
NSString *eth = [utils gweiToEth:@"1000000000"];
Ethereum to Wei
#import <ZumoKitSDK/ZumoKit.h>
#import <ZumoKitSDK/CPUtils.h>
// Get utils property from zumokit implementation
CPUtils *utils = [_zumoKit utils];
// Converting 1 eth to gwei
NSString *wei = [utils ethToWei:@"1"];
Wei to Ethereum
#import <ZumoKitSDK/ZumoKit.h>
#import <ZumoKitSDK/CPUtils.h>
// Get utils property from zumokit implementation
CPUtils *utils = [_zumoKit utils];
// Converting 1000000000000000000 wei to eth
NSString *eth = [utils weiToEth:@"1000000000000000000"];
React Native
The React Native version of ZumoKit provides a ZKUtility
class with a few helper functions to make working with the blockchain easier. Not all native methods are currently exposed to React Native and some methods such as obtaining the balance for a wallet have been relocated.
Account status
Balance for unlocked wallet
Currently, ZumoKit on React Native only allows you to obtain the balance for an unlocked wallet. Once you have created or retrieved a wallet its balance can be pulled with the getBalance()
method which returns the balance in ETH.
The promise will be rejected and an exception thrown if there was a problem obtaining the balance. The most likely reason for this is that the wallet was not unlocked before calling getBalance()
.
import ZumoKit from "react-native-zumo-kit";
try {
const wallet = await ZumoKit.getWallet();
await wallet.unlock("password123");
const balance = await wallet.getBalance();
} catch (error) {
// Handle error here
}
// Balance was successfully obtained.
console.log(balance);
Currency Exchange
Ethereum to Fiat
In the likely event you wish to convert Ethereum to Fiat, this can be achieved with the getFiat(eth)
method on the ZKUtility
class. Currently this method only returns the GBP value for the given Ethereum amount.
import ZumoKit, { ZKUtility } from 'react-native-zumo-kit';
const fiat = await ZKUtility.getFiat(1.00);
Exchange rates
Exchange rates to multiple fiat and crypto currencies can also be obtained from the ZKUtility
class through the getExchangeRates()
method.
import ZumoKit, { ZKUtility } from 'react-native-zumo-kit';
const rates = await ZKUtility.getExchangeRates();
Other
Validate Ethereum address
Whether an Ethereum address is valid or not can be determined through the ZKUtility
class' isValidEthAddress(address)
method.
import ZumoKit, { ZKUtility } from 'react-native-zumo-kit';
const isValid = await ZKUtility.isValidEthAddress('0x123...');