Skip to content

Commit ef63e35

Browse files
authored
🎨 [feat]: send method, docs update (#6) (#22)
2 parents ad58918 + 62f42b6 commit ef63e35

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,23 @@ import nanojs from "https://deno.land/x/nanojs/mod.ts";
132132
}
133133
```
134134

135+
5. ### _Send XNO_
136+
```ts
137+
const sender_private_key = "40C146373BF03EF2D62E067D38A5E6BDE2B511B5C90A99C62B6F7C3D321DDEAC";
138+
const receiver_address = "nano_1trd73o8z76wnnwmuq6y5pe6r396p7m7qf5zufrox9uk3io8foyd8mowgxu3";
139+
const amount_xno = "10.31";
140+
141+
const { error, hash } = await send_xno(sender_private_key, receiver_address, amount_xno, RPC_Node_URL.RAINSTROM);
142+
143+
console.log({ error, hash });
144+
```
145+
146+
```bash
147+
{
148+
hash: "142A538F36833D1CC78B94E11C766F75818F8B940771335C6C1B8AB880C5BB1D"
149+
}
150+
```
151+
135152
<br />
136153

137154
</details>

config/rpc_node_url.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export enum RPC_Node_URL {
22
NANOS = "https://proxy.nanos.cc/proxy",
3+
RAINSTROM = "https://rainstorm.city/api"
34
}

mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export * from "./src/account/info.ts";
66
export * from "./src/account/pending_blocks.ts";
77
export * from "./src/account/receive_pending_block.ts";
88
export * from "./src/account/keys.ts";
9+
export * from "./src/account/send.ts";
910

1011
/**
1112
* Block

src/account/send.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { RPC_Node_URL } from "../../config/rpc_node_url.ts";
2+
import { representatives } from "../../config/representatives.ts";
3+
import { BigNumber, nanocurrency } from "../../deps.ts";
4+
import {
5+
get_account_info,
6+
get_account_keys,
7+
Publish_Block,
8+
publish_block,
9+
work_generate,
10+
xno_to_raw,
11+
} from "../../mod.ts";
12+
13+
export type Send_XNO = Publish_Block;
14+
15+
/**
16+
*
17+
* @name Send Amount
18+
* @param {string} sender_private_key
19+
* @param {string} receiver_address
20+
* @param {string} amount_xno
21+
* @param {RPC_Node_URL} rpc_node_url
22+
* @returns {Send_XNO}
23+
24+
* @example
25+
* ```ts
26+
* import { RPC_Node_URL, send_xno } from './deps.ts';
27+
*
28+
* const sender_private_key = "40C146373BF03EF2D62E067D38A5E6BDE2B511B5C90A99C62B6F7C3D321DDEAC";
29+
* const receiver_address = "nano_1trd73o8z76wnnwmuq6y5pe6r396p7m7qf5zufrox9uk3io8foyd8mowgxu3";
30+
* const amount_xno = "10.31";
31+
*
32+
* const { error, hash } = await send_xno(sender_private_key, receiver_address, amount_xno, RPC_Node_URL.RAINSTROM);
33+
* ```
34+
*/
35+
export const send_xno = async (
36+
sender_private_key: string,
37+
receiver_address: string,
38+
amount_xno: string,
39+
rpc_node_url: RPC_Node_URL = RPC_Node_URL.RAINSTROM,
40+
): Promise<Send_XNO> => {
41+
const { address, private_key } = get_account_keys(sender_private_key);
42+
const sender_account_info = await get_account_info(address, rpc_node_url);
43+
44+
if ("error" in sender_account_info) {
45+
return { error: sender_account_info.error };
46+
}
47+
48+
const { error, work } = await work_generate(
49+
sender_account_info.frontier,
50+
rpc_node_url,
51+
);
52+
53+
if (error || !work) return { error };
54+
55+
const sender = {
56+
address,
57+
balance: new BigNumber(sender_account_info.balance),
58+
frontier: sender_account_info.frontier,
59+
work: work,
60+
};
61+
62+
const amount_to_send_raw = xno_to_raw(amount_xno);
63+
const balance = (sender.balance).minus(amount_to_send_raw);
64+
65+
if (balance.isGreaterThanOrEqualTo(0)) {
66+
const block_data: nanocurrency.BlockData = {
67+
balance: balance.toFixed(),
68+
link: receiver_address,
69+
previous: sender.frontier,
70+
representative: representatives.KRAKEN,
71+
work: sender.work,
72+
};
73+
74+
const { block } = nanocurrency.createBlock(private_key, block_data);
75+
return await publish_block(block, rpc_node_url);
76+
} else return { error: "No sufficient balance" };
77+
};

0 commit comments

Comments
 (0)