Skip to content

Instantly share code, notes, and snippets.

@anonymoussprocket

anonymoussprocket/README.md Secret

Last active Sep 14, 2020
Embed
What would you like to do?
Cryptonomic Tezos Handbook supporting materials

Cryptonomic Tezos Handbook supporting materials

import * as fs from 'fs';
import * as glob from 'glob';
import { TezosConseilClient, TezosWalletUtil, TezosNodeWriter, setLogLevel, TezosParameterFormat, KeyStore, OperationKindType, TezosNodeReader } from 'conseiljs';
setLogLevel('debug');
const tezosNode = 'https://tezos-dev.cryptonomic-infra.tech:443';
const conseilServer = { url: 'https://conseil-dev.cryptonomic-infra.tech:443', apiKey: 'ab682065-864a-4f11-bc77-0ef4e9493fa1', network: 'carthagenet' };
const networkBlockTime = 30 + 1;
let faucetAccount = {};
let keystore: KeyStore;
let contractAddress: string = 'KT1EhyNUyAUG42udmJzfAz7WDL2daDNb2cwv';
function clearRPCOperationGroupHash(hash: string) {
return hash.replace(/\"/g, '').replace(/\n/, '');
}
async function initAccount(): Promise<KeyStore> {
console.log('~~ initAccount');
let faucetFiles: string[] = glob.sync('tz1*.json');
if (faucetFiles.length === 0) {
throw new Error('Did not find any faucet files, please go to faucet.tzalpha.net to get one');
}
console.log(`loading ${faucetFiles[0]} faucet file`);
faucetAccount = JSON.parse(fs.readFileSync(faucetFiles[0], 'utf8'));
const keystore = await TezosWalletUtil.unlockFundraiserIdentity(faucetAccount['mnemonic'].join(' '), faucetAccount['email'], faucetAccount['password'], faucetAccount['pkh']);
console.log(`public key: ${keystore.publicKey}`);
console.log(`secret key: ${keystore.privateKey}`);
console.log(`account hash: ${keystore.publicKeyHash}`);
console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
return keystore;
}
async function activateAccount(): Promise<string> {
console.log(`~~ activateAccount`);
const accountRecord = await TezosConseilClient.getAccount(conseilServer, conseilServer.network, keystore.publicKeyHash);
if (accountRecord !== undefined) { return accountRecord['account_id']; }
const nodeResult = await TezosNodeWriter.sendIdentityActivationOperation(tezosNode, keystore, faucetAccount['secret']);
const groupid = clearRPCOperationGroupHash(nodeResult.operationGroupID);
console.log(`Injected activation operation with ${groupid}`);
const conseilResult = await TezosConseilClient.awaitOperationConfirmation(conseilServer, conseilServer.network, groupid, 5, networkBlockTime);
console.log(`Activated account at ${conseilResult.pkh}`);
console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
return conseilResult.pkh;
}
async function revealAccount(): Promise<string> {
console.log(`~~ revealAccount`);
if (await TezosNodeReader.isManagerKeyRevealedForAccount(tezosNode, keystore.publicKeyHash)) {
return keystore.publicKeyHash;
}
const nodeResult = await TezosNodeWriter.sendKeyRevealOperation(tezosNode, keystore);
const groupid = clearRPCOperationGroupHash(nodeResult.operationGroupID);
console.log(`Injected reveal operation with ${groupid}`);
const conseilResult = await TezosConseilClient.awaitOperationConfirmation(conseilServer, conseilServer.network, groupid, 5, networkBlockTime);
console.log(`Revealed account at ${conseilResult.source}`);
console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
return conseilResult.source;
}
async function deployMichelsonContract(): Promise<string> {
console.log(`~~ deployMichelsonContract`);
const contract = `parameter string;
storage string;
code
{
DUP;
CDR;
SWAP;
CAR;
SWAP;
DROP;
NIL operation;
PAIR;
}`;
const storage = '"Hello"';
const fee = Number((await TezosConseilClient.getFeeStatistics(conseilServer, conseilServer.network, OperationKindType.Origination))[0]['high']);
const nodeResult = await TezosNodeWriter.sendContractOriginationOperation(tezosNode, keystore, 0, undefined, fee, '', 1000, 100000, contract, storage, TezosParameterFormat.Michelson);
const groupid = clearRPCOperationGroupHash(nodeResult['operationGroupID']);
console.log(`Injected origination operation with ${groupid}`);
const conseilResult = await TezosConseilClient.awaitOperationConfirmation(conseilServer, conseilServer.network, groupid, 5, networkBlockTime);
console.log(`Originated contract at ${conseilResult.originated_contracts}`);
console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
return conseilResult.originated_contracts;
}
async function invokeContract(address: string, parameter: string, entrypoint: string = '') {
console.log(`~~ invokeContract`);
const fee = Number((await TezosConseilClient.getFeeStatistics(conseilServer, conseilServer.network, OperationKindType.Transaction))[0]['high']);
let storageResult = await TezosNodeReader.getContractStorage(tezosNode, address);
console.log(`initial storage: ${JSON.stringify(storageResult)}`);
const { gas, storageCost: freight } = await TezosNodeWriter.testContractInvocationOperation(tezosNode, 'main', keystore, address, 10000, fee, 1000, 100000, entrypoint, parameter, TezosParameterFormat.Michelson);
console.log(`cost: ${JSON.stringify(await TezosNodeWriter.testContractInvocationOperation(tezosNode, 'main', keystore, address, 10000, fee, 1000, 100000, entrypoint, parameter, TezosParameterFormat.Michelson))}`)
const nodeResult = await TezosNodeWriter.sendContractInvocationOperation(tezosNode, keystore, address, 10000, fee, '', freight, gas, entrypoint, parameter, TezosParameterFormat.Michelson);
const groupid = clearRPCOperationGroupHash(nodeResult.operationGroupID);
console.log(`Injected transaction(invocation) operation with ${groupid}`);
const conseilResult = await TezosConseilClient.awaitOperationConfirmation(conseilServer, conseilServer.network, groupid, 5, networkBlockTime);
console.log(`Completed invocation of ${conseilResult.destination}`);
storageResult = await TezosNodeReader.getContractStorage(tezosNode, address);
console.log(`modified storage: ${JSON.stringify(storageResult)}`);
console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
}
async function run() {
keystore = await initAccount(); // TODO: read/write settings
await activateAccount();
await revealAccount();
contractAddress = await deployMichelsonContract();
await invokeContract(contractAddress, '"Goodbye"');
}
run();
{
"name": "twenty-minute-tour",
"version": "0.0.1",
"description": "Cryptonomic 20-minute Tezos Tour",
"main": "index.ts",
"scripts": {
"postinstall": "npm run build",
"build": "tsc index.ts",
"start": "node index.js"
},
"author": "anonymoussprocket",
"license": "UNLICENSED",
"engines": {
"node": ">=12.14.1",
"npm": ">=6.13.4"
},
"homepage": "https://gist.github.com/anonymoussprocket",
"dependencies": {
"conseiljs": "^0.4.1-beta.1",
"glob": "^7.1.6"
},
"devDependencies": {
"typescript": "3.8.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.