Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
rantan committed Nov 25, 2024
1 parent 3dfcf34 commit 76dc418
Showing 1 changed file with 70 additions and 18 deletions.
88 changes: 70 additions & 18 deletions tapyrus-wallet-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,27 @@ mod test {
(env, color_id, client)
}

fn distribute_token(wallet: &HdWallet, env: &TestEnv, color_id: &ColorIdentifier, client: &BlockingClient) {
let GetNewAddressResult {
address: address, ..
} = wallet.get_new_address(Some(color_id.to_string())).unwrap();

let txid: MalFixTxid = env
.tapyrusd
.client
.call(
"transfertoken",
&[address.to_string().into(), 100.into()],
)
.unwrap();

wait_for_confirmation(&env, &client, 1);
wallet.sync().expect("Failed to sync");

let balance = wallet.balance(Some(color_id.clone().to_string())).unwrap();
assert_eq!(balance, 100);
}

#[test]
fn test_receive_pay_to_contract_transfer_and_transfer_pay_to_contract_utxo() {
let (env, color_id, client) = prepare_token();
Expand Down Expand Up @@ -1239,28 +1260,59 @@ mod test {
#[test]
fn test_refund() {
let (env, color_id, client) = prepare_token();
let wallet = get_wallet_testenv(&env, &client, None);
let sender_wallet = get_wallet_testenv(&env, &client, None);
distribute_token(&sender_wallet, &env, &color_id, &client);
let receiver_wallet = get_wallet_testenv(&env, &client, Some("tprv8ZgxMBicQKsPfKH3fHRJGBs9Vt2hMHfroZuZ5yYLYZgwvC3Hc8Wksn1HDinon77ZvDNEo25BEefQ6Ldgi4Nw29o1gP7pY8QzAyn1WQimrdc".to_string()));
distribute_token(&receiver_wallet, &env, &color_id, &client);

// Receiver generate new public key and notify to the sender
let GetNewAddressResult { public_key: receiver_public_key, .. } =
receiver_wallet.get_new_address(Some(color_id.to_string())).unwrap();

// Sender creates P2C address and transfers to the P2C address
let p2c_address = sender_wallet
.calc_p2c_address(
receiver_public_key.clone(),
"content".to_string(),
Some(color_id.clone().to_string()),
)
.unwrap();
sender_wallet.transfer(
vec![TransferParams {
amount: 10,
to_address: p2c_address.clone(),
}],
vec![],
).expect("Failed to transfer");

let GetNewAddressResult {
address: address, ..
} = wallet.get_new_address(None).unwrap();
let address = Address::from_str(&address).unwrap().assume_checked();
let _ = env.tapyrusd.client.send_to_address(
&address,
Amount::from_tap(20000),
None,
None,
None,
None,
Some(1),
None,
);
wait_for_confirmation(&env, &client, 1);
wallet.sync().expect("Failed to sync");
let balance = wallet.balance(None).unwrap();
assert_eq!(balance, 20000);
sender_wallet.sync().expect("Failed to sync");
assert_eq!(sender_wallet.balance(Some(color_id.clone().to_string())).unwrap(), 90);

// Receiver confirms the transfer
let contract = Contract {
contract_id: "contract_id".to_string(),
contract: "content".to_string(),
payment_base: receiver_public_key,
payable: false,
};
receiver_wallet
.store_contract(contract.clone())
.expect("Failed to store contract");
receiver_wallet.sync().expect("Failed to sync");
assert_eq!(receiver_wallet.balance(Some(color_id.clone().to_string())).unwrap(), 110);

// Sender check refund but it will be failure
//sender_wallet.check_trust_layer_refund()

// Receiver refund the token
let GetNewAddressResult { address: refund_address, .. } =
sender_wallet.get_new_address(Some(color_id.to_string())).unwrap();
let utxo = Utxo
receiver_wallet.transfer(vec![TransferParams {
amount: 10,
to_address: refund_address
}], );

}

Expand Down

0 comments on commit 76dc418

Please sign in to comment.