Skip to content

Commit

Permalink
add missing min_ada_required() and fix txBody.ttl()
Browse files Browse the repository at this point in the history
  • Loading branch information
v-almonacid committed Feb 19, 2021
1 parent 6584c07 commit aef38ca
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ public final void hashTransaction(String txBody, Promise promise) {
.pour(promise);
}

@ReactMethod
public final void minAdaRequired(String assets, String minimumUtxoVal, Promise promise) {
Native.I
.minAdaRequired(new RPtr(assets), new RPtr(minimumUtxoVal))
.map(RPtr::toJs)
.pour(promise);
}

// BigNum

@ReactMethod
Expand Down Expand Up @@ -1495,6 +1503,7 @@ public final void transactionBodyFee(String txBody, Promise promise) {
public final void transactionBodyTtl(String txBody, Promise promise) {
Native.I
.transactionBodyTtl(new RPtr(txBody))
.map(Long::intValue)
.pour(promise);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ private Native() { }
public final native Result<RPtr> makeIcarusBootstrapWitness(RPtr txBodyHash, RPtr addr, RPtr key);
public final native Result<RPtr> makeVkeyWitness(RPtr txBodyHash, RPtr sk);
public final native Result<RPtr> hashTransaction(RPtr txBody);
public final native Result<RPtr> minAdaRequired(RPtr assets, RPtr minUtxoVal);

// BigNum
public final native Result<RPtr> bigNumFromStr(String str);
Expand Down
1 change: 1 addition & 0 deletions example/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export default class App extends Component<{}> {
this.setState({
status: 'tests finished',
})
console.log('test finished successfully')
mounted = true
}
}
Expand Down
16 changes: 16 additions & 0 deletions example/runtimeTests/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import {
make_icarus_bootstrap_witness,
make_vkey_witness,
hash_transaction,
min_ada_required,
BootstrapWitness,
TransactionHash,
ByronAddress,
Bip32PrivateKey,
Vkeywitness,
TransactionBody,
Value,
Coin,
BigNum,
} from '@emurgo/react-native-haskell-shelley'

import {assert} from '../util'
Expand Down Expand Up @@ -83,6 +87,18 @@ const test: () => void = async () => {
'hash_transaction should return instance of TransactionHash',
)
assert(hash.ptr !== undefined, 'hash_transaction:: returns non-undefined')

/**
* min_ada_required
*/
const value = await Value.new(await Coin.from_str('200'))
const minUtxoVal = await Coin.from_str('1000000')
const minAda = await min_ada_required(value, minUtxoVal)
assert(
minAda instanceof BigNum,
'min_ada_required should return instance of BigNum',
)
assert(minAda.ptr !== undefined, 'min_ada_required:: returns non-undefined')
}

export default test
10 changes: 10 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ export const hash_transaction: (
txBody: TransactionBody,
) => Promise<TransactionHash>;

/**
* @param {Value} assets
* @param {BigNum} minUtxoVal
* @returns {Promise<BigNum>}
*/
export const min_ada_required: (
assets: Value,
minUtxoVal: BigNum,
) => Promise<BigNum>

/**
* Generic u64 wrapper for platforms that don't support u64 or BigInt/etc
* This is an unsigned type - no negative numbers.
Expand Down
12 changes: 12 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ export const make_vkey_witness = async (txBodyHash, sk) => {
return Ptr._wrap(ret, Vkeywitness);
};

/**
* @param {Value} assets
* @param {BigNum} minUtxoVal
* @returns {Promise<BigNum>}
*/
export const min_ada_required = async (assets, minUtxoVal) => {
const assetsPtr = Ptr._assertClass(assets, Value);
const minUtxoValPtr = Ptr._assertClass(minUtxoVal, BigNum);
const ret = await HaskellShelley.minAdaRequired(assetsPtr, minUtxoValPtr);
return Ptr._wrap(ret, BigNum);
};

/**
* @param {TransactionBody} txBody
* @returns {Promise<TransactionHash>}
Expand Down
12 changes: 12 additions & 0 deletions ios/HaskellShelley.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ @implementation HaskellShelley
}] exec:txBodyPtr andResolve:resolve orReject:reject];
}

RCT_EXPORT_METHOD(minAdaRequired:(nonnull NSString *)assetsPtr withMinUtxoVal:(nonnull NSString *)minUtxoValPtr withResolve:(RCTPromiseResolveBlock)resolve andReject:(RCTPromiseRejectBlock)reject)
{
[[CSafeOperation new:^NSString*(NSArray* params, CharPtr* error) {
RPtr assets = [[params objectAtIndex:0] rPtr];
RPtr minUtxoVal = [[params objectAtIndex:1] rPtr];
RPtr result;
return utils_min_ada_required(assets, minUtxoVal, &result, error)
? [NSString stringFromPtr:result]
: nil;
}] exec:@[assetsPtr, minUtxoValPtr] andResolve:resolve orReject:reject];
}

// BigNum

RCT_EXPORT_METHOD(bigNumFromStr:(nonnull NSString *)string withResolve:(RCTPromiseResolveBlock)resolve andReject:(RCTPromiseRejectBlock)reject)
Expand Down
8 changes: 5 additions & 3 deletions rust/src/android/transaction_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@ pub unsafe extern "C" fn Java_io_emurgo_rnhaskellshelley_Native_transactionBodyT
let rptr = ptr.rptr(&env)?;
rptr
.typed_ref::<TransactionBody>()
.map(|tx_body| tx_body.ttl())
.map(|tx_body| tx_body.ttl().map(|ttl| (ttl as jlong)))
.and_then(|ttl| {
let ttl_jint: jlong = ttl.unwrap().into();
ttl_jint.jobject(&env)
match ttl {
Some(ttl) => ttl.jobject(&env),
None => Ok(JObject::null())
}
})
})
.jresult(&env)
Expand Down
26 changes: 25 additions & 1 deletion rust/src/android/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ use jni::sys::{jbyteArray, jobject};
use jni::JNIEnv;
use crate::utils::ToFromBytes;

use cardano_serialization_lib::utils::{hash_transaction, make_vkey_witness, make_icarus_bootstrap_witness};
use cardano_serialization_lib::utils::{
hash_transaction,
make_vkey_witness,
make_icarus_bootstrap_witness,
min_ada_required,
Value,
BigNum
};
use cardano_serialization_lib::{TransactionBody};
use cardano_serialization_lib::crypto::{Bip32PrivateKey, PrivateKey, TransactionHash};
use cardano_serialization_lib::address::ByronAddress;
Expand Down Expand Up @@ -94,3 +101,20 @@ pub unsafe extern "C" fn Java_io_emurgo_rnhaskellshelley_Native_hashTransaction(
})
.jresult(&env)
}

#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn Java_io_emurgo_rnhaskellshelley_Native_minAdaRequired(
env: JNIEnv, _: JObject, assets: JRPtr, minimum_utxo_val: JRPtr
) -> jobject {
handle_exception_result(|| {
let assets = assets.rptr(&env)?;
let minimum_utxo_val = minimum_utxo_val.rptr(&env)?;
assets.typed_ref::<Value>().zip(minimum_utxo_val.typed_ref::<BigNum>()).and_then(
|(assets, minimum_utxo_val)| {
min_ada_required(assets, minimum_utxo_val).rptr().jptr(&env)
}
)
})
.jresult(&env)
}
17 changes: 16 additions & 1 deletion rust/src/ios/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::utils::ToFromBytes;
use crate::panic::*;
use crate::ptr::{RPtr, RPtrRepresentable};

use cardano_serialization_lib::utils::{hash_transaction, make_vkey_witness, make_icarus_bootstrap_witness};
use cardano_serialization_lib::utils::{hash_transaction, make_vkey_witness, make_icarus_bootstrap_witness, min_ada_required, Value, BigNum};
use cardano_serialization_lib::{TransactionBody};
use cardano_serialization_lib::crypto::{Bip32PrivateKey, PrivateKey, TransactionHash};
use cardano_serialization_lib::address::ByronAddress;
Expand Down Expand Up @@ -76,3 +76,18 @@ pub unsafe extern "C" fn utils_hash_transaction(
.map(|hash| hash.rptr())
.response(result, error)
}

#[no_mangle]
pub unsafe extern "C" fn utils_min_ada_required(
assets: RPtr, min_utxo_val: RPtr, result: &mut RPtr, error: &mut CharPtr
) -> bool {
handle_exception_result(|| {
assets.typed_ref::<Value>()
.zip(min_utxo_val.typed_ref::<BigNum>())
.map(|(assets, min_utxo_val)| {
make_vkey_witness(assets, min_utxo_val)
})
})
.map(|min_ada| min_ada.rptr())
.response(result, error)
}

0 comments on commit aef38ca

Please sign in to comment.