Skip to content

Commit

Permalink
Adding the structure for support for spanish Documentation (#104)
Browse files Browse the repository at this point in the history
* config docs in spanish + overview translation

* added support for translating versioning docs

Co-authored-by: Santi Balaguer <santibalaguer@Santis-MacBook-Pro.local>
  • Loading branch information
SBalaguer and Santi Balaguer authored Jan 18, 2023
1 parent 5d34729 commit a26c94c
Show file tree
Hide file tree
Showing 132 changed files with 8,820 additions and 0 deletions.
17 changes: 17 additions & 0 deletions docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ module.exports = {
position: 'right',
dropdownActiveClassDisabled: true,
},
{
type: 'localeDropdown',
position: 'right',
}
],
},
},
Expand Down Expand Up @@ -83,4 +87,17 @@ module.exports = {
},
}],
],
i18n: {
defaultLocale: 'en',
locales: ['en', 'es'],
localeConfigs: {
en: {
htmlLang: 'en-GB',
label: 'English'
},
es: {
label: 'Español'
}
},
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: Cargo.toml
slug: /basics/cargo-toml
---

TODO go through a typical Cargo.toml and explain what the dependencies mean
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
title: Contract Template
slug: /basics/contract-template
---

Change into your working directory and run:

```bash
cargo contract new foobar
```

This will create a new project folder named `foobar`.

```bash
cd foobar/
```

In the `lib.rs` file you find initial scaffolded code, which you can use as a starting point.

Quickly check that it compiles and the trivial tests pass with:

```bash
cargo +nightly test
```

Also check that you can build the Wasm file by running:

```bash
cargo +nightly contract build
```

If everything looks good, then we are ready to start programming!
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
---
title: Cross-Contract Calling
slug: /basics/cross-contract-calling
---

In ink! contracts it is possible to call ink! messages and ink! constructors. So ink! constructors allow
delegation and ink! messages can easily call other ink! messages.
Given another ink! contract like, we can call any of its functions.

See our [`delegator example contract`](https://github.com/paritytech/ink/blob/master/examples/delegator/lib.rs)
for an elaborate example which uses cross-contract calling.

### How it Works

In order to deploy the delegator smart contract we first
have to manually put the code of the other contract, receive
its code hash from the signalled event and put their code hash
into our calling smart contract.

The calling contract looks like this:

```rust
use other_contract::OtherContract;

//--snip--
#[ink(storage)]
struct MyContract {
/// The other contract.
other_contract: OtherContract,
}

impl MyContract {
/// Instantiate `MyContract with the given
/// sub-contract codes and some initial value.
#[ink(constructor)]
pub fn new(
other_contract_code_hash: Hash,
) -> Self {
let other_contract = OtherContract::new(1337)
.endowment(total_balance / 4)
.code_hash(other_contract_code_hash)
.instantiate()
.expect("failed at instantiating the `OtherContract` contract");
Self {
other_contract
}
}

/// Calls the other contract.
#[ink(message)]
pub fn call_other_contract(&self) -> i32 {
self.other_contract.get_value()
}
}
//--snip--
```

It's `Cargo.toml` contains
```toml
other_contract = { path = "other_contract", default-features = false, features = ["ink-as-dependency"] }
```

The `other_contract/Cargo.toml` contains this:

```toml
[features]
ink-as-dependency = []
```

Tells the ink! code generator to **always** or **never**
compile the smart contract as if it was used as a dependency of another ink!
smart contract.

The `other_contract/lib.rs`:

```rust
#[ink::contract]
pub mod other_contract {
/// Storage for the other contract.
#[ink(storage)]
pub struct OtherContract {
value: i32,
}

impl OtherContract {
/// Initializes the contract.
#[ink(constructor)]
pub fn new(value: i32) -> Self {
Self { value }
}

/// Returns the current state.
#[ink(message)]
pub fn get_value(&self) -> i32 {
self.value
}
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
title: Environment Functions
slug: /basics/environment-functions
---

ink! exposes a number of handy environment functions.
A full overview [is found here](https://docs.rs/ink_env/3.3.1/ink_env/#functions).

In an `#[ink(constructor)]` use `Self::env()` to access those,
in an `#[ink(message)]` use `self.env()`.
So e.g. `Self::env().caller()` or `self.env().caller()`.

Some handy functions include:

* [`caller()`](https://docs.rs/ink_env/3.3.1/ink_env/fn.caller.html): Returns the address of the caller of the executed contract.
* [`account_id()`](https://docs.rs/ink_env/3.3.1/ink_env/fn.account_id.html): Returns the account ID of the executed contract.
* [`balance()`](https://docs.rs/ink_env/3.3.1/ink_env/fn.balance.html): Returns the balance of the executed contract.
* [`block_number()`](https://docs.rs/ink_env/3.3.1/ink_env/fn.block_number.html): Returns the current block number.
* [`random()`](https://docs.rs/ink_env/3.3.1/ink_env/fn.random.html): Returns a random hash seed.
* [`emit_event(…)`](https://docs.rs/ink_env/3.3.1/ink_env/fn.emit_event.html): Emits an event with the given event data.
* [`transfer(…)`](https://docs.rs/ink_env/3.3.1/ink_env/fn.transfer.html): Transfers value from the contract to the destination account ID.
* [`hash_bytes(…)`](https://docs.rs/ink_env/3.3.1/ink_env/fn.hash_bytes.html): Conducts the crypto hash of the given input and stores the result in output.
* […and many more](https://docs.rs/ink_env/3.3.1/ink_env/#functions).
123 changes: 123 additions & 0 deletions i18n/es/docusaurus-plugin-content-docs/current/basics/events.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
---
title: Events
slug: /basics/events
---

An ink! smart contract may define events that it can emit during contract execution.
Emitting events can be used by third party tools to query information about a contract's
execution and state.

The following example ink! contract shows how an event `Transferred` is defined and
emitted in the `#[ink(constructor)]`.

```rust
use ink_lang as ink;

#[ink::contract]
mod erc20 {
/// Defines an event that is emitted
/// every time value is transferred.
#[ink(event)]
pub struct Transferred {
from: Option<AccountId>,
to: Option<AccountId>,
value: Balance,
}

#[ink(storage)]
pub struct Erc20 {
total_supply: Balance,
// more fields ...
}

impl Erc20 {
#[ink(constructor)]
pub fn new(initial_supply: Balance) -> Self {
let caller = Self::env().caller();
Self::env().emit_event(Transferred {
from: None,
to: Some(caller),
value: initial_supply,
});
Self { total_supply: initial_supply }
}

#[ink(message)]
pub fn total_supply(&self) -> Balance {
self.total_supply
}
}
}
```

See our [`ERC20 example contract`](https://github.com/paritytech/ink/blob/master/examples/erc20/lib.rs)
for an elaborate example which uses events.

## Event Definition

This is how an event definition looks:

```rust
#[ink(event)]
pub struct Transferred {
#[ink(topic)]
from: Option<AccountId>,

#[ink(topic)]
to: Option<AccountId>,

amount: Balance

}
```

Add the `#[ink(topic)]` attribute tag to each item in your event that you want to have indexed.
A good rule of thumb is to ask yourself if somebody might want to search for this topic.
For this reason the `amount` in the exemplary event above was not
made indexable ‒ there will most probably be a lot of different events with
differing amounts each.

The signature of the event is by default one of the topics of the event, except
if you annotate the event with `#[ink(anonymous)]`.
See [here](/macros-attributes/anonymous) for details on this attribute.


## Emitting Events in a Constructor

In a constructor events are emitted via `Self::env().emit_event()`.
See this example:

```rust
#[ink(constructor)]
pub fn new(initial_value: Balance) -> Self {
let caller = Self::env().caller();
let mut balances = HashMap::new();
balances.insert(caller, initial_supply);

Self::env().emit_event(Transferred {
from: None,
to: Some(caller),
amount: initial_supply
});

Self { total_supply: initial_supply, balances }
}
```

## Emitting Events from Messages

In a message events are emitted via `self.env().emit_event()`:

```rust
#[ink(message)]
pub fn transfer(&mut self, to: AccountId, amount: Balance) -> Result {
let from = self.env().caller();
// implementation hidden
self.env().emit_event(Transferred {
from: Some(from),
to: Some(to),
amount
});
Ok(())
}
```
Loading

0 comments on commit a26c94c

Please sign in to comment.