diff --git a/bridge-token-factory/Cargo.lock b/bridge-token-factory/Cargo.lock index fff249b7..027148f7 100644 --- a/bridge-token-factory/Cargo.lock +++ b/bridge-token-factory/Cargo.lock @@ -307,8 +307,8 @@ dependencies = [ name = "bridge-token" version = "0.1.0" dependencies = [ - "near-contract-standards", - "near-sdk 3.0.0-pre.3", + "near-contract-standards 3.0.0-pre.3 (git+https://github.com/near/near-sdk-rs?rev=af0e826f916f3aa3ea51e8b675ecebdd37176671)", + "near-sdk 3.0.0-pre.3 (git+https://github.com/near/near-sdk-rs?rev=af0e826f916f3aa3ea51e8b675ecebdd37176671)", ] [[package]] @@ -321,8 +321,8 @@ dependencies = [ "hex", "lazy_static", "mock-prover", - "near-contract-standards", - "near-sdk 3.0.0-pre.3", + "near-contract-standards 3.0.0-pre.3 (git+https://github.com/near/near-sdk-rs?rev=613adcab6322e4f830efb4d5bde2a810ae642158)", + "near-sdk 3.0.0-pre.3 (git+https://github.com/near/near-sdk-rs?rev=613adcab6322e4f830efb4d5bde2a810ae642158)", "near-sdk-sim", "rand 0.7.3", "rlp 0.4.6", @@ -730,7 +730,7 @@ dependencies = [ [[package]] name = "eth-types" version = "0.1.0" -source = "git+https://github.com/near/rainbow-bridge#ca01d9e39403a08397de63fb5ae544f760ffcedd" +source = "git+https://github.com/near/rainbow-bridge#ede62fcb3614e28e8c5406300381319fec846100" dependencies = [ "borsh 0.6.2", "derive_more", @@ -1312,20 +1312,28 @@ checksum = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3" name = "mock-prover" version = "0.1.0" dependencies = [ - "near-contract-standards", - "near-sdk 3.0.0-pre.3", + "near-contract-standards 3.0.0-pre.3 (git+https://github.com/near/near-sdk-rs?rev=af0e826f916f3aa3ea51e8b675ecebdd37176671)", + "near-sdk 3.0.0-pre.3 (git+https://github.com/near/near-sdk-rs?rev=af0e826f916f3aa3ea51e8b675ecebdd37176671)", "serde", "serde_json", "uint 0.8.5", "wee_alloc", ] +[[package]] +name = "near-contract-standards" +version = "3.0.0-pre.3" +source = "git+https://github.com/near/near-sdk-rs?rev=613adcab6322e4f830efb4d5bde2a810ae642158#613adcab6322e4f830efb4d5bde2a810ae642158" +dependencies = [ + "near-sdk 3.0.0-pre.3 (git+https://github.com/near/near-sdk-rs?rev=613adcab6322e4f830efb4d5bde2a810ae642158)", +] + [[package]] name = "near-contract-standards" version = "3.0.0-pre.3" source = "git+https://github.com/near/near-sdk-rs?rev=af0e826f916f3aa3ea51e8b675ecebdd37176671#af0e826f916f3aa3ea51e8b675ecebdd37176671" dependencies = [ - "near-sdk 3.0.0-pre.3", + "near-sdk 3.0.0-pre.3 (git+https://github.com/near/near-sdk-rs?rev=af0e826f916f3aa3ea51e8b675ecebdd37176671)", ] [[package]] @@ -1534,6 +1542,22 @@ dependencies = [ "serde", ] +[[package]] +name = "near-sdk" +version = "3.0.0-pre.3" +source = "git+https://github.com/near/near-sdk-rs?rev=613adcab6322e4f830efb4d5bde2a810ae642158#613adcab6322e4f830efb4d5bde2a810ae642158" +dependencies = [ + "base64 0.13.0", + "borsh 0.8.2", + "bs58 0.4.0", + "near-primitives-core 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "near-sdk-macros 3.0.0-pre.3 (git+https://github.com/near/near-sdk-rs?rev=613adcab6322e4f830efb4d5bde2a810ae642158)", + "near-vm-logic 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "serde", + "serde_json", + "wee_alloc", +] + [[package]] name = "near-sdk" version = "3.0.0-pre.3" @@ -1543,7 +1567,7 @@ dependencies = [ "borsh 0.8.2", "bs58 0.4.0", "near-primitives-core 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "near-sdk-macros 3.0.0-pre.3", + "near-sdk-macros 3.0.0-pre.3 (git+https://github.com/near/near-sdk-rs?rev=af0e826f916f3aa3ea51e8b675ecebdd37176671)", "near-vm-logic 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde", "serde_json", @@ -1562,6 +1586,17 @@ dependencies = [ "syn", ] +[[package]] +name = "near-sdk-core" +version = "3.0.0-pre.3" +source = "git+https://github.com/near/near-sdk-rs?rev=613adcab6322e4f830efb4d5bde2a810ae642158#613adcab6322e4f830efb4d5bde2a810ae642158" +dependencies = [ + "Inflector", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "near-sdk-core" version = "3.0.0-pre.3" @@ -1585,12 +1620,23 @@ dependencies = [ "syn", ] +[[package]] +name = "near-sdk-macros" +version = "3.0.0-pre.3" +source = "git+https://github.com/near/near-sdk-rs?rev=613adcab6322e4f830efb4d5bde2a810ae642158#613adcab6322e4f830efb4d5bde2a810ae642158" +dependencies = [ + "near-sdk-core 3.0.0-pre.3 (git+https://github.com/near/near-sdk-rs?rev=613adcab6322e4f830efb4d5bde2a810ae642158)", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "near-sdk-macros" version = "3.0.0-pre.3" source = "git+https://github.com/near/near-sdk-rs?rev=af0e826f916f3aa3ea51e8b675ecebdd37176671#af0e826f916f3aa3ea51e8b675ecebdd37176671" dependencies = [ - "near-sdk-core 3.0.0-pre.3", + "near-sdk-core 3.0.0-pre.3 (git+https://github.com/near/near-sdk-rs?rev=af0e826f916f3aa3ea51e8b675ecebdd37176671)", "proc-macro2", "quote", "syn", @@ -1599,14 +1645,14 @@ dependencies = [ [[package]] name = "near-sdk-sim" version = "3.0.0-pre.3" -source = "git+https://github.com/near/near-sdk-rs?rev=af0e826f916f3aa3ea51e8b675ecebdd37176671#af0e826f916f3aa3ea51e8b675ecebdd37176671" +source = "git+https://github.com/near/near-sdk-rs?rev=613adcab6322e4f830efb4d5bde2a810ae642158#613adcab6322e4f830efb4d5bde2a810ae642158" dependencies = [ "funty", "lazy-static-include", "near-crypto", "near-pool", "near-primitives", - "near-sdk 3.0.0-pre.3", + "near-sdk 3.0.0-pre.3 (git+https://github.com/near/near-sdk-rs?rev=613adcab6322e4f830efb4d5bde2a810ae642158)", "near-store", "near-vm-logic 3.0.0 (git+https://github.com/near/nearcore.git?rev=1e88c6c699e3a2c49c6cbd09d284f69885982e80)", "node-runtime", @@ -2590,8 +2636,8 @@ name = "test-token" version = "0.1.0" source = "git+https://github.com/mfornet/contracts#177acd35c73e251bc1c4d10a2b87ae0b2b49b508" dependencies = [ - "near-contract-standards", - "near-sdk 3.0.0-pre.3", + "near-contract-standards 3.0.0-pre.3 (git+https://github.com/near/near-sdk-rs?rev=af0e826f916f3aa3ea51e8b675ecebdd37176671)", + "near-sdk 3.0.0-pre.3 (git+https://github.com/near/near-sdk-rs?rev=af0e826f916f3aa3ea51e8b675ecebdd37176671)", ] [[package]] diff --git a/bridge-token-factory/Cargo.toml b/bridge-token-factory/Cargo.toml index 8c1ec556..b3f72aaa 100644 --- a/bridge-token-factory/Cargo.toml +++ b/bridge-token-factory/Cargo.toml @@ -18,8 +18,10 @@ panic = "abort" overflow-checks = true [dependencies] -near-sdk = { git = "https://github.com/near/near-sdk-rs", rev = "af0e826f916f3aa3ea51e8b675ecebdd37176671" } -near-contract-standards = { git = "https://github.com/near/near-sdk-rs", rev = "af0e826f916f3aa3ea51e8b675ecebdd37176671" } +near-sdk = { git = "https://github.com/near/near-sdk-rs", rev = "613adcab6322e4f830efb4d5bde2a810ae642158" } +near-contract-standards = { git = "https://github.com/near/near-sdk-rs", rev = "613adcab6322e4f830efb4d5bde2a810ae642158" } +#near-sdk = { path = "../../near-sdk-rs/near-sdk" } +#near-contract-standards = { path = "../../near-sdk-rs/near-contract-standards" } serde = { version = "*", features = ["derive"] } serde_json = "*" uint = { version = "0.8.3", default-features = false } @@ -31,7 +33,8 @@ hex = "0.4.2" tiny-keccak = "1.4.0" [dev-dependencies] -near-sdk-sim = { git = "https://github.com/near/near-sdk-rs", rev = "af0e826f916f3aa3ea51e8b675ecebdd37176671" } +near-sdk-sim = { git = "https://github.com/near/near-sdk-rs", rev = "613adcab6322e4f830efb4d5bde2a810ae642158" } +#near-sdk-sim = { path = "../../near-sdk-rs/near-sdk-sim" } test-token = { git = "https://github.com/mfornet/contracts" } bridge-token = { path = "../bridge-token" } mock-prover = { path = "../mock-prover" } diff --git a/bridge-token-factory/src/lib.rs b/bridge-token-factory/src/lib.rs index fae14546..de79bcdb 100644 --- a/bridge-token-factory/src/lib.rs +++ b/bridge-token-factory/src/lib.rs @@ -191,21 +191,18 @@ impl BridgeTokenFactory { assert_self(); assert!(verification_success, "Failed to verify the proof"); - let initial_storage_usage = env::storage_usage(); - self.record_proof(&proof); - let storage_used = env::storage_usage() - initial_storage_usage; - let balance_required = storage_used as u128 * STORAGE_PRICE_PER_BYTE; + let required_deposit = self.record_proof(&proof); assert!( env::attached_deposit() - >= balance_required + self.bridge_token_storage_deposit_required + >= required_deposit + self.bridge_token_storage_deposit_required ); ext_bridge_token::mint( new_owner_id, amount.into(), &self.get_bridge_token_account_id(token), - env::attached_deposit() - balance_required, + env::attached_deposit() - required_deposit, env::prepaid_gas() / 2, ); } @@ -351,10 +348,9 @@ impl BridgeTokenFactory { ); self.used_events.insert(&key); let current_storage = env::storage_usage(); - let attached_deposit = env::attached_deposit(); let required_deposit = Balance::from(current_storage - initial_storage) * STORAGE_PRICE_PER_BYTE; - attached_deposit - required_deposit + required_deposit } } diff --git a/bridge-token-factory/tests/token_transfer.rs b/bridge-token-factory/tests/token_transfer.rs index 130160d1..fe09e999 100644 --- a/bridge-token-factory/tests/token_transfer.rs +++ b/bridge-token-factory/tests/token_transfer.rs @@ -1,4 +1,4 @@ -use near_sdk::borsh::BorshSerialize; +use near_sdk::borsh::{self, BorshSerialize}; use near_sdk::{AccountId, Balance}; use serde_json::json; @@ -6,8 +6,11 @@ use bridge_token::BridgeTokenContract; use bridge_token_factory::BridgeTokenFactoryContract; use bridge_token_factory::{validate_eth_address, EthLockedEvent, EthUnlockedEvent, Proof}; use mock_prover::MockProverContract; +use near_sdk_sim::runtime::GenesisConfig; +use near_sdk_sim::transaction::ExecutionStatus; use near_sdk_sim::{ - call, deploy, init_simulator, units::to_yocto, view, ContractAccount, UserAccount, + call, deploy, init_simulator, units::to_yocto, view, ContractAccount, ExecutionResult, + UserAccount, }; use test_token::ContractContract as TestTokenContract; @@ -21,12 +24,15 @@ const TEST_TOKEN: &str = "test-token"; near_sdk_sim::lazy_static_include::lazy_static_include_bytes! { TEST_TOKEN_WASM_BYTES => "../res/test_token.wasm", + TOKEN_WASM_BYTES => "../res/bridge_token.wasm", FACTORY_WASM_BYTES => "../res/bridge_token_factory.wasm", MOCK_PROVER_WASM_BYTES => "../res/mock_prover.wasm", } fn setup_token_factory() -> (UserAccount, ContractAccount) { - let root = init_simulator(None); + let mut config = GenesisConfig::default(); + config.runtime_config.storage_amount_per_byte = 10u128.pow(19); + let root = init_simulator(Some(config)); let prover = deploy!( contract: MockProverContract, contract_id: PROVER.to_string(), @@ -42,20 +48,36 @@ fn setup_token_factory() -> (UserAccount, ContractAccount for AugmentedProof { + fn from(proof: Proof) -> Self { + Self { + proof, + skip_call: false, + } + } +} + #[test] fn test_eth_token_transfer() { let (user, factory) = setup_token_factory(); let root = "root".to_string(); + let alice = user.create_user(ALICE.to_string(), to_yocto("100")); + call!( user, factory.deploy_bridge_token(DAI_ADDRESS.to_string()), - deposit = to_yocto("35") + deposit = to_yocto("3500") ) .assert_success(); @@ -63,12 +85,30 @@ fn test_eth_token_transfer() { view!(factory.get_bridge_token_account_id(DAI_ADDRESS.to_string())).unwrap_json(); assert_eq!(token_account_id, format!("{}.{}", DAI_ADDRESS, FACTORY)); - // TODO: use BridgeTokenContract - // let token = BridgeToken { - // contract_id: token_account_id, - // }; - // assert_eq!(token.get_balance(&mut runtime, ALICE.to_string()), "0"); - // assert_eq!(token.get_total_supply(&mut runtime), "0"); + let alice_balance: String = user + .call( + token_account_id.clone(), + "ft_balance_of", + json!({ "account_id": ALICE.to_string() }) + .to_string() + .into_bytes() + .as_ref(), + near_sdk_sim::DEFAULT_GAS, + near_sdk_sim::STORAGE_AMOUNT, + ) + .unwrap_json(); + assert_eq!(alice_balance, "0"); + + let total_supply: String = user + .call( + token_account_id.clone(), + "ft_total_supply", + json!({}).to_string().into_bytes().as_ref(), + near_sdk_sim::DEFAULT_GAS, + near_sdk_sim::STORAGE_AMOUNT, + ) + .unwrap_json(); + assert_eq!(total_supply, "0"); let mut proof = Proof::default(); proof.log_entry_data = EthLockedEvent { @@ -79,22 +119,81 @@ fn test_eth_token_transfer() { recipient: ALICE.to_string(), } .to_log_entry_data(); - call!(user, factory.deposit(proof)).assert_success(); - - // assert_eq!(token.get_balance(&mut runtime, ALICE.to_string()), "1000"); - // assert_eq!(token.get_total_supply(&mut runtime), "1000"); - - // token - // .withdraw( - // &mut runtime, - // ALICE.to_string(), - // "100".to_string(), - // SENDER_ADDRESS.to_string(), - // ) - // .unwrap(); - // - // assert_eq!(token.get_balance(&mut runtime, ALICE.to_string()), "900"); - // assert_eq!(token.get_total_supply(&mut runtime), "900"); + + let result = user.call( + FACTORY.to_string(), + "deposit", + &proof.try_to_vec().unwrap(), + near_sdk_sim::DEFAULT_GAS, + near_sdk_sim::STORAGE_AMOUNT, + ); + + let alice_balance: String = user + .call( + token_account_id.clone(), + "ft_balance_of", + json!({ "account_id": ALICE.to_string() }) + .to_string() + .into_bytes() + .as_ref(), + near_sdk_sim::DEFAULT_GAS, + near_sdk_sim::STORAGE_AMOUNT, + ) + .unwrap_json(); + + assert_eq!(alice_balance, "1000"); + + let total_supply: String = user + .call( + token_account_id.clone(), + "ft_total_supply", + json!({}).to_string().into_bytes().as_ref(), + near_sdk_sim::DEFAULT_GAS, + near_sdk_sim::STORAGE_AMOUNT, + ) + .unwrap_json(); + assert_eq!(total_supply, "1000"); + + alice + .call( + token_account_id.clone(), + "withdraw", + json!({ + "amount" : "100", + "recipient" : SENDER_ADDRESS.to_string() + }) + .to_string() + .into_bytes() + .as_ref(), + near_sdk_sim::DEFAULT_GAS, + 1, + ) + .assert_success(); + + let alice_balance: String = user + .call( + token_account_id.clone(), + "ft_balance_of", + json!({ "account_id": ALICE.to_string() }) + .to_string() + .into_bytes() + .as_ref(), + near_sdk_sim::DEFAULT_GAS, + near_sdk_sim::STORAGE_AMOUNT, + ) + .unwrap_json(); + assert_eq!(alice_balance, "900"); + + let total_supply: String = user + .call( + token_account_id, + "ft_total_supply", + json!({}).to_string().into_bytes().as_ref(), + near_sdk_sim::DEFAULT_GAS, + near_sdk_sim::STORAGE_AMOUNT, + ) + .unwrap_json(); + assert_eq!(total_supply, "900"); } // #[test] diff --git a/res/bridge_token.wasm b/res/bridge_token.wasm index 81ae7bad..50c72607 100755 Binary files a/res/bridge_token.wasm and b/res/bridge_token.wasm differ diff --git a/res/bridge_token_factory.wasm b/res/bridge_token_factory.wasm index 41b16406..e7b90f1d 100755 Binary files a/res/bridge_token_factory.wasm and b/res/bridge_token_factory.wasm differ