Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mixmix/new sdk rc #263

Merged
merged 7 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 25 additions & 28 deletions tests/account.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'tape'
import { wasmGlobalsReady } from '@entropyxyz/sdk'
import { Entropy, wasmGlobalsReady } from '@entropyxyz/sdk'
// @ts-ignore
import { isValidSubstrateAddress } from '@entropyxyz/sdk/utils'
import { jumpStartNetwork } from '@entropyxyz/sdk/testing'
Expand Down Expand Up @@ -80,60 +80,57 @@ test('Account - import', async t => {
t.end()
})

const networkType = 'four-nodes'
const endpoint = 'ws://127.0.0.1:9944'

async function fundAccount (t, entropy: Entropy) {
const { entropy: charlie } = await setupTest(t, { seed: charlieStashSeed })
const transfer = new EntropyTransfer(charlie, endpoint)

await transfer.transfer(entropy.keyring.accounts.registration.address, "1000")
}


test('Account - Register: Default Program', async (t) => {
const { run, entropy } = await setupTest(t, { networkType, seed: charlieStashSeed })
// Naynay entropy instance required here as @JesseAbram brought to attn that running jumpstart using the charlie seed will result in errors.
// the error found here was: RpcError: 1014: Priority is too low: (4471 vs 4471): The transaction has too low priority to replace another transaction already in the pool.
// which indicated a potential nonce issue, where the solution would be to manually wait for ~50 blocks to make any subsequent calls to the network. However,
// using naynay instance (fresh seed) jump start and the subsequent calls to the network (i.e register), the flow worked without issues.
const { entropy: naynay } = await setupTest(t, { networkType })

const accountService = new EntropyAccount(naynay, endpoint)
const transferService = new EntropyTransfer(entropy, endpoint)

// use of charlie seed is solely to transfer funds to the naynay (fresh seed) instance. these funds will be used for the jumpstart and register call
await run('transferring funds', transferService.transfer(naynay.keyring.accounts.registration.address, "1000"))
const { run, entropy: naynay } = await setupTest(t)
// NOTE: we fund a new account "naynay" because jumpStart has problems with charlie (T_T)
await run('fund naynay', fundAccount(t, naynay))

await run('jump-start network', jumpStartNetwork(naynay))

const verifyingKey = await run('register account', accountService.register())
const account = new EntropyAccount(naynay, endpoint)
const verifyingKey = await run('register account', account.register())

const fullAccount = naynay.keyring.getAccount()

t.equal(verifyingKey, fullAccount?.registration?.verifyingKeys?.[0], 'verifying key matches key added to registration account')

t.end()
})

test('Account - Register: Barebones Program', async t => {
const { run, entropy } = await setupTest(t, { networkType, seed: eveSeed })
const { entropy: naynay } = await setupTest(t, { networkType })
// await run('jump-start network', jumpStartNetwork(entropy))
const { run, entropy: naynay } = await setupTest(t)
await run('fund naynay', fundAccount(t, naynay))
Copy link
Contributor Author

@mixmix mixmix Oct 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I loved the pattern you started extracting some other setup. I noticed that we want a funded account which is just "set up" for us and that extracting it made the tests a whole lot simpler (to my mind any way)

// NOTE: we fund a new account "naynay" because jumpStart has problems with charlie (T_T)

const dummyProgram: any = readFileSync(
new URL('./programs/template_barebones.wasm', import.meta.url)
)

const accountService = new EntropyAccount(naynay, endpoint)
const transferService = new EntropyTransfer(entropy, endpoint)

await run('transferring funds', transferService.transfer(naynay.keyring.accounts.registration.address, "1000"))
await run('jump-start network', jumpStartNetwork(naynay))

const account = new EntropyAccount(naynay, endpoint)
const pointer = await run(
'deploy program',
entropy.programs.dev.deploy(dummyProgram)
naynay.programs.dev.deploy(dummyProgram)
)
const verifyingKey = await run(
'register - using custom params',
accountService.register({
programModAddress: entropy.keyring.accounts.registration.address,
'register account - with custom params',
account.register({
programModAddress: naynay.keyring.accounts.registration.address,
programData: [{ program_pointer: pointer, program_config: '0x' }],
})
)

const fullAccount = naynay.keyring.getAccount()

t.equal(verifyingKey, fullAccount?.registration?.verifyingKeys?.[0], 'verifying key matches key added to registration account')

t.end()
Expand Down
93 changes: 49 additions & 44 deletions tests/faucet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,12 @@ import { EntropyAccount } from '../src/account/main'
async function setupAndFundFaucet (t) {
const { run, entropy, endpoint } = await setupTest(t, { seed: eveSeed })
await run('jump-start network', jumpStartNetwork(entropy))
const accountService = new EntropyAccount(entropy, endpoint)
const transferService = new EntropyTransfer(entropy, endpoint)
const faucetService = new EntropyFaucet(entropy, endpoint)
const account = new EntropyAccount(entropy, endpoint)
const transfer = new EntropyTransfer(entropy, endpoint)
const faucet = new EntropyFaucet(entropy, endpoint)

// Deploy faucet program
const faucetProgram = readFileSync('tests/programs/faucet_program.wasm')

const genesisHash = await entropy.substrate.rpc.chain.getBlockHash(0)

const userConfig = {
max_transfer_amount: 20_000_000_000,
genesis_hash: stripHexPrefix(genesisHash.toString())
}
const configurationSchema = {
max_transfer_amount: "number",
genesis_hash: "string"
Expand All @@ -34,63 +28,74 @@ async function setupAndFundFaucet (t) {
spec_version: "number",
transaction_version: "number",
}

// Deploy faucet program
const faucetProgramPointer = await run('Deploy faucet program', entropy.programs.dev.deploy(faucetProgram, configurationSchema, auxDataSchema))

// Confirm faucetPointer matches deployed program pointer
const faucetProgramPointer = await run(
'Deploy faucet program',
entropy.programs.dev.deploy(faucetProgram, configurationSchema, auxDataSchema)
)
t.equal(faucetProgramPointer, LOCAL_PROGRAM_HASH, 'Program pointer matches')

// register with faucet program
await run('Register Faucet Program for eve', accountService.register(
{
// Register with faucet program
const genesisHash = await entropy.substrate.rpc.chain.getBlockHash(0)
const userConfig = {
max_transfer_amount: 20_000_000_000,
genesis_hash: stripHexPrefix(genesisHash.toString())
}
await run(
'Register Faucet Program for eve',
account.register({
programModAddress: entropy.keyring.accounts.registration.address,
programData: [{ program_pointer: faucetProgramPointer, program_config: userConfig }]
}
))
const verifyingKeys = await faucetService.getAllFaucetVerifyingKeys(entropy.keyring.accounts.registration.address)
programData: [{
program_pointer:
faucetProgramPointer,
program_config: userConfig
}]
})
)

// Fund the Faucet
const verifyingKeys = await faucet.getAllFaucetVerifyingKeys(entropy.keyring.accounts.registration.address)
// @ts-expect-error
const { chosenVerifyingKey, faucetAddress } = faucetService.getRandomFaucet([], verifyingKeys)
// adding funds to faucet address
await run('Transfer funds to faucet address', transferService.transfer(faucetAddress, "1000"))
const { chosenVerifyingKey, faucetAddress } = faucet.getRandomFaucet([], verifyingKeys)
await run('Transfer funds to faucet address', transfer.transfer(faucetAddress, "1000"))

return { faucetProgramPointer, chosenVerifyingKey, faucetAddress }
}

test('Faucet Tests: Successfully send funds and register', async t => {
const { run, endpoint, entropy: naynayEntropy } = await setupTest(t)
const { run, endpoint, entropy: naynay } = await setupTest(t)
const { faucetAddress, chosenVerifyingKey, faucetProgramPointer } = await setupAndFundFaucet(t)

const faucetService = new EntropyFaucet(naynayEntropy, endpoint)
const balanceService = new EntropyBalance(naynayEntropy, endpoint)
const faucet = new EntropyFaucet(naynay, endpoint)
const balance = new EntropyBalance(naynay, endpoint)

const { faucetAddress, chosenVerifyingKey, faucetProgramPointer } = await setupAndFundFaucet(t)

let naynayBalance = await balanceService.getBalance(naynayEntropy.keyring.accounts.registration.address)
let naynayBalance = await balance.getBalance(naynay.keyring.accounts.registration.address)
t.equal(naynayBalance, 0, 'Naynay is broke af')

const transferStatus = await run('Sending faucet funds to account', faucetService.sendMoney(
{
amount: "20000000000",
addressToSendTo: naynayEntropy.keyring.accounts.registration.address,

const amount = 20000000000
const transferStatus = await run(
'Sending faucet funds to account',
faucet.sendMoney({
amount: `${amount}`,
addressToSendTo: naynay.keyring.accounts.registration.address,
faucetAddress,
chosenVerifyingKey,
faucetProgramPointer
}
))
})
)

t.ok(transferStatus.isFinalized, 'Transfer is good')

naynayBalance = await balanceService.getBalance(naynayEntropy.keyring.accounts.registration.address)
naynayBalance = await balance.getBalance(naynay.keyring.accounts.registration.address)

t.ok(naynayBalance > 0, 'Naynay is drippin in faucet tokens')
t.equal(naynayBalance, 20000000000, 'Naynay is drippin in faucet tokens')
mixmix marked this conversation as resolved.
Show resolved Hide resolved

// Test if user can register after receiving funds
const naynayAccountService = new EntropyAccount(naynayEntropy, endpoint)
const verifyingKey = await run('register account', naynayAccountService.register())
const account = new EntropyAccount(naynay, endpoint)
const verifyingKey = await run('register account', account.register())

t.ok(!!verifyingKey, 'Verifying key exists and is returned from register method')

const fullAccount = naynayEntropy.keyring.getAccount()
const fullAccount = naynay.keyring.getAccount()
t.equal(verifyingKey, fullAccount?.registration?.verifyingKeys?.[0], 'verifying key matches key added to registration account')

t.end()
Expand Down Expand Up @@ -137,4 +142,4 @@ test('Faucet Tests: Successfully send funds and register', async t => {
// t.pass('Regsitration failed')
// t.end()
// }
// })
// })
Loading