ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Klaytn] Contract Execution (feat. Fee Delegated)
    BlockChain Tech 2019. 4. 6. 22:32

     지난 번 글에서 Klaytn의 대(신)납(부) 기능 중 'FEE_DELEGATED_VALUE_TRANSFER'를 살펴보았다.

    이번 글에서는 Klaytn IDE가 아닌, Java Script에서 Klaytn Contract 실행 그리고, 컨트랙트 호출 대납을 실행해보려고 한다.

     

    기본 Base 코드는 Klaytn Tutorial 'Count App(링크)'를 사용했다.

    이전 글과 대부분 겹치기 때문에 자세한 설명은 생략! (이전 글 링크)

    [1 Step] Normal Contract Execution

    setPlus = async () => {
        const userPrivateKey = '0xb83edaf89879b3c615bb6019af65313984812aa94dce7ca5b5d8abef899c1141'
        const userAddress = '0x22Bd0365568F9266810A69a2b90f36064148042D'
        const plusMethodData = this.countContract.methods.plus()._method.signature
    
        // (1) Contract Execution Transaction
        const senderTransaction = {
          type: 'SMART_CONTRACT_EXECUTION',
          from: userAddress,
          to: DEPLOYED_ADDRESS,
          data: plusMethodData,
          gas: '300000'
        }
       
        cav.klay.accounts.wallet.add(userPrivateKey)
    
        // (2) feePayer(EnterPrise Account) Send Transaction
        cav.klay.sendTransaction(senderTransaction)
        .on('transactionHash', (txHash) => {
          // 생략
        })
        .on('receipt', async (receipt) => {
          // 생략
        })
        .on('error', (error) => {
          // 생략
        })
      }
    

     

    (1) Contract Execution Transaction

    > SmartContract 호출시 Transaction의 Type은 'SMART_CONTRACT_EXECUTION'으로 설정 한다.

    > to는 당연 호출 할 Contract의 Address

    > data에는 호출하려는 Method의 ABI를 입력

     

    'Count App(링크)' 예제에서는 보다 간편하게 Contract Method를 호출 하였다.

    this.countContract.methods.minus().send({
          from: walletInstance.address,
          gas: '200000',
        })
    

     

    [2 Step] Fee Delegated Contract Execution

    // Contract Execution Fee Delegated
      feeDelegatedContractExecution = async () => {
        const feePayerPrivateKey = '0xb83edaf89879b3c615bb6019af65313984812aa94dce7ca5b5d8abef899c1141';
        const feePayerAddress = '0x22Bd0365568F9266810A69a2b90f36064148042D'
        const userPrivateKey = '0x89ebc9dc49cd8279eee0d5878daa045b88ec119a9f6857d9efafb95b522c4461'
        const userAddress = '0xa16555477CbCf44793754F5B40Ec41C04fe931A2'
        const plusMethodData = this.countContract.methods.plus()._method.signature
    
        // (1) Create Transaction (User Transaction)
        const senderTransaction = {
          type: 'FEE_DELEGATED_SMART_CONTRACT_EXECUTION',
          from: userAddress,
          to: DEPLOYED_ADDRESS,
          data: plusMethodData,
          gas: '300000',
        }
    
        // (2) Create rawTransaction
        const { rawTransaction: senderRawTransaction } = await cav.klay.accounts.signTransaction(senderTransaction, userPrivateKey)
    
        // (3) Create Fee Delegated Transaction
        const feePayerTransaction = {
          type: 'FEE_DELEGATED_SMART_CONTRACT_EXECUTION',
          senderRawTransaction: senderRawTransaction,
          feePayer: feePayerAddress,
        }
    
        console.log(feePayerTransaction);
    
        // (4) feePayer(EnterPrise Account) Send Transaction
        cav.klay.accounts.wallet.add(feePayerPrivateKey)
    
        cav.klay.sendTransaction(feePayerTransaction)
        .on('transactionHash', (txHash) => {
          // 생략
        })
        .on('receipt', async (receipt) => {
          // 생략
        })
        .on('error', (error) => {
          // 생략
        })
      }
    

    (1) Create Transaction (User Transaction)

    > Transaction의 Type은 'FEE_DELEGATED_SMART_CONTRACT_EXECUTION'

     

    Fee Delegate 사용전 balance 1 KLAY
    Fee Delegate 사용 후 balance 1 KLAY
    Transaction으로 조회 하면, 이전 FEE_DELEGATED_VALUE_TRANSFER와 동일하게 Type에 표시

     

    예제를 통해 정리된 것

    (1) 예제가 잘 실행 된다!

    > 이전 Transfer 예제 이후 진행하여 크게 새로운 점이 없었다.

    > 이전 글에서 정리한 것과 같이 balance가 0이라면 Fee Delegate가 불가능했다.

     


    (이전글) [Klaytn] 대(신)납(부) 기능 (feat. Fee Delegated)

    'BlockChain Tech' 카테고리의 다른 글

    Transaction Data 살펴보기 (feat. ABI)  (0) 2019.04.10
    [Klaytn] 대(신)납(부) 기능 (feat. Fee Delegated)  (2) 2019.04.06
    ERC-721 살펴보기  (0) 2019.03.31

    댓글

Developer RyuK