Просмотр исходного кода

sdk/contract_id: Use From for instantiation.

parazyd 3 лет назад
Родитель
Сommit
4b0a57cdc8
2 измененных файлов с 11 добавлено и 5 удалено
  1. 1 1
      example/smart-contract/tests/runtime.rs
  2. 10 4
      src/sdk/src/crypto/contract_id.rs

+ 1 - 1
example/smart-contract/tests/runtime.rs

@@ -51,7 +51,7 @@ fn run_contract() -> Result<()> {
     // Load the wasm binary into memory and create an execution runtime
     // ================================================================
     let wasm_bytes = std::fs::read("contract.wasm")?;
-    let contract_id = ContractId::new(pallas::Base::from(1));
+    let contract_id = ContractId::from(pallas::Base::from(1));
     let mut runtime = Runtime::new(&wasm_bytes, blockchain.clone(), contract_id)?;
 
     // Deploy function to initialize the smart contract state.

+ 10 - 4
src/sdk/src/crypto/contract_id.rs

@@ -19,18 +19,18 @@
 use darkfi_serial::{serialize, SerialDecodable, SerialEncodable};
 use pasta_curves::{group::ff::PrimeField, pallas};
 
+/// ContractId represents an on-chain identifier for a certain
+/// smart contract.
 #[derive(Copy, Clone, Debug, Eq, PartialEq, SerialEncodable, SerialDecodable)]
 pub struct ContractId(pallas::Base);
 
 impl ContractId {
-    pub fn new(contract_id: pallas::Base) -> Self {
-        Self(contract_id)
-    }
-
+    /// Get the inner `pallas::Base` element.
     pub fn inner(&self) -> pallas::Base {
         self.0
     }
 
+    /// Create a `ContractId` object from given bytes.
     pub fn from_bytes(x: [u8; 32]) -> Self {
         // FIXME: Handle Option
         Self(pallas::Base::from_repr(x).unwrap())
@@ -47,6 +47,12 @@ impl ContractId {
     }
 }
 
+impl From<pallas::Base> for ContractId {
+    fn from(x: pallas::Base) -> Self {
+        Self(x)
+    }
+}
+
 impl std::fmt::Display for ContractId {
     fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
         // base58 encoding