Skip to content

Instantly share code, notes, and snippets.

@zmovane
Last active December 15, 2023 06:04
Show Gist options
  • Save zmovane/b86d2971a4bacdd93a1fd02e99618e60 to your computer and use it in GitHub Desktop.
Save zmovane/b86d2971a4bacdd93a1fd02e99618e60 to your computer and use it in GitHub Desktop.
let IOST = require("iost");
import * as bs58 from "bs58";
import { randomInt } from "crypto";
const RPCS = [
'https://api.iost.io',
'http://35.176.24.11:30001',
'http://18.209.137.246:30001',
'http://54.180.196.80:30001',
'http://iost.mytokenpocket.vip'
];
const iost = new IOST.IOST({
gasRatio: 100,
gasLimit: 4000000,
delay: 0,
expiration: 90,
});
const account = new IOST.Account('YOUR ACCOUNT');
const keypair = new IOST.KeyPair(bs58.decode('YOUR PRIVATE KEY'));
account.addKeyPair(keypair, 'owner');
account.addKeyPair(keypair, 'active');
iost.setAccount(account);
async function mint(): Promise<any> {
const rpc = new IOST.RPC(new IOST.HTTPProvider(randomRPC()));
iost.setRPC(rpc);
const contract = 'Contract6vU3ZWL57jQeFpbuqUxQfL5PGeFJekWrDG2WVGjWqrKx';
const tx = iost.callABI(contract, 'mint', ['iOSI', '1000']);
tx.addApprove('iost', 0.001);
return new Promise((resolve, reject) => {
iost.signAndSend(tx)
.on('pending', console.log)
.on('success', resolve)
.on('failed', reject);
});
}
function randomRPC() {
return RPCS[randomInt(RPCS.length)];
}
function delay(mills: number) {
return new Promise(resolve => setTimeout(resolve, mills));
}
function duplex(n: number, task: Function) {
return Promise.allSettled(Array.from({ length: n }, () => task()));
}
function semaphore(max: number, fn: Function, ...outer: any[]){
let run = 0;
const waits: Function[] = [];
function next(x: Promise<any>){
if (run < max && waits.length)
waits.shift()?.(++run)
return x;
}
return () => next(
new Promise((resolve) => waits.push(resolve))
.then(() => fn(...outer))
.catch(console.log)
.finally(() => run--)
.finally(() => next)
);
}
enum TxStatus {
Requested,
Submitted,
Confirmed
}
async function main() {
const statusRequired: TxStatus = TxStatus.Requested;
const execute = semaphore(10, mint);
for(;;){
switch(+status) {
case TxStatus.Requested:
execute();
break;
case TxStatus.Submitted:
await execute();
break;
case TxStatus.Confirmed:
// TODO
await execute();
break;
}
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment