UNPKG

axie-ronin-ethers-js-tools

Version:

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

81 lines (67 loc) 2.77 kB
import { ethers } from 'ethers'; import { getAxieIdsFromAccount, approveMarketplaceContract, createMarketplaceOrder, } from "axie-ronin-ethers-js-tools"; import 'dotenv/config' async function sale() { if (!process.env.PRIVATE_KEY || !process.env.MARKETPLACE_ACCESS_TOKEN) { throw new Error('Please set your PRIVATE_KEY in a .env file') } // Connect to Ronin network rpc, see https://docs.skymavis.com/ronin/rpc/overview const connection = { url: 'https://api.roninchain.com/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) // Get address from wallet const addressFrom = (await wallet.getAddress()).toLowerCase() console.log(`Wallet address: ${addressFrom}`) // Get RON balance const balance = await provider.getBalance(addressFrom) const balanceInRON = ethers.utils.formatEther(balance) console.log(`Balance: ${balanceInRON} RON`) if (balanceInRON < 0.001) { throw new Error('Not enough RON to pay for the transaction') } // Get axieId from command line args const args = process.argv.slice(2) const axieId = parseInt(args[0], 10) if (isNaN(axieId) || axieId < 1) { throw new Error('Please provide a valid axieID as the first argument') } // Check if the axieId is owned by the wallet addressFrom const axieIds = await getAxieIdsFromAccount(addressFrom, provider) if (axieIds.length === 0 || !axieIds.includes(axieId)) { throw new Error(`Axie ${axieId} is not owned by ${addressFrom}`) } // Check if axie contract is approved in the marketplace contract, if not, approve the marketplace contract to transfer axies const isApproved = await approveMarketplaceContract(addressFrom, wallet) if (!isApproved) { console.log(`Axie Contract is not approved in the Marketplace Contract for ${addressFrom}`) throw new Error('Marketplace contract is not approved to transfer axies') } // get current block timestamp const currentBlock = await provider.getBlock('latest') const startedAt = currentBlock.timestamp const endedAt = 0 // 0 means no end date, max is 6 months right now const basePrice = ethers.utils.parseUnits('0.1', 'ether').toString() const endedPrice = '0' // ~ 6 months default and max listing duration const expiredAt = startedAt + 15634800 const orderData = { address: addressFrom, axieId: axieId.toString(), // the axieId to sell, must be a string here basePrice, endedPrice, startedAt, endedAt, expiredAt, } // Wait for markeplace api result const result = await createMarketplaceOrder(orderData, accessToken, wallet) console.log(result) } sale();