Skip to content
This repository was archived by the owner on Jul 31, 2019. It is now read-only.

Commit b706e0f

Browse files
hadleymrytmarsh
authored andcommitted
Implements API call vcx_wallet_send_tokens (#309)
* vcx_wallet_send_tokens API call. * Modifies python Arbitrary Payment Test * Changes Test for Nodejs and Payment to Error With Insufficient Funds
1 parent 737a4be commit b706e0f

File tree

3 files changed

+53
-17
lines changed

3 files changed

+53
-17
lines changed

vcx/libvcx/src/api/wallet.rs

+39-8
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ use std::thread;
66
use utils::cstring::CStringUtils;
77
use utils::error;
88
use utils::error::error_string;
9+
use error::ToErrorCode;
910
use serde_json;
10-
use utils::libindy::payments::{get_wallet_token_info, create_address};
11+
use utils::libindy::payments::{pay_a_payee, get_wallet_token_info, create_address};
1112

1213
/// Get the total balance from all addresses contained in the configured wallet
1314
///
@@ -371,13 +372,20 @@ pub extern fn vcx_wallet_send_tokens(command_handle: u32,
371372
command_handle, payment_handle, tokens, recipient);
372373

373374
thread::spawn(move|| {
374-
let msg = format!("{{\"paid\":\"true\"}}");
375-
376-
info!("vcx_wallet_send_tokens_cb(command_handle: {}, rc: {}, receipt: {})",
377-
command_handle, error_string(0), msg);
378-
379-
let msg = CStringUtils::string_to_cstring(msg);
380-
cb(command_handle, error::SUCCESS.code_num, msg.as_ptr());
375+
match pay_a_payee(tokens, &recipient) {
376+
Ok(msg) => {
377+
info!("vcx_wallet_send_tokens_cb(command_handle: {}, rc: {}, receipt: {})",
378+
command_handle, error_string(0), msg);
379+
let msg = CStringUtils::string_to_cstring(msg);
380+
cb(command_handle, error::SUCCESS.code_num, msg.as_ptr());
381+
},
382+
Err(e) => {
383+
let msg = "Failed to send tokens".to_string();
384+
info!("vcx_wallet_send_tokens_cb(command_handle: {}, rc: {}, reciept: {})", command_handle, e.to_error_code(), msg);
385+
let msg = CStringUtils::string_to_cstring(msg);
386+
cb(command_handle, e.to_error_code(), msg.as_ptr());
387+
},
388+
}
381389
});
382390

383391
error::SUCCESS.code_num
@@ -531,4 +539,27 @@ mod tests {
531539
assert_eq!(vcx_wallet_create_payment_address(0, Some(generic_cb)), error::SUCCESS.code_num);
532540
thread::sleep(Duration::from_millis(200));
533541
}
542+
543+
#[cfg(feature = "nullpay")]
544+
#[test]
545+
fn test_send_payment() {
546+
use utils::devsetup::tests;
547+
use utils::libindy::payments::{mint_tokens, get_wallet_token_info, init_payments};
548+
let name = "test_send_payment";
549+
tests::setup_dev_env(name);
550+
init_payments().unwrap();
551+
mint_tokens(Some(1), Some(1000)).unwrap();
552+
let balance = get_wallet_token_info().unwrap().get_balance();
553+
let recipient = CStringUtils::string_to_cstring("pay:null:iXvVdM4mjCUZFrnnFU2F0VoJrkzQEoLy".to_string());
554+
let command_handle = 0;
555+
let payment_handle = 0;
556+
let tokens = 100;
557+
let cb = generic_cb;
558+
let err = vcx_wallet_send_tokens(command_handle, payment_handle, tokens, recipient.as_ptr(), Some(cb));
559+
assert_eq!(err,0);
560+
thread::sleep(Duration::from_secs(2));
561+
let new_balance = get_wallet_token_info().unwrap().get_balance();
562+
assert_eq!(balance - tokens, new_balance);
563+
tests::cleanup_dev_env(name);
564+
}
534565
}

vcx/wrappers/node/test/walletTest.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,18 @@ describe('A Connection object with ', function () {
4949
})
5050

5151
// sendToken tests
52-
it('can send tokens', async () => {
53-
const receipt = await Wallet.sendTokens({
54-
payment: 0,
55-
tokens: 30,
56-
recipient: 'address'
57-
})
58-
assert(receipt)
52+
it('errors when sending tokens and has insufficient amount', async () => {
53+
try {
54+
const receipt = await Wallet.sendTokens({
55+
payment: 0,
56+
tokens: 30,
57+
recipient: 'address'
58+
})
59+
assert(receipt)
60+
} catch (error) {
61+
assert.equal(error.vcxCode, 1064)
62+
assert.equal(error.message, 'Insufficient amount of tokens to process request')
63+
}
5964
})
6065

6166
// createPaymentAddress tests

vcx/wrappers/python3/tests/test_wallet.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ async def test_get_token_info():
3232
@pytest.mark.asyncio
3333
@pytest.mark.usefixtures('vcx_init_test_mode')
3434
async def test_send_tokens():
35-
receipt = await Wallet.send_tokens(0,50,"address")
36-
assert receipt
35+
with pytest.raises(VcxError) as e:
36+
receipt = await Wallet.send_tokens(0,50,"address")
3737

3838
@pytest.mark.asyncio
3939
@pytest.mark.usefixtures('vcx_init_test_mode')

0 commit comments

Comments
 (0)