Skip to content

Commit 661553a

Browse files
committed
feat: add wip, configurables - pending rust sdk bug resolution
1 parent 66634bc commit 661553a

File tree

4 files changed

+244
-3
lines changed

4 files changed

+244
-3
lines changed

packages/fungible-token/bridge-fungible-token/src/main.sw

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ impl Bridge for Contract {
260260
}
261261

262262
fn bridged_token_decimals() -> u8 {
263-
BRIDGED_TOKEN_DECIMALS
263+
DECIMALS
264264
}
265265

266266
fn bridged_token_gateway() -> b256 {

packages/fungible-token/bridge-fungible-token/src/utils.sw

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ library;
33
use ::cast::*;
44
use ::data_structures::MessageData;
55
use ::errors::BridgeFungibleTokenError;
6+
use ::events::DepositEvent;
67
use std::{
78
bytes::Bytes,
89
constants::ZERO_B256,
@@ -55,6 +56,9 @@ pub fn adjust_deposit_decimals(
5556
decimals: u8,
5657
bridged_token_decimals: u8,
5758
) -> Result<u64, BridgeFungibleTokenError> {
59+
log(decimals);
60+
log(decimals);
61+
log(decimals);
5862
let value = val.as_u256();
5963

6064
let adjusted: u256 = if bridged_token_decimals > decimals {

packages/fungible-token/bridge-fungible-token/tests/functions/message_receiver/mod.rs

+230-2
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@ use std::str::FromStr;
1313
mod success {
1414
use super::*;
1515

16-
use crate::utils::interface::src20::total_supply;
17-
use crate::utils::setup::get_asset_id;
16+
use crate::utils::constants::BRIDGED_TOKEN_GATEWAY;
17+
use crate::utils::interface::src20::{decimals, total_supply};
18+
use crate::utils::setup::{decode_hex, get_asset_id};
1819
use crate::utils::{
1920
constants::MESSAGE_AMOUNT,
2021
setup::{
2122
contract_balance, create_recipient_contract, encode_hex, precalculate_deposit_id,
2223
wallet_balance, RefundRegisteredEvent,
2324
},
2425
};
26+
use fuels::types::SizedAsciiString;
2527
use fuels::{prelude::AssetId, programs::contract::SettableContract, types::Bits256};
2628
use primitive_types::U256 as Unsigned256;
2729

@@ -74,6 +76,232 @@ mod success {
7476
assert_eq!(balance, config.fuel_equivalent_amount(config.amount.test));
7577
}
7678

79+
// This test is akin to bridging WBTC and mimicking its decimals on Fuel
80+
#[tokio::test]
81+
async fn deposit_to_wallet_with_6_decimals() {
82+
let mut wallet: fuels::accounts::wallet::WalletUnlocked = create_wallet();
83+
let proxy_token_decimals = 6u8;
84+
let bridged_token_decimals = 6u8;
85+
let config = BridgingConfig::new(bridged_token_decimals, proxy_token_decimals);
86+
87+
let configurables: Option<BridgeFungibleTokenContractConfigurables> = Some(
88+
BridgeFungibleTokenContractConfigurables::new()
89+
.with_DECIMALS(proxy_token_decimals)
90+
.with_BRIDGED_TOKEN_DECIMALS(bridged_token_decimals)
91+
);
92+
93+
let (message, coin, deposit_contract) = create_msg_data(
94+
BRIDGED_TOKEN,
95+
BRIDGED_TOKEN_ID,
96+
FROM,
97+
*wallet.address().hash(),
98+
config.amount.test,
99+
configurables.clone(),
100+
false,
101+
None,
102+
)
103+
.await;
104+
105+
assert_eq!(config.amount.test.as_u64(), config.fuel_equivalent_amount(config.amount.test));
106+
dbg!(config.amount.test.as_u64());
107+
dbg!(config.fuel_equivalent_amount(config.amount.test));
108+
109+
let (bridge, utxo_inputs) = setup_environment(
110+
&mut wallet,
111+
vec![coin],
112+
vec![message],
113+
deposit_contract,
114+
None,
115+
configurables,
116+
)
117+
.await;
118+
119+
let provider = wallet.provider().expect("Needs provider");
120+
121+
// Relay the test message to the bridge contract
122+
let tx = relay_message_to_contract(
123+
&wallet,
124+
utxo_inputs.message[0].clone(),
125+
utxo_inputs.contract,
126+
)
127+
.await;
128+
129+
let receipts = provider.tx_status(&tx).await.unwrap().take_receipts();
130+
131+
let refund_registered_events = bridge
132+
.log_decoder()
133+
.decode_logs_with_type::<RefundRegisteredEvent>(&receipts)
134+
.unwrap();
135+
136+
assert_eq!(refund_registered_events.len(), 0);
137+
138+
let asset_balance =
139+
contract_balance(provider, bridge.contract_id(), AssetId::default()).await;
140+
let balance = wallet_balance(&wallet, &get_asset_id(bridge.contract_id())).await;
141+
142+
// Verify the message value was received by the bridge
143+
assert_eq!(asset_balance, MESSAGE_AMOUNT);
144+
145+
// Check that wallet now has bridged coins
146+
assert_eq!(balance, config.fuel_equivalent_amount(config.amount.test));
147+
}
148+
149+
// This test is akin to bridging USDC or USDT and mimicking its decimals on Fuel
150+
#[tokio::test]
151+
async fn deposit_to_wallet_with_8_decimals() {
152+
let mut wallet: fuels::accounts::wallet::WalletUnlocked = create_wallet();
153+
let proxy_token_decimals = 8u8;
154+
let bridged_token_decimals = 8u8;
155+
let config = BridgingConfig::new(bridged_token_decimals, proxy_token_decimals);
156+
157+
let configurables: Option<BridgeFungibleTokenContractConfigurables> = Some(
158+
BridgeFungibleTokenContractConfigurables::new()
159+
.with_DECIMALS(proxy_token_decimals)
160+
.with_BRIDGED_TOKEN_DECIMALS(bridged_token_decimals)
161+
);
162+
163+
let (message, coin, deposit_contract) = create_msg_data(
164+
BRIDGED_TOKEN,
165+
BRIDGED_TOKEN_ID,
166+
FROM,
167+
*wallet.address().hash(),
168+
config.amount.test,
169+
configurables.clone(),
170+
false,
171+
None,
172+
)
173+
.await;
174+
175+
assert_eq!(config.amount.test.as_u64(), config.fuel_equivalent_amount(config.amount.test));
176+
dbg!(config.amount.test.as_u64());
177+
dbg!(config.fuel_equivalent_amount(config.amount.test));
178+
179+
let (bridge, utxo_inputs) = setup_environment(
180+
&mut wallet,
181+
vec![coin],
182+
vec![message],
183+
deposit_contract,
184+
None,
185+
configurables,
186+
)
187+
.await;
188+
189+
let provider = wallet.provider().expect("Needs provider");
190+
191+
// Relay the test message to the bridge contract
192+
let tx = relay_message_to_contract(
193+
&wallet,
194+
utxo_inputs.message[0].clone(),
195+
utxo_inputs.contract,
196+
)
197+
.await;
198+
199+
let receipts = provider.tx_status(&tx).await.unwrap().take_receipts();
200+
201+
let refund_registered_events = bridge
202+
.log_decoder()
203+
.decode_logs_with_type::<RefundRegisteredEvent>(&receipts)
204+
.unwrap();
205+
206+
assert_eq!(refund_registered_events.len(), 0);
207+
208+
let asset_balance =
209+
contract_balance(provider, bridge.contract_id(), AssetId::default()).await;
210+
let balance = wallet_balance(&wallet, &get_asset_id(bridge.contract_id())).await;
211+
212+
// Verify the message value was received by the bridge
213+
assert_eq!(asset_balance, MESSAGE_AMOUNT);
214+
215+
// Check that wallet now has bridged coins
216+
assert_eq!(balance, config.fuel_equivalent_amount(config.amount.test));
217+
}
218+
219+
// This test is akin to bridging USDC or USDT and using the standard 9 decimals on Fuel
220+
#[tokio::test]
221+
async fn deposit_to_wallet_with_6_decimals_and_conversion() {
222+
let mut wallet: fuels::accounts::wallet::WalletUnlocked = create_wallet();
223+
let proxy_token_decimals = 9u8;
224+
let bridged_token_decimals = 6u8;
225+
let config = BridgingConfig::new(bridged_token_decimals, proxy_token_decimals);
226+
227+
let configurables: Option<BridgeFungibleTokenContractConfigurables> = Some(
228+
BridgeFungibleTokenContractConfigurables::new()
229+
.with_DECIMALS(proxy_token_decimals)
230+
.with_BRIDGED_TOKEN_DECIMALS(bridged_token_decimals)
231+
.with_BRIDGED_TOKEN_GATEWAY(Bits256::from_hex_str(BRIDGED_TOKEN_GATEWAY).unwrap())
232+
.with_BRIDGED_TOKEN(Bits256::from_hex_str(BRIDGED_TOKEN).unwrap())
233+
.with_NAME(SizedAsciiString::<64>::new(String::from("MY_TOKEN ")).unwrap())
234+
.with_SYMBOL(SizedAsciiString::<32>::new(String::from("MYTKN ")).unwrap())
235+
);
236+
237+
let deposit_amount = config.amount.min;
238+
239+
let (message, coin, deposit_contract) = create_msg_data(
240+
BRIDGED_TOKEN,
241+
BRIDGED_TOKEN_ID,
242+
FROM,
243+
*wallet.address().hash(),
244+
deposit_amount,
245+
configurables.clone(),
246+
false,
247+
None,
248+
)
249+
.await;
250+
251+
let (bridge, utxo_inputs) = setup_environment(
252+
&mut wallet,
253+
vec![coin],
254+
vec![message],
255+
deposit_contract,
256+
None,
257+
configurables,
258+
)
259+
.await;
260+
261+
let provider = wallet.provider().expect("Needs provider");
262+
263+
let decimals = decimals(&bridge, get_asset_id(bridge.contract_id())).await;
264+
dbg!(decimals);
265+
266+
let decimals = &bridge
267+
.methods()
268+
.bridged_token_decimals()
269+
.call()
270+
.await
271+
.unwrap()
272+
.value;
273+
274+
dbg!(decimals);
275+
276+
// Relay the test message to the bridge contract
277+
let tx = relay_message_to_contract(
278+
&wallet,
279+
utxo_inputs.message[0].clone(),
280+
utxo_inputs.contract,
281+
)
282+
.await;
283+
284+
let receipts = provider.tx_status(&tx).await.unwrap().take_receipts();
285+
dbg!(&receipts);
286+
287+
let refund_registered_events = bridge
288+
.log_decoder()
289+
.decode_logs_with_type::<RefundRegisteredEvent>(&receipts)
290+
.unwrap();
291+
292+
assert_eq!(refund_registered_events.len(), 0);
293+
294+
let asset_balance =
295+
contract_balance(provider, bridge.contract_id(), AssetId::default()).await;
296+
let balance = wallet_balance(&wallet, &get_asset_id(bridge.contract_id())).await;
297+
298+
// Verify the message value was received by the bridge
299+
assert_eq!(asset_balance, MESSAGE_AMOUNT);
300+
301+
// Check that wallet now has bridged coins
302+
assert_eq!(balance, config.fuel_equivalent_amount(deposit_amount));
303+
}
304+
77305
#[tokio::test]
78306
async fn deposit_to_wallet_max_amount() {
79307
let mut wallet = create_wallet();

packages/fungible-token/bridge-fungible-token/tests/utils/setup.rs

+9
Original file line numberDiff line numberDiff line change
@@ -41,30 +41,35 @@ abigen!(
4141
);
4242

4343
/// Used for setting up tests with various message values
44+
#[derive(Debug)]
4445
pub struct BridgingConfig {
4546
pub adjustment: Adjustment,
4647
pub amount: TxAmount,
4748
pub overflow: Overflow,
4849
}
4950

51+
#[derive(Debug)]
5052
pub struct Adjustment {
5153
pub factor: Unsigned256,
5254
pub is_div: bool,
5355
}
5456

57+
#[derive(Debug)]
5558
pub struct TxAmount {
5659
pub min: Unsigned256,
5760
pub max: Unsigned256,
5861
pub test: Unsigned256,
5962
pub not_enough: Unsigned256,
6063
}
6164

65+
#[derive(Debug)]
6266
pub struct Overflow {
6367
pub one: Unsigned256,
6468
pub two: Unsigned256,
6569
pub three: Unsigned256,
6670
}
6771

72+
#[derive(Debug)]
6873
pub struct UTXOInputs {
6974
pub contract: Vec<Input>,
7075
pub coin: Vec<Input>,
@@ -139,6 +144,8 @@ impl BridgingConfig {
139144
}
140145

141146
pub fn fuel_equivalent_amount(&self, amount: Unsigned256) -> u64 {
147+
dbg!(amount);
148+
dbg!(&self.adjustment);
142149
if self.adjustment.is_div {
143150
(amount * self.adjustment.factor).as_u64()
144151
} else {
@@ -215,6 +222,8 @@ pub(crate) async fn setup_environment(
215222
None => LoadConfiguration::default(),
216223
};
217224

225+
dbg!(&load_configuration);
226+
218227
let test_contract_id =
219228
Contract::load_from(BRIDGE_FUNGIBLE_TOKEN_CONTRACT_BINARY, load_configuration)
220229
.unwrap()

0 commit comments

Comments
 (0)