Skip to content

Commit

Permalink
[NayNay] File Restructure: Faucet
Browse files Browse the repository at this point in the history
- restructured faucet to match the new structure
  • Loading branch information
rh0delta committed Sep 16, 2024
1 parent 16eb051 commit 8c80fa4
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 105 deletions.
2 changes: 2 additions & 0 deletions src/common/entropy-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { EntropyLogger } from "./logger";
export abstract class EntropyBase {
protected logger: EntropyLogger
protected entropy: Entropy
protected endpoint: string

constructor (entropy: Entropy, endpoint: string, flowContext: string) {
this.logger = new EntropyLogger(flowContext, endpoint)
this.entropy = entropy
this.endpoint = endpoint
}
}
Empty file added src/faucet/command.ts
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import Entropy from "@entropyxyz/sdk";
import type { Signer, SignerResult } from "@polkadot/api/types";
import { Registry, SignerPayloadJSON } from "@polkadot/types/types";
import { u8aToHex } from "@polkadot/util";
import { stripHexPrefix } from "../../common/utils";
import { blake2AsHex, decodeAddress, encodeAddress, signatureVerify } from "@polkadot/util-crypto";
import { stripHexPrefix } from "../../common/utils";

let id = 0
export default class FaucetSigner implements Signer {
Expand Down
14 changes: 7 additions & 7 deletions src/flows/entropyFaucet/index.ts → src/faucet/interaction.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Entropy from "@entropyxyz/sdk"
import { getSelectedAccount, print } from "../../common/utils"
import { initializeEntropy } from "../../common/initializeEntropy"
import { EntropyLogger } from '../../common/logger'
import { getRandomFaucet, sendMoney } from "./faucet"
import { TESTNET_PROGRAM_HASH } from "./constants"
import { getSelectedAccount, print } from "../common/utils"
import { initializeEntropy } from "../common/initializeEntropy"
import { EntropyLogger } from '../common/logger'
import { TESTNET_PROGRAM_HASH } from "./utils"
import { EntropyFaucet } from "./main"

let chosenVerifyingKeys = []
export async function entropyFaucet ({ accounts, selectedAccount: selectedAccountAddress }, options, logger: EntropyLogger) {
Expand All @@ -20,12 +20,12 @@ export async function entropyFaucet ({ accounts, selectedAccount: selectedAccoun
try {
// @ts-ignore (see TODO on aliceAccount)
entropy = await initializeEntropy({ keyMaterial: selectedAccount.data, endpoint })

const FaucetService = new EntropyFaucet(entropy, options.endpoint)
if (!entropy.registrationManager.signer.pair) {
throw new Error("Keys are undefined")
}

({ chosenVerifyingKey, faucetAddress, verifyingKeys } = await getRandomFaucet(entropy, chosenVerifyingKeys))
({ chosenVerifyingKey, faucetAddress, verifyingKeys } = await FaucetService.getRandomFaucet(chosenVerifyingKeys))

await sendMoney(entropy, options.endpoint, { amount, addressToSendTo: selectedAccountAddress, faucetAddress, chosenVerifyingKey, faucetProgramPointer: TESTNET_PROGRAM_HASH })
// reset chosen keys after successful transfer
Expand Down
100 changes: 100 additions & 0 deletions src/faucet/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import Entropy from "@entropyxyz/sdk";
import { EntropyBase } from "../common/entropy-base";
import { blake2AsHex, encodeAddress } from "@polkadot/util-crypto";
import { FAUCET_PROGRAM_MOD_KEY, TESTNET_PROGRAM_HASH } from "./utils";
import { EntropyBalance } from "src/balance/main";
import { viewPrograms } from "src/flows/programs/view";
import FaucetSigner from "./helpers/signer";

const FLOW_CONTEXT = 'ENTROPY-FAUCET'

export class EntropyFaucet extends EntropyBase {
constructor(entropy: Entropy, endpoint: string) {
super(entropy, endpoint, FLOW_CONTEXT)
}

async faucetSignAndSend (call: any, amount: number, senderAddress: string, chosenVerifyingKey: any): Promise<any> {
const api = this.entropy.substrate
const faucetSigner = new FaucetSigner(api.registry, this.entropy, amount, chosenVerifyingKey)

const sig = await call.signAsync(senderAddress, {
signer: faucetSigner,
});
return new Promise((resolve, reject) => {
sig.send(({ status, dispatchError }: any) => {
// status would still be set, but in the case of error we can shortcut
// to just check it (so an error would indicate InBlock or Finalized)
if (dispatchError) {
let msg: string
if (dispatchError.isModule) {
// for module errors, we have the section indexed, lookup
const decoded = api.registry.findMetaError(dispatchError.asModule);
// @ts-ignore
const { documentation, method, section } = decoded;

msg = `${section}.${method}: ${documentation.join(' ')}`
} else {
// Other, CannotLookup, BadOrigin, no extra info
msg = dispatchError.toString()
}
return reject(Error(msg))
}
if (status.isFinalized) resolve(status)
})
})
}

async getRandomFaucet (previousVerifyingKeys: string[] = [], programModKey = FAUCET_PROGRAM_MOD_KEY) {
const modifiableKeys = await this.entropy.substrate.query.registry.modifiableKeys(programModKey)
const verifyingKeys = JSON.parse(JSON.stringify(modifiableKeys.toJSON()))

// Choosing one of the 5 verifiying keys at random to be used as the faucet sender
if (verifyingKeys.length === previousVerifyingKeys.length) {
throw new Error('FaucetError: There are no more faucets to choose from')
}
let chosenVerifyingKey = verifyingKeys[Math.floor(Math.random() * verifyingKeys.length)]
if (previousVerifyingKeys.length && previousVerifyingKeys.includes(chosenVerifyingKey)) {
const filteredVerifyingKeys = verifyingKeys.filter((key: string) => !previousVerifyingKeys.includes(key))
chosenVerifyingKey = filteredVerifyingKeys[Math.floor(Math.random() * filteredVerifyingKeys.length)]
}
const hashedKey = blake2AsHex(chosenVerifyingKey)
const faucetAddress = encodeAddress(hashedKey, 42).toString()

return { chosenVerifyingKey, faucetAddress, verifyingKeys }
}

async sendMoney (
{
amount,
addressToSendTo,
faucetAddress,
chosenVerifyingKey,
faucetProgramPointer = TESTNET_PROGRAM_HASH
}: {
amount: string,
addressToSendTo: string,
faucetAddress: string,
chosenVerifyingKey: string,
faucetProgramPointer: string
}
): Promise<any> {
const BalanceService = new EntropyBalance(this.entropy, this.endpoint)
// check balance of faucet address
const balance = await BalanceService.getBalance(faucetAddress)
if (balance <= 0) throw new Error('FundsError: Faucet Account does not have funds')
// check verifying key for only one program matching the program hash
const programs = await viewPrograms(this.entropy, { verifyingKey: chosenVerifyingKey })
if (programs.length) {
if (programs.length > 1) throw new Error('ProgramsError: Faucet Account has too many programs attached, expected less')
if (programs.length === 1 && programs[0].program_pointer !== faucetProgramPointer) {
throw new Error('ProgramsError: Faucet Account does not possess Faucet program')
}
} else {
throw new Error('ProgramsError: Faucet Account has no programs attached')
}

const transfer = this.entropy.substrate.tx.balances.transferAllowDeath(addressToSendTo, BigInt(amount));
const transferStatus = await this.faucetSignAndSend(transfer, parseInt(amount), faucetAddress, chosenVerifyingKey)
if (transferStatus.isFinalized) return transferStatus
}
}
Empty file added src/faucet/types.ts
Empty file.
File renamed without changes.
96 changes: 0 additions & 96 deletions src/flows/entropyFaucet/faucet.ts

This file was deleted.

2 changes: 1 addition & 1 deletion tests/faucet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ test('Faucet Tests', async t => {
const { chosenVerifyingKey, faucetAddress } = await getRandomFaucet(entropy, [], entropy.keyring.accounts.registration.address)
// adding funds to faucet address

await run('Transfer funds to faucet address', TransferService.transfer(faucetAddress, "100000000000000"))
await run('Transfer funds to faucet address', TransferService.transfer(faucetAddress, "1000"))

const transferStatus = await sendMoney(
naynayEntropy,
Expand Down

0 comments on commit 8c80fa4

Please sign in to comment.