From a863b5c9e922afb5a4e558e1ff62d66db8211600 Mon Sep 17 00:00:00 2001 From: Daniel Swid Date: Mon, 1 Aug 2022 05:54:48 +0300 Subject: [PATCH] Add payment delegation unit tests, generate docs * upstream pr https://github.com/celo-org/celo-monorepo/pull/9700 * test for invalid payment delegation fraction greater than 1 * test that fetched delegation values are equal to values set * test that deleting delegation clears values * fix up cli example help * generate docs for account.md [finishes #182759476] [Test] y [Review] m-chrzan add docs --- .../account/set-payment-delegation.ts | 2 +- .../docs/command-line-interface/account.md | 88 ++++++++++++++++++- .../contractkit/src/wrappers/Accounts.test.ts | 50 +++++++++++ 3 files changed, 138 insertions(+), 2 deletions(-) diff --git a/packages/cli/src/commands/account/set-payment-delegation.ts b/packages/cli/src/commands/account/set-payment-delegation.ts index c2f12856517..f0ba062bfa7 100644 --- a/packages/cli/src/commands/account/set-payment-delegation.ts +++ b/packages/cli/src/commands/account/set-payment-delegation.ts @@ -19,7 +19,7 @@ export default class SetPaymentDelegation extends BaseCommand { static args = [] static examples = [ - 'set-payment-delegation --account 0x5409ed021d9299bf6814279a6a1411a7e866a631 --beneficiary 0x5409ed021d9299bf6814279a6a1411a7e866a631 --fraction 0.1', + 'set-payment-delegation --account 0x5409ed021d9299bf6814279a6a1411a7e866a631 --beneficiary 0x6Ecbe1DB9EF729CBe972C83Fb886247691Fb6beb --fraction 0.1', ] async run() { diff --git a/packages/docs/command-line-interface/account.md b/packages/docs/command-line-interface/account.md index bf96c839100..8947397ce4e 100644 --- a/packages/docs/command-line-interface/account.md +++ b/packages/docs/command-line-interface/account.md @@ -2,7 +2,6 @@ Manage your account, keys, and metadata - ## `celocli account:authorize` Keep your locked Gold more secure by authorizing alternative keys to be used for signing attestations, voting, or validating. By doing so, you can continue to participate in the protocol while keeping the key with access to your locked Gold in cold storage. You must include a "proof-of-possession" of the key being authorized, which can be generated with the "account:proof-of-possession" command. @@ -324,6 +323,26 @@ EXAMPLE _See code: [src/commands/account/deauthorize.ts](https://github.com/celo-org/celo-monorepo/tree/master/packages/cli/src/commands/account/deauthorize.ts)_ +## `celocli account:delete-payment-delegation` + +Removes a validator's payment delegation by setting benficiary and fraction to 0. + +``` +Removes a validator's payment delegation by setting benficiary and fraction to 0. + +USAGE + $ celocli account:delete-payment-delegation + +OPTIONS + --account=0xc1912fEE45d61C87Cc5EA59DaE31190FFFFf232d (required) Account Address + --globalHelp View all available global flags + +EXAMPLE + delete-payment-delegation --account 0x5409ED021D9299bf6814279A6A1411A7e866A631 +``` + +_See code: [src/commands/account/delete-payment-delegation.ts](https://github.com/celo-org/celo-monorepo/tree/master/packages/cli/src/commands/account/delete-payment-delegation.ts)_ + ## `celocli account:get-metadata ADDRESS` Show information about an address. Retreives the metadata URL for an account from the on-chain, then fetches the metadata file off-chain and verifies proofs as able. @@ -354,6 +373,48 @@ EXAMPLE _See code: [src/commands/account/get-metadata.ts](https://github.com/celo-org/celo-monorepo/tree/master/packages/cli/src/commands/account/get-metadata.ts)_ +## `celocli account:get-payment-delegation` + +Get the payment delegation account beneficiary and fraction allocated from a validator's payment each epoch. The fraction cannot be greater than 1. + +``` +Get the payment delegation account beneficiary and fraction allocated from a validator's payment each epoch. The fraction cannot be greater than 1. + +USAGE + $ celocli account:get-payment-delegation + +OPTIONS + -x, --extended show extra columns + --account=0xc1912fEE45d61C87Cc5EA59DaE31190FFFFf232d (required) Account Address + + --columns=columns only show provided columns + (comma-separated) + + --csv output is csv format [alias: + --output=csv] + + --filter=filter filter property by partial + string matching, ex: name=foo + + --globalHelp View all available global flags + + --no-header hide table header from output + + --no-truncate do not truncate output to fit + screen + + --output=csv|json|yaml output in a more machine + friendly format + + --sort=sort property to sort by (prepend '-' + for descending) + +EXAMPLE + get-payment-delegation --account 0x5409ed021d9299bf6814279a6a1411a7e866a631 +``` + +_See code: [src/commands/account/get-payment-delegation.ts](https://github.com/celo-org/celo-monorepo/tree/master/packages/cli/src/commands/account/get-payment-delegation.ts)_ + ## `celocli account:list` List the addresses from the node and the local instance @@ -729,6 +790,31 @@ EXAMPLE _See code: [src/commands/account/set-name.ts](https://github.com/celo-org/celo-monorepo/tree/master/packages/cli/src/commands/account/set-name.ts)_ +## `celocli account:set-payment-delegation` + +Sets a payment delegation beneficiary, an account address to receive a fraction of the validator's payment every epoch. The fraction must not be greater than 1. + +``` +Sets a payment delegation beneficiary, an account address to receive a fraction of the validator's payment every epoch. The fraction must not be greater than 1. + +USAGE + $ celocli account:set-payment-delegation + +OPTIONS + --account=0xc1912fEE45d61C87Cc5EA59DaE31190FFFFf232d (required) Account Address + --beneficiary=0xc1912fEE45d61C87Cc5EA59DaE31190FFFFf232d (required) Account Address + --fraction=fraction (required) + + --globalHelp View all available global + flags + +EXAMPLE + set-payment-delegation --account 0x5409ed021d9299bf6814279a6a1411a7e866a631 + --beneficiary 0x5409ed021d9299bf6814279a6a1411a7e866a631 --fraction 0.1 +``` + +_See code: [src/commands/account/set-payment-delegation.ts](https://github.com/celo-org/celo-monorepo/tree/master/packages/cli/src/commands/account/set-payment-delegation.ts)_ + ## `celocli account:set-wallet` Sets the wallet of a registered account on-chain. An account's wallet is an optional wallet associated with an account. Can be set by the account or an account's signer. diff --git a/packages/sdk/contractkit/src/wrappers/Accounts.test.ts b/packages/sdk/contractkit/src/wrappers/Accounts.test.ts index 9bbafad3c86..7a51b13dcd4 100644 --- a/packages/sdk/contractkit/src/wrappers/Accounts.test.ts +++ b/packages/sdk/contractkit/src/wrappers/Accounts.test.ts @@ -3,6 +3,7 @@ import { addressToPublicKey, parseSignature } from '@celo/utils/lib/signatureUti import Web3 from 'web3' import { ContractKit, newKitFromWeb3 } from '../kit' import { AccountsWrapper } from './Accounts' +import { valueToBigNumber, valueToFixidityString } from './BaseWrapper' import { LockedGoldWrapper } from './LockedGold' import { ValidatorsWrapper } from './Validators' @@ -167,4 +168,53 @@ testWithGanache('Accounts Wrapper', (web3) => { await accountsInstance.createAccount().sendAndWaitForReceipt({ from: account }) await expect(accountsInstance.setWalletAddress(wallet)).rejects }) + + test('DELEGATE fraction greater than 1 should fail', async () => { + const account = accounts[0] + const beneficiary = accounts[1] + const fractionInvalid = valueToFixidityString(valueToBigNumber('2.5')) + + kit.defaultAccount = account + + await accountsInstance.createAccount().sendAndWaitForReceipt({ from: account }) + await expect( + accountsInstance.setPaymentDelegation(beneficiary, fractionInvalid).sendAndWaitForReceipt({}) + ).rejects.toEqual( + new Error( + 'Error: VM Exception while processing transaction: revert Fraction must not be greater than 1' + ) + ) + }) + + test('DELEGATE beneficiary and fraction returned from get should equal set value', async () => { + const account = accounts[0] + const beneficiary = accounts[1] + const fractionValid = valueToFixidityString(valueToBigNumber('.25')) + const expectedRetval = { 0: beneficiary, 1: fractionValid } + + kit.defaultAccount = account + + await accountsInstance.createAccount().sendAndWaitForReceipt({ from: account }) + await accountsInstance.setPaymentDelegation(beneficiary, fractionValid).sendAndWaitForReceipt() + + const retval = await accountsInstance.getPaymentDelegation(account) + expect(retval).toEqual(expectedRetval) + }) + + test('DELEGATE delete expected to clear beneficiary and fraction after setting values', async () => { + const account = accounts[0] + const beneficiary = accounts[1] + const fractionValid = valueToFixidityString(valueToBigNumber('.25')) + const expectedRetval = { 0: '0x0000000000000000000000000000000000000000', 1: '0' } + + kit.defaultAccount = account + + await accountsInstance.createAccount().sendAndWaitForReceipt({ from: account }) + await accountsInstance.setPaymentDelegation(beneficiary, fractionValid).sendAndWaitForReceipt() + + await accountsInstance.deletePaymentDelegation().sendAndWaitForReceipt() + + const retval = await accountsInstance.getPaymentDelegation(account) + expect(retval).toEqual(expectedRetval) + }) })