How to use the @celo/contractkit.newKit function in @celo/contractkit

To help you get started, we’ve selected a few @celo/contractkit examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github celo-org / celo-monorepo / packages / celotool / src / e2e-tests / blockchain_parameters_tests.ts View on Github external
const restartGeth = async () => {
    // Restart the validator node
    await hooks.restart()

    // TODO(mcortesi): magic sleep. without it unlockAccount sometimes fails
    await sleep(2)
    kit = newKit('http://localhost:8545')
    await kit.web3.eth.personal.unlockAccount(validatorAddress, '', 1000)
    parameters = await kit.contracts.getBlockchainParameters()
  }
github celo-org / celo-monorepo / packages / attestation-service / src / db.ts View on Github external
export async function initializeKit() {
  if (kit === undefined) {
    kit = newKit(fetchEnv('CELO_PROVIDER'))
    // Copied from @celo/cli/src/utils/helpers
    try {
      const syncProgress = await kit.web3.eth.isSyncing()
      if (typeof syncProgress === 'boolean' && !syncProgress) {
        const latestBlock: Block = await kit.web3.eth.getBlock('latest')
        if (latestBlock && latestBlock.number > 0) {
          // To catch the case in which syncing has happened in the past,
          // has stopped, and hasn't started again, check for an old timestamp
          // on the latest block
          const ageOfBlock = Date.now() / 1000 - latestBlock.timestamp
          if (ageOfBlock > 120) {
            throw new Error(
              `Latest block is ${ageOfBlock} seconds old, and syncing is not currently in progress`
            )
          }
        }
github celo-org / celo-monorepo / packages / celotool / src / cmds / bots / auto-verify.ts View on Github external
export const handler = async function autoVerify(argv: AutoVerifyArgv) {
  let logger: Logger = createLogger({
    name: 'attestation-bot',
    serializers: stdSerializers,
    streams: [createStream(Level.INFO)],
  })
  try {
    const kit = newKit(argv.celoProvider)
    const mnemonic = fetchEnv(envVar.MNEMONIC)
    // This really should be the ATTESTATION_BOT key, but somehow we can't get it to have cUSD
    const clientKey = ensure0x(
      generatePrivateKey(mnemonic, AccountType.ATTESTATION_BOT, argv.index)
    )
    const clientAddress = privateKeyToAddress(clientKey)
    logger = logger.child({ address: clientAddress })
    kit.addAccount(clientKey)

    const twilioClient = twilio(
      fetchEnv(envVar.TWILIO_ACCOUNT_SID),
      fetchEnv(envVar.TWILIO_ACCOUNT_AUTH_TOKEN)
    )

    const attestations = await kit.contracts.getAttestations()
    const stableToken = await kit.contracts.getStableToken()
github celo-org / celo-monorepo / packages / celotool / src / lib / geth.ts View on Github external
export const simulateClient = async (
  senderAddress: string,
  recipientAddress: string,
  txPeriodMs: number, // time between new transactions in ms
  blockscoutUrl: string,
  blockscoutMeasurePercent: number, // percent of time in range [0, 100] to measure blockscout for a tx
  index: number
) => {
  // Assume the node is accessible via localhost with senderAddress unlocked
  const kit = newKit('http://localhost:8545')
  kit.defaultAccount = senderAddress

  const baseLogMessage: any = {
    loadTestID: index,
    sender: senderAddress,
    recipient: recipientAddress,
    feeCurrency: '',
    txHash: '',
  }

  while (true) {
    const sendTransactionTime = Date.now()

    // randomly choose which token to use
    const transferGold = Boolean(Math.round(Math.random()))
    const transferFn = transferGold ? transferCeloGold : transferCeloDollars
github celo-org / celo-monorepo / packages / celotool / src / e2e-tests / transfer_tests.ts View on Github external
constructor(readonly validatorUri: string, readonly validatorAddress: string) {
    this.kit = newKit(validatorUri)
    this.kit.defaultAccount = validatorAddress
  }
github celo-org / celo-monorepo / packages / celotool / src / cmds / bots / voting.ts View on Github external
export const handler = async function simulateVoting(argv: SimulateVotingArgv) {
  try {
    const mnemonic = fetchEnv(envVar.MNEMONIC)
    const numBotAccounts = parseInt(fetchEnv(envVar.VOTING_BOTS), 10)

    const kit: ContractKit = newKit(argv.celoProvider)
    const election = await kit.contracts.getElection()
    const validators = await kit.contracts.getValidators()

    const changeVoteProbability = new BigNumber(fetchEnv(envVar.VOTING_BOT_CHANGE_PROBABILITY))

    const allBotKeys = getPrivateKeysFor(AccountType.VOTING_BOT, mnemonic, numBotAccounts)
    for (const key of allBotKeys) {
      kit.addAccount(key)
      const account = ensure0x(getAccountAddressFromPrivateKey(key))
      try {
        const activateTxs = await election.activate(account)
        for (const tx of activateTxs) {
          await tx.sendAndWaitForReceipt()
        }
      } catch (error) {
        console.error(`Failed to activate pending votes for ${account}`)
github celo-org / celo-monorepo / packages / celotool / src / cmds / account / lookup.ts View on Github external
const cb = async () => {
    const kit = newKit('http://localhost:8545')
    const phoneHash = PhoneNumberUtils.getPhoneHash(argv.phone)
    const attestations = await kit.contracts.getAttestations()
    const lookupResult = await attestations.lookupPhoneNumbers([phoneHash])

    const matchingAddresses = lookupResult[phoneHash]

    if (matchingAddresses === undefined) {
      console.info(`No addresses attested to ${argv.phone}`)
      return
    }

    Object.keys(matchingAddresses).map((address) => {
      const attestationsStats = matchingAddresses[address]
      console.info(
        `${address} is attested to ${argv.phone} with ${
          attestationsStats.completed
github celo-org / celo-monorepo / packages / transaction-metrics-exporter / src / blockchain.ts View on Github external
export async function metricExporterWithRestart(providerUrl: string) {
  try {
    console.log('MetricExporter: Start')
    let kit = newKit(providerUrl)
    while (true) {
      console.log('MetricExporter: Run Start')
      const reason = await runMetricExporter(kit)

      if (reason.reason === 'not-listening') {
        console.error('MetricExporter: Web3 Not listening... retrying')
        const maybeKit = await newListeningKit(providerUrl)
        if (maybeKit != null) {
          kit = maybeKit
        } else {
          console.error('MetricExporter: Retry failed. Exiting')
        }
      } else {
        console.error('MetricExporter: Error %s', reason.reason)
        console.error(reason.error)
        process.exit(1)
github celo-org / celo-monorepo / packages / celotool / src / cmds / deploy / initial / contracts.ts View on Github external
export async function registerMetadata(testnet: string, privateKey: string, index: number) {
  const address = privateKeyToAddress(privateKey)
  await makeMetadata(testnet, address, index)

  const kit = newKit('http://localhost:8545')
  kit.addAccount(privateKey)
  kit.defaultAccount = address

  const attestations = await kit.contracts.getAttestations()
  return attestations
    .setMetadataURL(metadataURLForCLabsValidator(testnet, address))
    .sendAndWaitForReceipt()
}