This section explains the steps to import now.gg Wallet libraries to your development environment and integrate the now.gg Gaming Wallet with your Android app/game.
To get started, download the nowSDK package that contains the now.gg Wallet module and add them to your development environment.
Note: The now.gg SDK download package contains the Wallet module, demo app, and code.
1. Extract the compressed now.gg Wallet module and locate the .aar file in the package:
Wallet.aar
2. Add the following dependency to the build.gradle
file of your game:
implementation fileTree(dir: 'libs', include: ['*.aar', '*.jar'], exclude: []) implementation "com.walletconnect:sign:2.7.0" implementation "com.walletconnect:android-core:1.9.0" implementation "com.squareup.moshi:moshi:1.14.0" implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup.retrofit2:converter-gson:2.9.0' implementation 'com.squareup.retrofit2:converter-scalars:2.9.0'
Once the now.gg Wallet Module has been imported, you can now initialize it.
To initialize now.gg Wallet module, you should call the initialize
function of the NowGGWalletSdkManager
class along with the required params, as illustrated below:
object Constants { const val CHAIN_NAMESPACE: String = "eip155" const val CHAIN_ID: String = "80001" // Replace this with your chain Id const val CAIP_CHAIN_ID: String = "$CHAIN_NAMESPACE:$CHAIN_ID" const val COIN_CONTRACT_ADDRESS: String = "0x1921fd1833efb4c7b9fbd635fb6710648f497e9a" // Replace this with your own ERC20 contract address const val API_KEY: String = "<YOUR_API_KEY>" const val APP_ID: String = "<YOUR_APP_ID>" } fun initializeWalletSdk(activity: MainActivity) { NowGGWalletSdkManager.getInstance() .initialize(activity, Constants.API_KEY, Constants.APP_ID, WalletDelegateHandler(), { //on success - Call other methods only after you receive this callback. }, { // on failed }) }
public class ConstantsJava { public static final String CHAIN_NAMESPACE = "eip155"; public static final String CHAIN_ID = "80001"; //your chain id public static final String CAIP_CHAIN_ID = "$CHAIN_NAMESPACE:$CHAIN_ID"; public static final String COIN_CONTRACT_ADDRESS = "0x1921fd1833efb4c7b9fbd635fb6710648f497e9a"; // Replace this with your own ERC20 contract address public static final String API_KEY = "<YOUR_API_KEY>"; public static final String APP_ID = "<YOUR_APP_ID>"; } public void initializeWalletSdk(Activity activity) { NowGGWalletSdkManager.Companion.getInstance().initialize(activity, Constants.API_KEY, Constants.APP_ID, new WalletDelegateHandler(), () -> { //init success - Call other methods only after you receive this callback. return Unit.INSTANCE; }, (error) -> { //init failed return Unit.INSTANCE; }); }
API_KEY
and APP_ID
by emailing us at dev-support@now.gg.NowGGWalletSdkManager
class are listed here.Once you have initialized the Wallet module, you must declare a wallet delegate to receive the responses using callbacks.
Create an implementation of the NowGGWalletDelegate
as illustrated below:
class WalletDelegateHandler: NowGGWalletDelegate { // Triggered when the connection state changes. override fun onConnectionStateChange(isAvailable: Boolean) { } // Triggered when there is an error. override fun onError(error: String) { if (error == ErrorCodes.SESSION_EXPIRED){ //Indicates that the earlier established session has failed. This may be because i.) the wallet is not responding or ii.) the wallet is in the killed state. //Call Connect again to reestablish the connection with the wallet. } } // Triggered when the session request is approved by the wallet. override fun onSessionApproved(approvedSession: ApprovedSession, user: User) { val approvedSessionTopic = approvedSession.topic val username = user.name val avatarUrl = user.avatar val walletAddress = approvedSession.accounts[0].split(":")[2] } // Triggered when the session is deleted override fun onSessionDelete(deletedSession: DeletedSession) { } // Triggered when the session request is rejected by the wallet. override fun onSessionRejected(rejectedSession: RejectedSession) { } // Triggered when session information is updated. override fun onSessionUpdate(updatedSession: UpdatedSession) { } // Triggered when there is an error while purchasing token override fun onTokenPurchaseError(error: String, extra: String? ) { // handle error while purchasing token } // Triggered when token purchase is successful. override fun onTokenPurchaseSuccess(result: SessionRequestResponse, extra: String? ) { Log.d("Your Tag", "onTokenPurchaseSuccess: " + result.result + " extra: $extra") } // Triggered when a transaction is completed with an error post wallet approval request. override fun onTransactionCompletedError(transactionInfo: TransactionInfo, extra: String? ) { } // Triggered when transaction was completed successful. override fun onTransactionCompletedSuccess(transaction: TransactionInfo, extra: String? ) { // handle successful transaction } // Triggered when there is an error while sending transaction to blockchain. override fun onTransactionSendError(error: String, extra: String? ) { //handle error while sending transaction to blockchain } // Triggered once a transaction is submitted to the blockchain by Wallet. override fun onTransactionSendSuccess( txnHash: String, caipChainId: String, extra: String? ) { } }
class WalletDelegateHandler implements NowGGWalletDelegate{ // Triggered when the connection state changes. @Override public void onConnectionStateChange(boolean isAvailable) { } // Triggered when there is an error. @Override public void onError(@NonNull String error) { if (error == ErrorCodes.SESSION_EXPIRED){ //Indicates that the earlier established session has failed. This may be because i.) the wallet is not responding or ii.) the wallet is in the killed state. //Call Connect again to reestablish the connection with the wallet. } } // Triggered when the session request is approved by the wallet. @Override public void onSessionApproved(@NonNull ApprovedSession approvedSession, @NonNull User user) { String approvedSessionTopic = approvedSession.getTopic(); String username = user.getName(); String avatarUrl = user.getAvatar(); String walletAddress = approvedSession.getAccounts().get(0).split(":")[2]; } // Triggered when the session is deleted @Override public void onSessionDelete(@NonNull DeletedSession deletedSession) { } // Triggered when the session request is rejected by the wallet @Override public void onSessionRejected(@NonNull RejectedSession rejectedSession) { } // Triggered when session information is updated. @Override public void onSessionUpdate(@NonNull UpdatedSession updatedSession) { } // Triggered when there is an error while purchasing token @Override public void onTokenPurchaseError(@NonNull String error, @Nullable String extra) { // handle error while purchasing token } // Triggered when token purchase is successful. @Override public void onTokenPurchaseSuccess(@NonNull SessionRequestResponse sessionRequestResponse, @Nullable String extra) { Log.d("Your Tag", "onTokenPurchaseSuccess: " + sessionRequestResponse.getResult() + " extra:" +extra ); } // Triggered when a transaction is completed with an error post wallet approval request. @Override public void onTransactionCompletedError(@NonNull TransactionInfo transactionInfo, @Nullable String extra) { } // Triggered when transaction was completed successful. @Override public void onTransactionCompletedSuccess(@NonNull TransactionInfo transactionInfo, @Nullable String extra) { // handle successful transaction } // Triggered when there is an error while sending transaction to blockchain. @Override public void onTransactionSendError(@NonNull String error, @Nullable String extra) { //handle error while sending transaction to blockchain } // Triggered once a transaction is submitted to the blockchain by Wallet. @Override public void onTransactionSendSuccess(@NonNull String txnHash, @NonNull String caipChainId, @Nullable String extra) { } }
isAvailable
in onConnectionStateChange
callback is true, before calling any now.gg wallet functions.NowGGWalletDelegate
and the associated methods are listed here.The next step is to connect your game to the now.gg Wallet.
Connecting to the now.gg Wallet is required to carry out various operations, such as:
To connect to now.gg Wallet, call the connect
function as illustrated below:
fun connectWithWallet(activity: Activity) { try { NowGGWalletSdkManager.getInstance().connect( Constants.CAIP_CHAIN_ID, activity, { // on pairing invoked }, { error -> // on error } ) } catch (ex: SocketNotConnectedException) { //This will occur if the connection state is not available. //Please wait for the connection state to be available before calling this function. //Add your handling here. } catch (ex: WalletSdkNotInitialisedException) { //This will occur if you call this function before initialization is successful. //Add your handling here. } }
public void connectWithWallet(Activity activity) { try { NowGGWalletSdkManager.Companion.getInstance().connect(Constants.CAIP_CHAIN_ID, activity, () -> { // on pairing invoked return Unit.INSTANCE; }, (error) -> { // on error return Unit.INSTANCE; }); } catch (SocketNotConnectedException ex) { //This will occur if the connection state is not available. //Please wait for the connection state to be available before calling this function. //Add your handling here. } catch (WalletSdkNotInitialisedException ex) { //This will occur if you call this function before initialization is successful. //Add your handling here. } }
NowGGWalletSdkManager
and the associated methods are listed here.Once you have connected to the now.gg Wallet, you can use the following functions:
isAvailable
in onConnectionStateChange
callback is true, before calling any now.gg wallet functions.You can use the now.gg Wallet module to enable the purchase of supported tokens by using the buyToken
function, as illustrated below:
fun buyToken(amount: Int) { val extra = "BuyToken" try { NowGGWalletSdkManager.getInstance().buyToken(Constants.COIN_CONTRACT_ADDRESS, amount.toString(), Constants.CAIP_CHAIN_ID, extra) } catch (ex: SocketNotConnectedException) { //This will occur if the connection state is not available. //Please wait for the connection state to be available before calling this function. //Add your handling here. } catch (ex: WalletSdkNotInitialisedException) { //This will occur if you call this function before initialization is successful. //Add your handling here. } }
public void buyToken(int amount) { String extra = "BuyToken"; try { NowGGWalletSdkManager.Companion.getInstance().buyToken(Constants.COIN_CONTRACT_ADDRESS, String.valueOf(amount), Constants.CAIP_CHAIN_ID, extra); } catch (SocketNotConnectedException ex) { //This will occur if the connection state is not available. //Please wait for the connection state to be available before calling this function. //Add your handling here. } catch (WalletSdkNotInitialisedException ex) { //This will occur if you call this function before initialization is successful. //Add your handling here. } }
buyToken
function is listed here.You can request the user’s Token balance using the getTokenBalance
function and receive the Token balance as a response, as illustrated below:
fun getTokenBalance() { try { NowGGWalletSdkManager.getInstance().getTokenBalance(Constants.COIN_CONTRACT_ADDRESS, Constants.CAIP_CHAIN_ID, walletAddress, { balance -> //You can update user about the balance here }, { error -> //handle error } ) } catch (ex: WalletSdkNotInitialisedException) { //This will occur if you call this function before initialization is successful. //Add your handling here. } }
public void getTokenBalance() { try { NowGGWalletSdkManager.Companion.getInstance().getTokenBalance(Constants.COIN_CONTRACT_ADDRESS, Constants.CAIP_CHAIN_ID, walletAddress, (balance -> { //you can update user about the balance here return Unit.INSTANCE; }), (error -> { //handle error return Unit.INSTANCE; })); } catch (WalletSdkNotInitialisedException ex) { //This will occur if you call this function before initialization is successful. //Add your handling here. } }
getTokenBalance
function is listed here.You can use the sendTransaction
function to execute a transaction on the blockchain.
For instance, you can get approval to deduct a certain amount from the user’s wallet to purchase an NFT.
The following illustrates executing a contract on the blockchain using the sendTransaction
function:
Use the sendTransaction
function to get the NFT purchase approval.
fun sendTransaction() { val toAddress: String = Constants.COIN_CONTRACT_ADDRESS val nativeTokenValue = "0x0" //nftData is encoded method name with parameters of the NFT contract val nftData = "0x095ea7b3000000000000000000001111ff9f4c235d1508176cbe99891585e86b8fd3f09100000000000000000000000000000000000000000000001043561a8829300000" val extra = nftName try { NowGGWalletSdkManager.getInstance().sendTransaction( toAddress, nativeTokenValue, nftData, Constants.CAIP_CHAIN_ID, extra ) } catch (ex: SocketNotConnectedException) { //This will occur if the connection state is not available. //Please wait for the connection state to be available before calling this function. //Add your handling here. } catch (ex: WalletSdkNotInitialisedException) { //This will occur if you call this function before initialization is successful. //Add your handling here. } }
public void sendTransaction() { String toAddress = Constants.COIN_CONTRACT_ADDRESS; String nativeTokenValue = "0x0"; //hex value in wei String nftData = "0x095ea7b3000000000000000000001111ff9f4c235d1508176cbe99891585e86b8fd3f09100000000000000000000000000000000000000000000001043561a8829300000"; String extra = nftName; try { NowGGWalletSdkManager.Companion.getInstance().sendTransaction( toAddress, nativeTokenValue, nftData, Constants.CAIP_CHAIN_ID, extra ); } catch (SocketNotConnectedException ex) { //This will occur if the connection state is not available. //Please wait for the connection state to be available before calling this function. //Add your handling here. } catch (WalletSdkNotInitialisedException ex) { //This will occur if you call this function before initialization is successful. //Add your handling here. } }
nftData
is an encoded method name with parameters of the NFT contract.nftName
within the extra parameter, you may pass any additional information here based on your requirement.sendTransaction
function is listed here.You can request the list of NFTs purchased by the user with getNfts
function, as illustrated below:
fun getNfts() { try { NowGGWalletSdkManager.getInstance() .getNfts(Constants.CAIP_CHAIN_ID, "Users_walletAddress_here", { nfts: List < Nft > -> //todo: handle the list of nfts. }, { //todo: handle error }) } catch (ex: WalletSdkNotInitialisedException) { //This will occur if you call this function before initialization is successful. //Add your handling here. } }
public void getNfts() { try { NowGGWalletSdkManager.Companion.getInstance().getNfts(Constants.CAIP_CHAIN_ID, "Users_walletAddress_here", (nfts -> { //todo: handle the list of nfts return Unit.INSTANCE; }), (error -> { //todo: handle error return Unit.INSTANCE; })); } catch (WalletSdkNotInitialisedException ex) { //This will occur if you call this function before initialization is successful. //Add your handling here. } }
Note: Replace “Users_walletAddress_here” with the actual walletAddress
received in onSessionApproved
.
getNfts
function is listed here.Wallet Module
Wallet Module
Document Rev. 1.0