lesson-1_ftコントラクトを接続しよう
🚆 ftコントラクト
をフロントに接続しよう
これまでにあなたは以下のことを達成しました ✨
ftコントラクト
のデプロイ- バイクを管理するコントラクト(以降
bikeコントラクト
と呼びます)のデプロイ bikeコントラクト
とフロントエンドの接続
このセクションであなたは以下のことを行います 🛫
ftコントラクト
をフロントエンドに接続してftコントラクト
のメソッドも利用可能にする。ftコントラクト
とbikeコントラクト
を接続する。 これを行うことでバイクの使用や点検にトークンのやり取りを組み込むことができます。
それではftコントラクト
を接続するために
再びnear_bike_share_dapp
の中にあるfrontend
ディレクトリの中身を変更していきます。
以降のセクションでは、今回作成したオリジナルftのことをftと便宜上呼ぶことにします。
初めにfrontend/assets/js/near/config.js
を次のように変更しましょう!
// config.js
const CONTRACT_NAME = process.env.CONTRACT_NAME || "new-awesome-project";
const FT_CONTRACT_NAME = "sub.ft_account.testnet";
function getConfig(env) {
switch (env) {
case "production":
case "mainnet":
return {
networkId: "mainnet",
nodeUrl: "https://rpc.mainnet.near.org",
contractName: CONTRACT_NAME,
ftContractName: FT_CONTRACT_NAME,
walletUrl: "https://wallet.near.org",
helperUrl: "https://helper.mainnet.near.org",
explorerUrl: "https://explorer.mainnet.near.org",
};
case "development":
case "testnet":
return {
networkId: "testnet",
nodeUrl: "https://rpc.testnet.near.org",
contractName: CONTRACT_NAME,
ftContractName: FT_CONTRACT_NAME,
walletUrl: "https://wallet.testnet.near.org",
helperUrl: "https://helper.testnet.near.org",
explorerUrl: "https://explorer.testnet.near.org",
};
case "betanet":
return {
networkId: "betanet",
nodeUrl: "https://rpc.betanet.near.org",
contractName: CONTRACT_NAME,
ftContractName: FT_CONTRACT_NAME,
walletUrl: "https://wallet.betanet.near.org",
helperUrl: "https://helper.betanet.near.org",
explorerUrl: "https://explorer.betanet.near.org",
};
case "local":
return {
networkId: "local",
nodeUrl: "http://localhost:3030",
keyPath: `${process.env.HOME}/.near/validator_key.json`,
walletUrl: "http://localhost:4000/wallet",
contractName: CONTRACT_NAME,
ftContractName: FT_CONTRACT_NAME,
};
case "test":
case "ci":
return {
networkId: "shared-test",
nodeUrl: "https://rpc.ci-testnet.near.org",
contractName: CONTRACT_NAME,
ftContractName: FT_CONTRACT_NAME,
masterAccount: "test.near",
};
case "ci-betanet":
return {
networkId: "shared-test-staging",
nodeUrl: "https://rpc.ci-betanet.near.org",
contractName: CONTRACT_NAME,
ftContractName: FT_CONTRACT_NAME,
masterAccount: "test.near",
};
default:
throw Error(
`Unconfigured environment '${env}'. Can be configured in src/config.js.`
);
}
}
module.exports = getConfig;
変更点は、FT_CONTRACT_NAME
を追加しています。
"sub.ft_account.testnet"
の部分はあなたがftコントラクト
をデプロイした先のアカウントIDを入力してください。
またgetConfig
が返却するオブジェクトにftContractName
フィールドを追加しています。
次にfrontend/assets/js/near/utils.js
を次のように変 更します。
// utils.js
import { connect, Contract, keyStores, WalletConnection } from "near-api-js";
import getConfig from "./config";
const nearConfig = getConfig(process.env.NODE_ENV || "development");
/** コントラクトを初期化し、グローバル変数(window)をセットします. */
export async function initContract() {
// ...
// ftコントラクトとの接続を追加
window.ftContract = await new Contract(
window.walletConnection.account(),
nearConfig.ftContractName,
{
viewMethods: ["ft_balance_of", "storage_balance_of"],
changeMethods: ["storage_deposit", "storage_unregister", "ft_transfer"],
}
);
}
// ...
export async function return_bike(index) {
// ...
}
// 以下ftのメソッド呼び出しを追加しています。
/**
* account_idのftの残高を取得します。
*/
export async function ft_balance_of(account_id) {
const balance = await window.ftContract.ft_balance_of({
account_id: account_id,
});
return balance;
}
/**
* account_idのストレージの使用状況を表すデータ構造を取得します。
* account_idが登録されていない場合はnullが返るので、登録されているかどうかの判断にこの関数を使用します。
*/
export async function storage_balance_of(account_id) {
const balance = await window.ftContract.storage_balance_of({
account_id: account_id,
});
return balance;
}
/** ストレージ使用量の支払い登録を行います。 */
export async function storage_deposit() {
const response = await window.ftContract.storage_deposit(
{}, // 引数の省略 = このメソッドを呼び出しているアカウントを登録
"300000000000000", // ガス量の制限(in gas units)
"1250000000000000000000" // デポジット (in yoctoNEAR, 1 yoctoNEAR = 10^-24 NEAR)
);
return response;
}
/** アカウントの登録を解除します。 */
// 今回は簡単のため強制的に解除する方法を引数指定でとっています。
export async function storage_unregister() {
const response = await window.ftContract.storage_unregister(
{ force: true }, // アカウントの情報に関わらず登録を解除する、所持しているftはバーンされる
"300000000000000",
"1"
);
return response;
}
/** ftをreceiver_idへ転送します。 */
export async function ft_transfer(receiver_id, amount) {
const response = await window.ftContract.ft_transfer(
{
receiver_id: receiver_id,
amount: amount,
},
"300000000000000",
"1" // セキュリティ上必要な 1 yoctoNEAR
);
return response;
}
//ファイル終端
window
変数にftContract
を追加し、ftコントラクト
上 のメソッドをいくつか呼び出すようにしています。
各メソッドの意味はコード内コメントにて記載しています。
詳しくはこちらとこちらを参照してください。
ここに注目しましょう。
export async function storage_deposit() {
const response = await window.ftContract.storage_deposit(
{}, // 引数の省略 = このメソッドを呼び出しているアカウントを登録
"300000000000000", // ガス量の制限(in gas units)
"1250000000000000000000" // デポジット (in yoctoNEAR, 1 yoctoNEAR = 10^-24 NEAR)
);
return response;
}
メソッド呼び出し後、ガス量の制限とデポジットの付与を上記のような形でつけることができます。