Skip to content

Polkamarkets/polkamarkets-js

Repository files navigation

alt tag

Introductions

polkamarkets-js is the Polkamarkets' Javascript SDK to integrate Prediction Markets into any dapp.

Installation

Using npm:

npm install "https://github.com/Polkamarkets/polkamarkets-js" --save

Using yarn:

yarn add https://github.com/Polkamarkets/polkamarkets-js

Usage

Initializing App

polkamarkets-js library initialization is performed in Application.js.

You'll need to provide a web3 RPC provider (e.g., Infura for Ethereum dapps)

import * as polkamarketsjs from 'polkamarkets-js';

// Moonriver RPC
const web3Provider = 'https://rpc.moonriver.moonbeam.network';

const polkamarkets = new polkamarketsjs.Application({ web3Provider });

// Starting application
polkamarkets.start();

// Connecting wallet
await polkamarkets.login();

Prediction Markets

Once the library is initialized, it's ready to interact with prediction markets smart contracts.

const contractAddress = '0xDcBe79f74c98368141798eA0b7b979B9bA54b026';
const contract = polkamarkets.getPredictionMarketContract({ contractAddress });

Once initialized, you'll be able to interact with the smart contract.

Here's a few call examples

const marketId = 1;
const outcomeId = 2;
const ethAmount = 0.1;

// Fetching all Market Ids
await contract.getMarkets();

// Fetching Market Details
await contract.getMarketData({ marketId });

// Buying Outcome Shares
const mintOutcomeSharesToBuy = await contract.calcBuyAmount({ marketId, outcomeId, ethAmount })
await contract.buy({ marketId, outcomeId, ethAmount, minOutcomeSharesToBuy });

// Selling Outcome Shares
const maxOutcomeSharesToSell = await contract.calcSellAmount({ marketId, outcomeId, ethAmount })
await contract.buy({ marketId, outcomeId, ethAmount, maxOutcomeSharesToSell });

// Claiming Winnings
await contract.claimWinnings({ marketId });

// Fetching portfolio data
await contract.getMyPortfolio();

Contribution

Contributions are welcomed but we ask to red existing code guidelines, specially the code format. Please review Contributor guidelines

License

MIT

Notes

The usage of ETH in all methods or params means using the native currency of that blockchain, example BSC in Binance Chain would still be nominated as ETH