Skip to content

Commit 0cbe5a1

Browse files
committed
Add wasm bindings for orders
1 parent eebed1b commit 0cbe5a1

File tree

3 files changed

+135
-10
lines changed

3 files changed

+135
-10
lines changed

wasm-wrappers/WASM-API.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,22 @@ It is recommended to use a strict `Transaction` size and set the second paramete
277277
Calculate the "effective balance" of a pool, given the total pool balance and pledge by the pool owner/staker.
278278
The effective balance is how the influence of a pool is calculated due to its balance.
279279

280+
### Function: `encode_create_order_output`
281+
282+
Given ask and give amounts and a conclude key create output that creates an order.
283+
284+
'ask_token_id' parameter represents a Token is it's Some and a Coin otherwise.
285+
'give_token_id' parameter represents a Token is it's Some and a Coin otherwise.
286+
287+
### Function: `encode_input_for_fill_order`
288+
289+
Given amount to fill order (which is described in terms of ask currency) and a destination
290+
for for result outputs create an input that fills and order.
291+
292+
### Function: `encode_input_for_conclude_order`
293+
294+
Given and order id create an input that concludes an order.
295+
280296
### Enum: `Network`
281297

282298
The network, for which an operation to be done. Mainnet, testnet, etc.

wasm-wrappers/js-bindings/wasm_test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ import {
3333
encode_multisig_challenge,
3434
encode_witness_htlc_multisig,
3535
extract_htlc_secret,
36+
encode_create_order_output,
37+
encode_input_for_fill_order,
38+
encode_input_for_conclude_order,
3639
SignatureHashType,
3740
encode_input_for_withdraw_from_delegation,
3841
estimate_transaction_size,
@@ -776,6 +779,43 @@ export async function run_test() {
776779
);
777780
console.log("htlc with tokens encoding ok");
778781

782+
encode_create_order_output(
783+
Amount.from_atoms("40000"),
784+
undefined,
785+
Amount.from_atoms("10000"),
786+
token_id,
787+
address,
788+
Network.Testnet
789+
);
790+
console.log("create order coins for tokens encoding ok");
791+
792+
encode_create_order_output(
793+
Amount.from_atoms("10000"),
794+
token_id,
795+
Amount.from_atoms("40000"),
796+
undefined,
797+
address,
798+
Network.Testnet
799+
);
800+
console.log("create order tokens for coins encoding ok");
801+
802+
const order_id = "tordr1xxt0avjtt4flkq0tnlyphmdm4aaj9vmkx5r2m4g863nw3lgf7nzs7mlkqc";
803+
encode_input_for_fill_order(
804+
order_id,
805+
Amount.from_atoms("40000"),
806+
address,
807+
BigInt(1),
808+
Network.Testnet
809+
);
810+
console.log("fill order encoding ok");
811+
812+
encode_input_for_conclude_order(
813+
order_id,
814+
BigInt(1),
815+
Network.Testnet
816+
);
817+
console.log("conclude order encoding ok");
818+
779819
try {
780820
const invalid_inputs = "invalid inputs";
781821
encode_transaction(invalid_inputs, outputs, BigInt(0));

wasm-wrappers/src/lib.rs

Lines changed: 79 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ use common::{
4545
make_token_id, IsTokenFreezable, Metadata, NftIssuance, NftIssuanceV0, TokenCreator,
4646
TokenId, TokenIssuance, TokenIssuanceV1, TokenTotalSupply,
4747
},
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,
5051
},
5152
primitives::{
5253
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 {
793794
)
794795
}
795796

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+
796812
/// Given the parameters needed to create hash timelock contract, and a network type (mainnet, testnet, etc),
797813
/// this function creates an output.
798814
#[wasm_bindgen]
@@ -806,14 +822,7 @@ pub fn encode_output_htlc(
806822
network: Network,
807823
) -> Result<Vec<u8>, Error> {
808824
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)?;
817826
let refund_timelock = OutputTimeLock::decode_all(&mut &refund_timelock[..])
818827
.map_err(|_| Error::InvalidTimeLock)?;
819828
let secret_hash =
@@ -1248,6 +1257,66 @@ pub fn effective_pool_balance(
12481257
Ok(Amount::from_internal_amount(effective_balance))
12491258
}
12501259

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+
12511320
#[cfg(test)]
12521321
mod tests {
12531322
use randomness::Rng;

0 commit comments

Comments
 (0)