diff --git a/Cargo.lock b/Cargo.lock index 4e26d6f..39d330d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -38,6 +38,12 @@ dependencies = [ "rustc-demangle", ] +[[package]] +name = "base32" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23ce669cd6c8588f79e15cf450314f9638f967fc5770ff1c7c1deb0925ea7cfa" + [[package]] name = "base64" version = "0.13.0" @@ -79,9 +85,9 @@ checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" [[package]] name = "bytes-lit" -version = "0.0.3" +version = "0.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3a949b8abd1d84c29a91f4c8480dac1b426d53cebc9a31a8424a5cbe5be32f6" +checksum = "1c181bca161a49348b90fa75f9a54fe11b7138098eed90d841a1055d574b4250" dependencies = [ "num-bigint", "proc-macro2", @@ -216,7 +222,7 @@ checksum = "c762bae6dcaf24c4c84667b8579785430908723d5c889f469d76a41d59cc7a9d" dependencies = [ "curve25519-dalek", "ed25519", - "rand", + "rand 0.7.3", "serde", "sha2 0.9.9", "zeroize", @@ -252,7 +258,18 @@ checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce" dependencies = [ "cfg-if", "libc", - "wasi", + "wasi 0.9.0+wasi-snapshot-preview1", +] + +[[package]] +name = "getrandom" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6" +dependencies = [ + "cfg-if", + "libc", + "wasi 0.11.0+wasi-snapshot-preview1", ] [[package]] @@ -435,6 +452,16 @@ version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" +[[package]] +name = "prettyplease" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a49e86d2c26a24059894a3afa13fd17d063419b05dfb83f06d9c3566060c3f5a" +dependencies = [ + "proc-macro2", + "syn", +] + [[package]] name = "proc-macro2" version = "1.0.43" @@ -459,13 +486,24 @@ version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" dependencies = [ - "getrandom", + "getrandom 0.1.16", "libc", - "rand_chacha", + "rand_chacha 0.2.2", "rand_core 0.5.1", "rand_hc", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha 0.3.1", + "rand_core 0.6.4", +] + [[package]] name = "rand_chacha" version = "0.2.2" @@ -476,13 +514,23 @@ dependencies = [ "rand_core 0.5.1", ] +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core 0.6.4", +] + [[package]] name = "rand_core" version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" dependencies = [ - "getrandom", + "getrandom 0.1.16", ] [[package]] @@ -490,6 +538,9 @@ name = "rand_core" version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom 0.2.7", +] [[package]] name = "rand_hc" @@ -595,8 +646,10 @@ dependencies = [ [[package]] name = "soroban-auth" version = "0.0.4" -source = "git+https://github.com/stellar/rs-soroban-sdk?rev=bf6b383e#bf6b383ef4cc817223e7bd07725df468d880bb27" +source = "git+https://github.com/stellar/rs-soroban-sdk?rev=864a309b#864a309bca7ad91d6c7189fe87e018f64ad6ba06" dependencies = [ + "ed25519-dalek", + "rand 0.7.3", "soroban-sdk", ] @@ -604,20 +657,17 @@ dependencies = [ name = "soroban-crowdfund-contract" version = "0.0.0" dependencies = [ - "ed25519-dalek", - "rand", - "sha2 0.10.6", + "rand 0.7.3", "soroban-auth", "soroban-crowdfund-contract", "soroban-sdk", - "soroban-token-contract", "stellar-xdr 0.0.1", ] [[package]] name = "soroban-env-common" version = "0.0.5" -source = "git+https://github.com/stellar/rs-soroban-env?rev=0a826765#0a826765e9728e263ec3eafa3ed81a1d38a8716a" +source = "git+https://github.com/stellar/rs-soroban-env?rev=fb8a384e#fb8a384eb73b1d157c13b073efa9c82ff490119f" dependencies = [ "soroban-env-macros", "soroban-wasmi", @@ -628,7 +678,7 @@ dependencies = [ [[package]] name = "soroban-env-guest" version = "0.0.5" -source = "git+https://github.com/stellar/rs-soroban-env?rev=0a826765#0a826765e9728e263ec3eafa3ed81a1d38a8716a" +source = "git+https://github.com/stellar/rs-soroban-env?rev=fb8a384e#fb8a384eb73b1d157c13b073efa9c82ff490119f" dependencies = [ "soroban-env-common", "static_assertions", @@ -637,7 +687,7 @@ dependencies = [ [[package]] name = "soroban-env-host" version = "0.0.5" -source = "git+https://github.com/stellar/rs-soroban-env?rev=0a826765#0a826765e9728e263ec3eafa3ed81a1d38a8716a" +source = "git+https://github.com/stellar/rs-soroban-env?rev=fb8a384e#fb8a384eb73b1d157c13b073efa9c82ff490119f" dependencies = [ "backtrace", "dyn-fmt", @@ -660,7 +710,7 @@ dependencies = [ [[package]] name = "soroban-env-macros" version = "0.0.5" -source = "git+https://github.com/stellar/rs-soroban-env?rev=0a826765#0a826765e9728e263ec3eafa3ed81a1d38a8716a" +source = "git+https://github.com/stellar/rs-soroban-env?rev=fb8a384e#fb8a384eb73b1d157c13b073efa9c82ff490119f" dependencies = [ "itertools", "proc-macro2", @@ -672,7 +722,7 @@ dependencies = [ [[package]] name = "soroban-native-sdk-macros" version = "0.0.5" -source = "git+https://github.com/stellar/rs-soroban-env?rev=0a826765#0a826765e9728e263ec3eafa3ed81a1d38a8716a" +source = "git+https://github.com/stellar/rs-soroban-env?rev=fb8a384e#fb8a384eb73b1d157c13b073efa9c82ff490119f" dependencies = [ "itertools", "proc-macro2", @@ -683,19 +733,21 @@ dependencies = [ [[package]] name = "soroban-sdk" version = "0.0.4" -source = "git+https://github.com/stellar/rs-soroban-sdk?rev=bf6b383e#bf6b383ef4cc817223e7bd07725df468d880bb27" +source = "git+https://github.com/stellar/rs-soroban-sdk?rev=864a309b#864a309bca7ad91d6c7189fe87e018f64ad6ba06" dependencies = [ "bytes-lit", "ed25519-dalek", + "rand 0.8.5", "soroban-env-guest", "soroban-env-host", "soroban-sdk-macros", + "stellar-strkey", ] [[package]] name = "soroban-sdk-macros" version = "0.0.4" -source = "git+https://github.com/stellar/rs-soroban-sdk?rev=bf6b383e#bf6b383ef4cc817223e7bd07725df468d880bb27" +source = "git+https://github.com/stellar/rs-soroban-sdk?rev=864a309b#864a309bca7ad91d6c7189fe87e018f64ad6ba06" dependencies = [ "darling", "itertools", @@ -711,11 +763,12 @@ dependencies = [ [[package]] name = "soroban-spec" version = "0.0.4" -source = "git+https://github.com/stellar/rs-soroban-sdk?rev=bf6b383e#bf6b383ef4cc817223e7bd07725df468d880bb27" +source = "git+https://github.com/stellar/rs-soroban-sdk?rev=864a309b#864a309bca7ad91d6c7189fe87e018f64ad6ba06" dependencies = [ "base64", "darling", "itertools", + "prettyplease", "proc-macro2", "quote", "serde", @@ -729,16 +782,6 @@ dependencies = [ "wasmparser", ] -[[package]] -name = "soroban-token-contract" -version = "0.0.4" -source = "git+https://github.com/stellar/soroban-examples?rev=821dd95#821dd958275c6af827091c5fed75ed72f6dcbcc1" -dependencies = [ - "ed25519-dalek", - "soroban-auth", - "soroban-sdk", -] - [[package]] name = "soroban-wasmi" version = "0.16.0" @@ -773,6 +816,15 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" +[[package]] +name = "stellar-strkey" +version = "0.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "350ec80765002d972f7fa9bc2bd9e550fc3bf9fbe3f1c0e55b30755fccc2fd34" +dependencies = [ + "base32", +] + [[package]] name = "stellar-xdr" version = "0.0.1" @@ -782,7 +834,7 @@ checksum = "015594b09942b9f9cbe6972ba8c7de3a05a25c4994bbc7282f5f2fa6d28a81c4" [[package]] name = "stellar-xdr" version = "0.0.2" -source = "git+https://github.com/stellar/rs-stellar-xdr?rev=cebf5259#cebf52595c78fc3a4b5b40ba53ea1c0753777b5d" +source = "git+https://github.com/stellar/rs-stellar-xdr?rev=88ded341#88ded341ffbf54372ce851f766610ef978784d2a" dependencies = [ "base64", "serde", @@ -888,6 +940,12 @@ version = "0.9.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + [[package]] name = "wasmparser" version = "0.88.0" diff --git a/Cargo.toml b/Cargo.toml index fe564dc..a9c7b3a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,14 +16,14 @@ codegen-units = 1 lto = true [patch.crates-io] -soroban-sdk = { git = "https://github.com/stellar/rs-soroban-sdk", rev = "bf6b383e" } -soroban-spec = { git = "https://github.com/stellar/rs-soroban-sdk", rev = "bf6b383e" } -soroban-auth = { git = "https://github.com/stellar/rs-soroban-sdk", rev = "bf6b383e" } -soroban-sdk-macros = { git = "https://github.com/stellar/rs-soroban-sdk", rev = "bf6b383e" } -soroban-env-common = { git = "https://github.com/stellar/rs-soroban-env", rev = "0a826765" } -soroban-env-guest = { git = "https://github.com/stellar/rs-soroban-env", rev = "0a826765" } -soroban-env-host = { git = "https://github.com/stellar/rs-soroban-env", rev = "0a826765" } -soroban-env-macros = { git = "https://github.com/stellar/rs-soroban-env", rev = "0a826765" } -soroban-native-sdk-macros = { git = "https://github.com/stellar/rs-soroban-env", rev = "0a826765" } -stellar-xdr = { git = "https://github.com/stellar/rs-stellar-xdr", rev = "cebf5259" } -wasmi = { package = "soroban-wasmi", git = "https://github.com/stellar/wasmi", rev = "a61b6df" } \ No newline at end of file +soroban-sdk = { git = "https://github.com/stellar/rs-soroban-sdk", rev = "864a309b" } +soroban-spec = { git = "https://github.com/stellar/rs-soroban-sdk", rev = "864a309b" } +soroban-auth = { git = "https://github.com/stellar/rs-soroban-sdk", rev = "864a309b" } +soroban-sdk-macros = { git = "https://github.com/stellar/rs-soroban-sdk", rev = "864a309b" } +soroban-env-common = { git = "https://github.com/stellar/rs-soroban-env", rev = "fb8a384e" } +soroban-env-guest = { git = "https://github.com/stellar/rs-soroban-env", rev = "fb8a384e" } +soroban-env-host = { git = "https://github.com/stellar/rs-soroban-env", rev = "fb8a384e" } +soroban-env-macros = { git = "https://github.com/stellar/rs-soroban-env", rev = "fb8a384e" } +soroban-native-sdk-macros = { git = "https://github.com/stellar/rs-soroban-env", rev = "fb8a384e" } +stellar-xdr = { git = "https://github.com/stellar/rs-stellar-xdr", rev = "88ded341" } +wasmi = { package = "soroban-wasmi", git = "https://github.com/stellar/wasmi", rev = "a61b6df" } diff --git a/contracts/crowdfund/Cargo.toml b/contracts/crowdfund/Cargo.toml index 49ac25e..95f8613 100644 --- a/contracts/crowdfund/Cargo.toml +++ b/contracts/crowdfund/Cargo.toml @@ -10,21 +10,15 @@ crate-type = ["cdylib", "rlib"] [features] default = ["export"] export = [] -testutils = ["soroban-sdk/testutils", "soroban-auth/testutils", "soroban-token-contract/testutils", "dep:ed25519-dalek", "dep:sha2", "dep:stellar-xdr"] +testutils = ["soroban-sdk/testutils", "soroban-auth/testutils", "dep:stellar-xdr"] [dependencies] soroban-sdk = { version = "0.0.4" } soroban-auth = { version = "0.0.4" } stellar-xdr = { version = "0.0.1", features = ["next", "std"], optional = true } -ed25519-dalek = { version = "1.0.1", optional = true } -sha2 = { version = "0.10.2", optional = true } - -[target.'cfg(not(target_family="wasm"))'.dependencies] -soroban-token-contract = { git = "https://github.com/stellar/soroban-examples", rev = "821dd95", default-features = false, optional = true } [dev_dependencies] soroban-sdk = { version = "0.0.4", features = ["testutils"] } soroban-auth = { version = "0.0.4", features = ["testutils"] } -soroban-token-contract = { git = "https://github.com/stellar/soroban-examples", rev = "821dd95", default-features = false, features = ["testutils"] } soroban-crowdfund-contract = { path = ".", default-features = false, features = ["testutils"] } rand = { version = "0.7.3" } diff --git a/contracts/crowdfund/src/lib.rs b/contracts/crowdfund/src/lib.rs index c659096..b65233b 100644 --- a/contracts/crowdfund/src/lib.rs +++ b/contracts/crowdfund/src/lib.rs @@ -3,7 +3,7 @@ use soroban_auth::{Identifier, Signature}; use soroban_sdk::{contractimpl, contracttype, BigInt, BytesN, Env, IntoVal, RawVal}; mod token { - soroban_sdk::contractimport!(file = "../token/soroban_token_contract.wasm"); + soroban_sdk::contractimport!(file = "../token/soroban_token_spec.wasm"); } mod test; @@ -43,49 +43,49 @@ fn get_ledger_timestamp(e: &Env) -> u64 { } fn get_owner(e: &Env) -> Identifier { - e.contract_data() + e.data() .get(DataKey::Owner) .expect("not initialized") .unwrap() } fn get_deadline(e: &Env) -> u64 { - e.contract_data() + e.data() .get(DataKey::Deadline) .expect("not initialized") .unwrap() } fn get_started(e: &Env) -> u64 { - e.contract_data() + e.data() .get(DataKey::Started) .expect("not initialized") .unwrap() } fn get_target_amount(e: &Env) -> BigInt { - e.contract_data() + e.data() .get(DataKey::Target) .expect("not initialized") .unwrap() } fn get_token(e: &Env) -> BytesN<32> { - e.contract_data() + e.data() .get(DataKey::Token) .expect("not initialized") .unwrap() } fn get_user_deposited(e: &Env, user: &Identifier) -> BigInt { - e.contract_data() + e.data() .get(DataKey::User(user.clone())) .unwrap_or_else(|| Ok(BigInt::zero(&e))) .unwrap() } fn get_balance(e: &Env, contract_id: BytesN<32>) -> BigInt { - let client = token::ContractClient::new(&e, &contract_id); + let client = token::Client::new(&e, &contract_id); client.balance(&get_contract_id(e)) } @@ -106,13 +106,13 @@ fn get_state(e: &Env) -> State { } fn set_user_deposited(e: &Env, user: &Identifier, amount: BigInt) { - e.contract_data().set(DataKey::User(user.clone()), amount) + e.data().set(DataKey::User(user.clone()), amount) } fn transfer(e: &Env, contract_id: BytesN<32>, to: &Identifier, amount: &BigInt) { let nonce: BigInt = BigInt::zero(&e); - let client = token::ContractClient::new(&e, &contract_id); - client.xfer(&Signature::Contract, &nonce, to, amount); + let client = token::Client::new(&e, &contract_id); + client.xfer(&Signature::Invoker, &nonce, to, amount); } struct Crowdfund; @@ -134,16 +134,15 @@ impl Crowdfund { target_amount: BigInt, token: BytesN<32>, ) { - if e.contract_data().has(DataKey::Owner) { + if e.data().has(DataKey::Owner) { panic!("already initialized"); } - e.contract_data().set(DataKey::Owner, owner); - e.contract_data() - .set(DataKey::Started, get_ledger_timestamp(&e)); - e.contract_data().set(DataKey::Deadline, deadline); - e.contract_data().set(DataKey::Target, target_amount); - e.contract_data().set(DataKey::Token, token); + e.data().set(DataKey::Owner, owner); + e.data().set(DataKey::Started, get_ledger_timestamp(&e)); + e.data().set(DataKey::Deadline, deadline); + e.data().set(DataKey::Target, target_amount); + e.data().set(DataKey::Token, token); } pub fn deadline(e: Env) -> u64 { @@ -192,9 +191,9 @@ impl Crowdfund { set_user_deposited(&e, &user, balance + amount.clone()); let nonce = BigInt::zero(&e); - let client = token::ContractClient::new(&e, &get_token(&e)); + let client = token::Client::new(&e, &get_token(&e)); client.xfer_from( - &Signature::Contract, + &Signature::Invoker, &nonce, &user, &get_contract_id(&e), diff --git a/contracts/crowdfund/src/test.rs b/contracts/crowdfund/src/test.rs index 7250d59..95666fd 100644 --- a/contracts/crowdfund/src/test.rs +++ b/contracts/crowdfund/src/test.rs @@ -1,11 +1,13 @@ #![cfg(test)] +use super::token::{Client as Token, TokenMetadata}; use crate::testutils::{register_test_contract as register_crowdfund, Crowdfund}; -use ed25519_dalek::Keypair; use rand::{thread_rng, RngCore}; -use soroban_auth::Identifier; -use soroban_sdk::{testutils::LedgerInfo, BigInt, BytesN, Env, IntoVal}; -use soroban_token_contract::testutils::{register_test_contract as register_token, Token}; +use soroban_auth::{Identifier, Signature}; +use soroban_sdk::{ + testutils::{Accounts, Ledger}, + AccountId, BigInt, BytesN, Env, IntoVal, +}; fn generate_contract_id() -> [u8; 32] { let mut id: [u8; 32] = Default::default(); @@ -13,26 +15,25 @@ fn generate_contract_id() -> [u8; 32] { id } -fn generate_keypair() -> Keypair { - Keypair::generate(&mut thread_rng()) -} - -fn to_ed25519(e: &Env, kp: &Keypair) -> Identifier { - Identifier::Ed25519(kp.public.to_bytes().into_val(e)) -} - -fn create_token_contract(e: &Env, admin: &Keypair) -> (BytesN<32>, Token) { +fn create_token_contract(e: &Env, admin: &AccountId) -> (BytesN<32>, Token) { let id = generate_contract_id(); - register_token(&e, &id); + e.register_contract_token(&BytesN::from_array(e, &id)); let token = Token::new(e, &id); // decimals, name, symbol don't matter in tests - token.initialize(&to_ed25519(&e, admin), 7, "name", "symbol"); + token.init( + &Identifier::Account(admin.clone()), + &TokenMetadata { + name: "name".into_val(&e), + symbol: "symbol".into_val(&e), + decimals: 7, + }, + ); (BytesN::from_array(&e, &id), token) } fn create_crowdfund_contract( e: &Env, - owner: &Keypair, + owner: &AccountId, deadline: &u64, target_amount: &BigInt, token: &BytesN<32>, @@ -40,25 +41,24 @@ fn create_crowdfund_contract( let id = generate_contract_id(); register_crowdfund(&e, &id); let crowdfund = Crowdfund::new(e, &id); - crowdfund - .client() - .initialize(&to_ed25519(&e, owner), &deadline, &target_amount, &token); + crowdfund.client().initialize( + &Identifier::Account(owner.clone()), + &deadline, + &target_amount, + &token, + ); (BytesN::from_array(&e, &id), crowdfund) } fn advance_ledger(e: &Env, delta: u64) { - e.set_ledger(LedgerInfo { - protocol_version: e.ledger().protocol_version(), - sequence_number: e.ledger().sequence(), - timestamp: e.ledger().timestamp() + delta, - base_reserve: 1, // TODO: can't get current base reserve... does this matter for tests? - network_passphrase: Default::default(), //TODO: Figure out how to go from Bytes to Vec so we can use ledger.network_passphrase? + e.ledger().with_mut(|l| { + l.timestamp += delta; }); } struct Setup { env: Env, - user2: Keypair, + user2: AccountId, owner_id: Identifier, user1_id: Identifier, user2_id: Identifier, @@ -75,28 +75,43 @@ struct Setup { impl Setup { fn new() -> Self { let e: Env = Default::default(); - let owner = generate_keypair(); - let owner_id = to_ed25519(&e, &owner); - let user1 = generate_keypair(); - let user1_id = to_ed25519(&e, &user1); - let user2 = generate_keypair(); - let user2_id = to_ed25519(&e, &user2); + let owner = e.accounts().generate_and_create(); + let owner_id = Identifier::Account(owner.clone()); + let user1 = e.accounts().generate_and_create(); + let user1_id = Identifier::Account(user1.clone()); + let user2 = e.accounts().generate_and_create(); + let user2_id = Identifier::Account(user2.clone()); // the deadline is 10 seconds from now let deadline = e.ledger().timestamp() + 10; let target_amount = BigInt::from_i32(&e, 15); - let token_admin = generate_keypair(); + let token_admin = e.accounts().generate_and_create(); let (contract_token, token) = create_token_contract(&e, &token_admin); let (contract_crowdfund, crowdfund) = create_crowdfund_contract(&e, &owner, &deadline, &target_amount, &contract_token); let crowdfund_id = Identifier::Contract(contract_crowdfund); - token.mint(&token_admin, &user1_id, &BigInt::from_u32(&e, 10)); - token.mint(&token_admin, &user2_id, &BigInt::from_u32(&e, 5)); - - token.approve(&user1, &crowdfund_id, &BigInt::from_u32(&e, 10)); + token.with_source_account(&token_admin).mint( + &Signature::Invoker, + &BigInt::zero(&e), + &user1_id, + &BigInt::from_u32(&e, 10), + ); + token.with_source_account(&token_admin).mint( + &Signature::Invoker, + &BigInt::zero(&e), + &user2_id, + &BigInt::from_u32(&e, 5), + ); + + token.with_source_account(&user1).approve( + &Signature::Invoker, + &BigInt::zero(&e), + &crowdfund_id, + &BigInt::from_u32(&e, 10), + ); crowdfund .client() .deposit(&user1_id, &BigInt::from_u32(&e, 10)); @@ -134,8 +149,9 @@ fn test_expired() { #[test] fn test_success() { let setup = Setup::new(); - setup.token.approve( - &setup.user2, + setup.token.with_source_account(&setup.user2).approve( + &Signature::Invoker, + &BigInt::zero(&setup.env), &setup.crowdfund_id, &BigInt::from_u32(&setup.env, 5), ); @@ -189,8 +205,9 @@ fn sale_still_running() { #[should_panic(expected = "sale was successful, only the owner may withdraw")] fn sale_successful_only_owner() { let setup = Setup::new(); - setup.token.approve( - &setup.user2, + setup.token.with_source_account(&setup.user2).approve( + &Signature::Invoker, + &BigInt::zero(&setup.env), &setup.crowdfund_id, &BigInt::from_u32(&setup.env, 5), ); diff --git a/contracts/token/soroban_token_contract.wasm b/contracts/token/soroban_token_contract.wasm deleted file mode 100755 index f158a54..0000000 Binary files a/contracts/token/soroban_token_contract.wasm and /dev/null differ diff --git a/contracts/token/soroban_token_spec.wasm b/contracts/token/soroban_token_spec.wasm new file mode 100755 index 0000000..ed5e094 Binary files /dev/null and b/contracts/token/soroban_token_spec.wasm differ diff --git a/initialize.sh b/initialize.sh index 17fdd9b..7fcd8b4 100755 --- a/initialize.sh +++ b/initialize.sh @@ -3,31 +3,32 @@ set -e # TODO: Set the owner to something reasonable here. Probably whatever account -# soroban-cli is running stuff as? -admin="AAAABAAAAAEAAAAAAAAAAgAAAAUAAAAHQWNjb3VudAAAAAAEAAAAAQAAAAQAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=" +# soroban is running stuff as? +# This is an Identifier for Account GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF +# TODO: Have a nicer way to build Identifiers on the CLI +admin="AAAABAAAAAEAAAAAAAAAAgAAAAUAAAAHQWNjb3VudAAAAAAEAAAAAQAAAAcAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" echo Deploy the token contract -soroban-cli deploy --id 1 --wasm contracts/token/soroban_token_contract.wasm - -echo Initialize the token contract -soroban-cli invoke --id 1 \ - --fn initialize \ - --arg-xdr "$admin" \ - --arg-xdr AAAAAQAAAAc= \ - --arg-xdr AAAABAAAAAEAAAAEAAAAEENpcmNsZSBVUyBEb2xsYXI= \ - --arg-xdr AAAABAAAAAEAAAAEAAAABFVTREM= +TOKEN_ID="$( + soroban token create \ + --name "Example Token" \ + --symbol "EXT" \ + --decimal 2 +)" +mkdir -p .soroban +echo "$TOKEN_ID" > .soroban/token_id echo Build the crowdfund contract -cargo build --release --target wasm32-unknown-unknown +make build echo Deploy the crowdfund contract -soroban-cli deploy --id 0 --wasm target/wasm32-unknown-unknown/release/soroban_crowdfund_contract.wasm +soroban deploy --id 0 --wasm target/wasm32-unknown-unknown/release/soroban_crowdfund_contract.wasm echo Initialize the crowdfund contract deadline="$(($(date +"%s") + 86400))" -soroban-cli invoke --id 0 \ +soroban invoke --id 0 \ --fn initialize \ --arg-xdr "$admin" \ --arg "$deadline" \ --arg "1000000000" \ - --arg '[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]' + --arg "$TOKEN_ID" diff --git a/next.config.js b/next.config.js index 3dd7ef1..d37a6ab 100644 --- a/next.config.js +++ b/next.config.js @@ -1,4 +1,8 @@ +var fs = require('fs'); /** @type {import('next').NextConfig} */ module.exports = { reactStrictMode: true, + env: { + TOKEN_ID: fs.readFileSync('.soroban/token_id').toString().trim(), + }, }; diff --git a/pages/index.tsx b/pages/index.tsx index 6fffbf9..a6fe70e 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -5,32 +5,25 @@ import Head from 'next/head'; import * as SorobanSdk from 'soroban-sdk'; import styles from '../styles/Home.module.css'; import * as convert from "../convert"; -import { useNetwork, useAccount, useContractValue, useSendTransaction, ConnectButton } from "../wallet"; +import { ContractValue, useNetwork, useAccount, useContractValue, useSendTransaction, ConnectButton } from "../wallet"; let xdr = SorobanSdk.xdr; // Stub dummy data for now. const source = new SorobanSdk.Account('GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ', '0'); const CROWDFUND_ID = "0000000000000000000000000000000000000000000000000000000000000000"; -const TOKEN_ID = "0000000000000000000000000000000000000000000000000000000000000001"; +const TOKEN_ID: string = process.env.TOKEN_ID ?? ""; const Home: NextPage = () => { const { data: account } = useAccount(); // Call the contract rpcs to fetch values const token = { - balance: useContractValue(TOKEN_ID, "balance", xdr.ScVal.scvObject(xdr.ScObject.scoVec([ - xdr.ScVal.scvSymbol("Account"), - xdr.ScVal.scvObject(xdr.ScObject.scoBytes(Buffer.from(CROWDFUND_ID, 'hex'))) - ]))), + balance: useContractValue(TOKEN_ID, "balance", accountIdentifier(Buffer.from(CROWDFUND_ID, 'hex'))), decimals: useContractValue(TOKEN_ID, "decimals"), name: useContractValue(TOKEN_ID, "name"), symbol: useContractValue(TOKEN_ID, "symbol"), }; const deadline = useContractValue(CROWDFUND_ID, "deadline"); const started = useContractValue(CROWDFUND_ID, "started"); - const yourDepositsXdr = useContractValue(CROWDFUND_ID, "balance", xdr.ScVal.scvObject(xdr.ScObject.scoVec([ - xdr.ScVal.scvSymbol("Account"), - xdr.ScVal.scvObject(account ? xdr.ScObject.scoBytes(Buffer.from(account.address)) : null) - ]))); // Convert the result ScVals to js types const tokenBalance = convert.scvalToBigNumber(token.balance.result); @@ -39,7 +32,6 @@ const Home: NextPage = () => { const tokenSymbol = token.symbol.result && convert.scvalToString(token.symbol.result); const deadlineDate = deadline.result && new Date(convert.xdrUint64ToNumber(deadline.result.obj()?.u64() ?? xdr.Int64.fromString("0")) * 1000); const startedDate = started.result && new Date(convert.xdrUint64ToNumber(started.result.obj()?.u64() ?? xdr.Int64.fromString("0")) * 1000); - const yourDeposits = convert.scvalToBigNumber(yourDepositsXdr.result); return (