axie-ronin-ethers-js-tools
TypeScript icon, indicating that this package has built-in type declarations

1.3.0 • Public • Published

Axie Infinity ether js tools

This repository provides a set of functions that make it easier for developers to interact with their Axies on the Ronin network and the maketplace.

How to use

Install the dependencies

npm install axie-ronin-ethers-js-tools ethers@5.7.0 dotenv

Generate a wallet and provider, which will be used to interact with the Ronin network using ethers.js

import { ethers } from 'ethers';
import * as dotenv from 'dotenv'
dotenv.config()

// Connection to the Ronin network using the RPC endpoint
const connection = {
  url: 'https://api-gateway.skymavis.com/rpc',
  headers: {
      'x-api-key': 'xxxxx' // get from https://developers.skymavis.com/console/applications/
  }
}

// See https://docs.skymavis.com/api/rpc
const provider = new ethers.providers.JsonRpcProvider(connection);

// Import the wallet private key from the environment
const wallet = new ethers.Wallet(process.env.PRIVATE_KEY, provider) 

Generate a marketplace access token, which is required to interact with the marketplace

import { generateAccessTokenMessage, exchangeToken } from 'axie-ronin-ethers-js-tools';

const getMarketplaceAccessToken = async (wallet: ethers.Wallet): Promise<string> => {
  // Get address from signer
  const address = await wallet.getAddress()
  // Generate message to sign
  const domain = `example.com`
  const uri = "https:/example.com"
  const statement = `any statement`
  const message = await generateAccessTokenMessage(address, domain, uri, statement)
  // Sign the message
  const signature = await wallet.signMessage(message)
  // Exchange the signature for an access token
  const { accessToken } = await exchangeToken(signature, message)
  return accessToken
}

List an axie for sale on the marketplace

Full example here examples/marketplace-js

const createAxieSale = async () => {
  // 1 ETH in wei
  const basePrice = ethers.utils.parseUnits('1', 'ether').toString()
  // This is just for auctions
  const endedPrice = '0'
  // ID of the axie to list for sale on the marketplace
  const axieId = '9604431'
  // Generate marketplace access token (see above)
  const accessToken = await getMarketplaceAccessToken(wallet)
  // Get address from wallet
  const addressFrom = await wallet.getAddress()
  // Approve the axie contract to transfer axies from address to the marketplace contract
  const isApproved = await approveMarketplaceContract(addressFrom, wallet)
  // Get current block timestamp
  const currentBlock = await provider.getBlock('latest')
  const startedAt = currentBlock.timestamp
  const endedAt = 0 // 0 means no end time, use startedAt + duration for auctions
  // ~ 6 months default and max listing duration
  const expiredAt = startedAt + 15634800
  // Create the order
  const orderData = {
    address,
    axieId,
    basePrice,
    endedPrice,
    startedAt,
    endedAt,
    expiredAt,
  }
  // Wait for markeplace api result
  const skyMavisApiKey = 'xxxxx' // get from https://developers.skymavis.com/console/applications/
  const result = await createMarketplaceOrder(orderData, accessToken, wallet, skyMavisApiKey)
}

Unlist an axie from the marketplace

Full example here examples/marketplace-js

import { cancelMarketplaceOrder } from "axie-ronin-ethers-js-tools";

const cancelAxieSale = async (axieId: number) => {
    // Wait for the transaction to be mined
    const skyMavisApiKey = 'xxxxx' // get from https://developers.skymavis.com/console/applications/
    const receipt = await cancelMarketplaceOrder(axieId, wallet, skyMavisApiKey)
}

Buy an axie

Full example here examples/buy

import { buyMarketplaceOrder } from "axie-ronin-ethers-js-tools";

const buyAxieFromMarketplace = async (axieId: number) => {
  // Wait for the transaction to be mined
  const skyMavisApiKey = 'xxxxx' // get from https://developers.skymavis.com/console/applications/
  const receipt = await buyMarketplaceOrder(axieId, wallet, provider, skyMavisApiKey)
  console.log(receipt.transactionHash)
}

Batch transfer all axies in the account

Full example here examples/batch-transfer-js

This will transfer all axies from the wallet to the specified address, it uses the ERC721 Batch Transfer contract: https://app.roninchain.com/address/0x2368dfed532842db89b470fde9fd584d48d4f644

import { getAxieIdsFromAccount, batchTransferAxies } from "axie-ronin-ethers-js-tools";

const batchTransferAllAxies = async (addressTo:string) => {
  // Get address from wallet
  const address: string = await wallet.getAddress()
  // get all axies ids from the account
  const axieIds: number[] = await getAxieIdsFromAccount(address, provider)
  // wait for tx to be mined and get receipt
  const receipt = await batchTransferAxies(wallet, addressTo, axies)
}

How to dev/test locally

Clone the repository, copy .env.example to .env and fill in your account private key (you can get this from the Ronin wallet). Please do not share your private key with anyone or commit it to a public repository.

npm install
npx hardhat account
npx hardhat generate-access-token
npx hardhat list --axie $AXIE_ID --base-price 0.1
npx hardhat list --axie $AXIE_ID --base-price 0.1 --ended-price 0.2 --duration 1
npx hardhat list-all --base-price 0.1
npx hardhat list-all --base-price 0.1 --ended-price 0.2 --duration 1
npx hardhat unlist --axie $AXIE_ID
npx hardhat unlist-all
npx hardhat buy --axie $AXIE_ID
npx hardhat transfer-axie --axie $AXIE_ID --address $ADDRESS
npx hardhat transfer-all-axies --address $ADDRESS
npx hardhat transfer-all-axies --address $ADDRESS --axies "$AXIE_ID,$AXIE_ID"

Contributing

Feel free to open an issue or a pull request if you have any questions or suggestions.

Package Sidebar

Install

npm i axie-ronin-ethers-js-tools

Weekly Downloads

100

Version

1.3.0

License

ISC

Unpacked Size

714 kB

Total Files

93

Last publish

Collaborators

  • alexx855