Commit 95024fb7 by haojie

Initial commit

parents
/node_modules
/env.js
import request from './src/axios.js';
import { login_api, login_params, task_api } from './env.js';
import { sleep, getTokenHeader } from './utils/tool.js';
import { sendToken, inscriptionTransfer } from './utils/ethers.js';
// 交易状态
const statusInfo = {
TRADE_STATUS_WAIT: 1, // 未开始
TRADE_STATUS_SUCCESS: 2, // 已完成
TRADE_STATUS_FAIL: 3, // 失败
TRADE_STATUS_FAIL_2: 4, // 支付成功,接收地址错误
TRADE_STATUS_ERROR: 5, // 检测失败
};
// 当前token
let login_token = '';
// 登录过期校验
async function checkToken(code) {
if (code == 403) {
// 重新登录
await login();
}
}
// 获取铭文列表
async function getInscriptionList(address, inscription_hash) {
let check_url =
'https://mainnet-api.ethscriptions.com/api/ethscriptions/owned_by/' +
address;
// 转小写
inscription_hash = inscription_hash.toLowerCase();
try {
// 检测五次
for (let check_time = 0; check_time < 5; check_time++) {
let result = await request.get(check_url);
if (result) {
if (typeof result === 'string') {
result = JSON.parse(result);
}
if (result.length) {
// 找到铭文
for (let i = 0; i < result.length; i++) {
let item = result[i];
let item_hash = item.transaction_hash.toLowerCase();
if (item_hash === inscription_hash) {
return item;
}
}
}
}
}
return false;
} catch (e) {
console.log(e);
return false;
}
}
// 登录
async function login() {
try {
const res = await request.post(login_api, login_params);
if (res.code == 0) {
console.log('登录成功');
login_token = res.data.access_token;
}
} catch (e) {
console.log(e);
}
}
// 获取后台任务
const get_task = async () => {
const header = getTokenHeader(login_token);
let res = await request.get(task_api, {
headers: {
...header,
},
});
if (res.code == 0) {
if (typeof res.data === 'string') {
return JSON.parse(res.data);
}
return res.data;
}
await checkToken(res.code);
return false;
};
// 交易回调
const Transfer_callBack = async (task) => {
let for_status = true;
let url = task.notify_url;
// 提交5次
for (let i = 0; i < 5; i++) {
const header = getTokenHeader(login_token);
try {
let res = await request.post(url, task, {
headers: {
...header,
},
});
if (res.code == 0) {
console.log(res);
for_status = false;
}
await checkToken(res.code);
if (!for_status) {
console.log('结束循环');
break;
}
} catch (e) {
console.log(e);
}
}
};
// 连接上之后开始循环获取任务
async function run() {
let status = true;
// 获取启动时间
let start_time = new Date().valueOf();
let max_runing_time = 1000 * 60 * 60 * 24;
while (status) {
let cur_time = new Date().valueOf();
// 一天关一次
if (cur_time - start_time >= max_runing_time) {
// 结束
status = false;
break;
}
// 获取队列任务
let task = await get_task();
if (task) {
let from = task.from;
let to = task.to;
try {
if (task.type == 1) {
let inscription_hash = task.inscription_hash;
// 铭文转移
console.log('铭文转移');
const result = await inscriptionTransfer(to, from, inscription_hash);
if (result.hash && result.status) {
task.hash = result.hash;
// 判断是否转移成功--并更新回传铭文信息
const inscription_info = await getInscriptionList(
task.to,
inscription_hash
);
if (inscription_info) {
task.status = statusInfo.TRADE_STATUS_SUCCESS;
task.inscriptionInfo = inscription_info;
} else {
task.status = statusInfo.TRADE_STATUS_FAIL;
}
Transfer_callBack(task);
} else {
// 失败
task.status = statusInfo.TRADE_STATUS_FAIL;
task.message = result.message ?? '';
task.hash = result.hash ?? '';
Transfer_callBack(task);
}
} else if (task.type == 2) {
let amount = task.amount;
// 转账
const result = await sendToken(to, from, amount);
if (result && result.hash) {
// 成功
task.status = statusInfo.TRADE_STATUS_SUCCESS;
task.hash = result.hash;
Transfer_callBack(task);
} else {
// 失败
task.status = statusInfo.TRADE_STATUS_FAIL;
task.message = result.message;
Transfer_callBack(task);
}
}
} catch (e) {}
} else {
console.log('等待');
sleep(2000);
}
}
}
await login();
await run();
// await Transfer_callBack();
// 登录接口
export const login_api = '';
// 用户名密码
export const login_params = {
username: '',
password: '',
};
// 获取任务接口
export const task_api = '';
export const private_key = [
{
address: '',
key: '',
},
];
This source diff could not be displayed because it is too large. You can view the blob instead.
{
"scripts": {
"start": "node ./app.js"
},
"dependencies": {
"axios": "^1.3.4",
"ethereumjs-common": "^1.5.2",
"ethereumjs-tx": "^2.1.2",
"ethers": "^5.7.2",
"web3": "^1.8.1"
},
"type": "module"
}
This diff is collapsed. Click to expand it.
[
{
"constant": true,
"inputs": [],
"name": "name",
"outputs": [{ "name": "", "type": "string" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [{ "name": "_upgradedAddress", "type": "address" }],
"name": "deprecate",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{ "name": "_spender", "type": "address" },
{ "name": "_value", "type": "uint256" }
],
"name": "approve",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "deprecated",
"outputs": [{ "name": "", "type": "bool" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [{ "name": "_evilUser", "type": "address" }],
"name": "addBlackList",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "totalSupply",
"outputs": [{ "name": "", "type": "uint256" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{ "name": "_from", "type": "address" },
{ "name": "_to", "type": "address" },
{ "name": "_value", "type": "uint256" }
],
"name": "transferFrom",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "upgradedAddress",
"outputs": [{ "name": "", "type": "address" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [{ "name": "", "type": "address" }],
"name": "balances",
"outputs": [{ "name": "", "type": "uint256" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "decimals",
"outputs": [{ "name": "", "type": "uint256" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "maximumFee",
"outputs": [{ "name": "", "type": "uint256" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "_totalSupply",
"outputs": [{ "name": "", "type": "uint256" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [],
"name": "unpause",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [{ "name": "_maker", "type": "address" }],
"name": "getBlackListStatus",
"outputs": [{ "name": "", "type": "bool" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{ "name": "", "type": "address" },
{ "name": "", "type": "address" }
],
"name": "allowed",
"outputs": [{ "name": "", "type": "uint256" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "paused",
"outputs": [{ "name": "", "type": "bool" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [{ "name": "who", "type": "address" }],
"name": "balanceOf",
"outputs": [{ "name": "", "type": "uint256" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [],
"name": "pause",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "getOwner",
"outputs": [{ "name": "", "type": "address" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "owner",
"outputs": [{ "name": "", "type": "address" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "symbol",
"outputs": [{ "name": "", "type": "string" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{ "name": "_to", "type": "address" },
{ "name": "_value", "type": "uint256" }
],
"name": "transfer",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{ "name": "newBasisPoints", "type": "uint256" },
{ "name": "newMaxFee", "type": "uint256" }
],
"name": "setParams",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [{ "name": "amount", "type": "uint256" }],
"name": "issue",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [{ "name": "amount", "type": "uint256" }],
"name": "redeem",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [
{ "name": "_owner", "type": "address" },
{ "name": "_spender", "type": "address" }
],
"name": "allowance",
"outputs": [{ "name": "remaining", "type": "uint256" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "basisPointsRate",
"outputs": [{ "name": "", "type": "uint256" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [{ "name": "", "type": "address" }],
"name": "isBlackListed",
"outputs": [{ "name": "", "type": "bool" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [{ "name": "_clearedUser", "type": "address" }],
"name": "removeBlackList",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "MAX_UINT",
"outputs": [{ "name": "", "type": "uint256" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [{ "name": "newOwner", "type": "address" }],
"name": "transferOwnership",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [{ "name": "_blackListedUser", "type": "address" }],
"name": "destroyBlackFunds",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{ "name": "_initialSupply", "type": "uint256" },
{ "name": "_name", "type": "string" },
{ "name": "_symbol", "type": "string" },
{ "name": "_decimals", "type": "uint256" }
],
"payable": false,
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [{ "indexed": false, "name": "amount", "type": "uint256" }],
"name": "Issue",
"type": "event"
},
{
"anonymous": false,
"inputs": [{ "indexed": false, "name": "amount", "type": "uint256" }],
"name": "Redeem",
"type": "event"
},
{
"anonymous": false,
"inputs": [{ "indexed": false, "name": "newAddress", "type": "address" }],
"name": "Deprecate",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{ "indexed": false, "name": "feeBasisPoints", "type": "uint256" },
{ "indexed": false, "name": "maxFee", "type": "uint256" }
],
"name": "Params",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{ "indexed": false, "name": "_blackListedUser", "type": "address" },
{ "indexed": false, "name": "_balance", "type": "uint256" }
],
"name": "DestroyedBlackFunds",
"type": "event"
},
{
"anonymous": false,
"inputs": [{ "indexed": false, "name": "_user", "type": "address" }],
"name": "AddedBlackList",
"type": "event"
},
{
"anonymous": false,
"inputs": [{ "indexed": false, "name": "_user", "type": "address" }],
"name": "RemovedBlackList",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{ "indexed": true, "name": "owner", "type": "address" },
{ "indexed": true, "name": "spender", "type": "address" },
{ "indexed": false, "name": "value", "type": "uint256" }
],
"name": "Approval",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{ "indexed": true, "name": "from", "type": "address" },
{ "indexed": true, "name": "to", "type": "address" },
{ "indexed": false, "name": "value", "type": "uint256" }
],
"name": "Transfer",
"type": "event"
},
{ "anonymous": false, "inputs": [], "name": "Pause", "type": "event" },
{ "anonymous": false, "inputs": [], "name": "Unpause", "type": "event" }
]
import axios from 'axios';
const request = axios.create({
timeout: 30000,
withCredentials: true,
});
request.interceptors.response.use(
(res) => {
const { data } = res;
if (data.code == 0) {
return data;
}
},
(err) => {
return err;
}
);
export default request;
import { private_key } from '../env.js';
import { ethers } from 'ethers';
import { readFile } from 'fs/promises';
import common from 'ethereumjs-common';
import Web3 from 'web3';
import tx from 'ethereumjs-tx';
const Common = common.default;
const Tx = tx.Transaction;
const json_abi = JSON.parse(
await readFile(new URL('../src/ERC20ABI.json', import.meta.url))
);
// 节点
const chain_node =
'https://eth-mainnet.g.alchemy.com/v2/fH-3go199OKvbKSovc84gAC6xa5Givlb';
// eth链的 usdt 地址
const usdt = '0xdAC17F958D2ee523a2206206994597C13D831ec7';
const ETH_FORK = Common.forCustomChain(
'mainnet',
{
name: 'Ethereum',
networkId: 1,
chainId: 1,
url: chain_node,
},
'istanbul'
);
// 获取私钥
export const getPrivateKey = (address) => {
for (let i = 0; i < private_key.length; i++) {
let item = private_key[i];
if (item.address == address) {
return item.key;
}
}
return '';
};
// 实例化节点
export const newWeb3 = () => {
return new Web3(new Web3.providers.HttpProvider(chain_node));
};
// 转账
export const sendToken = async (to, from, amount) => {
// 获取私钥地址
const key = getPrivateKey(from);
let is_local = true;
const eth_web3 = newWeb3();
// let eth_web3 = new ethers.providers.JsonRpcProvider(chain_node);
let bscUsdtTokenContract = new eth_web3.eth.Contract(json_abi, usdt);
if (is_local) {
amount = Number(amount).toFixed(2);
amount = BigInt(amount * 10 ** 6).toString();
let count = await eth_web3.eth.getTransactionCount(from);
let data = bscUsdtTokenContract.methods.transfer(to, amount);
let rawTransaction = {
from: from,
gasPrice: eth_web3.utils.toHex(eth_web3.utils.toWei('18', 'gwei')),
gasLimit: 80000,
to: usdt,
data: data.encodeABI(),
nonce: count,
};
let transaction = new Tx(rawTransaction, { common: ETH_FORK });
// let privateKey = key.slice(2);
transaction.sign(Buffer.from(key, 'hex'));
let result = await eth_web3.eth.sendSignedTransaction(
'0x' + transaction.serialize().toString('hex')
);
return {
hash: result['transactionHash'],
// status: result['status'],
};
} else {
// ether.js
if (key) {
const provider = new ethers.providers.JsonRpcProvider(chain_node);
const signer = provider.getSigner();
let wallet = new ethers.Wallet(key, provider);
// 转换
let tokenAmount = amount * Math.pow(10, 6);
let big_num = BigInt(Math.floor(tokenAmount));
if (usdt) {
try {
let contract = new ethers.Contract(usdt, json, wallet);
let data = contract.transfer(to, big_num);
const res = await signer.sendTransaction({
to: contract,
// value: ethers.utils.parseUnits('0.000', 'ether'),
data: data.encodeABI(),
from: wallet.getAddress(),
nonce: signer.getTransactionCount(),
maxPriorityFeePerGas: ethers.utils.parseUnits('5', 'gwei'),
gasLimit: 80000,
// chainId: 5,
});
// const res = await contract.transfer(to, big_num);
console.log(res);
if (res.hash) {
// 交易成功
return {
hash: res.hash,
message: '',
};
} else {
// 交易失败
return {
hash: '',
message: '没有hash',
};
}
} catch (e) {
return {
hash: '',
message: e,
};
}
} else {
console.log('未设置usdt地址');
}
} else {
console.log('未获取到私钥');
}
}
};
// 铭文转移
export const inscriptionTransfer = async (to, from, data) => {
try {
const eth_web3 = newWeb3();
const senderPrivateKey = getPrivateKey(from);
const senderAccount =
eth_web3.eth.accounts.privateKeyToAccount(senderPrivateKey);
eth_web3.eth.accounts.wallet.add(senderAccount);
const transfer = {
from: senderAccount.address,
to: to,
value: 0,
data: data,
gas: 80000,
gasPrice: eth_web3.utils.toWei('18', 'gwei'),
};
// 使用私钥对交易进行签名
const signedTransaction = await eth_web3.eth.accounts.signTransaction(
transfer,
senderPrivateKey
);
// 发起交易
const transactionReceipt = await eth_web3.eth.sendSignedTransaction(
signedTransaction.rawTransaction
);
if (transactionReceipt.transactionHash) {
return {
hash: transactionReceipt.transactionHash,
status: transactionReceipt.status,
};
}
} catch (e) {
console.log(e);
return {
hash: '',
message: e,
};
}
};
inscriptionTransfer(
'0x53d05d2f8bdbb5ce389d313cd6d320fb0fb1c397',
'0x46DD0A127C7819e21591927aFb62518284150860',
'0x911d92a48d489ad367a50688e15af6a49274045c919305f0601b7c8f78098295'
);
// 等待
export function sleep(time) {
var timeStamp = new Date().valueOf();
var endTime = timeStamp + time;
while (true) {
if (new Date().valueOf() >= endTime) {
return;
}
}
}
// 获取带token的请求头
export const getTokenHeader = (token) => {
return {
authorization: `Bearer ${token}`,
};
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment