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

Add Payment Delegation Commands to CLI #9700

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
29 changes: 29 additions & 0 deletions packages/cli/src/commands/account/delete-payment-delegation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { BaseCommand } from '../../base'
import { displaySendTx } from '../../utils/cli'
import { Flags } from '../../utils/command'

export default class DeletePaymentDelegation extends BaseCommand {
static description =
"Removes a validator's payment delegation by setting benficiary and fraction to 0."

static flags = {
...BaseCommand.flags,
account: Flags.address({ required: true }),
}

static args = []

static examples = [
'delete-payment-delegation --account 0x5409ED021D9299bf6814279A6A1411A7e866A631',
]

async run() {
const res = this.parse(DeletePaymentDelegation)
this.kit.defaultAccount = res.flags.account
const accounts = await this.kit.contracts.getAccounts()

await displaySendTx('deletePaymentDelegation', accounts.deletePaymentDelegation())

console.log('Deleted payment delegation.')
}
}
35 changes: 35 additions & 0 deletions packages/cli/src/commands/account/get-payment-delegation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import BigNumber from 'bignumber.js'
import { cli } from 'cli-ux'
import { BaseCommand } from '../../base'
import { Flags } from '../../utils/command'

export default class GetPaymentDelegation extends BaseCommand {
static description =
"Get the payment delegation account beneficiary and fraction allocated from a validator's payment each epoch. The fraction cannot be greater than 1."

static flags = {
...BaseCommand.flags,
account: Flags.address({ required: true }),
...(cli.table.flags() as object),
}

static args = []

static examples = ['get-payment-delegation --account 0x5409ed021d9299bf6814279a6a1411a7e866a631']

async run() {
const res = this.parse(GetPaymentDelegation)
this.kit.defaultAccount = res.flags.account
const accounts = await this.kit.contracts.getAccounts()

console.log('Payment delegation beneficiary and fraction are: \n')
const retval = await accounts.getPaymentDelegation(res.flags.account)

const beneficiary = retval[0]

// translate fixidity value to human readable fraction
const fraction = new BigNumber(retval[1]).shiftedBy(-24).toNumber()

console.log(beneficiary, fraction)
}
}
39 changes: 39 additions & 0 deletions packages/cli/src/commands/account/set-payment-delegation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { valueToFixidityString } from '@celo/contractkit/src/wrappers/BaseWrapper'
import { flags } from '@oclif/command'
import { BaseCommand } from '../../base'
import { newCheckBuilder } from '../../utils/checks'
import { displaySendTx } from '../../utils/cli'
import { Flags } from '../../utils/command'

export default class SetPaymentDelegation extends BaseCommand {
static description =
"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."

static flags = {
...BaseCommand.flags,
account: Flags.address({ required: true }),
beneficiary: Flags.address({ required: true }),
fraction: flags.string({ required: true }),
}

static args = []

static examples = [
'set-payment-delegation --account 0x5409ed021d9299bf6814279a6a1411a7e866a631 --beneficiary 0x6Ecbe1DB9EF729CBe972C83Fb886247691Fb6beb --fraction 0.1',
]

async run() {
const res = this.parse(SetPaymentDelegation)
this.kit.defaultAccount = res.flags.account
const accounts = await this.kit.contracts.getAccounts()

await newCheckBuilder(this).isAccount(res.flags.beneficiary).runChecks()
await displaySendTx(
'setPaymentDelegation',
accounts.setPaymentDelegation(
res.flags.beneficiary,
valueToFixidityString(res.flags.fraction)
)
)
}
}
87 changes: 87 additions & 0 deletions packages/docs/command-line-interface/account.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/sdk/contractkit/src/contract-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ export class WrapperCache implements ContractCacheType {
getMultiSig(address: string) {
return this.getContract(CeloContract.MultiSig, address)
}

getReserve() {
return this.getContract(CeloContract.Reserve)
}
Expand Down
50 changes: 50 additions & 0 deletions packages/sdk/contractkit/src/wrappers/Accounts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -167,4 +168,53 @@ testWithGanache('Accounts Wrapper', (web3) => {
await accountsInstance.createAccount().sendAndWaitForReceipt({ from: account })
await expect(accountsInstance.setWalletAddress(wallet)).rejects
})

test('SNBAT fraction greater than 1', 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('SNBAT beneficiary and fraction', 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('SNBAT delete expected to clear beneficiary and fraction', 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)
})
})
27 changes: 27 additions & 0 deletions packages/sdk/contractkit/src/wrappers/Accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,33 @@ export class AccountsWrapper extends BaseWrapper<Accounts> {
*/
setMetadataURL = proxySend(this.connection, this.contract.methods.setMetadataURL)

/**
* Set a validator's payment delegation settings.
* @param beneficiary The address that should receive a portion of validator
* payments.
* @param fraction The fraction of the validator's payment that should be
* diverted to `beneficiary` every epoch, given as FixidityLib value. Must not
* be greater than 1.
* @dev Use `deletePaymentDelegation` to unset the payment delegation.
*/
setPaymentDelegation = proxySend(this.connection, this.contract.methods.setPaymentDelegation)
swidnikk marked this conversation as resolved.
Show resolved Hide resolved

/**
* Remove a validator's payment delegation by setting benficiary and
* fraction to 0.
*/
deletePaymentDelegation = proxySend(
this.connection,
this.contract.methods.deletePaymentDelegation
)

/**
* Get a validator's payment delegation settings.
* @param account Account of the validator.
* @return Beneficiary address and fraction of payment delegated.
*/
getPaymentDelegation = proxyCall(this.contract.methods.getPaymentDelegation)

/**
* Sets the wallet address for the account
* @param address The address to set
Expand Down