Last active
February 19, 2019 04:12
-
-
Save kaidiren/29b1bb1309c225c066c15be9af9c9db3 to your computer and use it in GitHub Desktop.
vite send VTT by pow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// node version v10.15.0 | |
// "@vite/vitejs": "^1.1.4", | |
// "sleep-promise": "^8.0.1" | |
// output example | |
// generate account from: vite_b91c215ee845e80744c7d3536c5246724d9bd8938b710e96e9 | |
// generate account to: vite_2118b87c74c01abee0b95e160b4d0e615cc5a22a96e23d50c7 | |
// get some VTT to vite_b91c215ee845e80744c7d3536c5246724d9bd8938b710e96e9 | |
// https://explorer.vite.net/transaction/17fbe92009a549b332930c9a532076e90caa6bc2bcadab9ac95840bb4b66d8a3 | |
// send some VTT to vite_2118b87c74c01abee0b95e160b4d0e615cc5a22a96e23d50c7 | |
// https://explorer.vite.net/transaction/545bbdfa555a67f6e80109a26f07d18922058520ec8cb516b67b1cfbe16fc244 | |
// get the onroad VTT to vite_2118b87c74c01abee0b95e160b4d0e615cc5a22a96e23d50c7 | |
// https://explorer.vite.net/transaction/5f39722f1786f8512cd776e3d64cdfdf552346453a63ab19b389be815f9df2e4 | |
// send VTT back to faucet | |
// https://explorer.vite.net/transaction/7e21ca3e70f9f72201b6851313baea694ac84c7110845ed84ac158efda7de105 | |
// https://explorer.vite.net/transaction/ef3202239e17ac3a5ab8a940e26531049eb55b5c9886ed82f3033f8f9983b7f4 | |
// all tasks completed | |
const sleep = require('sleep-promise') | |
const vite = require('@vite/vitejs') | |
const { default: Provider } = require('@vite/vitejs/dist/es5/provider/WS') | |
const { client: Client, wallet: Wallet } = require('@vite/vitejs') | |
const { account: Account } = Wallet | |
let WS_RPC = new Provider('wss://testnet.vitewallet.com/ws') | |
let client = new Client(WS_RPC) | |
const DefaultDifficulty = '67108864' | |
const VTT = 'tti_c55ec37a916b7f447575ae59' | |
const explorerUrl = 'https://explorer.vite.net/transaction/' | |
const faucetAddress = 'vite_56fd05b23ff26cd7b0a40957fb77bde60c9fd6ebc35f809c23' | |
// generate account instance | |
function genAccount () { | |
const key = vite.utils.ed25519.keyPair() | |
const account = vite.utils.address.privToAddr.newHexAddr(key.secretKey) | |
return new Account({ | |
privateKey: account.privKey, | |
client | |
}) | |
} | |
// get Pow Nonce | |
async function getPowNonce (addr, prevHash, difficulty = DefaultDifficulty) { | |
let realAddr = vite.utils.address.privToAddr.getAddrFromHexAddr(addr) | |
let hash = vite.utils.encoder.bytesToHex( | |
vite.utils.encoder.blake2b( | |
vite.utils.encoder.hexToBytes(realAddr + prevHash), | |
null, | |
32 | |
) | |
) | |
const result = await client.request('pow_getPowNonce', difficulty, hash) | |
return { | |
nonce: result, | |
difficulty | |
} | |
} | |
// send task loop | |
// send random amount of VTT token from `account_from` to `account_to` with `count` times | |
// if out of quota, just run pow | |
async function sendLoop (from, to, count) { | |
while (count--) { | |
try { | |
await from.sendTx({ | |
toAddress: to.address, | |
tokenId: VTT, | |
amount: (Math.random() * 1e19).toString(), | |
message: '' | |
}) | |
} catch (e) { | |
try { | |
console.log(explorerUrl + await calcPowAndSend(from, e)) | |
} catch (e) { | |
} | |
} | |
await sleep(2000) | |
} | |
} | |
// send VTT back to the faucet | |
async function sendBackToFaucet (from, to) { | |
let { balance } = await from.getBalance() | |
let totalAmount = balance && balance.tokenBalanceInfoMap[VTT] && balance.tokenBalanceInfoMap[VTT]['totalAmount'] | |
if (totalAmount) { | |
try { | |
await from.sendTx({ | |
toAddress: faucetAddress, | |
tokenId: VTT, | |
amount: totalAmount, | |
message: '' | |
}) | |
} catch (e) { | |
try { | |
console.log(explorerUrl + await calcPowAndSend(from, e)) | |
} catch (e) { | |
} | |
} | |
} | |
await sleep(2000); | |
({ balance } = await to.getBalance()) | |
totalAmount = balance && balance.tokenBalanceInfoMap[VTT] && balance.tokenBalanceInfoMap[VTT]['totalAmount'] | |
if (totalAmount) { | |
try { | |
await to.sendTx({ | |
toAddress: faucetAddress, | |
tokenId: VTT, | |
amount: totalAmount, | |
message: '' | |
}) | |
} catch (e) { | |
try { | |
console.log(explorerUrl + await calcPowAndSend(to, e)) | |
} catch (e) { | |
} | |
} | |
} | |
} | |
// receive loop | |
// receive all tokens | |
async function receiveLoop (account) { | |
try { | |
const onroadBlocks = await client.onroad.getOnroadBlocksByAddress(account.address, 0, 100) | |
for (const onroadBlock of onroadBlocks) { | |
try { | |
await account.receiveTx({ | |
fromBlockHash: onroadBlock.hash | |
}) | |
} catch (e) { | |
console.log(explorerUrl + await calcPowAndSend(account, e)) | |
} | |
await sleep(2000) | |
} | |
} catch (e) {} | |
} | |
// get some TestToken VTT and receive them | |
async function getTestToken (account) { | |
try { | |
await client.request('testapi_getTestToken', account.address) | |
const onroadBlocks = await client.onroad.getOnroadBlocksByAddress(account.address, 0, 100) | |
for (const onroadBlock of onroadBlocks) { | |
try { | |
await account.receiveTx({ | |
fromBlockHash: onroadBlock.hash | |
}) | |
} catch (e) { | |
console.log(explorerUrl + await calcPowAndSend(account, e)) | |
} | |
await sleep(2000) | |
} | |
} catch (e) {} | |
} | |
// if error msg is out of quota then just run pow | |
async function calcPowAndSend (account, e) { | |
if (!e || !e.accountBlock || !e.error || e.error.message !== 'out of quota') { | |
throw e | |
} | |
let accountBlock = e.accountBlock | |
accountBlock = await vite.utils.accountBlock.getAccountBlock(accountBlock) | |
accountBlock.height = String(Number(accountBlock.height) - 1) | |
const data = await getPowNonce(accountBlock.accountAddress, accountBlock.prevHash) | |
accountBlock.difficulty = data.difficulty | |
accountBlock.nonce = data.nonce | |
await account.sendRawTx(accountBlock) | |
return vite.utils.accountBlock.getBlockHash(accountBlock) | |
} | |
// generate task | |
async function genTask () { | |
try { | |
const from = genAccount() | |
console.log('generate account from:', from.address) | |
const to = genAccount() | |
console.log('generate account to:', to.address) | |
console.log('get some VTT to', from.address) | |
await getTestToken(from) | |
console.log('send some VTT to', to.address) | |
await sendLoop(from, to, 1) | |
console.log('get the onroad VTT to', to.address) | |
await receiveLoop(to) | |
console.log('send VTT back to faucet') | |
await sendBackToFaucet(from, to) | |
} catch (e) {} | |
} | |
(async () => { | |
const tasks = [] | |
let count = 1 | |
while (count--) { | |
tasks.push(genTask()) | |
} | |
try { | |
await Promise.all(tasks) | |
} catch (e) { | |
console.log(e) | |
} | |
console.log('all tasks completed') | |
process.exit() | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment