Skip to content

Commit

Permalink
馃帹 [feat]: send method, docs update (#6) (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
tanishq-singh-2407 committed Feb 9, 2023
2 parents ad58918 + 62f42b6 commit ef63e35
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,23 @@ import nanojs from "https://deno.land/x/nanojs/mod.ts";
}
```

5. ### _Send XNO_
```ts
const sender_private_key = "40C146373BF03EF2D62E067D38A5E6BDE2B511B5C90A99C62B6F7C3D321DDEAC";
const receiver_address = "nano_1trd73o8z76wnnwmuq6y5pe6r396p7m7qf5zufrox9uk3io8foyd8mowgxu3";
const amount_xno = "10.31";

const { error, hash } = await send_xno(sender_private_key, receiver_address, amount_xno, RPC_Node_URL.RAINSTROM);

console.log({ error, hash });
```

```bash
{
hash: "142A538F36833D1CC78B94E11C766F75818F8B940771335C6C1B8AB880C5BB1D"
}
```

<br />

</details>
Expand Down
1 change: 1 addition & 0 deletions config/rpc_node_url.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export enum RPC_Node_URL {
NANOS = "https://proxy.nanos.cc/proxy",
RAINSTROM = "https://rainstorm.city/api"
}
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * from "./src/account/info.ts";
export * from "./src/account/pending_blocks.ts";
export * from "./src/account/receive_pending_block.ts";
export * from "./src/account/keys.ts";
export * from "./src/account/send.ts";

/**
* Block
Expand Down
77 changes: 77 additions & 0 deletions src/account/send.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { RPC_Node_URL } from "../../config/rpc_node_url.ts";
import { representatives } from "../../config/representatives.ts";
import { BigNumber, nanocurrency } from "../../deps.ts";
import {
get_account_info,
get_account_keys,
Publish_Block,
publish_block,
work_generate,
xno_to_raw,
} from "../../mod.ts";

export type Send_XNO = Publish_Block;

/**
*
* @name Send Amount
* @param {string} sender_private_key
* @param {string} receiver_address
* @param {string} amount_xno
* @param {RPC_Node_URL} rpc_node_url
* @returns {Send_XNO}
* @example
* ```ts
* import { RPC_Node_URL, send_xno } from './deps.ts';
*
* const sender_private_key = "40C146373BF03EF2D62E067D38A5E6BDE2B511B5C90A99C62B6F7C3D321DDEAC";
* const receiver_address = "nano_1trd73o8z76wnnwmuq6y5pe6r396p7m7qf5zufrox9uk3io8foyd8mowgxu3";
* const amount_xno = "10.31";
*
* const { error, hash } = await send_xno(sender_private_key, receiver_address, amount_xno, RPC_Node_URL.RAINSTROM);
* ```
*/
export const send_xno = async (
sender_private_key: string,
receiver_address: string,
amount_xno: string,
rpc_node_url: RPC_Node_URL = RPC_Node_URL.RAINSTROM,
): Promise<Send_XNO> => {
const { address, private_key } = get_account_keys(sender_private_key);
const sender_account_info = await get_account_info(address, rpc_node_url);

if ("error" in sender_account_info) {
return { error: sender_account_info.error };
}

const { error, work } = await work_generate(
sender_account_info.frontier,
rpc_node_url,
);

if (error || !work) return { error };

const sender = {
address,
balance: new BigNumber(sender_account_info.balance),
frontier: sender_account_info.frontier,
work: work,
};

const amount_to_send_raw = xno_to_raw(amount_xno);
const balance = (sender.balance).minus(amount_to_send_raw);

if (balance.isGreaterThanOrEqualTo(0)) {
const block_data: nanocurrency.BlockData = {
balance: balance.toFixed(),
link: receiver_address,
previous: sender.frontier,
representative: representatives.KRAKEN,
work: sender.work,
};

const { block } = nanocurrency.createBlock(private_key, block_data);
return await publish_block(block, rpc_node_url);
} else return { error: "No sufficient balance" };
};

0 comments on commit ef63e35

Please sign in to comment.