Skip to content

Commit

Permalink
Automatically register account if needed when locking (#210)
Browse files Browse the repository at this point in the history
* implement 142 -- simplify the process of voting by registering eoa as account.

Given that account is often used a synonym for eoa it was not clear from error messages that one must run account:register. this was it happens automatically
  • Loading branch information
aaronmgdr authored Apr 2, 2024
1 parent 47e661d commit 5f304f0
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/early-geckos-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@celo/celocli': minor
---

feat: Running celocli lockedgold:lock will now automatically register the eoa as an account if needed before locking
76 changes: 75 additions & 1 deletion packages/cli/src/commands/lockedgold/lock.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { newKitFromWeb3 } from '@celo/contractkit'
import { testWithGanache } from '@celo/dev-utils/lib/ganache-test'
import { ux } from '@oclif/core'
import BigNumber from 'bignumber.js'
import Web3 from 'web3'
import { LONG_TIMEOUT_MS, testLocally } from '../../test-utils/cliUtils'
import {
LONG_TIMEOUT_MS,
stripAnsiCodesFromNestedArray,
testLocally,
} from '../../test-utils/cliUtils'
import Register from '../account/register'
import Lock from './lock'
import Unlock from './unlock'
Expand All @@ -27,4 +33,72 @@ testWithGanache('lockedgold:lock cmd', (web3: Web3) => {
},
LONG_TIMEOUT_MS
)
describe('when EOA is not yet an account', () => {
it('performs the registration and locks the value', async () => {
const eoaAddresses = await web3.eth.getAccounts()
const eoa = eoaAddresses[1]
const kit = newKitFromWeb3(web3)
const accountsContract = await kit.contracts.getAccounts()
const lockedGoldContract = await kit.contracts.getLockedGold()

const logSpy = jest.spyOn(console, 'log')
const actionSpy = jest.spyOn(ux.action, 'stop')

// pre check
expect(await accountsContract.isAccount(eoa)).toBe(false)

await testLocally(Lock, ['--from', eoa, '--value', '100'])

expect(stripAnsiCodesFromNestedArray(logSpy.mock.calls)).toMatchInlineSnapshot(`
[
[
"Running Checks:",
],
[
" ✔ Value [100] is > 0 ",
],
[
"All checks passed",
],
[
"Address will be registered with Account contract to enable locking.",
],
[
"SendTransaction: register",
],
[
"txHash: 0x9322aba7cf28f466f1377b1da9a1e7ed94c3109aa5fd2f4ea23caab371f1cb0f",
],
[
"Running Checks:",
],
[
" ✔ Account has at least 0.0000000000000001 CELO ",
],
[
"All checks passed",
],
[
"SendTransaction: lock",
],
[
"txHash: 0x947e5f8c97fdfabf688b3879f5856e1165c3578f2741d2481c3c961aa83bba51",
],
]
`)

expect(actionSpy.mock.calls).toMatchInlineSnapshot(`
[
[],
[],
]
`)

expect(await accountsContract.isAccount(eoa)).toBe(true)

expect(await lockedGoldContract.getAccountTotalLockedGold(eoa)).toEqBigNumber(
new BigNumber('100')
)
})
})
})
14 changes: 12 additions & 2 deletions packages/cli/src/commands/lockedgold/lock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,20 @@ export default class Lock extends BaseCommand {

await newCheckBuilder(this)
.addCheck(`Value [${value.toFixed()}] is > 0`, () => value.gt(0))
.isAccount(address)
.runChecks()

const lockedGold = await kit.contracts.getLockedGold()
const [lockedGold, accountsContract] = await Promise.all([
kit.contracts.getLockedGold(),
kit.contracts.getAccounts(),
])

const isAccount = await accountsContract.isAccount(address)

if (!isAccount) {
console.log('Address will be registered with Account contract to enable locking.')
await displaySendTx('register', accountsContract.createAccount())
}

const pendingWithdrawalsValue = await lockedGold.getPendingWithdrawalsTotalValue(address)
const relockValue = BigNumber.minimum(pendingWithdrawalsValue, value)
const lockValue = value.minus(relockValue)
Expand Down

0 comments on commit 5f304f0

Please sign in to comment.