123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- import { BaseClient } from './BaseClient'
- import { bcs, Coin, Coins, Fee, MsgExecute } from '@initia/initia.js'
- import { getAxiosClient } from './utils'
- export abstract class JennieModule extends BaseClient {
- protected constructor(mnemonic: string, useProxy: boolean = false) {
- super(mnemonic, useProxy)
- }
- async getXpAmount() {
- const viewResult = await this.lcd.move.viewFunction(
- '0x9065fda28f52bb14ade545411f02e8e07a9cb4ba',
- 'initia_xp',
- 'get_xp_amount',
- undefined,
- [bcs.address().serialize(this.key.accAddress).toBase64()],
- )
- return viewResult
- }
- async drawFood() {
- const res = await this.claimableXp()
- if (res != `done`) {
- return `needFood`
- }
- const xp = await this.getXpAmount()
- if (Number(xp) < 700 && Number(xp) > 200) {
- const msg = new MsgExecute(
- this.key.accAddress,
- '0x9065fda28f52bb14ade545411f02e8e07a9cb4ba',
- 'jennie',
- 'draw_food',
- [],
- ['AQ=='],
- )
- return await this.broadcast(msg)
- } else if (Number(xp) >= 700 && Number(xp) < 1200) {
- const msg = new MsgExecute(
- this.key.accAddress,
- '0x9065fda28f52bb14ade545411f02e8e07a9cb4ba',
- 'jennie',
- 'draw_food',
- [],
- ['Ag=='],
- )
- return await this.broadcast(msg)
- } else if (Number(xp) >= 1200) {
- const msg = new MsgExecute(
- this.key.accAddress,
- '0x9065fda28f52bb14ade545411f02e8e07a9cb4ba',
- 'jennie',
- 'draw_food',
- [],
- ['Aw=='],
- )
- return await this.broadcast(msg)
- } else {
- return `done`
- }
- }
- async feedJennie() {
- const viewResult: { hp: string; update_at: string } =
- await this.lcd.move.viewFunction(
- '0x9065fda28f52bb14ade545411f02e8e07a9cb4ba',
- 'jennie',
- 'get_jennie_state',
- undefined,
- [bcs.address().serialize(this.key.accAddress).toBase64()],
- )
- // const stageInfo: {
- // current_stage: number
- // stage_interval: number
- // start_timestamp: number
- // } = await this.lcd.move.viewFunction(
- // '0x9065fda28f52bb14ade545411f02e8e07a9cb4ba',
- // 'jennie',
- // 'stage_info',
- // undefined,
- // [],
- // )
- const kt = Number(viewResult.update_at)
- const now = 1718765974
- // const currentStage = stageInfo.current_stage
- // const stageInternal = stageInfo.stage_interval
- // const stTimestamp = stageInfo.start_timestamp
- // const yt =
- // stTimestamp +
- // Math.floor(((currentStage ?? 1) - 1) / 7) * stageInternal * 7
- // const St =
- // stTimestamp +
- // (Math.floor(((currentStage ?? 1) - 1) / 7) + 1) * stageInternal * 7
- // const canFeed = kt < Math.floor(yt / 1000000)
- if (!(kt < now)) {
- return `done`
- }
- const rep = await getAxiosClient(true).get(
- `${this.rpcUrlUsing}initia/move/v1/accounts/${this.key.accAddress}/resources/by_struct_tag?struct_tag=0x9065fda28f52bb14ade545411f02e8e07a9cb4ba::jennie::FoodStore`,
- )
- const store = JSON.parse(rep.data.resource.move_resource)
- const data = store.data
- if (Number(data.tier2) > 0) {
- const msg = new MsgExecute(
- this.key.accAddress,
- '0x9065fda28f52bb14ade545411f02e8e07a9cb4ba',
- 'jennie',
- 'feed_jennie',
- [],
- [bcs.u8().serialize(2).toBase64()],
- )
- return await this.broadcast(msg)
- } else if (Number(data.tier3) > 0) {
- const msg = new MsgExecute(
- this.key.accAddress,
- '0x9065fda28f52bb14ade545411f02e8e07a9cb4ba',
- 'jennie',
- 'feed_jennie',
- [],
- [bcs.u8().serialize(3).toBase64()],
- )
- return await this.broadcast(msg)
- } else if (Number(data.tier1) > 0) {
- const msg = new MsgExecute(
- this.key.accAddress,
- '0x9065fda28f52bb14ade545411f02e8e07a9cb4ba',
- 'jennie',
- 'feed_jennie',
- [],
- [bcs.u8().serialize(1).toBase64()],
- )
- return await this.broadcast(msg)
- } else {
- await this.drawFood()
- return `needFood`
- }
- }
- async mintJennie() {
- // Fetch mint progress details for 'jennie'
- const mintProgress: MintProgress = await this.lcd.move.viewFunction(
- '0x9065fda28f52bb14ade545411f02e8e07a9cb4ba',
- 'jennie',
- 'get_jennie_mint_progress',
- [],
- [bcs.address().serialize(this.key.accAddress).toBase64()],
- )
- console.log(mintProgress)
- // Check if 'jennie' is already minted
- if (mintProgress.is_minted) {
- return `done`
- }
- // Initialize a flag to determine if all parts are minted
- let allPartsMinted = true
- // Iterate over each part progress
- for (const partProgress of mintProgress.jennie_part_progresses) {
- // If any part is not minted, set the flag to false
- if (!partProgress.is_minted) {
- allPartsMinted = false
- break // No need to check further parts
- }
- }
- // If not all parts are minted, exit the function
- if (!allPartsMinted) {
- return `undone`
- }
- // Prepare the minting message
- const msg = new MsgExecute(
- this.key.accAddress,
- '0x9065fda28f52bb14ade545411f02e8e07a9cb4ba',
- 'jennie',
- 'mint_jennie',
- [],
- [],
- )
- // Broadcast the minting message
- return await this.broadcast(msg)
- }
- async mintPartApprovedAll() {
- const axios = getAxiosClient(true)
- const res = await axios.get(
- `https://xp-api.initiation-1.initia.xyz/xp/mint_parts/${this.key.accAddress}`,
- )
- const data = res.data
- let undone = [1, 2, 3, 4, 5, 6]
- for (const item of data) {
- const task = Number(item.task_id) + 1
- undone = undone.filter(value => value !== task)
- }
- console.log(undone)
- return undone
- }
- async mintPart() {
- // 获取 'jennie' 的铸造进度
- const axios = getAxiosClient(true)
- const res = await axios.get(
- `https://xp-api.initiation-1.initia.xyz/xp/mint_parts/${this.key.accAddress}`,
- )
- const data = res.data
- if (data.length !== 6) {
- return `undone`
- }
- const msgs = []
- const mintProgress: MintProgress = await this.lcd.move.viewFunction(
- '0x9065fda28f52bb14ade545411f02e8e07a9cb4ba',
- 'jennie',
- 'get_jennie_mint_progress',
- [],
- [bcs.address().serialize(this.key.accAddress).toBase64()],
- )
- const progresses = mintProgress.jennie_part_progresses
- console.log(progresses)
- for (const item of progresses) {
- if (item.is_minted) {
- continue
- }
- for (let i = 0; i < data.length; i++) {
- if (Number(data[i].task_id) === Number(item.task)) {
- const sig = data[i]
- const msg = new MsgExecute(
- this.key.accAddress,
- '0x9065fda28f52bb14ade545411f02e8e07a9cb4ba',
- 'jennie',
- 'mint_part_v2',
- [],
- [
- bcs.u8().serialize(sig.task_id).toBase64(),
- bcs
- .vector(bcs.u8())
- .serialize(Buffer.from(sig.signature, 'base64'))
- .toBase64(),
- ],
- )
- msgs.push(msg)
- }
- }
- }
- // 初始化消息数组
- if (msgs.length === 0) {
- return `done`
- }
- return await this.broadcast(msgs)
- }
- async claimableXp() {
- //https://xp-api.initiation-1.initia.xyz/xp/claimable/init1w6ftxlsv7y5putq955ymxh2kd0wru7nqshetka
- const res = getAxiosClient(true)
- const data = await res.get(
- `https://xp-api.initiation-1.initia.xyz/xp/claimable/${this.key.accAddress}`,
- )
- if (data.data.claimable_list.length > 0) {
- const msgs = []
- for (let i = 0; i < data.data.claimable_list.length; i++) {
- const proofs = data.data.claimable_list[i].merkle_proofs
- const proofsu8vec = proofs.map(p =>
- Uint8Array.from(Buffer.from(p, 'base64')),
- )
- const sd = bcs
- .vector(bcs.vector(bcs.u8()))
- .serialize(proofsu8vec)
- .toBase64()
- const msg = new MsgExecute(
- this.key.accAddress,
- '0x9065fda28f52bb14ade545411f02e8e07a9cb4ba',
- 'initia_xp',
- 'claim_point',
- [],
- [
- bcs.u64().serialize(data.data.claimable_list[i].stage).toBase64(),
- sd,
- bcs
- .u64()
- .serialize(data.data.claimable_list[i].task_point)
- .toBase64(),
- bcs
- .u64()
- .serialize(data.data.claimable_list[i].referral_point)
- .toBase64(),
- ],
- )
- msgs.push(msg)
- }
- return await this.broadcast(msgs)
- // console.log(broadcast.raw_log)
- // console.log(this.key.accAddress)
- } else {
- return `done`
- }
- }
- }
|