From 8e762f5b417cd1736c7263e5d39485ab692ca26f Mon Sep 17 00:00:00 2001 From: Nayyir Jutha Date: Wed, 16 Oct 2024 12:52:03 -0400 Subject: [PATCH 1/2] udpated tests, wrote new test to test failed route for register but running into an issue with tape possibly, commented test out for now --- src/faucet/interaction.ts | 6 ++- src/faucet/utils.ts | 1 + tests/faucet.test.ts | 93 ++++++++++++++++++++++++++++++--------- 3 files changed, 79 insertions(+), 21 deletions(-) diff --git a/src/faucet/interaction.ts b/src/faucet/interaction.ts index 54cc647b..fbd2a610 100644 --- a/src/faucet/interaction.ts +++ b/src/faucet/interaction.ts @@ -5,7 +5,10 @@ import { EntropyFaucet } from "./main" import { print } from "src/common/utils" let chosenVerifyingKeys = [] -const amount = "10000000000" +// Sending only 1e10 BITS does not allow user's to register after receiving funds +// there are limits in place to ensure user's are leftover with a certain balance in their accounts +// increasing amount send here, will allow user's to register right away +const amount = "20000000000" // context for logging file const FLOW_CONTEXT = 'ENTROPY_FAUCET_INTERACTION' export async function entropyFaucet (entropy: Entropy, options, logger: EntropyLogger) { @@ -35,6 +38,7 @@ async function sendMoneyFromRandomFaucet (entropy: Entropy, endpoint: string, ve chosenVerifyingKeys.push(chosenVerifyingKey) if (error.message.includes('FaucetError') || chosenVerifyingKeys.length === verifyingKeys.length) { console.error('ERR::', error.message) + chosenVerifyingKeys = [] return } else { // Check for non faucet errors (FaucetError) and retry faucet diff --git a/src/faucet/utils.ts b/src/faucet/utils.ts index 0630208a..f9c4f868 100644 --- a/src/faucet/utils.ts +++ b/src/faucet/utils.ts @@ -7,4 +7,5 @@ export const FAUCET_PROGRAM_MOD_KEY = '5GWamxgW4XWcwGsrUynqnFq2oNZPqNXQhMDfgNH9x // this is differnt from tests because the fauce that is live now was lazily deployed without schemas // TO-DO: update this when faucet is deployed properly export const TESTNET_PROGRAM_HASH = '0x12af0bd1f2d91f12e34aeb07ea622c315dbc3c2bdc1e25ff98c23f1e61106c77' +// Hash with max send of 1e10 export const LOCAL_PROGRAM_HASH = '0x5fa0536818acaa380b0c349c8e887bf269d593a47e30c8e31de53a75d327f7b1' \ No newline at end of file diff --git a/tests/faucet.test.ts b/tests/faucet.test.ts index 3a151fe8..44f895c3 100644 --- a/tests/faucet.test.ts +++ b/tests/faucet.test.ts @@ -8,21 +8,18 @@ import { EntropyFaucet } from '../src/faucet/main' import { LOCAL_PROGRAM_HASH } from '../src/faucet/utils' import { EntropyAccount } from '../src/account/main' -test('Faucet Tests', async t => { +async function setupAndFundFaucet (t, naynayEntropy) { const { run, entropy, endpoint } = await setupTest(t, { seed: charlieStashSeed }) - const { entropy: naynayEntropy } = await setupTest(t) - - const balanceService = new EntropyBalance(entropy, endpoint) + const accountService = new EntropyAccount(entropy, endpoint) const transferService = new EntropyTransfer(entropy, endpoint) const faucetService = new EntropyFaucet(naynayEntropy, endpoint) - const accountService = new EntropyAccount(entropy, endpoint) const faucetProgram = readFileSync('tests/programs/faucet_program.wasm') const genesisHash = await entropy.substrate.rpc.chain.getBlockHash(0) const userConfig = { - max_transfer_amount: 10_000_000_000, + max_transfer_amount: 20_000_000_000, genesis_hash: stripHexPrefix(genesisHash.toString()) } const configurationSchema = { @@ -41,11 +38,7 @@ test('Faucet Tests', async t => { // Confirm faucetPointer matches deployed program pointer t.equal(faucetProgramPointer, LOCAL_PROGRAM_HASH, 'Program pointer matches') - let entropyBalance = await balanceService.getBalance(entropy.keyring.accounts.registration.address) - console.log('Balance Charlie::', entropyBalance); - - let naynayBalance = await balanceService.getBalance(naynayEntropy.keyring.accounts.registration.address) - t.equal(naynayBalance, 0, 'Naynay is broke af') + // register with faucet program await run('Register Faucet Program for charlie stash', accountService.register( { @@ -57,23 +50,31 @@ test('Faucet Tests', async t => { // @ts-expect-error const { chosenVerifyingKey, faucetAddress } = faucetService.getRandomFaucet([], verifyingKeys) // adding funds to faucet address - entropyBalance = await balanceService.getBalance(entropy.keyring.accounts.registration.address) - const faucetAddressBalance = await balanceService.getBalance(faucetAddress) - console.log('Balance faucetAddress::', faucetAddressBalance); - console.log('Balance charlie 2::', entropyBalance); - - await run('Transfer funds to faucet address', transferService.transfer(faucetAddress, "1000")) - const transferStatus = await faucetService.sendMoney( + return { faucetProgramPointer, chosenVerifyingKey, faucetAddress } +} + +test('Faucet Tests: Successfully send funds and register', async t => { + const { run, endpoint, entropy: naynayEntropy } = await setupTest(t) + + const faucetService = new EntropyFaucet(naynayEntropy, endpoint) + const balanceService = new EntropyBalance(naynayEntropy, endpoint) + + const { faucetAddress, chosenVerifyingKey, faucetProgramPointer } = await setupAndFundFaucet(t, naynayEntropy) + + let naynayBalance = await balanceService.getBalance(naynayEntropy.keyring.accounts.registration.address) + t.equal(naynayBalance, 0, 'Naynay is broke af') + + const transferStatus = await run('Sending faucet funds to account', faucetService.sendMoney( { - amount: "10000000000", + amount: "20000000000", addressToSendTo: naynayEntropy.keyring.accounts.registration.address, faucetAddress, chosenVerifyingKey, faucetProgramPointer } - ) + )) t.ok(transferStatus.isFinalized, 'Transfer is good') @@ -81,5 +82,57 @@ test('Faucet Tests', async t => { t.ok(naynayBalance > 0, 'Naynay is drippin in faucet tokens') + // Test if user can register after receiving funds + const naynayAccountService = new EntropyAccount(naynayEntropy, endpoint) + const verifyingKey = await run('register account', naynayAccountService.register()) + + t.ok(!!verifyingKey, 'Verifying key exists and is returned from register method') + + const fullAccount = naynayEntropy.keyring.getAccount() + t.equal(verifyingKey, fullAccount?.registration?.verifyingKeys?.[0], 'verifying key matches key added to registration account') + t.end() }) + +// TODO: @naynay fix below test for register failing when only sending 1e10 bits +// test('Faucet Tests: Successfully send funds but cannot register', async t => { +// const { run, endpoint, entropy: naynayEntropy } = await setupTest(t) + +// const faucetService = new EntropyFaucet(naynayEntropy, endpoint) +// const balanceService = new EntropyBalance(naynayEntropy, endpoint) + +// const { faucetAddress, chosenVerifyingKey, faucetProgramPointer } = await setupAndFundFaucet(t, naynayEntropy) + +// let naynayBalance = await balanceService.getBalance(naynayEntropy.keyring.accounts.registration.address) +// t.equal(naynayBalance, 0, 'Naynay is broke af') + +// const transferStatus = await run('Sending faucet funds to account', faucetService.sendMoney( +// { +// amount: "10000000000", +// addressToSendTo: naynayEntropy.keyring.accounts.registration.address, +// faucetAddress, +// chosenVerifyingKey, +// faucetProgramPointer +// } +// )) + +// t.ok(transferStatus.isFinalized, 'Transfer is good') + +// naynayBalance = await balanceService.getBalance(naynayEntropy.keyring.accounts.registration.address) + +// t.ok(naynayBalance > 0, 'Naynay is drippin in faucet tokens') + +// // Test if user can register after receiving funds +// const naynayAccountService = new EntropyAccount(naynayEntropy, endpoint) +// try { +// const verifyingKey = await naynayAccountService.register() +// console.log('ver key', verifyingKey); + +// // t.fail('Register should fail') +// } catch (error) { +// console.log('error', error); + +// t.pass('Regsitration failed') +// t.end() +// } +// }) \ No newline at end of file From 24ee4f8e40811333728d68445c72634d3d2bab2b Mon Sep 17 00:00:00 2001 From: Nayyir Jutha Date: Thu, 17 Oct 2024 11:28:08 -0400 Subject: [PATCH 2/2] removing unnecessary reset --- src/faucet/interaction.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/faucet/interaction.ts b/src/faucet/interaction.ts index fbd2a610..6c98bca4 100644 --- a/src/faucet/interaction.ts +++ b/src/faucet/interaction.ts @@ -38,7 +38,6 @@ async function sendMoneyFromRandomFaucet (entropy: Entropy, endpoint: string, ve chosenVerifyingKeys.push(chosenVerifyingKey) if (error.message.includes('FaucetError') || chosenVerifyingKeys.length === verifyingKeys.length) { console.error('ERR::', error.message) - chosenVerifyingKeys = [] return } else { // Check for non faucet errors (FaucetError) and retry faucet