# 컨트랙트 실행

### 1. Request.executeContractWithEncoded (>= v1.2.0) <a href="#id-1.-request.executecontractwithencoded-greater-than-v1.2.0" id="id-1.-request.executecontractwithencoded-greater-than-v1.2.0"></a>

**Parameters**

<table><thead><tr><th width="181.33333333333331">이름</th><th width="138">타입</th><th>설명</th></tr></thead><tbody><tr><td>chainId</td><td>number</td><td>해당 네트워크의 체인 아이디</td></tr><tr><td>appName</td><td>string</td><td>컨트랙트 실행 시 FAVORLET 앱에 노출될 이름</td></tr><tr><td>transactions</td><td>object array</td><td><p>컨트랙트를 실행하기 위해 필요한 정보</p><p>(자세한 내용은 아래를 참고)</p></td></tr></tbody></table>

&#x20;

**transactions 객체는 다음을 포함합니다.**

<table><thead><tr><th width="113">이름</th><th width="127.33333333333331">타입</th><th>설명</th></tr></thead><tbody><tr><td>from</td><td>string</td><td>트랜잭션을 호출할 지갑 주소</td></tr><tr><td>to</td><td>string</td><td>호출되는 컨트랙트 주소</td></tr><tr><td>value</td><td>string</td><td>(optional) 호출하는 abi 함수가 payable 인 경우 플랫폼 코인 전송</td></tr><tr><td>data</td><td>string</td><td><p>함수 signature와 Solidity tightly packed 형식으로 전달된 매개 변수를 인코딩한 값입니다.<br><a href="https://docs.ethers.org/v5/api/utils/abi/interface/#Interface--encoding">Interface</a> </p><p><a href="https://web3js.readthedocs.io/en/v1.2.7/web3-eth-contract.html#methods-mymethod-encodeabi">web3.eth.Contract — web3.js 1.0.0 documentation</a></p></td></tr><tr><td>gasLimit</td><td>string</td><td>(optional) 사용할 gasLimit 없을시 앱내 estimate 함수실행</td></tr></tbody></table>

&#x20;

**Response**

<table><thead><tr><th width="163.33333333333331">이름</th><th width="139">타입</th><th>설명</th></tr></thead><tbody><tr><td>requestId</td><td>string</td><td>작업 또는 트랜잭션 요청을 추적하기 위한 id</td></tr><tr><td>expiredAt</td><td>number</td><td>작업 또는 트랜잭션 요청이 만료 되는 시간(단위: 초)</td></tr></tbody></table>

&#x20;

**Example**

```javascript
import { Request } from "favorlet.js";

const response = await Request.executeContractWithEncoded({
  chainId: 8217, // 해당 체인 id
  appName: "BlockChainApp", // FAVORLET 앱에 노출
  transactions: [
    {
      from: "0x{address in hex}", // 트랜잭션을 호출할 지갑 주소
      to: "0x{address in hex}", // 호출되는 컨트랙트 주소
      value: "1000000000000000000", // 호출하는 abi 함수가 payable 인 경우 플랫폼 코인 전송
      data: "0x{hex}"
    },
  ],
});

console.log(response);
/*
{
  requestId: "4ee3ed57-b785-4196-9018-7b682c7a8048",
  expiredAt: 1667893310,
}
*/
```

### 2. createDeepLink <a href="#id-2.-createdeeplink" id="id-2.-createdeeplink"></a>

**Parameters**

<table><thead><tr><th width="185">타입</th><th>설명</th></tr></thead><tbody><tr><td>string</td><td>Request 요청시 전달받은 requestId<br></td></tr></tbody></table>

**Response**

<table><thead><tr><th width="185">타입</th><th>설명</th></tr></thead><tbody><tr><td>string</td><td>FAVORLET 앱 내 해당 요청 페이지 DeepLink</td></tr></tbody></table>

**Example**

```javascript
import { createDeepLink } from "favorlet.js";

const deepLink = createDeepLink(response.requestId); // Request를 통해 받은 requestId

/*
- PC: 생성된 deepLink를 qrcode 라이브러리를 이용하여 QR 생성 후 스마트폰으로 스캔
- Mobile: window.location.href 등을 사용하여 생성된 deepLink로 직접 이동
*/
console.log(deepLink);
// https://app.favorlet.link/?requestId=4ee3ed57-b785-4196-9018-7b682c7a8048
```

### 3. receipt <a href="#id-3.-receipt" id="id-3.-receipt"></a>

```javascript
import { receipt } from "favorlet.js";

const result = await receipt(response.requestId); // Request를 통해 받은 requestId

console.log(result);
/*
- status에 따라 이후 분기 처리

### requested
{
  requestId: "4ee3ed57-b785-4196-9018-7b682c7a8048",
  expiredAt: 1667893310,
  action: "executeContract",
  transactions: [
    { status: "requested" }
  ]
}

### executed
{
  requestId: "4ee3ed57-b785-4196-9018-7b682c7a8048",
  expiredAt: 1667893310,
  action: "executeContract",
  transactions: [
    { status: "executed" }
  ]
}

### canceled
{
  requestId: "4ee3ed57-b785-4196-9018-7b682c7a8048",
  expiredAt: 1667893310,
  action: "executeContract",
  transactions: [
    { 
      status: "canceled",
      txHash: null,
      errorMessage: null
    }
  ]
}

### failed
{
  requestId: "4ee3ed57-b785-4196-9018-7b682c7a8048",
  expiredAt: 1667893310,
  action: "executeContract",
  transactions: [
    { 
      status: "failed",
      txHash: "0x5a4e16cfe0c21c3cd20456340b8614f585d951d9b931b515b11575d08142a6e2",
      errorMessage: "execution reverted : invalid id"
    }
  ]
}

### succeed
{
  requestId: "4ee3ed57-b785-4196-9018-7b682c7a8048",
  expiredAt: 1667893310,
  action: "executeContract",
  transactions: [
    { 
      status: "succeed",
      txHash: "0x5a4e16cfe0c21c3cd20456340b8614f585d951d9b931b515b11575d08142a6e2",
      errorMessage: null
    }
  ]
}
*/
```

## Deprecated

### 1. Request.executeContract

{% hint style="warning" %}
FAVORLET js 1.2.0부터는 Request.executeContractWithEncoded를 사용해주세요.
{% endhint %}

**Parameters**

<table><thead><tr><th width="183">이름</th><th width="178.33333333333331">타입</th><th>설명</th></tr></thead><tbody><tr><td>chainId</td><td>number</td><td>해당 네트워크의 체인 아이디</td></tr><tr><td>appName</td><td>string</td><td>컨트랙트 실행 시 FAVORLET 앱에 노출될 이름</td></tr><tr><td>transactions</td><td>object array</td><td><p>컨트랙트를 실행하기 위해 필요한 정보</p><p>(자세한 내용은 아래를 참고)</p></td></tr></tbody></table>

**transactions 객체는 다음을 포함합니다.**

<table><thead><tr><th width="183">이름</th><th width="178.33333333333331">타입</th><th>설명</th></tr></thead><tbody><tr><td>from</td><td>string</td><td>트랜잭션을 호출할 지갑 주소</td></tr><tr><td>to</td><td>string</td><td>호출되는 컨트랙트 주소</td></tr><tr><td>value</td><td>string</td><td>(optional) 호출하는 abi 함수가 payable 인 경우 플랫폼 코인 전송</td></tr><tr><td>abi</td><td>string</td><td>트랜잭션을 발생시키기 위한 abi</td></tr><tr><td>params</td><td>string</td><td>트랜잭션을 발생시키기 위한 abi 함수의 파라미터</td></tr><tr><td>functionName</td><td>string</td><td>abi에서 호출 하려는 함수 이름</td></tr><tr><td>gasLimit</td><td>string</td><td>사용할 gasLimit (optional) 없을 시, <br>앱 내 estimate 함수 실행</td></tr></tbody></table>

**Response**

<table><thead><tr><th width="183">이름</th><th width="178.33333333333331">타입</th><th>설명</th></tr></thead><tbody><tr><td>requestId</td><td>string</td><td>작업 또는 트랜잭션 요청을 추적하기 위한 id</td></tr><tr><td>appName</td><td>number</td><td>작업 또는 트랜잭션 요청이 만료 되는 시간(단위: 초)</td></tr></tbody></table>

**Example**

<details>

<summary>TRANSFER_ABI.json</summary>

```json
// TRANSFER_ABI.json (테스트 파일)
[
  {
    "inputs": [
      {
        "name": "_to",
        "type": "address"
      },
      {
        "name": "_value",
        "type": "uint256"
      }
    ],
    "outputs": [
      {
        "name": "",
        "type": "bool"
      }
    ],
    "name": "transfer",
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "function"
  }
]
```

</details>

```javascript
import { Request } from "favorlet.js";

const response = await Request.executeContract({
  chainId: 8217, // 해당 체인 id
  appName: "BlockChainApp", // FAVORLET 앱에 노출
  transactions: [
    {
      from: "0x{address in hex}", // 트랜잭션을 호출할 지갑 주소
      to: "0x{address in hex}", // 호출되는 컨트랙트 주소
      value: "1000000000000000000", // 호출하는 abi 함수가 payable 인 경우 플랫폼 코인 전송
      abi: JSON.stringify(TRANSFER_ABI), // 트랜잭션을 발생시키기 위한 abi
      params: `["0x{address in hex}", 1]`, // 트랜잭션을 발생시키기 위한 abi 함수의 파라미터
      functionName: "transfer", // abi에서 호출 하려는 함수 이름
    },
  ],
});

console.log(response);
/*
{
  requestId: "4ee3ed57-b785-4196-9018-7b682c7a8048",
  expiredAt: 1667893310,
}
*/
```
