Skip to content

Commit

Permalink
Undo breaking api change to registering tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmcculloch committed Dec 2, 2022
1 parent 39d223c commit 11bb481
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 3 deletions.
37 changes: 34 additions & 3 deletions soroban-sdk/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,11 +588,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;
63 changes: 63 additions & 0 deletions soroban-sdk/src/tests/contracttoken.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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"))
}

0 comments on commit 11bb481

Please sign in to comment.