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

Undo breaking api change to registering tokens #777

Merged
merged 3 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
39 changes: 36 additions & 3 deletions soroban-sdk/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,8 @@ impl Env {
/// 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
Expand All @@ -594,11 +596,42 @@ impl Env {
///
/// # fn main() {
/// let env = Env::default();
/// env.register_contract_token();
/// env.register_contract_token(None);
/// # }
/// ```
pub fn register_contract_token(&self) -> BytesN<32> {
self.register_contract_with_source(xdr::ScContractCode::Token)
pub fn register_contract_token<'a>(
&self,
contract_id: impl Into<Option<&'a BytesN<32>>>,
) -> BytesN<32> {
if let Some(contract_id) = contract_id.into() {
let contract_id_hash = Hash(contract_id.into());
let data_key = xdr::ScVal::Static(xdr::ScStatic::LedgerKeyContractCode);
let key = LedgerKey::ContractData(LedgerKeyContractData {
contract_id: contract_id_hash.clone(),
key: data_key.clone(),
});
self.env_impl
.with_mut_storage(|storage| {
storage.put(
&key,
&LedgerEntry {
ext: xdr::LedgerEntryExt::V0,
last_modified_ledger_seq: 0,
data: xdr::LedgerEntryData::ContractData(xdr::ContractDataEntry {
contract_id: contract_id_hash.clone(),
key: data_key,
val: xdr::ScVal::Object(Some(xdr::ScObject::ContractCode(
xdr::ScContractCode::Token,
))),
}),
},
)
})
.unwrap();
contract_id.clone()
} else {
self.register_contract_with_source(xdr::ScContractCode::Token)
}
}

fn register_contract_with_source(&self, source: xdr::ScContractCode) -> BytesN<32> {
Expand Down
1 change: 1 addition & 0 deletions soroban-sdk/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ mod contractfile_with_sha256;
mod contractimport;
mod contractimport_with_error;
mod contractimport_with_sha256;
mod contracttoken;
85 changes: 85 additions & 0 deletions soroban-sdk/src/tests/contracttoken.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use crate as soroban_sdk;
use soroban_sdk::{
contractclient, contracttype, testutils::AccountId as _, AccountId, Bytes, BytesN, Env,
};

#[contractclient(name = "TokenClient")]
pub trait Token {
fn init(env: Env, admin: Identifier, metadata: TokenMetadata);
fn name(env: Env) -> Bytes;
}

#[derive(Clone)]
#[contracttype]
pub enum Identifier {
Account(AccountId),
}

#[derive(Clone)]
#[contracttype]
pub struct TokenMetadata {
pub name: Bytes,
pub symbol: Bytes,
pub decimals: u32,
}

#[test]
fn test_register_token() {
let e = Env::default();

let contract_id = e.register_contract_token(None);
let client = TokenClient::new(&e, &contract_id);

client.init(
&Identifier::Account(AccountId::random(&e)),
&TokenMetadata {
name: Bytes::from_slice(&e, b"testme"),
decimals: 7,
symbol: Bytes::from_slice(&e, &[]),
},
);

assert_eq!(client.name(), Bytes::from_slice(&e, b"testme"))
}

#[test]
fn test_register_token_at_id() {
let e = Env::default();

let contract_id = BytesN::from_array(&e, &[1; 32]);
e.register_contract_token(&contract_id);
let client = TokenClient::new(&e, &contract_id);

client.init(
&Identifier::Account(AccountId::random(&e)),
&TokenMetadata {
name: Bytes::from_slice(&e, b"testme"),
decimals: 7,
symbol: Bytes::from_slice(&e, &[]),
},
);

assert_eq!(client.name(), Bytes::from_slice(&e, b"testme"))
}

#[test]
fn test_reregister_over_wasm_with_token() {
let e = Env::default();

// Register a contract with bogus wasm.
let contract_id = e.register_contract_wasm(None, &[]);
// Reregister the contract with a token instead.
e.register_contract_token(&contract_id);
let client = TokenClient::new(&e, &contract_id);

client.init(
&Identifier::Account(AccountId::random(&e)),
&TokenMetadata {
name: Bytes::from_slice(&e, b"testme"),
decimals: 7,
symbol: Bytes::from_slice(&e, &[]),
},
);

assert_eq!(client.name(), Bytes::from_slice(&e, b"testme"))
}