Skip to content

Instantly share code, notes, and snippets.

@kolya-t
Created February 25, 2019 14:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kolya-t/c52ed141ce9e33d23996d41ea8770954 to your computer and use it in GitHub Desktop.
Save kolya-t/c52ed141ce9e33d23996d41ea8770954 to your computer and use it in GitHub Desktop.
const TronWeb = require("tronweb");
const axios = require("axios");
const privateKey = "da146374a75310b9666e834ee4ad0866d6f4035967bfc76217c5a495fff9f0d0";
const tronWeb = new TronWeb(
"https://trontestnet.mywish.io/",
"https://trontestnet.mywish.io/",
"https://trontestnet.mywish.io/",
privateKey
);
const { constants } = require("./c-preprocessor-config");
const { abi, bytecode } = require("./build/contracts/LostKeyMain");
let lostKey;
const deployContract = async () => {
const transaction = await tronWeb.transactionBuilder.createSmartContract({ abi, bytecode });
const signedTransaction = await tronWeb.trx.sign(transaction);
return await tronWeb.trx.sendRawTransaction(signedTransaction);
};
const getTransactionById = async txID => {
const result = await axios.get(
"https://trontestnet.mywish.io/wallet/gettransactionbyid",
{ params: { value: txID } }
);
return result.data;
};
const getTransactionInfoById = async txID => {
const result = await axios.get(
"https://trontestnet.mywish.io/wallet/gettransactioninfobyid",
{ params: { value: txID } }
);
return result.data;
};
const getDeployCost = async (msg) => {
const {
result,
transaction: {
txID,
contract_address
}
} = await deployContract();
if (!result) {
throw "Deploy failed";
}
const txInfo = await getTransactionInfoById(txID);
console.info(msg + ":", txInfo.receipt);
lostKey = await tronWeb.contract(abi, contract_address);
};
const sleep = seconds => {
return new Promise(resolve => setTimeout(resolve, seconds * 1000));
};
const addTokens = async (count) => {
const { abi, bytecode } = require('./build/contracts/TestToken');
const targetAddress = constants.D_TARGET.replace(/^0x/, "41");
let tokens = [];
for (let i = 0; i < count; i++) {
const token = await tronWeb.contract().new({ abi, bytecode });
await token.mint(targetAddress, 1000).send();
await token.approve(lostKey.address, 1000).send();
tokens.push(token);
}
await lostKey.addTokenAddresses(tokens.map(t => t.address)).send();
};
const check = async (msg) => {
const tx = await lostKey.check().send();
await sleep(1);
const txInfo = await getTransactionInfoById(tx);
console.info(tx);
console.info(msg + ":", txInfo.receipt);
};
(async () => {
await getDeployCost('Deploy contract');
await check('Empty check');
await addTokens(2);
await sleep(constants.D_PERIOD_SECONDS + 1);
await check('Check after');
// console.info(await tronWeb.address.toHex(await tronWeb.address.fromPrivateKey(privateKey)));
// const contract = await tronWeb.contract().new({ abi, bytecode });
// const transaction = await tronWeb.transactionBuilder.createSmartContract({ abi: tokenAbi, bytecode: tokenBytecode });
// const signedTransaction = await tronWeb.trx.sign(transaction);
// const contract = await tronWeb.trx.sendRawTransaction(signedTransaction);
// console.info(contract.address);
// console.info(await tronWeb.contract(tokenAbi, tokenAddr).mint(myAddress, 1000).send());
// console.info(await tronWeb.contract(tokenAbi, tokenAddr).approve(targetAddr, 1000).send());
// console.info(await tronWeb.contract(tokenAbi, tokenAddr).allowance(myAddress, targetAddr).call());
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment