# Smart Contract Interaction

> Source: https://taquito.io/docs/25.1.0/smartcontracts

Taquito allows developers to interact with Smart Contracts as if they are "Plain Old Javascript Objects."

The "Machine Language" of Tezos Smart Contracts is named [Michelson][3]. Michelson is a stack-based language that is human-readable. It's possible to author Smart-Contracts directly in Michelson. However, developers can use High-Level Languages (such as [Ligo][0] or [SmartPy][1]) to write smart contracts.

Taquito makes developing applications (dApps or traditional programs) around a Tezos Smart Contract easy. Taquito can also "originate" (create) a new Smart Contract to the Tezos Blockchain.

Michelson is a somewhat specialized language that isn't typical in Javascript or Typescript development contexts. Taquito helps to bridge the gap between the Tezos blockchain and a standard Javascript or Typescript development environment.

## Taquito's smart contract abstraction

Taquito assists developers by reading the Michelson code for a given contract from the blockchain. Based on the retrieved Michelson code, Taquito generates a `contract` javascript object with methods and storage that correspond to the contract's Michelson entry points, storage definitions, and values.

## The counter contract

In this guide, we use a straightforward "counter" smart contract to illustrate how Taquito works.

The counter contract has two entry points named `increment` and `decrement.` Taquito uses these entrypoints to generate corresponding javascript methods available to the developer.

The counter contract's storage is a simple integer that gets increased or decreased based on the calls to the entrypoints.

### Counter contract in JSLIGO v1.3.0

```
namespace Counter {
  type storage = int;
  type ret = [list<operation>, storage];

  // Three entrypoints

  @entry
  const increment = (delta : int, store : storage) : ret => [list([]), store + delta];

  @entry
  const decrement = (delta : int, store : storage) : ret => [list([]), store - delta];

  @entry
  const reset = (_p : unit, _s : storage) : ret => [list([]), 0];
};
```

You can view this contract and deploy it to a testnet using the [Ligo WebIDE][2]

### Counter contract Michelson source code

```
{ parameter (or (unit %reset) (or (int %decrement) (int %increment))) ;
  storage int ;
  code { UNPAIR ;
         IF_LEFT { DROP 2 ; PUSH int 0 } { IF_LEFT { SWAP ; SUB } { ADD } } ;
         NIL operation ;
         PAIR } }
```

## Loading the contract in Taquito

To load the contract from the Tezos Blockchain, we use the `Tezos.contract.at` method.
We can inspect the contract methods and data types using the `c.parameterSchema.ExtractSignatures()` method.

The following example shows how to load the contract and view the methods on that contract.

#### Contract API

```js
try {
  const contract = await Tezos.contract.at('KT1VNTTHJ56xDoBoS1n3HDcr4s2zt6yDshbG');
  const methods = contract.parameterSchema.ExtractSignatures();
  console.log(JSON.stringify(methods, null, 2));
} catch (error) {
  console.log(`Error: ${error}`);
}
```

  
#### Wallet API

```js
try {
  const contract = await Tezos.wallet.at('KT1VNTTHJ56xDoBoS1n3HDcr4s2zt6yDshbG');
  const methods = contract.parameterSchema.ExtractSignatures();
  console.log(JSON.stringify(methods, null, 2));
} catch (error) {
  console.log(`Error: ${error}`);
}
```
  

The `at()` method causes Taquito to query a Tezos nodes RPC API for the contracts "script" and "entrypoints." From these two inputs, Taquito builds an ordinary JavaScript object with methods that correspond to the Smart Contracts entrypoints.

The `at` method returns a representation of the contract as a plain old javascript object. Taquito dynamically creates an `increment` and `decrement` method that the developer can call as follows:

- `contract.methodsObject.increment()`
- `contract.methodsObject.decrement()`

In Tezos, to call an entrypoint on a contract, one must send a transfer operation. In the counter contract case, the transfer value can be `0` as the contract does not expect to receive any tokens. The transfer must have the appropriate Michelson values specified as "params" to call the `increment` entrypoint.

We can inspect the transfer params produced by Taquito using the `toTransferParams()` method:

#### Contract API

```js
try {
  const contract = await Tezos.contract.at('KT1VNTTHJ56xDoBoS1n3HDcr4s2zt6yDshbG');
  const incrementParams = contract.methodsObject.increment(2).toTransferParams();
  console.log(JSON.stringify(incrementParams, null, 2));
} catch (error) {
  console.log(`Error: ${error}`);
}
```

  
#### Wallet API

```js
try {
  const contract = await Tezos.wallet.at('KT1VNTTHJ56xDoBoS1n3HDcr4s2zt6yDshbG');
  const incrementParams = contract.methodsObject.increment(2).toTransferParams();
  console.log(JSON.stringify(incrementParams, null, 2));
} catch (error) {
  console.log(`Error: ${error}`);
}
```
  

## Calling the increment function

In the next example, we call the `send()` method. This example requires a different ceremony for getting a temporary key for signing.

We call the `send()` method on the `increment()` method. Taquito then forges this operation into a transfer operation (with a transfer value of zero), signs the operation with our testing key, and injects or broadcasts the operation to the Tezos RPC node.

Then we wait for the `confirmation(3)` to complete. The `3` number tells Taquito how many confirmations to wait for before resolving the promise. `3` is a good value for this type of demonstration, but we recommend a higher value if you are dealing with mainnet transactions.

#### Contract API

```js
try {
  const contract = await Tezos.contract.at('KT1VNTTHJ56xDoBoS1n3HDcr4s2zt6yDshbG');

  console.log(`Incrementing storage value by 7...`);
  const operation = await contract.methodsObject.increment(7).send();

  console.log(`Waiting for ${operation.hash} to be confirmed...`);
  const hash = await operation.confirmation(3);

  console.log(`Operation injected: https://shadownet.tzkt.io/${hash}/operations`);
} catch (error) {
  console.log(`Error: ${JSON.stringify(error, null, 2)}`);
}
```

  
#### Wallet API

```js
try {
  const contract = await Tezos.wallet.at('KT1VNTTHJ56xDoBoS1n3HDcr4s2zt6yDshbG');

  console.log(`Incrementing storage value by 7...`);
  const operation = await contract.methodsObject.increment(7).send();

  console.log(`Waiting for ${operation.opHash} to be confirmed...`);
  const confirmation = await operation.confirmation(3);

  console.log(`Operation injected: https://shadownet.tzkt.io/${confirmation.block.hash}/operations`);
} catch (error) {
  console.log(`Error: ${JSON.stringify(error, null, 2)}`);
}
```
  

[0]: https://ligolang.org/
[1]: https://smartpy.io/
[2]: https://ide.ligolang.org/local/default-project
[3]: https://tezos.gitlab.io/whitedoc/michelson.html
