# Usando el Contract Kit

### Configuración de opciones <a href="#setting-default-tx-options" id="setting-default-tx-options"></a>

`kit`le permite configurar opciones de transacción predeterminadas:

```
import { CeloContract } from "@celo/contractkit";

let accounts = await kit.web3.eth.getAccounts();
kit.defaultAccount = accounts[0];
// paid gas in cUSD
await kit.setFeeCurrency(CeloContract.StableToken);
```

### Configurado `feeCurrency`para una transacción <a href="#set-feecurrency-for-a-transaction" id="set-feecurrency-for-a-transaction"></a>

Puede configurar `feeCurrency`para cada transacción individualmente configurando el `feeCurrency`campo en el `.send()`método. El `feeCurrency`campo acepta direcciones de contrato de monedas de tarifa incluidas en la lista blanca.

```
let cUSDcontract = await kit.contracts.getStableToken();
let cUSDtx = await cUSDcontract
  .transfer(someAddress, amount)
  .send({ feeCurrency: cUSDcontract.address });
```

### Obtener el saldo <a href="#getting-the-total-balance" id="getting-the-total-balance"></a>

Este método `kit`devolverá el CELO, el CELO bloqueado, el cUSD y el saldo total de la dirección.

```
let totalBalance = await kit.getTotalBalance(myAddress);
```

### Implementar un contrato <a href="#deploy-a-contract" id="deploy-a-contract"></a>

Implementar un contrato con la cuenta predeterminada ya configurada. Simplemente envíe una transacción sin `to:`campo. Vea más sobre el envío de transacciones personalizadas a continuación.

Puede verificar la implementación en el [explorador de bloques de Alfajores aquí](https://alfajores-blockscout.celo-testnet.org/) . Espere el recibo y regístrelo para obtener los detalles de la transacción.

```
let bytecode = "0x608060405234..."; // compiled Solidity deployment bytecode

let tx = await kit.sendTransaction({
  data: bytecode,
});

let receipt = tx.waitReceipt();
console.log(receipt);
```

### Envío de transacciones <a href="#sending-custom-transactions" id="sending-custom-transactions"></a>

El objeto de transacción de Celo no es el mismo que el de Ethereum. Hay un nuevo campo opcional presente:

* `feeCurrency`(dirección del contrato ERC20 a utilizar para pagar el gas)

`feeCurrency`permite pagar las tarifas de transacción en monedas distintas a CELO. Las monedas de tarifa admitidas actualmente son CELO, cUSD y cEUR. Puede especificar la moneda pasando la dirección del contrato de la moneda en la que desea que se paguen las tarifas de transacción.

Celo también acepta transacciones originales de tipo Ethereum, por lo que puede utilizar herramientas de firma de Ethereum (como Metamask), así como carteras y herramientas específicas de Celo. Puede leer más sobre estos formatos de transacciones en [CIP 35](https://github.com/celo-org/celo-proposals/blob/master/CIPs/cip-0035.md) .

Para una transacción sin procesar:

```
const tx = kit.sendTransaction({
  from: myAddress,
  to: someAddress,
  value: oneGold,
});
const hash = await tx.getHash();
const receipt = await tx.waitReceipt();
```

Al interactuar con un objeto de contrato web3:

```
const goldtoken = await kit._web3Contracts.getGoldToken();
const oneGold = kit.web3.utils.toWei("1", "ether");

const txo = await goldtoken.methods.transfer(someAddress, oneGold);
const tx = await kit.sendTransactionObject(txo, { from: myAddress });
const hash = await tx.getHash();
const receipt = await tx.waitReceipt();
```

### Interactuar con contratos <a href="#interacting-with-custom-contracts" id="interacting-with-custom-contracts"></a>

Puede utilizar ContractKit para interactuar con cualquier contrato inteligente implementado, siempre que tenga la dirección del contrato y la [ABI](https://docs.soliditylang.org/en/latest/abi-spec.html) . Para hacerlo, inicializará una nueva `web3`instancia de Contrato. Luego puede llamar funciones en la instancia del contrato para leer el estado o enviar transacciones para actualizar el contrato. Puede ver algunos fragmentos de código a continuación. Para obtener un ejemplo más completo, consulte la sección [Interactuar con contratos personalizados](https://docs.celo.org/developer/walkthrough/hello-contract-remote-node#interacting-with-custom-contracts) del ejemplo de código Implementar un contrato.

```
let cUSDcontract = await kit.contracts.getStableToken();
let contract = new kit.connection.web3.eth.Contract(ABI, address); // Init a web3.js contract instance
let name = await instance.methods.getName().call(); // Read contract state

// Specifying the 'from' account and 'feeCurrency' is optional
// Transactions with an unspecified feeCurrency field will default to paying fees in CELO
const tx = await instance.methods
  .setName(newName)
  .send({ from: account.address, feeCurrency: cUSDcontract.address });
```

### Vender CELO sólo si la tasa es favorable <a href="#selling-celo-only-if-the-rate-is-favorable" id="selling-celo-only-if-the-rate-is-favorable"></a>

```
// This is at lower price I will accept in cUSD for every CELO
const favorableAmount = 100;
const amountToExchange = kit.web3.utils.toWei("10", "ether");
const oneGold = kit.web3.utils.toWei("1", "ether");
const exchange = await kit.contracts.getExchange();

const amountOfcUsd = await exchange.quoteGoldSell(oneGold);

if (amountOfcUsd > favorableAmount) {
  const goldToken = await kit.contracts.getGoldToken();
  const approveTx = await goldToken
    .approve(exchange.address, amountToExchange)
    .send();
  const approveReceipt = await approveTx.waitReceipt();

  const usdAmount = await exchange.quoteGoldSell(amountToExchange);
  const sellTx = await exchange.sellGold(amountToExchange, usdAmount).send();
  const sellReceipt = await sellTx.waitReceipt();
}
```

### Comprando todos los CELO que puedo, con los cUSD en mi cuenta <a href="#buying-all-the-celo-i-can-with-the-cusd-in-my-account" id="buying-all-the-celo-i-can-with-the-cusd-in-my-account"></a>

```
const stableToken = await this.contracts.getStableToken();
const exchange = await this.contracts.getExchange();

const cUsdBalance = await stableToken.balanceOf(myAddress);

const approveTx = await stableToken
  .approve(exchange.address, cUsdBalance)
  .send();
const approveReceipt = await approveTx.waitReceipt();

const goldAmount = await exchange.quoteUsdSell(cUsdBalance);
const sellTx = await exchange.sellDollar(cUsdBalance, goldAmount).send();
const sellReceipt = await sellTx.waitReceipt();
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://andino.gitbook.io/andino-recursos/celo/construir-en-celo/herramientas-para-desarrolladores/contract-kit/usando-el-contract-kit.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
