import { APIRequester, bcs, Coin, Coins, Fee, Height, LCDClient, MnemonicKey, Msg, MsgExecute, MsgInitiateTokenDeposit, MsgTransfer, Wallet, } from '@initia/initia.js' import { generateRandomString, getAxiosClient } from './utils' import { HttpsProxyAgent } from 'https-proxy-agent' const rpcUrl = [ 'https://lcd.initiation-1.initia.xyz/', 'https://api-initia-testnet.whispernode.com/', 'https://initia-testnet-lcd.orbitalcommand.io/', 'https://testnet-initia-api.lavenderfive.com/', 'https://api.initiation.test.pfc.zone/', ] export abstract class BaseClient { key: MnemonicKey lcd: LCDClient tucana: LCDClient initAi: LCDClient civi: LCDClient blackWings: LCDClient noon: LCDClient miniMove: LCDClient wallet: Wallet tucanaWallet: Wallet rpcUrlUsing: string protected constructor(mnemonic: string, useProxy: boolean = true) { this.key = new MnemonicKey({ mnemonic: mnemonic, }) const rpc = rpcUrl[Math.floor(Math.random() * rpcUrl.length)] this.rpcUrlUsing = rpc const pass = `test1234_session-${generateRandomString(8)}_lifetime-20m` const proxyAgent = useProxy ? new HttpsProxyAgent(`http://tuxla:${pass}@geo.iproyal.com:12321`) : undefined const username = `u3e4c2852572e05c5-zone-custom-session-${generateRandomString( 9, )}-sessTime-20` const password = 'u3e4c2852572e05c5' // const proxyAgent = new HttpsProxyAgent( // `http://${username}:${password}@43.152.113.55:2334`, // ) const requester = new APIRequester(rpc, { httpsAgent: proxyAgent, headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3', 'Sec-Ch-Ua': '"Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"', 'Sec-Ch-Ua-Mobile': '?0', 'Sec-Ch-Ua-Platform': '"Windows"', // Origin: 'https://faucet.testnet.initia.xyz/', // Referer: 'https://faucet.testnet.initia.xyz/', 'Sec-Fetch-Dest': 'empty', 'Sec-Fetch-Mode': 'cors', 'Sec-Fetch-Site': 'same-site', }, }) this.lcd = new LCDClient( rpc, { chainId: 'initiation-1', }, useProxy ? requester : null, ) const noonRequester = new APIRequester( 'https://burrito-1-lcd.lunchlunch.xyz/', { httpsAgent: proxyAgent, headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3', 'Sec-Ch-Ua': '"Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"', 'Sec-Ch-Ua-Mobile': '?0', 'Sec-Ch-Ua-Platform': '"Windows"', // Origin: 'https://faucet.testnet.initia.xyz/', // Referer: 'https://faucet.testnet.initia.xyz/', 'Sec-Fetch-Dest': 'empty', 'Sec-Fetch-Mode': 'cors', 'Sec-Fetch-Site': 'same-site', }, }, ) this.noon = new LCDClient( 'https://burrito-1-lcd.lunchlunch.xyz/', { chainId: 'burrito-1' }, useProxy ? noonRequester : null, ) const miniMoveRequester = new APIRequester( 'https://lcd.minimove-1.initia.xyz', { httpsAgent: proxyAgent, headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3', 'Sec-Ch-Ua': '"Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"', 'Sec-Ch-Ua-Mobile': '?0', 'Sec-Ch-Ua-Platform': '"Windows"', // Origin: 'https://faucet.testnet.initia.xyz/', // Referer: 'https://faucet.testnet.initia.xyz/', 'Sec-Fetch-Dest': 'empty', 'Sec-Fetch-Mode': 'cors', 'Sec-Fetch-Site': 'same-site', }, }, ) this.miniMove = new LCDClient( 'https://lcd.minimove-1.initia.xyz', { chainId: 'minimove-1' }, useProxy ? miniMoveRequester : null, ) const tucanaMoveRequester = new APIRequester( 'https://maze-rest-c9796789-107d-49ab-b6de-059724d2a91d.ue1-prod.newmetric.xyz/', { httpsAgent: proxyAgent, headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3', 'Sec-Ch-Ua': '"Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"', 'Sec-Ch-Ua-Mobile': '?0', 'Sec-Ch-Ua-Platform': '"Windows"', // Origin: 'https://faucet.testnet.initia.xyz/', // Referer: 'https://faucet.testnet.initia.xyz/', 'Sec-Fetch-Dest': 'empty', 'Sec-Fetch-Mode': 'cors', 'Sec-Fetch-Site': 'same-site', }, }, ) this.tucana = new LCDClient( 'https://maze-rest-c9796789-107d-49ab-b6de-059724d2a91d.ue1-prod.newmetric.xyz/', { chainId: 'birdee-1', }, useProxy ? tucanaMoveRequester : null, ) const civiMoveRequester = new APIRequester( 'https://maze-rest-sequencer-beab9b6f-d96d-435e-9caf-5679296d8172.ue1-prod.newmetric.xyz', { httpsAgent: proxyAgent, headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3', 'Sec-Ch-Ua': '"Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"', 'Sec-Ch-Ua-Mobile': '?0', 'Sec-Ch-Ua-Platform': '"Windows"', // Origin: 'https://faucet.testnet.initia.xyz/', // Referer: 'https://faucet.testnet.initia.xyz/', 'Sec-Fetch-Dest': 'empty', 'Sec-Fetch-Mode': 'cors', 'Sec-Fetch-Site': 'same-site', }, }, ) this.civi = new LCDClient( 'https://maze-rest-sequencer-beab9b6f-d96d-435e-9caf-5679296d8172.ue1-prod.newmetric.xyz', { chainId: 'landlord-1', }, useProxy ? civiMoveRequester : null, ) const blackWingsMoveRequester = new APIRequester( 'https://maze-rest-18bdff44-3aa4-425e-9bc0-06a2afa40af8.ue1-prod.newmetric.xyz', { httpsAgent: proxyAgent, headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3', 'Sec-Ch-Ua': '"Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"', 'Sec-Ch-Ua-Mobile': '?0', 'Sec-Ch-Ua-Platform': '"Windows"', // Origin: 'https://faucet.testnet.initia.xyz/', // Referer: 'https://faucet.testnet.initia.xyz/', 'Sec-Fetch-Dest': 'empty', 'Sec-Fetch-Mode': 'cors', 'Sec-Fetch-Site': 'same-site', }, }, ) this.blackWings = new LCDClient( 'https://maze-rest-18bdff44-3aa4-425e-9bc0-06a2afa40af8.ue1-prod.newmetric.xyz', { chainId: 'tomcat-1', }, useProxy ? blackWingsMoveRequester : null, ) const initAiRequester = new APIRequester( 'https://maze-rest-617bacff-7d34-4eb8-87f4-ee16fb4e0ac7.ue1-prod.newmetric.xyz', { httpsAgent: proxyAgent, headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3', 'Sec-Ch-Ua': '"Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"', 'Sec-Ch-Ua-Mobile': '?0', 'Sec-Ch-Ua-Platform': '"Windows"', // Origin: 'https://faucet.testnet.initia.xyz/', // Referer: 'https://faucet.testnet.initia.xyz/', 'Sec-Fetch-Dest': 'empty', 'Sec-Fetch-Mode': 'cors', 'Sec-Fetch-Site': 'same-site', }, }, ) this.initAi = new LCDClient( 'https://maze-rest-617bacff-7d34-4eb8-87f4-ee16fb4e0ac7.ue1-prod.newmetric.xyz', { chainId: 'init-ai-1', }, useProxy ? initAiRequester : null, ) this.wallet = new Wallet(this.lcd, this.key) } async broadcast(msg: Msg | Msg[]) { // const randLimit = 160000 + Math.floor(Math.random() * 10000) const simulate = await this.lcd.tx.estimateFee( [{ sequenceNumber: await this.wallet.sequence() }], { msgs: Array.isArray(msg) ? msg : [msg], }, ) // console.log('simulate', simulate) // console.log('simulate2', simulate.amount.toArray()[0].amount) const signed = await this.wallet.createAndSignTx({ msgs: Array.isArray(msg) ? msg : [msg], fee: new Fee( simulate.gas_limit, new Coins([ new Coin( 'move/944f8dd8dc49f96c25fea9849f16436dcfa6d564eec802f3ef7f8b3ea85368ff', simulate.amount.toArray()[0].amount, ), ]), ), }) // return `1` // const signed2 = await this.wallet.createAndSignTx({ // msgs: Array.isArray(msg) ? msg : [msg], // fee: new Fee( // Math.floor(Number(simulate) * 1.1), // new Coins([ // new Coin( // 'move/944f8dd8dc49f96c25fea9849f16436dcfa6d564eec802f3ef7f8b3ea85368ff', // Math.floor(Number(simulate) * 1.05), // ), // ]), // ), // }) const broadcast = await this.lcd.tx.broadcastAsync(signed) return broadcast.txhash } randomAmount(min: number, max: number) { return Math.floor(Math.random() * (max - min) + min) } async checkBalance() { const balances = await this.lcd.bank.balance(this.key.accAddress) return balances[0].toArray() } async axiosBalance() { const axios = getAxiosClient(true) const res = await axios.get( `https://celatone-api-prod.alleslabs.dev/v1/initia/initiation-1/accounts/${this.key.accAddress}/balances`, ) return res.data } async getGasAmount() { // const balances = await this.axiosBalance() // const balances = await this.axiosBalance() const balances = (await this.lcd.bank.balance(this.key.accAddress))[0] try { const usdc = balances.filter( token => token.denom === 'move/944f8dd8dc49f96c25fea9849f16436dcfa6d564eec802f3ef7f8b3ea85368ff', ) return Number(usdc.toArray()[0].amount) } catch (e) { throw new Error('..') } } async gasGot() { const balances = await this.lcd.bank.balanceByDenom( this.key.accAddress, 'move/944f8dd8dc49f96c25fea9849f16436dcfa6d564eec802f3ef7f8b3ea85368ff', ) return Number(balances.amount) > 3500000 } async funded() { const tokens = await this.checkBalance() const uinit = tokens.filter(token => token.denom === 'uinit')[0] return Number(uinit.amount) > 0 } async checkTucanaBalance() { const balances = await this.tucana.bank.balance(this.key.accAddress) const tuc = balances[0].toArray().filter(token => token.denom === 'utuc')[0] return tuc.amount } async needBlackWingsInit() { const balances = await this.blackWings.bank.balance(this.key.accAddress) const blackWings = balances[0] .toArray() .filter( token => token.denom === 'l2/aee375e9d0b181f0d9d3a49f9a3d1d6b05d62b0ac81f8c92b9282afa4213d884', )[0] return Number(blackWings.amount) < 1000000 } async checkBlackWingsBalance() { try { const balances = await this.blackWings.bank.balance(this.key.accAddress) return balances[0].toArray() } catch (e) { throw new Error('..') } } async checkAiBalance() { try { const balances = await this.initAi.bank.balance(this.key.accAddress) return balances[0].toArray() } catch (e) { throw new Error('..') } } async checkUsdcSwaped() { const tokens = await this.checkBalance() try { const usdc = tokens.filter(token => token.denom === 'uusdc')[0] return Number(usdc.amount) > 2000000 } catch (e) { console.log(`error`) throw new Error('..') } } async checkTiaSwaped() { const tokens = await this.checkBalance() const usdc = tokens.filter(token => token.denom === 'utia')[0] return !!usdc } async checkEthSwaped() { const tokens = await this.checkBalance() try { const usdc = tokens.filter(token => token.denom === 'ueth')[0] return !!usdc } catch (e) { return false } } async getUsdcAmount() { const tokens = await this.checkBalance() const usdc = tokens.filter(token => token.denom === 'uusdc')[0] return usdc.amount } async getTargetAmount(toAddr: string) { const data = await this.lcd.bank.balance(toAddr) const tokens = data[0].toArray() try { console.log(tokens) const usdc = tokens.filter(token => token.denom === 'uinit')[0] return usdc.amount } catch (e) { return 0 } } async getInitAmount() { const tokens = await this.checkBalance() try { console.log(tokens) const usdc = tokens.filter(token => token.denom === 'uinit')[0] return usdc.amount } catch (e) { return 0 } } async getTiaAmount() { const tokens = await this.checkBalance() try { const usdc = tokens.filter(token => token.denom === 'utia')[0] return usdc.amount } catch (e) { return 0 } } async getEthAmount() { const tokens = await this.checkBalance() try { const usdc = tokens.filter(token => token.denom === 'ueth')[0] return usdc.amount } catch (e) { return 0 } } async bridge() { const size = bcs.option(bcs.u64()).serialize(0).toBase64() const balance = await this.checkTucanaBalance() console.log('balance', balance) const randomLimit = 70000 + Math.floor(Math.random() * 1000) const value = Number(balance) - randomLimit const size2 = bcs.option(bcs.u64()).serialize(value.toString()).toBase64() const msgs = [ new MsgTransfer( 'transfer', // port 'channel-0', // src channel new Coin('utuc', value), // amount this.key.accAddress, // sender '0x42cd8467b1c86e59bf319e5664a09b6b5840bb3fac64f5ce690b5041c530565a::swap_transfer::mixed_route_swap_to', // receiver // you have to provide one of timeout new Height(0, 0), // timeout height ((new Date().valueOf() + 3500000) * 1000000).toString(), // timeout timestamp // timeout timestamp `{"move":{"message":{"module_address":"0x42cd8467b1c86e59bf319e5664a09b6b5840bb3fac64f5ce690b5041c530565a","module_name":"swap_transfer","function_name":"mixed_route_swap_to","type_args":[],"args":["KxToiwb36zAB/GsetggCNYVVpN+xpO02UHMJotdmzk0=","AQIBACBjlv8aKTjnJqzBAbnFQUuAXZpgXAPI4IMk9cDIgH98vA==",${size2},${size},${bcs .address() .serialize(this.key.accAddress) .toBase64()}]}}}`, ), ] const fee = { amount: [ { amount: '62579', denom: 'utuc', }, ], gasLimit: '417192', granter: '', payer: '', } // 创建 Coins 实例 const coin = new Coin('utuc', '70000') const coins = new Coins() coins.add(coin) // 创建 Fee 实例 const feeInstance = new Fee( parseInt(fee.gasLimit, 10), // gas_limit coins, // amount fee.payer, // payer (optional) fee.granter, // granter (optional) ) const signedTx = await this.tucanaWallet.createAndSignTx({ msgs, fee: feeInstance, }) await this.tucana.tx.broadcastSync(signedTx).then(res => console.log(res)) } async bridge2Blackwings(amount: number) { const size = bcs.option(bcs.u64()).serialize(0).toBase64() const size2 = bcs .u64() .serialize(amount * Math.pow(10, 6)) .toBase64() const msg = new MsgExecute( this.key.accAddress, '0x42cd8467b1c86e59bf319e5664a09b6b5840bb3fac64f5ce690b5041c530565a', 'swap_transfer', 'mixed_route_swap_transfer', [], [ 'jkczvavPfUr8PRTw3UbJv1L7D86eS5lsk54ZW4vIkdk=', 'AQIBACDb8GxIrzmE7G2a6KmqfbsLseeEqpuMSlaBr2YM+FWNfQ==', size2, size, bcs.string().serialize(this.key.accAddress).toBase64(), 'CHRyYW5zZmVy', 'CmNoYW5uZWwtMTM=', 'AA==', ], ) const signed = await this.wallet.createAndSignTx({ msgs: Array.isArray(msg) ? msg : [msg], feeDenoms: ['uinit'], }) await this.lcd.tx.broadcast(signed) } async bridge2Ai(amount: number) { const size = bcs .option(bcs.u64()) .serialize(amount * Math.pow(10, 6) * 0.995) .toBase64() const size2 = bcs .u64() .serialize(amount * Math.pow(10, 6)) .toBase64() const msg = new MsgExecute( this.key.accAddress, '0x42cd8467b1c86e59bf319e5664a09b6b5840bb3fac64f5ce690b5041c530565a', 'swap_transfer', 'mixed_route_swap_deposit', [], [ 'jkczvavPfUr8PRTw3UbJv1L7D86eS5lsk54ZW4vIkdk=', 'AA==', size2, size, 'BgAAAAAAAAA=', bcs.address().serialize(this.key.accAddress).toBase64(), 'AA==', ], ) // [ // ('jkczvavPfUr8PRTw3UbJv1L7D86eS5lsk54ZW4vIkdk=', // 'AA==', // 'QEIPAAAAAAA=', // 'AbguDwAAAAAA', // 'BgAAAAAAAAA=', // 'AAAAAAAAAAAAAAAAdpKzfgzxKB4sBaUJs11Wa9w+emA=', // 'AA==') // ][ // ('jkczvavPfUr8PRTw3UbJv1L7D86eS5lsk54ZW4vIkdk=', // 'AA==', // 'gIQeAAAAAAA=', // 'AXBdHgAAAAAA', // 'BgAAAAAAAAA=', // 'AAAAAAAAAAAAAAAAdpKzfgzxKB4sBaUJs11Wa9w+emA=', // 'AA==') // ] const signed = await this.wallet.createAndSignTx({ msgs: Array.isArray(msg) ? msg : [msg], feeDenoms: ['uinit'], }) const broadcast = await this.lcd.tx.broadcastSync(signed) console.log(broadcast) } async newBridgeUsdc2BlackWings() { const msg = new MsgTransfer( 'transfer', 'channel-13', new Coin('uusdc', 1000000), this.key.accAddress, this.key.accAddress, null, // timeout height ((new Date().valueOf() + 3500000) * 1000000).toString(), ) return await this.broadcast(msg) } async newBridgeUsdc2Tucana() { const random = this.randomAmount(1000000, 1500000) const msg = new MsgTransfer( 'transfer', 'channel-25', new Coin('uusdc', random), this.key.accAddress, this.key.accAddress, null, // timeout height ((new Date().valueOf() + 3500000) * 1000000).toString(), ) return await this.broadcast(msg) } async newBridgeTia2Noon() { const random = this.randomAmount(100000, 150000) const msg = new MsgTransfer( 'transfer', 'channel-31', new Coin('utia', random), this.key.accAddress, this.key.accAddress, null, // timeout height ((new Date().valueOf() + 3500000) * 1000000).toString(), ) return await this.broadcast(msg) } async newBridgeEth2MiniMove() { const msg = new MsgTransfer( 'transfer', 'channel-0', new Coin('ueth', 100), this.key.accAddress, this.key.accAddress, null, // timeout height ((new Date().valueOf() + 3500000) * 1000000).toString(), ) return await this.broadcast(msg) } async newBridge2BlackWings() { const random = this.randomAmount(1000000, 1500000) const msg = new MsgInitiateTokenDeposit( this.key.accAddress, 8, this.key.accAddress, new Coin('uinit', random), ) return await this.broadcast(msg) } async newBridge2Noon() { const msg = new MsgInitiateTokenDeposit( this.key.accAddress, 17, this.key.accAddress, new Coin('uinit', 1500000), ) return await this.broadcast(msg) } async newBridge2Ai() { const random = this.randomAmount(1500000, 2000000) const msg = new MsgInitiateTokenDeposit( this.key.accAddress, 6, this.key.accAddress, new Coin('uinit', random), ) return await this.broadcast(msg) } async newBridge2Civi() { const random = this.randomAmount(1500000, 2000000) const msg = new MsgInitiateTokenDeposit( this.key.accAddress, 24, this.key.accAddress, new Coin('uinit', random), ) return await this.broadcast(msg) } async newBridge2Tucana() { const msg = new MsgInitiateTokenDeposit( this.key.accAddress, 14, this.key.accAddress, new Coin('uinit', 1000000), ) return await this.broadcast(msg) } async newBridge2Miniwasm() { const msg = new MsgInitiateTokenDeposit( this.key.accAddress, 2, this.key.accAddress, new Coin('uinit', 1000000), ) return await this.broadcast(msg) } async newBridge2Minimove() { const msg = new MsgInitiateTokenDeposit( this.key.accAddress, 1, this.key.accAddress, new Coin('uinit', 1000000), ) return await this.broadcast(msg) } }