Skip to content

Commit

Permalink
add data deposit fee to wasm functions
Browse files Browse the repository at this point in the history
- normalise parameter types for nft and fungible token functions
  • Loading branch information
OBorce committed Sep 20, 2024
1 parent 43b3bfb commit dff6c7b
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 27 deletions.
4 changes: 4 additions & 0 deletions wasm-wrappers/WASM-API.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ this function creates an output that issues that NFT.

Given data to be deposited in the blockchain, this function provides the output that deposits this data

### Function: `data_deposit_fee`

Returns the fee that needs to be paid by a transaction for issuing a data deposit

### Function: `encode_output_htlc`

Given the parameters needed to create hash timelock contract, and a network type (mainnet, testnet, etc),
Expand Down
75 changes: 67 additions & 8 deletions wasm-wrappers/js-bindings/wasm_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ import {
get_transaction_id,
effective_pool_balance,
Amount,
TotalSupply,
FreezableToken,
encode_output_issue_nft,
encode_output_issue_fungible_token,
sign_challenge,
verify_challenge,
} from "../pkg/wasm_wrappers.js";
Expand Down Expand Up @@ -83,7 +86,7 @@ export async function run_test() {
// Attempt to use a bad private key to get a public key (test returned Result<> object, which will become a string error)
const bad_priv_key = "bad";
try {
const bad_pub_key = public_key_from_private_key(bad_priv_key);
public_key_from_private_key(bad_priv_key);
throw new Error("Invalid private key worked somehow!");
} catch (e) {
if (!e.includes("Invalid private key encoding")) {
Expand Down Expand Up @@ -502,6 +505,68 @@ export async function run_test() {

assert_eq_arrays(pool_data, expected_pool_data);

let encoded_fungible_token = encode_output_issue_fungible_token(
address,
"XXX",
"http://uri.com",
2,
TotalSupply.Unlimited,
null,
FreezableToken.Yes,
BigInt(1),
Network.Testnet
);

const expected_fungible_token = [
7, 1, 12, 88, 88, 88, 2, 56, 104, 116,
116, 112, 58, 47, 47, 117, 114, 105, 46, 99,
111, 109, 2, 1, 91, 58, 110, 176, 100, 207,
6, 194, 41, 193, 30, 91, 4, 195, 202, 103,
207, 80, 217, 178, 1
];

assert_eq_arrays(encoded_fungible_token, expected_fungible_token);


const account_pubkey = make_default_account_privkey(
mnemonic,
Network.Testnet
);
const receiving_privkey = make_receiving_address(account_pubkey, 0);
const receiving_pubkey = public_key_from_private_key(receiving_privkey);

let encoded_nft = encode_output_issue_nft(
token_id,
address,
"nft",
"XXX",
"desc",
"1234",
receiving_pubkey,
"http://uri",
"http://icon",
"http://foo",
BigInt(1),
Network.Testnet
);

const expected_nft_encoding = [
8, 162, 208, 145, 194, 165, 27, 14, 118, 31, 139, 199,
254, 11, 190, 108, 15, 64, 180, 50, 106, 211, 26, 107,
242, 121, 29, 55, 172, 185, 5, 196, 119, 0, 1, 0,
2, 227, 252, 33, 195, 223, 44, 38, 35, 73, 145, 212,
180, 49, 115, 4, 150, 204, 250, 205, 123, 131, 201, 114,
130, 186, 209, 98, 181, 118, 233, 133, 89, 12, 110, 102,
116, 16, 100, 101, 115, 99, 12, 88, 88, 88, 44, 104,
116, 116, 112, 58, 47, 47, 105, 99, 111, 110, 40, 104,
116, 116, 112, 58, 47, 47, 102, 111, 111, 40, 104, 116,
116, 112, 58, 47, 47, 117, 114, 105, 16, 1, 2, 3,
4, 1, 91, 58, 110, 176, 100, 207, 6, 194, 41, 193,
30, 91, 4, 195, 202, 103, 207, 80, 217, 178
];

assert_eq_arrays(encoded_nft, expected_nft_encoding);

try {
const invalid_token_id = "asd";
encode_output_issue_nft(
Expand All @@ -510,7 +575,7 @@ export async function run_test() {
"nft",
"XXX",
"desc",
"123",
"12345",
undefined,
undefined,
undefined,
Expand Down Expand Up @@ -732,12 +797,6 @@ export async function run_test() {
assert_eq_arrays(witness, expected_no_signature_witness);
console.log("empty witness encoding ok");

const account_pubkey = make_default_account_privkey(
mnemonic,
Network.Testnet
);
const receiving_privkey = make_receiving_address(account_pubkey, 0);

const opt_utxos = [1, ...output, 1, ...stake_pool_output];

try {
Expand Down
41 changes: 22 additions & 19 deletions wasm-wrappers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,8 +676,8 @@ pub fn token_change_authority_fee(current_block_height: u64, network: Network) -
#[wasm_bindgen]
pub fn encode_output_issue_fungible_token(
authority: &str,
token_ticker: &[u8],
metadata_uri: &[u8],
token_ticker: &str,
metadata_uri: &str,
number_of_decimals: u8,
total_supply: TotalSupply,
supply_amount: Option<Amount>,
Expand Down Expand Up @@ -718,10 +718,10 @@ pub fn encode_output_issue_nft(
ticker: &str,
description: &str,
media_hash: &[u8],
creator: Option<String>,
media_uri: Option<Vec<u8>>,
icon_uri: Option<Vec<u8>>,
additional_metadata_uri: Option<Vec<u8>>,
creator: Option<Vec<u8>>,
media_uri: Option<String>,
icon_uri: Option<String>,
additional_metadata_uri: Option<String>,
_current_block_height: u64,
network: Network,
) -> Result<Vec<u8>, Error> {
Expand All @@ -730,21 +730,15 @@ pub fn encode_output_issue_nft(
let authority = parse_addressable(&chain_config, authority)?;
let name = name.into();
let ticker = ticker.into();
let media_uri = media_uri.into();
let icon_uri = icon_uri.into();
let media_uri = media_uri.map(Into::into).into();
let icon_uri = icon_uri.map(Into::into).into();
let media_hash = media_hash.into();
let additional_metadata_uri = additional_metadata_uri.into();
let additional_metadata_uri = additional_metadata_uri.map(Into::into).into();
let creator = creator
.map(|addr| parse_addressable::<Destination>(&chain_config, &addr))
.transpose()?
.map(|dest| match dest {
Destination::PublicKey(public_key) => Ok(TokenCreator { public_key }),
Destination::AnyoneCanSpend
| Destination::ScriptHash(_)
| Destination::PublicKeyHash(_)
| Destination::ClassicMultisig(_) => Err(Error::InvalidCreatorPublicKey),
})
.transpose()?;
.map(|pk| PublicKey::decode_all(&mut pk.as_slice()))
.transpose()
.map_err(|_| Error::InvalidCreatorPublicKey)?
.map(|public_key| TokenCreator { public_key });

let nft_issuance = NftIssuanceV0 {
metadata: Metadata {
Expand Down Expand Up @@ -772,6 +766,15 @@ pub fn encode_output_data_deposit(data: &[u8]) -> Result<Vec<u8>, Error> {
Ok(output.encode())
}

/// Returns the fee that needs to be paid by a transaction for issuing a data deposit
#[wasm_bindgen]
pub fn data_deposit_fee(current_block_height: u64, network: Network) -> Amount {
let chain_config = Builder::new(network.into()).build();
Amount::from_internal_amount(
chain_config.data_deposit_fee(BlockHeight::new(current_block_height)),
)
}

/// Given the parameters needed to create hash timelock contract, and a network type (mainnet, testnet, etc),
/// this function creates an output.
#[wasm_bindgen]
Expand Down

0 comments on commit dff6c7b

Please sign in to comment.