-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathsend_transaction.c
More file actions
85 lines (69 loc) · 3.16 KB
/
send_transaction.c
File metadata and controls
85 lines (69 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/// sending a transaction including signing it with a private key
#include <in3/client.h> // the core client
#include <in3/eth_api.h> // functions for direct api-access
#include <in3/in3_init.h> // if included the verifier will automaticly be initialized.
#include <in3/log.h> // logging functions
#include <in3/plugin.h> // the plugin api
#include <in3/signer.h> // default signer implementation
#include <in3/utils.h>
#include <stdio.h>
// fixme: This is only for the sake of demo. Do NOT store private keys as plaintext.
#define ETH_PRIVATE_KEY "0x8da4ef21b864d2cc526dbdb2a120bd2874c36c9d0a1fb7f8c63d7f7a8b41de8f"
static void send_tx_rpc(in3_t* in3);
static void send_tx_api(in3_t* in3);
int main() {
// create new incubed client
in3_t* in3 = in3_for_chain(CHAIN_ID_MAINNET);
// convert the hexstring to bytes
bytes32_t pk;
hex_to_bytes(ETH_PRIVATE_KEY, -1, pk, 32);
// create a simple signer with this key
eth_set_pk_signer(in3, pk, SIGNER_ECDSA);
// send tx using raw RPC call
send_tx_rpc(in3);
// send tx using API
send_tx_api(in3);
// cleanup client after usage
in3_free(in3);
}
void send_tx_rpc(in3_t* in3) {
// prepare 2 pointers for the result.
char *result, *error;
// send raw rpc-request, which is then verified
in3_ret_t res = in3_client_rpc(
in3, // the configured client
"eth_sendRawTransaction", // the rpc-method you want to call.
"[\"0xf892808609184e72a0008296c094d46e8dd67c5d32be8058bb8eb970870f0724456"
"7849184e72aa9d46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb9"
"70870f07244567526a06f0103fccdcae0d6b265f8c38ee42f4a722c1cb36230fe8da40315acc3051"
"9a8a06252a68b26a5575f76a65ac08a7f684bc37b0c98d9e715d73ddce696b58f2c72\"]", // the signed raw txn, same as the one used in the API example
&result, // the reference to a pointer which will hold the result
&error); // the pointer which may hold a error message
// check and print the result or error
if (res == IN3_OK) {
printf("Result: \n%s\n", result);
free(result);
}
else {
printf("Error sending tx: \n%s\n", error);
free(error);
}
}
void send_tx_api(in3_t* in3) {
// prepare parameters
address_t to, from;
hex_to_bytes("0x63FaC9201494f0bd17B9892B9fae4d52fe3BD377", -1, from, 20);
hex_to_bytes("0xd46e8dd67c5d32be8058bb8eb970870f07244567", -1, to, 20);
bytes_t* data = hex_to_new_bytes("d46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675", 82);
// send the tx
bytes_t* tx_hash = eth_sendTransaction(in3, from, to, OPTIONAL_T_VALUE(uint64_t, 0x96c0), OPTIONAL_T_VALUE(uint64_t, 0x9184e72a000), OPTIONAL_T_VALUE(uint256_t, to_uint256(0x9184e72a)), OPTIONAL_T_VALUE(bytes_t, *data), OPTIONAL_T_UNDEFINED(uint64_t));
// if the result is null there was an error and we can get the latest error message from eth_last_error()
if (!tx_hash)
printf("error sending the tx : %s\n", eth_last_error());
else {
printf("Transaction hash: ");
b_print(tx_hash);
b_free(tx_hash);
}
b_free(data);
}