0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Transactions

Last updated at Posted at 2024-12-07

Previous << Scripts
Next >> Signing and Verifying Arbitrary Data

トランザクションにより、FlowブロックチェーンにCadenceコードを送信して、その状態を恒久的に変更することができます。

トランザクションはスクリプトの一種ですが、より多くの要件があるため、ここの前にスクリプトのドキュメントをお読みいただいたものとします。

queryはチェーンにスクリプトを送信するために使用されますが、mutateはトランザクションを構築して送信するために使用されます。スクリプトと同様に、fcl.mutateはCadenceコードを渡すことができるJavaScript Tagged Template Literalです。

スクリプトとは異なり、提案者、承認者、支払者などの追加情報を必要とします。そのため、少し混乱するかもしれません。

Sending your first Transaction

以下のコード・スニペットには、多くの内容が含まれています。これは、トランザクションをFlowブロックチェーンに送信します。このトランザクションでは、現在のユーザーがproposerおよびpayerとして承認しています。Flowに特有の点として、トランザクションの支払者は、必ずしもトランザクションの実行者である必要はありません。ProposersとPayersは、トランザクションに必ず必要な特別な種類の承認処理です。proposerは、Ethereum トランザクションにおける nonceと類似した動作をし、繰り返し攻撃を防止するのに役立ちます。payerは、トランザクション料の支払者となります。これらの設定がない場合、FCL はすべてのロールに対し現在のユーザーを使うデフォルト設定をします。

fcl.mutatetransactionId を返します。私たちはこのレスポンスを直接 fcl.tx に渡し、そしてonceSealedメソッドを使用することで、トランザクションがsealedになったときにそのpromiseがresolveされることを確認できます。

import * as fcl from "@onflow/fcl"

const transactionId = await fcl.mutate({
  cadence: `
    transaction {
      execute {
        log("Hello from execute")
      }
    }
  `,
  proposer: fcl.currentUser,
  payer: fcl.currentUser,
  limit: 50
})

const transaction = await fcl.tx(transactionId).onceSealed()
console.log(transaction)
/* The transactions status and events after being sealed */

Authorizing a transaction

以下のコード・スニペットは、1つの非常に重要な違いを除いて、上記のものと同一です。今回のCadenceコードにはprepareステートメントがあり、トランザクションを構築する際にfcl.currentUserを使用しています。

prepareステートメントの引数は、authorizations配列の中のauthorizationsの順番に直接対応しています。4つのauthorizationsは、prepareに渡される引数として4つの&Accountを持つことを意味します。しかし、この場合、引数は1つだけで、それはcurrentUserです。

これらのauthorizationsは重要です。なぜなら、アカウントのauthorizationを持っている場合のみ、アカウントのストレージにアクセス/変更を行うことができるからです。

import * as fcl from "@onflow/fcl"

const transactionId = await fcl.mutate({
  cadence: `
    transaction {
      prepare(acct: &Account) {
        log("Hello from prepare")
      }
      execute {
        log("Hello from execute")
      }
    }
  `,
  proposer: fcl.currentUser,
  payer: fcl.currentUser,
  authorizations: [fcl.currentUser],
  limit: 50
})

const transaction = await fcl.tx(transactionId).onceSealed()
console.log(transaction)
/* The transactions status and events after being sealed */

To learn more about mutate, check out the API documentation.

Last updated on Nov 21, 2024 by Jordan Ribbink

翻訳元


Previous << Scripts

Flow BlockchainのCadence version1.0ドキュメント (Transactions)

Next >> Signing and Verifying Arbitrary Data

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?