Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove token deploy in anticipation of removal of the soroban only built-in token #798

Merged
merged 5 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions soroban-sdk/src/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,6 @@ impl DeployerWithCurrentContract {
);
unsafe { BytesN::<32>::unchecked_new(env.clone(), id) }
}

/// Deploy a built-in token contract.
///
/// The contract ID from the currently executing contract and the given salt
/// will be used to derive a contract ID for the deployed contract.
///
/// Returns the deployed contract's ID.
pub fn deploy_token(&self) -> BytesN<32> {
let env = &self.env;
let id = env.create_token_from_contract(self.salt.to_object());
unsafe { BytesN::<32>::unchecked_new(env.clone(), id) }
}
}

#[doc(hidden)]
Expand Down
42 changes: 12 additions & 30 deletions soroban-sdk/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,36 +438,18 @@ impl Env {
)
}

/// Register the built-in token contract with the [Env] for testing.
///
/// Passing a contract ID for the first arguments registers the contract
/// with that contract ID. Providing `None` causes a random ID to be
/// assigned to the contract.
///
/// Registering a contract that is already registered replaces it.
///
/// Returns the contract ID of the registered contract.
///
/// ### Examples
/// ```
/// use soroban_sdk::{BytesN, Env};
///
/// #[test]
/// fn test() {
/// # }
/// # fn main() {
/// let env = Env::default();
/// env.register_contract_token(None);
/// }
/// ```
pub fn register_contract_token<'a>(
&self,
contract_id: impl Into<Option<&'a BytesN<32>>>,
) -> BytesN<32> {
self.register_contract_with_optional_contract_id_and_source(
contract_id,
xdr::ScContractCode::Token,
)
/// Register the built-in Stellar Asset Contract for testing.
pub fn register_stellar_asset_contract(&self, asset: xdr::Asset) -> BytesN<32> {
let create = xdr::HostFunction::CreateContract(xdr::CreateContractArgs {
contract_id: xdr::ContractId::Asset(asset),
source: xdr::ScContractCode::Token,
});

self.env_impl
.invoke_function(create)
.unwrap()
.try_into_val(self)
.unwrap()
}

fn register_contract_with_optional_contract_id_and_source<'a>(
Expand Down
1 change: 0 additions & 1 deletion soroban-sdk/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@ mod contractfile_with_sha256;
mod contractimport;
mod contractimport_with_error;
mod contractimport_with_sha256;
mod contracttoken;
85 changes: 0 additions & 85 deletions soroban-sdk/src/tests/contracttoken.rs

This file was deleted.

2 changes: 1 addition & 1 deletion soroban-sdk/src/xdr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ pub use super::env::xdr::{
};

// XDR for ledger entries.
pub use super::env::xdr::{AccountEntry, AccountId};
pub use super::env::xdr::{AccountEntry, AccountId, Asset};
66 changes: 21 additions & 45 deletions soroban-token-spec/src/tests/use_token_contract.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use soroban_auth::{
testutils::ed25519::{generate, sign},
Identifier, Signature,
};
use soroban_sdk::{contractimpl, contracttype, symbol, BytesN, Env, IntoVal};
use soroban_auth::{testutils::ed25519::generate, Identifier, Signature};
use soroban_sdk::{contractimpl, contracttype, BytesN, Env, IntoVal};

mod token_contract {
soroban_sdk::contractimport!(
Expand All @@ -11,17 +8,13 @@ mod token_contract {
pub type TokenClient = Client;
}

use token_contract::{TokenClient, TokenMetadata};
use token_contract::TokenClient;

#[contracttype]
pub enum DataKey {
Token,
}

fn get_contract_id(e: &Env) -> Identifier {
Identifier::Contract(e.get_current_contract().into())
}

fn get_token(e: &Env) -> BytesN<32> {
e.storage().get_unchecked(DataKey::Token).unwrap()
}
Expand All @@ -30,62 +23,45 @@ pub struct TestContract;

#[contractimpl]
impl TestContract {
pub fn init(e: Env, salt: BytesN<32>) {
let id = e.deployer().with_current_contract(salt).deploy_token();
let metadata = TokenMetadata {
name: "name".into_val(&e),
symbol: "symbol".into_val(&e),
decimals: 7u32,
};
TokenClient::new(&e, &id).init(&get_contract_id(&e), &metadata);

e.storage().set(DataKey::Token, id);
pub fn init(e: Env, contract: BytesN<32>) {
e.storage().set(DataKey::Token, contract);
}

pub fn get_token(e: Env) -> BytesN<32> {
get_token(&e)
}

pub fn mint(e: Env, to: Identifier, amount: i128) {
TokenClient::new(&e, get_token(&e)).mint(&Signature::Invoker, &0, &to, &amount);
pub fn approve(e: Env, spender: Identifier, amount: i128) {
TokenClient::new(&e, get_token(&e)).approve(&Signature::Invoker, &0, &spender, &amount);
}

pub fn set_admin(e: Env, new_admin: Identifier) {
TokenClient::new(&e, get_token(&e)).set_admin(&Signature::Invoker, &0, &new_admin);
pub fn allowance(e: Env, from: Identifier, spender: Identifier) -> i128 {
TokenClient::new(&e, get_token(&e)).allowance(&from, &spender)
}
}

#[test]
fn test() {
use soroban_sdk::xdr::Asset;

let env = Env::default();

let token_contract_id = env.register_stellar_asset_contract(Asset::Native);

let contract_id = BytesN::from_array(&env, &[0; 32]);
env.register_contract(&contract_id, TestContract);
let client = TestContractClient::new(&env, &contract_id);

let salt = BytesN::from_array(&env, &[1; 32]);
client.init(&salt);
client.init(&token_contract_id);

let token_client = TokenClient::new(&env, &client.get_token());
assert_eq!(token_client.name(), "name".into_val(&env));
assert_eq!(token_client.name(), "native".into_val(&env));

let (id, signer) = generate(&env);
let (id2, _signer2) = generate(&env);
let (id, _signer) = generate(&env);

let amount = 10;
client.mint(&id, &amount);
assert_eq!(token_client.balance(&id), amount);

// transger admin so we can test ed25519 auth
client.set_admin(&id);

let nonce = &token_client.nonce(&id);
let sig = sign(
&env,
&signer,
&client.get_token(),
symbol!("mint"),
(&id, nonce, &id2, &amount),
client.approve(&id, &amount);
assert_eq!(
client.allowance(&Identifier::Contract(contract_id), &id),
amount
);
token_client.mint(&sig, nonce, &id2, &amount);
assert_eq!(token_client.balance(&id2), amount);
}