Skip to content

Commit

Permalink
Revert "Remove granda mento + metatransaction wallet (#10822)"
Browse files Browse the repository at this point in the history
This reverts commit 9ab9d00.
  • Loading branch information
aaronmgdr committed Dec 12, 2023
1 parent 9762ae4 commit 264cec7
Show file tree
Hide file tree
Showing 148 changed files with 7,002 additions and 3,005 deletions.
5 changes: 0 additions & 5 deletions .changeset/cold-jobs-pay.md

This file was deleted.

6 changes: 0 additions & 6 deletions .changeset/tiny-chairs-poke.md

This file was deleted.

54 changes: 54 additions & 0 deletions packages/cli/src/commands/grandamento/cancel.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Address } from '@celo/base/lib/address'
import { newKitFromWeb3 } from '@celo/contractkit'
import { assumeOwnership } from '@celo/contractkit/lib/test-utils/transferownership'
import { GrandaMentoWrapper } from '@celo/contractkit/lib/wrappers/GrandaMento'
import { testWithGanache } from '@celo/dev-utils/lib/ganache-test'
import BigNumber from 'bignumber.js'
import Web3 from 'web3'
import { testLocally } from '../../test-utils/cliUtils'
import Cancel from './cancel'
import Propose from './propose'

testWithGanache('grandamento:cancel cmd', (web3: Web3) => {
const kit = newKitFromWeb3(web3)
let grandaMento: GrandaMentoWrapper
const newLimitMin = new BigNumber('1000')
const newLimitMax = new BigNumber('1000000000000')
let accounts: Address[] = []

const increaseLimits = async () => {
await grandaMento
.setStableTokenExchangeLimits('StableToken', newLimitMin.toString(), newLimitMax.toString())
.sendAndWaitForReceipt()
}

beforeAll(async () => {
accounts = await web3.eth.getAccounts()
kit.defaultAccount = accounts[0]
grandaMento = await kit.contracts.getGrandaMento()
})

beforeEach(async () => {
await assumeOwnership(web3, accounts[0])
await increaseLimits()
// create mock proposal
await testLocally(Propose, [
'--from',
accounts[0],
'--sellCelo',
'true',
'--stableToken',
'cUSD',
'--value',
'10000',
])
})

describe('cancel', () => {
it('left no proposal', async () => {
await testLocally(Cancel, ['--from', accounts[0], '--proposalID', '1'])
const activeProposals = await grandaMento.getActiveProposalIds()
expect(activeProposals).toEqual([])
})
})
})
38 changes: 38 additions & 0 deletions packages/cli/src/commands/grandamento/cancel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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 Cancel extends BaseCommand {
static description = 'Cancels a Granda Mento exchange proposal'

static flags = {
...BaseCommand.flags,
from: Flags.address({
required: true,
description: 'The address allowed to cancel the proposal',
}),
proposalID: flags.string({
required: true,
exclusive: ['account', 'hotfix'],
description: 'UUID of proposal to view',
}),
}

async run() {
const grandaMento = await this.kit.contracts.getGrandaMento()

const res = this.parse(Cancel)
const proposalID = res.flags.proposalID

await newCheckBuilder(this).grandaMentoProposalExists(proposalID).runChecks()

await displaySendTx(
'cancelExchangeProposal',
grandaMento.cancelExchangeProposal(proposalID),
undefined,
'ExchangeProposalCancelled'
)
}
}
91 changes: 91 additions & 0 deletions packages/cli/src/commands/grandamento/execute.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { Address } from '@celo/base/lib/address'
import { newKitFromWeb3 } from '@celo/contractkit'
import { assumeOwnership } from '@celo/contractkit/lib/test-utils/transferownership'
import { GrandaMentoWrapper } from '@celo/contractkit/lib/wrappers/GrandaMento'
import { testWithGanache, timeTravel } from '@celo/dev-utils/lib/ganache-test'
import BigNumber from 'bignumber.js'
import Web3 from 'web3'
import { testLocally } from '../../test-utils/cliUtils'
import Execute from './execute'
import Propose from './propose'

testWithGanache('grandamento:execute cmd', (web3: Web3) => {
const kit = newKitFromWeb3(web3)
let grandaMento: GrandaMentoWrapper
const newLimitMin = new BigNumber('1000')
const newLimitMax = new BigNumber('1000000000000')
let accounts: Address[] = []
const dateNowOriginal = Date.now
let originalNoSyncCheck: string | undefined

const increaseLimits = () => {
return grandaMento
.setStableTokenExchangeLimits('StableToken', newLimitMin.toString(), newLimitMax.toString())
.sendAndWaitForReceipt()
}

const createExchangeProposal = () => {
// create mock proposal
return testLocally(Propose, [
'--from',
accounts[0],
'--sellCelo',
'true',
'--stableToken',
'cUSD',
'--value',
'10000',
])
}

const approveExchangeProposal = async (proposalID: number | string) => {
await grandaMento.setApprover(accounts[0]).sendAndWaitForReceipt()
await grandaMento.approveExchangeProposal(proposalID).sendAndWaitForReceipt()
}

const timeTravelDateAndChain = async (seconds: number) => {
await timeTravel(seconds, web3)
jest.useFakeTimers().setSystemTime(dateNowOriginal() + seconds * 1000)
// Otherwise contractkit complains there is a difference between Date.now()
// and the timestamp of the last block
process.env.NO_SYNCCHECK = 'true'
}

beforeAll(async () => {
accounts = await web3.eth.getAccounts()
kit.defaultAccount = accounts[0]
grandaMento = await kit.contracts.getGrandaMento()
originalNoSyncCheck = process.env.NO_SYNCCHECK
})

afterEach(() => {
process.env.NO_SYNCCHECK = originalNoSyncCheck
})

beforeEach(async () => {
await assumeOwnership(web3, accounts[0])
await increaseLimits()
await createExchangeProposal()
// Approve it
await approveExchangeProposal(1)
// Wait the veto period plus some extra time to be safe
await timeTravelDateAndChain((await grandaMento.vetoPeriodSeconds()).toNumber() + 1000)
})

describe('execute', () => {
it('executes the proposal', async () => {
await testLocally(Execute, ['--from', accounts[0], '--proposalID', '1'])
const activeProposals = await grandaMento.getActiveProposalIds()
expect(activeProposals).toEqual([])
})

it('fails if the exchange proposal is not executable', async () => {
// Create a proposal with proposalID 2, but don't wait the veto period
await createExchangeProposal()
await approveExchangeProposal(2)
await expect(
testLocally(Execute, ['--from', accounts[0], '--proposalID', '2'])
).rejects.toThrow()
})
})
})
40 changes: 40 additions & 0 deletions packages/cli/src/commands/grandamento/execute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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 Execute extends BaseCommand {
static description = 'Executes a Granda Mento exchange proposal'

static flags = {
...BaseCommand.flags,
from: Flags.address({
required: true,
description: 'The address to execute the exchange proposal',
}),
proposalID: flags.string({
required: true,
description: 'UUID of proposal to view',
}),
}

async run() {
const grandaMento = await this.kit.contracts.getGrandaMento()

const res = this.parse(Execute)
const proposalID = res.flags.proposalID

await newCheckBuilder(this)
.grandaMentoProposalExists(proposalID)
.grandaMentoProposalIsExecutable(proposalID)
.runChecks()

await displaySendTx(
'executeExchangeProposal',
grandaMento.executeExchangeProposal(proposalID),
undefined,
'ExchangeProposalExecuted'
)
}
}
32 changes: 32 additions & 0 deletions packages/cli/src/commands/grandamento/get-buy-amount.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Address } from '@celo/base/lib/address'
import { newKitFromWeb3 } from '@celo/contractkit'
import { assumeOwnership } from '@celo/contractkit/lib/test-utils/transferownership'
import { testWithGanache } from '@celo/dev-utils/lib/ganache-test'
import Web3 from 'web3'
import { testLocally } from '../../test-utils/cliUtils'
import GetBuyAmount from './get-buy-amount'

testWithGanache('grandamento:get-buy-amount cmd', (web3: Web3) => {
const kit = newKitFromWeb3(web3)
let accounts: Address[] = []

beforeAll(async () => {
accounts = await web3.eth.getAccounts()
kit.defaultAccount = accounts[0]
})

beforeEach(async () => {
await assumeOwnership(web3, accounts[0])
})

it('gets the buy amount', async () => {
await testLocally(GetBuyAmount, [
'--sellCelo',
'true',
'--stableToken',
'cusd',
'--value',
'100000000000000000000000',
])
})
})
55 changes: 55 additions & 0 deletions packages/cli/src/commands/grandamento/get-buy-amount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { StableToken } from '@celo/contractkit'
import { toFixed } from '@celo/utils/lib/fixidity'
import { flags } from '@oclif/command'
import { BaseCommand } from '../../base'
import { printValueMap } from '../../utils/cli'
import { Flags } from '../../utils/command'
import { enumEntriesDupWithLowercase } from '../../utils/helpers'

const stableTokenOptions = enumEntriesDupWithLowercase(Object.entries(StableToken))

export default class GetBuyAmount extends BaseCommand {
static description = 'Gets the buy amount for a prospective Granda Mento exchange'

static flags = {
...BaseCommand.flags,
value: Flags.wei({
required: true,
description: 'The value of the tokens to exchange',
}),
stableToken: flags.enum({
required: true,
options: Object.keys(stableTokenOptions),
description: 'Name of the stable to receive or send',
default: 'cUSD',
}),
sellCelo: flags.enum({
options: ['true', 'false'],
required: true,
description: 'Sell or buy CELO',
}),
}

async run() {
const grandaMento = await this.kit.contracts.getGrandaMento()

const res = this.parse(GetBuyAmount)
const sellAmount = res.flags.value
const stableToken = stableTokenOptions[res.flags.stableToken]
const sellCelo = res.flags.sellCelo === 'true'

const stableTokenAddress = await this.kit.celoTokens.getAddress(stableToken)
const sortedOracles = await this.kit.contracts.getSortedOracles()
const celoStableTokenOracleRate = (await sortedOracles.medianRate(stableTokenAddress)).rate

const buyAmount = await grandaMento.getBuyAmount(
toFixed(celoStableTokenOracleRate),
sellAmount,
sellCelo
)

printValueMap({
buyAmount,
})
}
}
51 changes: 51 additions & 0 deletions packages/cli/src/commands/grandamento/list.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Address } from '@celo/base/lib/address'
import { newKitFromWeb3 } from '@celo/contractkit'
import { StableToken } from '@celo/contractkit/lib/celo-tokens'
import { assumeOwnership } from '@celo/contractkit/lib/test-utils/transferownership'
import { GoldTokenWrapper } from '@celo/contractkit/lib/wrappers/GoldTokenWrapper'
import { GrandaMentoWrapper } from '@celo/contractkit/lib/wrappers/GrandaMento'
import { testWithGanache } from '@celo/dev-utils/lib/ganache-test'
import BigNumber from 'bignumber.js'
import Web3 from 'web3'
import { testLocally } from '../../test-utils/cliUtils'
import { setGrandaMentoLimits } from '../../test-utils/grandaMento'
import List from './list'

testWithGanache('grandamento:list cmd', (web3: Web3) => {
const kit = newKitFromWeb3(web3)
let grandaMento: GrandaMentoWrapper
let accounts: Address[] = []
let celoToken: GoldTokenWrapper

beforeAll(async () => {
accounts = await web3.eth.getAccounts()
kit.defaultAccount = accounts[0]
grandaMento = await kit.contracts.getGrandaMento()

celoToken = await kit.contracts.getGoldToken()
})

beforeEach(async () => {
await assumeOwnership(web3, accounts[0])
await setGrandaMentoLimits(grandaMento)
})

it('shows an empty list of proposals', async () => {
await testLocally(List, [])
})

it('shows proposals', async () => {
// create mock proposal
const sellAmount = new BigNumber('100000000')
await celoToken.increaseAllowance(grandaMento.address, sellAmount).sendAndWaitForReceipt()
await (
await grandaMento.createExchangeProposal(
kit.celoTokens.getContract(StableToken.cUSD),
sellAmount,
true
)
).sendAndWaitForReceipt()

await testLocally(List, [])
})
})
Loading

0 comments on commit 264cec7

Please sign in to comment.