New vertex
Initializing a new vertex.
Each device needs to initialize the Vertices SDK before being able to use it by calling the function vertices_new() and passing the vertex configuration. Let's see what are the configuration parameters needed.

The provider

The provider is the remote API that will be interrogated to interact with the blockchain. In the case of the Algorand API, an HTTP REST API is being used. It comprises 4 pieces of information:
    url: the base URL, in the case of Algorand we can think about Algoexplorer, Purestake, or even your local node (localhost). Make sure to provide the API on the network you want. The Testnet will always be used in the examples.
    port: the server port for the HTTP connection.
    header: some API providers require a specific header to pass an authorization token, for example, that's the case of Purestake. The header field will let the user pass those specific headers. Other headers might be appended as well in the HTTP calls.
    cert_pem: some HTTP clients need the provider certificates. That's not the case on Unix when using libcurl but the ESP32 example needs the certificate to be linked into the program.
To get the Algoexplorer certificates from the command line:
openssl s_client -showcerts -verify 5 -connect api.testnet.algoexplorer.io:443
Now that we have gathered the provider information, we can create the structure encapsulating it:
1
static provider_info_t algoexplorer_provider =
2
{.url = (char *) "https://api.testnet.algoexplorer.io",
3
.port = 0,
4
.header = (char *) ""};
5
​
6
static provider_info_t purestake_provider =
7
{.url = (char *) "https://testnet-algorand.api.purestake.io/ps2",
8
.port = 0,
9
.header = (char *) "x-api-key:xxx"}; // replace the xxx with your token
Copied!

The event handler

The user needs to implement a callback function that must be passed when initializing the library:
1
static ret_code_t
2
vertices_evt_handler(vtc_evt_t *evt)
3
{
4
/* to be implemented in the next chapter */
5
}
Copied!
The next chapter will be dedicated to understanding and responding to the different events emitted by the library.

Initialization

We can now create the vertex configuration structure and try to init the library. Below is the content of the minimum main.c file:
1
#include <stdio.h>
2
#include "vertices.h"
3
​
4
static provider_info_t algoexplorer_provider =
5
{.url = (char *) "https://api.testnet.algoexplorer.io",
6
.port = 0,
7
.header = (char *) ""};
8
​
9
static ret_code_t
10
vertices_evt_handler(vtc_evt_t *evt)
11
{
12
/* to be implemented in the next chapters */
13
14
return VTC_ERROR_INTERNAL;
15
}
16
​
17
int main(int argc, char *argv[])
18
{
19
printf("Hello world\n");
20
​
21
// vertex configuration
22
vertex_t vertex = {
23
.provider = &algoexplorer_provider,
24
.vertices_evt_handler = vertices_evt_handler
25
};
26
​
27
// create new vertex
28
ret_code_t err_code = vertices_new(&vertex);
29
VTC_ASSERT(err_code);
30
​
31
return 0;
32
}
Copied!

Compile and Run

Let's compile and run the example to see what's happening:
1
$ cd build && cmake .. && make myproject
2
[ 0%] Built target cjson
3
[ 80%] Built target mbedcrypto
4
[100%] Built target vertices
5
[100%] Linking C executable myproject
6
[100%] Built target myproject
7
​
8
$ ./myproject/myproject
9
Hello world
10
πŸ”΄ [/Users/cyril/Documents/work/vertices/porting_guide/external/c-vertices-sdk/src/http_weak.c:24] Weak implementation of http_init
11
πŸ”΄ [/Users/cyril/Documents/work/vertices/porting_guide/external/c-vertices-sdk/src/vertices_errors.c:23] Fatal error: 0x3 /Users/cyril/Documents/work/vertices/porting_guide/myproject/main.c:29
Copied!
Weak implementation of http_init is printed along with a fatal error from main.c:29, meaning the Vertices library initialization failed with code error: 0x3.

Weak implementation

The Vertices SDK provides a weak implementation of the HTTP functions: init, get, post & close. Implementing those functions cannot be done right from the SDK as we don't know the target you are trying to use and the underlying HTTP stack.
Fortunately, the next chapter will be dedicated to implementing those functions on your target.
Here is the link to our implementation of the above step 🀩
Last modified 3mo ago