Integration
We provided an SDK To make it easy to use Abeld services. For the Abeld SDK to work, your app needs to support deep linking in order to send and receive data from Abeld’s supported crypto currency wallets. If your app don’t support deep linking yet, we suggest you uni_links | Flutter Package.
To add Abled sdk to your Flutter app run the following command:
SDK Setup
flutter pub add abeld_flutter_sdk
Initialize the SDK as early as it’s possible:
ABX.initializeApp(
apiKey: "YOUR_PROJECT_API_KEY",
iosDeepLinkBaseUrl: "YOUR_PROJECT_IOS_DEEP_LINK_BASE_URI",
androidDeepLinkBaseUrl: "YOUR_PROJECT_ANDROID_DEEP_LINK_BASE_URI",
appUrl: "YOUR_APP_WEBSITE_URL_FOR_THE_NAME_AND_LOGO"
);
Operations
We provided some functions so the business could easily perform the gamification logic.
Instance
After initializing the abled SDK you can access the instance using this snippet:
ABX.instance
Connect
For spend operation (Sending tokens from the app’s user wallet to abeld) we need to get a wallet connection instance:
ABX.instance.connect(
internalId: "USER_ID_IN_YOUR_APP",
callback: ({required String status}) {
if (status == "success") {
// handle the successful connection
}
})
After calling this piece of code the installed Phantom wallet app on the user’s phone will be opened and the user should approve the connection. Otherwize, we cannot perform the other operations.
The connection will be persistent (If the user closes the app and opens it again, the SDK will recover the instance. We will be able to check the connection using:
ABX.instance.isConnected;
To disconnect and remove the wallet connection instance from the memory we can use:
ABX.instance.disconnect();
Spend
This operation allows the app’s user to send tokens from their wallet to the project’s wallet which is specified in the panel.
Please make sure you provide an action key with the User to Business direction to this method.
ABX.instance.spend(
actionKey: "ACTION_KEY",
callback: ({required String status}) {
if(status == "loading") {
// handle loading
} else if (status == "success") {
// handle the successful connection
}
})
After calling this piece of code the installed Phantom wallet app on the user’s phone will be opened and the user should confirm the transaction to send tokens to the business wallet.
Earn
This operation allows the business to transfer some amount of token from their wallet to the user’s wallet.It also can be done through the backend. However, make sure your project user connected their wallet to your project before.
Using the Abeld SDK
if(ABX.instance.hasWalletConnectionInstance == true) {
/// Get encoded transaction instruction
String encodedTransactionInstruction = await ABX.instance
.getTransactionInstruction(
"EARN_ACTION_KEY");
/// Send the transaction and get the signature
Response res = await post(
Uri.parse("http://192.168.1.100:3001/send-transaction"),
body: {
"encodedTransaction": encodedTransactionInstruction,
"recipientPubkey": ABX.instance.getUserPublicKey()
});
String signature = res.body;
/// Confirm the transaction
await ABX.instance.confirmTransaction(signature);
} else {
/// Connect first
}
It’s a three-step operation.
- An encrypted-unsigned transaction instruction will be generated on Abeld cloud using the given action key and project data.
- The transaction will be signed and sent in your backend server using the given project’s wallet secret key.
- Finally, the transaction will be confirmed on the Abeld server.
Using the Abeld Apis:
Get transaction instruction:
https://us-central1-abeld-reward.cloudfunctions.net/getTransactionInstruction
Params
{
"pubKey": "41844b2f06199ef81135817dbc16309fdf6d70cd",
"apiKey": "PROJECT_API_KEY",
"userId": "PROJECT_INTERNAL_USER_ID",
"actionKey": "ACTION_KEY"
}
Returns:
{
"data": "ENCODED_TRANSACTION_STRING",
"message": "success",
}
Confirm transaction:
https://us-central1-abeld-reward.cloudfunctions.net/confirmTransaction
Params
{
"pubKey": "41844b2f06199ef81135817dbc16309fdf6d70cd",
"apiKey": "PROJECT_API_KEY",
"signature": "SIGNATURE_AFTER_SENDING_THE_TRANSACTION"
}
Returns:
{
"message": "success",
}
Send Transaction Snippet:
To make it more secure, we let the businesses sign and send the Earn transactions (Tokens from business to user) on their own servers using their wallet secret keys.
As we saw in the previous sections, this part needs to be called and handled by the businesses. However the signature needs to be returned using a code snippet like the following:
const encodedTransaction = req.body.encodedTransaction;
const recipientPubkey = req.body.recipientPubkey;
const mintAddress =
new PublicKey("Ddw7WRuQUwbMggRb85k6dKUpbJ19cGM7AGG2ZFnYLau2");
const secretKey = "YOUR_PROJECT_WALLET_SECRET"
const connection = new Connection(clusterApiUrl('devnet'));
const senderKeypair = Keypair.fromSecretKey(
base58.decode(secretKey),
);
try {
const recipientAccount = await getOrCreateAssociatedTokenAccount(
connection,
senderKeypair,
mintAddress,
recipientPubkey,
);
console.log('recipientAccount', recipientAccount);
} catch (e) {
console.log('recipientAccount.error', e.message);
}
const senderAccount = await getOrCreateAssociatedTokenAccount(
connection,
senderKeypair,
mintAddress,
senderKeypair.publicKey,
);
console.log('senderAccount', senderAccount);
const transaction = Transaction.from(Buffer.from(encodedTransaction, 'base64'));
// Business submits the transaction using the given encoded string:
const signature = await connection.sendTransaction(
transaction,
[senderKeypair],
);
To do so, you may need to install these two packages on your node.js backend:
yarn add @solana/web3.js @solana/spl-token bs58