Git Product home page Git Product logo

vrf-consumer's Introduction

VRF Consumer

Consumer smart contract that utilizes VRF functionality from Orakl Network.

Internally, the scripts access following smart contracts:

Baobab

Cypress

If you want to access different deployments of Prepayment or VRFCoordinator, you can change it inside hardhat.config.ts in namedAccounts property.

Prerequisites

Create a copy of .env.example file and fill in values for PROVIDER, and MNEMONIC or PRIV_KEY (the difference is explained below) environment variables. These variables will be used for connection to JSON-RPC endpoint, deployment and use of your VRFConsumer smart contract.

cp .env.example .env

PROVIDER can be set to any JSON-RPC endpoint. The list of free available JSON-RPC endpoint can be found in official Klaytn documentation.

This repository supports connection to wallet either through mnemonic or private key.

Mnemonic

MNEMONIC can be generated using npm mnemonics package.

npx mnemonics

After mnemonic is generated, you need to convert it to public address and fund it with KLAY. If you do not have any KLAY in your account, you cannot deploy smart contracts or make any transactions.

You can convert your newly generated mnemonic with following hardhat task. Please replace the [MENONIC] with your mnemonic.

npx hardhat address --mnemonic [MNEMONIC]

The script will print out a public address corresponding to your mnemonic. Then, you can use this address to receive free KLAY using Baobab's faucet.

Private key

If you already have a wallet, you can reuse its private key, and connect to JSON-RPC endpoint with it. In case you use Metamask, read how to export an account's private key.. After you extract private key, store it in PRIV_KEY variable inside of .env file.

Installation

yarn install

Compilation

yarn compile

Deploy

npx hardhat deploy --network baobab

Get Estimated Service Fee

Prior to creating an account, you have the option to retrieve the estimated service fee for a single VRF request. To do this, you can run the provided script using the following command:

npx hardhat run scripts/get-estimated-service-fee.ts --network baobab

Executing the script will provide you with the cost associated with a single VRF request. This information will help you determine the amount you need to fund your account accordingly to meet your specific requirements.

Create and fund account

There are two types of payments supported by Orakle Network VRF: Prepayment and Direct Payment.

Prepayment requires user to create account and fund it with KLAY before being able to use it. The script below will create a new account and deposit 2.5 KLAY from address corresponding to your mnemonic or private key from .env file.

If you prefer to use Orakl Network VRF without having a long-lasting account, you can use Direct Payment method. In such case, you can skip the following command and go directly to Request VRF with Direct Payment.

npx hardhat run scripts/create-and-fund-account.ts --network baobab

After successfully executing the command above, set the value of environment variable ACC_ID inside of .env file to account ID that was generated using the script above. If you do not do it, the request for VRF using Prepayment won't be working properly. After setting the ACC_ID in .env file, you can move to the next step Request VRF with Prepayment.

If you'd like to use your existing prepayment account, set your account id in the .env file and add your deployed consumer to your account by executing the hardhat addConsumer task (described in more detail below). Note that providing account-id parameter is optional if your account id is already in the .env file and consumer parameter is also optional if you've deployed your consumer using hardhat under this repository.

Request & Read VRF

Before running following scripts, one must deploy VRFConsumer smart contract. To deploy VRFConsumer, run npx hardhat deploy --network baobab.

Request VRF with Prepayment

npx hardhat run scripts/request-vrf.ts --network baobab

Request VRF with Direct Payment

npx hardhat run scripts/request-vrf-direct.ts --network baobab

Read VRF response

npx hardhat run scripts/read-vrf.ts --network baobab

Hardhat tasks

The following tasks allow for more finer control over experimentation with the example code in this repository.

Create a new account

npx hardhat createAccount --network baobab

Deposit to an account

After you have created an account, you can deposit $KLAY to it anytime using the command below.

npx hardhat deposit \
    --account-id $ACCOUNT \
    --amount $AMOUNT \
    --network $NETWORK

Withdraw from an account

To withdraw the remaining balance from an account, you can use the command below.

npx hardhat withdraw \
    --account-id $ACCOUNT \
    --amount $AMOUNT \
    --network $NETWORK

Add a consumer

Add consumer contract to account. Then, the consumer contract will be able to request for VRF service.

npx hardhat addConsumer \
    --consumer $CONSUMERADDRESS \
    --account-id $ACCOUNT \
    --network $NETWORK

Remove a consumer

Remove a consumer contract from an account. Then, the consumer contract will not be able to request for VRF service anymore.

npx hardhat removeConsumer \
    --consumer ${CONSUMERADDRESS} \
    --account-id ${ACCOUNT} \
    --network ${NETWORK}

vrf-consumer's People

Contributors

bayram98 avatar bryan-bisonai avatar intizar-t avatar jo-bisonai avatar kelvinthai avatar martinkersner avatar nick-bisonai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

vrf-consumer's Issues

(Setup) Explain how to connect consumer to existing account

  • Current Readme assumes a user doesn't have a payment account and thus only explains how to create a new account and deposit KLAY. We should also cover the case where a user has an existing account and wants to deploy a consumer and connect the consumer to the account.
  • In hardhat tasks addConsumer and removeConsumer, consumer parameter is required. But task logic is checking if it's not provided then the consumer address is fetched using ethers.getContract('contractName'). Since the contract ABI will be stored in the artifacts, we might as well make the consumer param optional to make it easier for users to run those tasks. Plus we are properly informing the user about the absence of the contract address if it's not provided as a param and cannot be retrieved from artifacts

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.