Skip to content

Commit

Permalink
refactor(autonomi): rename calls for rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
b-zee committed Oct 11, 2024
1 parent 5fdb8b4 commit c3a3f3b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 29 deletions.
1 change: 1 addition & 0 deletions autonomi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ evmlib = { path = "../evmlib", version = "0.1", features = ["wasm-bindgen"] }
# See https://github.com/sebcrozet/instant/blob/7bd13f51f5c930239fddc0476a837870fb239ed7/README.md#using-instant-for-a-wasm-platform-where-performancenow-is-not-available
instant = { version = "0.1", features = ["wasm-bindgen", "inaccurate"] }
js-sys = "0.3.70"
test_utils = { path = "../test_utils" }
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
tracing-web = "0.1.3"

Expand Down
11 changes: 7 additions & 4 deletions autonomi/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,19 @@
console.log("wallet retrieved");

const data = new Uint8Array([1, 2, 3]);
console.log("our data: ", data);

console.log("calculating cost...");
let result = await client.cost(data, wallet);
let result = await client.dataCost(data, wallet);
console.log("calculated cost: ", result.toString());

console.log("putting...");
const dataAddr = await client.putData(data, wallet);
console.log("put done!");
const dataAddr = await client.dataPut(data, wallet);
console.log("put done: ", dataAddr.toString());

console.log("Uploaded data to: ", dataAddr.toString());
console.log("getting...");
const data_get = await client.dataGet(dataAddr);
console.log("get done: ", data_get, " (original data: ", data, ")");
}

document.getElementById ("btn-run").addEventListener("click", run, false);
Expand Down
42 changes: 17 additions & 25 deletions autonomi/src/client/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct DataAddr(xor_name::XorName);
impl DataAddr {
#[wasm_bindgen(js_name = toString)]
pub fn to_string(&self) -> String {
crate::client::address::xorname_to_str(self.0)
crate::client::address::addr_to_str(self.0)
}
}

Expand Down Expand Up @@ -41,34 +41,34 @@ impl Client {
Ok(Client(client))
}

#[wasm_bindgen(js_name = putChunk)]
pub async fn put_chunk(&self, _data: Vec<u8>, _wallet: Wallet) -> Result<ChunkAddr, JsError> {
unimplemented!()
#[wasm_bindgen(js_name = chunkPut)]
pub async fn chunk_put(&self, _data: Vec<u8>, _wallet: Wallet) -> Result<ChunkAddr, JsError> {
async { unimplemented!() }.await
}

#[wasm_bindgen(js_name = getChunk)]
pub async fn get_chunk(&self, addr: ChunkAddr) -> Result<Vec<u8>, JsError> {
let chunk = self.0.fetch_chunk(addr.0).await?;
#[wasm_bindgen(js_name = chunkGet)]
pub async fn chunk_get(&self, addr: ChunkAddr) -> Result<Vec<u8>, JsError> {
let chunk = self.0.chunk_get(addr.0).await?;
Ok(chunk.value().to_vec())
}

#[wasm_bindgen(js_name = putData)]
pub async fn put_data(&self, data: Vec<u8>, wallet: Wallet) -> Result<DataAddr, JsError> {
#[wasm_bindgen(js_name = dataPut)]
pub async fn data_put(&self, data: Vec<u8>, wallet: Wallet) -> Result<DataAddr, JsError> {
let data = crate::Bytes::from(data);
let xorname = self.0.put(data, &wallet.0).await?;
let xorname = self.0.data_put(data, &wallet.0).await?;
Ok(DataAddr(xorname))
}

#[wasm_bindgen(js_name = getData)]
pub async fn get_data(&self, addr: DataAddr) -> Result<Vec<u8>, JsError> {
let data = self.0.get(addr.0).await?;
#[wasm_bindgen(js_name = dataGet)]
pub async fn data_get(&self, addr: DataAddr) -> Result<Vec<u8>, JsError> {
let data = self.0.data_get(addr.0).await?;
Ok(data.to_vec())
}

#[wasm_bindgen]
pub async fn cost(&self, data: Vec<u8>) -> Result<AttoTokens, JsValue> {
#[wasm_bindgen(js_name = dataCost)]
pub async fn data_cost(&self, data: Vec<u8>) -> Result<AttoTokens, JsValue> {
let data = crate::Bytes::from(data);
let cost = self.0.cost(data).await.map_err(|e| JsError::from(e))?;
let cost = self.0.data_cost(data).await.map_err(JsError::from)?;

Ok(AttoTokens(cost))
}
Expand All @@ -81,15 +81,7 @@ pub struct Wallet(evmlib::wallet::Wallet);
/// environment variable that was used during the build process of this library.
#[wasm_bindgen(js_name = getFundedWallet)]
pub fn funded_wallet() -> Wallet {
let network = evmlib::utils::evm_network_from_env().expect("network init from env");

let private_key = option_env!("EVM_PRIVATE_KEY")
.unwrap_or_else(|| "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80");

Wallet(
evmlib::wallet::Wallet::new_from_private_key(network, private_key)
.expect("Invalid private key"),
)
Wallet(test_utils::evm::get_funded_wallet())
}

/// Enable tracing logging in the console.
Expand Down

0 comments on commit c3a3f3b

Please sign in to comment.