@@ -45,8 +45,9 @@ use common::{
45
45
make_token_id, IsTokenFreezable , Metadata , NftIssuance , NftIssuanceV0 , TokenCreator ,
46
46
TokenId , TokenIssuance , TokenIssuanceV1 , TokenTotalSupply ,
47
47
} ,
48
- AccountNonce , AccountOutPoint , AccountSpending , ChainConfig , Destination , OutPointSourceId ,
49
- SignedTransaction , Transaction , TxInput , TxOutput , UtxoOutPoint ,
48
+ AccountCommand , AccountNonce , AccountOutPoint , AccountSpending , ChainConfig , Destination ,
49
+ OrderData , OutPointSourceId , SignedTransaction , Transaction , TxInput , TxOutput ,
50
+ UtxoOutPoint ,
50
51
} ,
51
52
primitives:: {
52
53
self , amount:: UnsignedIntType , per_thousand:: PerThousand , BlockHeight , Idable , H256 ,
@@ -793,6 +794,21 @@ pub fn data_deposit_fee(current_block_height: u64, network: Network) -> Amount {
793
794
)
794
795
}
795
796
797
+ fn parse_output_value (
798
+ chain_config : & ChainConfig ,
799
+ amount : & Amount ,
800
+ token_id : Option < String > ,
801
+ ) -> Result < OutputValue , Error > {
802
+ let amount = amount. as_internal_amount ( ) ?;
803
+ match token_id {
804
+ Some ( token_id) => {
805
+ let token_id = parse_addressable ( chain_config, & token_id) ?;
806
+ Ok ( OutputValue :: TokenV1 ( token_id, amount) )
807
+ }
808
+ None => Ok ( OutputValue :: Coin ( amount) ) ,
809
+ }
810
+ }
811
+
796
812
/// Given the parameters needed to create hash timelock contract, and a network type (mainnet, testnet, etc),
797
813
/// this function creates an output.
798
814
#[ wasm_bindgen]
@@ -806,14 +822,7 @@ pub fn encode_output_htlc(
806
822
network : Network ,
807
823
) -> Result < Vec < u8 > , Error > {
808
824
let chain_config = Builder :: new ( network. into ( ) ) . build ( ) ;
809
- let amount = amount. as_internal_amount ( ) ?;
810
- let output_value = match token_id {
811
- Some ( token_id) => {
812
- let token_id = parse_addressable ( & chain_config, & token_id) ?;
813
- OutputValue :: TokenV1 ( token_id, amount)
814
- }
815
- None => OutputValue :: Coin ( amount) ,
816
- } ;
825
+ let output_value = parse_output_value ( & chain_config, & amount, token_id) ?;
817
826
let refund_timelock = OutputTimeLock :: decode_all ( & mut & refund_timelock[ ..] )
818
827
. map_err ( |_| Error :: InvalidTimeLock ) ?;
819
828
let secret_hash =
@@ -1248,6 +1257,66 @@ pub fn effective_pool_balance(
1248
1257
Ok ( Amount :: from_internal_amount ( effective_balance) )
1249
1258
}
1250
1259
1260
+ /// Given ask and give amounts and a conclude key create output that creates an order.
1261
+ ///
1262
+ /// 'ask_token_id' parameter represents a Token is it's Some and a Coin otherwise.
1263
+ /// 'give_token_id' parameter represents a Token is it's Some and a Coin otherwise.
1264
+ #[ wasm_bindgen]
1265
+ pub fn encode_create_order_output (
1266
+ ask_amount : Amount ,
1267
+ ask_token_id : Option < String > ,
1268
+ give_amount : Amount ,
1269
+ give_token_id : Option < String > ,
1270
+ conclude_address : & str ,
1271
+ network : Network ,
1272
+ ) -> Result < Vec < u8 > , Error > {
1273
+ let chain_config = Builder :: new ( network. into ( ) ) . build ( ) ;
1274
+ let ask = parse_output_value ( & chain_config, & ask_amount, ask_token_id) ?;
1275
+ let give = parse_output_value ( & chain_config, & give_amount, give_token_id) ?;
1276
+ let conclude_key = parse_addressable :: < Destination > ( & chain_config, conclude_address) ?;
1277
+
1278
+ let order = OrderData :: new ( conclude_key, ask, give) ;
1279
+ let output = TxOutput :: CreateOrder ( Box :: new ( order) ) ;
1280
+ Ok ( output. encode ( ) )
1281
+ }
1282
+
1283
+ /// Given amount to fill order (which is described in terms of ask currency) and a destination
1284
+ /// for for result outputs create an input that fills and order.
1285
+ #[ wasm_bindgen]
1286
+ pub fn encode_input_for_fill_order (
1287
+ order_id : & str ,
1288
+ fill_amount : Amount ,
1289
+ destination : & str ,
1290
+ nonce : u64 ,
1291
+ network : Network ,
1292
+ ) -> Result < Vec < u8 > , Error > {
1293
+ let chain_config = Builder :: new ( network. into ( ) ) . build ( ) ;
1294
+ let order_id = parse_addressable ( & chain_config, order_id) ?;
1295
+ let fill_amount = fill_amount. as_internal_amount ( ) ?;
1296
+ let destination = parse_addressable :: < Destination > ( & chain_config, destination) ?;
1297
+ let input = TxInput :: AccountCommand (
1298
+ AccountNonce :: new ( nonce) ,
1299
+ AccountCommand :: FillOrder ( order_id, fill_amount, destination) ,
1300
+ ) ;
1301
+ Ok ( input. encode ( ) )
1302
+ }
1303
+
1304
+ /// Given and order id create an input that concludes an order.
1305
+ #[ wasm_bindgen]
1306
+ pub fn encode_input_for_conclude_order (
1307
+ order_id : & str ,
1308
+ nonce : u64 ,
1309
+ network : Network ,
1310
+ ) -> Result < Vec < u8 > , Error > {
1311
+ let chain_config = Builder :: new ( network. into ( ) ) . build ( ) ;
1312
+ let order_id = parse_addressable ( & chain_config, order_id) ?;
1313
+ let input = TxInput :: AccountCommand (
1314
+ AccountNonce :: new ( nonce) ,
1315
+ AccountCommand :: ConcludeOrder ( order_id) ,
1316
+ ) ;
1317
+ Ok ( input. encode ( ) )
1318
+ }
1319
+
1251
1320
#[ cfg( test) ]
1252
1321
mod tests {
1253
1322
use randomness:: Rng ;
0 commit comments