How to use _grantPermissions method in Playwright Internal

Best JavaScript code snippet using playwright-internal

Run Playwright Internal automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

deployer.js

Source: deployer.js Github

copy
1const AgreementWrapper = require('../wrappers/agreement')
2const DisputableWrapper = require('../wrappers/disputable')
3
4const { utf8ToHex } = require('web3-utils')
5const { ANY_ENTITY, getInstalledApp } = require('@aragon/contract-helpers-test/src/aragon-os')
6const { ZERO_ADDRESS, NOW, ONE_DAY, bigExp, bn, getEventArgument } = require('@aragon/contract-helpers-test')
7
8
9const DEFAULT_AGREEMENT_INITIALIZATION_PARAMS = {
10  appId: '0xcafe1234cafe1234cafe1234cafe1234cafe1234cafe1234cafe1234cafe1234',
11  currentTimestamp: NOW,
12
13  title: 'Sample Agreement',
14  content: utf8ToHex('ipfs:QmdLu3XXT9uUYxqDKXXsTYG77qNYNPbhzL27ZYT9kErqcZ'),
15  setCashier: false,
16  arbitrator: {
17    feeAmount: bigExp(5, 18),            // 5 AFT
18    feeToken: {
19      symbol: 'AFT',
20      decimals: 18,
21      name: 'Arbitrator Fee Token'
22    }
23  },
24}
25
26const DEFAULT_DISPUTABLE_INITIALIZATION_PARAMS = {
27  appId: '0xdead1234dead1234dead1234dead1234dead1234dead1234dead1234dead1234',
28  currentTimestamp: NOW,
29
30  challengeDuration: bn(2 * ONE_DAY),    // 2 days
31  actionCollateral: bigExp(100, 18),     // 100 DAI
32  challengeCollateral: bigExp(200, 18),  // 200 DAI
33  collateralToken: {
34    symbol: 'DAI',
35    decimals: 18,
36    name: 'Sample DAI'
37  },
38}
39
40const SUBMIT_ROLE = '0x8a8601cc8e9efb544266baca5bffc5cea11aed5de937dc37810fd002b4010eac'
41const CHALLENGE_ROLE = '0xef025787d7cd1a96d9014b8dc7b44899b8c1350859fb9e1e05f5a546dd65158d'
42const SET_AGREEMENT_ROLE = '0x8dad640ab1b088990c972676ada708447affc660890ec9fc9a5483241c49f036'
43
44class AgreementDeployer {
45  constructor(artifacts, web3) {
46    this.web3 = web3
47    this.artifacts = artifacts
48    this.previousDeploy = {}
49  }
50
51  get owner() {
52    return this.previousDeploy.owner
53  }
54
55  get dao() {
56    return this.previousDeploy.dao
57  }
58
59  get acl() {
60    return this.previousDeploy.acl
61  }
62
63  get base() {
64    return this.previousDeploy.base
65  }
66
67  get baseDisputable() {
68    return this.previousDeploy.baseDisputable
69  }
70
71  get arbitrator() {
72    return this.previousDeploy.arbitrator
73  }
74
75  get agreement() {
76    return this.previousDeploy.agreement
77  }
78
79  get stakingFactory() {
80    return this.previousDeploy.stakingFactory
81  }
82
83  get disputable() {
84    return this.previousDeploy.disputable
85  }
86
87  get collateralToken() {
88    return this.previousDeploy.collateralToken
89  }
90
91  get clockMock() {
92    return this.previousDeploy.clockMock
93  }
94
95  get abi() {
96    return this.base.abi
97  }
98
99  async deployAndInitializeDisputableWrapper(options = {}) {
100    if (!this.agreement) await this.deployAndInitializeAgreement(options)
101    await this.deployDisputable(options)
102
103    const disputable = options.disputable || this.disputable
104    const arbitrator = options.arbitrator || this.arbitrator
105    const stakingFactory = options.stakingFactory || this.stakingFactory
106    const collateralToken = options.collateralToken || this.collateralToken
107    const { actionCollateral, challengeCollateral, challengeDuration } = { ...DEFAULT_DISPUTABLE_INITIALIZATION_PARAMS, ...options }
108
109    const collateralRequirement = { collateralToken, actionCollateral, challengeCollateral, challengeDuration }
110    return new DisputableWrapper(this.artifacts, this.web3, this.agreement, arbitrator, stakingFactory, this.clockMock, disputable, collateralRequirement)
111  }
112
113  async deployAndInitializeAgreementWrapper(options = {}) {
114    await this.deployAndInitializeAgreement(options)
115    const arbitrator = options.arbitrator || this.arbitrator
116    const stakingFactory = options.stakingFactory || this.stakingFactory
117    return new AgreementWrapper(this.artifacts, this.web3, this.agreement, arbitrator, stakingFactory, this.clockMock)
118  }
119
120  async deployAndInitializeAgreement(options = {}) {
121    await this.deploy(options)
122
123    if (!options.arbitrator && !this.arbitrator) await this.deployArbitrator(options)
124    const arbitrator = options.arbitrator || this.arbitrator
125
126    if (!options.stakingFactory && !this.stakingFactory) await this.deployStakingFactory()
127    const stakingFactory = options.stakingFactory || this.stakingFactory
128
129    const defaultOptions = { ...DEFAULT_AGREEMENT_INITIALIZATION_PARAMS, ...options }
130    const { title, content, setCashier } = defaultOptions
131
132    await this.agreement.initialize(arbitrator.address, setCashier, title, content, stakingFactory.address)
133    return this.agreement
134  }
135
136  async deploy(options = {}) {
137    const owner = options.owner || await this._getSender()
138    if (!this.dao) await this.deployDAO(owner)
139    if (!this.base) await this.deployBase()
140
141    const { appId, currentTimestamp } = { ...DEFAULT_AGREEMENT_INITIALIZATION_PARAMS, ...options }
142    const receipt = await this.dao.newAppInstance(appId, this.base.address, '0x', false, { from: owner })
143    const agreement = await this.base.constructor.at(getInstalledApp(receipt, appId))
144
145    const permissions = ['CHANGE_AGREEMENT_ROLE', 'MANAGE_DISPUTABLE_ROLE']
146    await this._createPermissions(agreement, permissions, owner)
147
148    if (currentTimestamp) await this.mockTime(agreement, currentTimestamp)
149    this.previousDeploy = { ...this.previousDeploy, agreement }
150    return agreement
151  }
152
153  async deployDisputable(options = {}) {
154    const owner = options.owner || await this._getSender()
155    if (!this.baseDisputable) await this.deployBaseDisputable()
156
157    const { appId, currentTimestamp } = { ...DEFAULT_DISPUTABLE_INITIALIZATION_PARAMS, ...options }
158    const receipt = await this.dao.newAppInstance(appId, this.baseDisputable.address, '0x', false, { from: owner })
159    const disputable = await this.baseDisputable.constructor.at(getInstalledApp(receipt, appId))
160
161    await this.acl.createPermission(this.agreement.address, disputable.address, SET_AGREEMENT_ROLE, owner, { from: owner })
162    await this._grantPermissions(disputable, SUBMIT_ROLE, options.submitters, owner)
163    await this._grantPermissions(disputable, CHALLENGE_ROLE, options.challengers, owner)
164
165    if (!options.collateralToken && !this.collateralToken) await this.deployCollateralToken(options)
166    await disputable.initialize()
167
168    if (options.activate || options.activate === undefined) {
169      const collateralToken = options.collateralToken || this.collateralToken
170      const { actionCollateral, challengeCollateral, challengeDuration } = { ...DEFAULT_DISPUTABLE_INITIALIZATION_PARAMS, ...options }
171      await this.agreement.activate(disputable.address, collateralToken.address, challengeDuration, actionCollateral, challengeCollateral, { from: owner })
172    }
173
174    if (currentTimestamp) await this.mockTime(disputable, currentTimestamp)
175    this.previousDeploy = { ...this.previousDeploy, disputable }
176    return disputable
177  }
178
179  async deployArbitrator(options = {}) {
180    let { feeToken, feeAmount } = { ...DEFAULT_AGREEMENT_INITIALIZATION_PARAMS.arbitrator, ...options }
181    if (!feeToken.address) feeToken = await this.deployToken(feeToken)
182
183    const Arbitrator = this._getContract('ArbitratorMock')
184    const arbitrator = await Arbitrator.new(feeToken.address, feeAmount)
185    this.previousDeploy = { ...this.previousDeploy, arbitrator }
186
187    return arbitrator
188  }
189
190  async deployStakingFactory() {
191    const StakingFactory = this._getContract('StakingFactory')
192    const stakingFactory = await StakingFactory.new()
193    this.previousDeploy = { ...this.previousDeploy, stakingFactory }
194    return stakingFactory
195  }
196
197  async deployCollateralToken(options = {}) {
198    const collateralToken = await this.deployToken(options)
199    this.previousDeploy = { ...this.previousDeploy, collateralToken }
200    return collateralToken
201  }
202
203  async deployBase() {
204    const Agreement = this._getContract('AgreementMock')
205    const base = await Agreement.new()
206    this.previousDeploy = { ...this.previousDeploy, base }
207    return base
208  }
209
210  async deployBaseDisputable() {
211    const Disputable = this._getContract('DisputableAppMock')
212    const baseDisputable = await Disputable.new()
213    this.previousDeploy = { ...this.previousDeploy, baseDisputable }
214    return baseDisputable
215  }
216
217  async deployDAO(owner) {
218    const Kernel = this._getContract('Kernel')
219    const kernelBase = await Kernel.new(true)
220
221    const ACL = this._getContract('ACL')
222    const aclBase = await ACL.new()
223
224    const EVMScriptRegistryFactory = this._getContract('EVMScriptRegistryFactory')
225    const regFact = await EVMScriptRegistryFactory.new()
226
227    const DAOFactory = this._getContract('DAOFactory')
228    const daoFact = await DAOFactory.new(kernelBase.address, aclBase.address, regFact.address)
229
230    const kernelReceipt = await daoFact.newDAO(owner)
231    const dao = await Kernel.at(getEventArgument(kernelReceipt, 'DeployDAO', 'dao'))
232    const acl = await ACL.at(await dao.acl())
233
234    const APP_MANAGER_ROLE = await kernelBase.APP_MANAGER_ROLE()
235    await acl.createPermission(owner, dao.address, APP_MANAGER_ROLE, owner, { from: owner })
236
237    this.previousDeploy = { ...this.previousDeploy, dao, acl, owner }
238    return dao
239  }
240
241  async deployToken({ name = 'My Sample Token', decimals = 18, symbol = 'MST' }) {
242    const MiniMeToken = this._getContract('MiniMeToken')
243    return MiniMeToken.new(ZERO_ADDRESS, ZERO_ADDRESS, 0, name, decimals, symbol, true)
244  }
245
246  async mockTime(timeMockable, timestamp) {
247    if (!this.clockMock) await this.deployClockMock()
248    await timeMockable.setClock(this.clockMock.address)
249    return this.clockMock.mockSetTimestamp(timestamp)
250  }
251
252  async deployClockMock() {
253    const ClockMock = this._getContract('TimeHelpersMock')
254    const clockMock = await ClockMock.new()
255    this.previousDeploy = { ...this.previousDeploy, clockMock }
256    return clockMock
257  }
258
259  async _createPermissions(app, permissions, to, manager = to) {
260    for (const permission of permissions) {
261      const ROLE = await app[permission]()
262      await this.acl.createPermission(to, app.address, ROLE, manager, { from: manager })
263    }
264  }
265
266  async _grantPermissions(app, permission, users, manager) {
267    if (!users) users = [ANY_ENTITY]
268    for (const user of users) {
269      if (users.indexOf(user) === 0) await this.acl.createPermission(user, app.address, permission, manager, { from: manager })
270      else await this.acl.grantPermission(user, app.address, permission, { from: manager })
271    }
272  }
273
274  _getContract(name) {
275    return this.artifacts.require(name)
276  }
277
278  async _getSender() {
279    if (!this.sender) {
280      const accounts = await this.web3.eth.getAccounts()
281      this.sender = accounts[0]
282    }
283    return this.sender
284  }
285}
286
287module.exports = (web3, artifacts) => new AgreementDeployer(artifacts, web3)
288
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Playwright Internal on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)