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

Force stop contracts #449

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
Factory_v2: forceStopContract
Added new function
Added tests
bra1nsurfer committed Apr 10, 2024

Verified

This commit was signed with the committer’s verified signature.
commit de530b74b77e98e20f4de068b507ee96a61a1568
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"type": 12,
"fee": 500000,
"feeAssetId": null,
"version": 2,
"senderPublicKey": "HBWgh7DKPyzCnEXKJAJ5dKQ3jmPtMhGD78tt6jRdkV61",
"data": [
{
"key": "%s__forceStopPermission",
"type": "string",
"value": ""
}
]
}
19 changes: 19 additions & 0 deletions ride/factory_v2.ride
Original file line number Diff line number Diff line change
@@ -1088,6 +1088,25 @@ func checkBalance(lpAssetIdStr: String) = {
(nil, balanceIsOk)
}

@Callable(i)
func forceStopContract(address: String, stop: Boolean) = {
let permissionListKey = ["%s", "forceStopPermission"].makeString(SEP)
let forceStopKey = ["%s%s", "stop", address].makeString(SEP)

# Format:
# "{addr1}_{addr2}_{addr3}" -> ["addr1", "addr2", "addr3"]
let permissionList = this.getString(permissionListKey).valueOrElse("").split(SEP)
let callerAddressString = i.caller.toString()

strict check = [
permissionList.containsElement(callerAddressString) || i.mustManager()
]

[
BooleanEntry(forceStopKey, stop)
]
}

@Verifier(tx)
func verify() = {
let targetPublicKey = match managerPublicKeyOrUnit() {
43 changes: 43 additions & 0 deletions test/components/factory_v2/RejectForceStopByNotAdmin.spec.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import chai, { expect } from 'chai';
import chaiAsPromised from 'chai-as-promised';
import chaiSubset from 'chai-subset';
import { address } from '@waves/ts-lib-crypto';
import {
invokeScript,
} from '@waves/waves-transactions';
import { create } from '@waves/node-api-js';

chai.use(chaiAsPromised);
chai.use(chaiSubset);
// const { expect } = chai;

const apiBase = process.env.API_NODE_URL;
const chainId = 'R';

const api = create(apiBase);

/** @typedef {
* Mocha.Suite & {accounts: Object.<string, number>, wxAssetId: string, usdnAssetId: string}
* } MochaSuiteModified
* */

describe('Factory V2 - force stop contract', /** @this {MochaSuiteModified} */() => {
it('Force stop contract is rejected if caller is not an Admin', async function () {
const forceStopInvokeTx = invokeScript({
dApp: address(this.accounts.factory, chainId),
call: {
function: 'forceStopContract',
args: [
{ type: 'string', value: address(this.accounts.lp, chainId) },
{ type: 'boolean', value: true },
],
},
fee: 1e8 + 9e5,
chainId,
}, this.accounts.lp);

await expect(api.transactions.broadcast(forceStopInvokeTx, {}))
.to.be
.rejectedWith('Permission denied');
});
});
7 changes: 6 additions & 1 deletion test/components/factory_v2/_hooks.mjs
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@ export const mochaHooks = {
async beforeAll() {
const spinner = ora('Initializing').start();
// setup accounts
const names = ['factory', 'store', 'lp', 'matcher', 'user', 'userpools', 'voting_emission'];
const names = ['factory', 'store', 'lp', 'matcher', 'user', 'userpools', 'voting_emission', 'forceStopAdmin'];
this.accounts = Object.fromEntries(names.map((item) => [item, randomSeed(seedWordsCount)]));
const seeds = Object.values(this.accounts);
const amount = 1e10;
@@ -48,6 +48,11 @@ export const mochaHooks = {
type: 'string',
value: address(this.accounts.voting_emission, chainId),
},
{
key: '%s__forceStopPermission',
type: 'string',
value: address(this.accounts.forceStopAdmin, chainId),
},
],
chainId,
}, this.accounts.factory);
54 changes: 54 additions & 0 deletions test/components/factory_v2/forceStopByAdmin.spec.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import chai, { expect } from 'chai';
import chaiAsPromised from 'chai-as-promised';
import chaiSubset from 'chai-subset';
import { address } from '@waves/ts-lib-crypto';
import {
invokeScript, nodeInteraction,
} from '@waves/waves-transactions';
import { create } from '@waves/node-api-js';

chai.use(chaiAsPromised);
chai.use(chaiSubset);
// const { expect } = chai;

const { waitForTx } = nodeInteraction;

const apiBase = process.env.API_NODE_URL;
const chainId = 'R';

const api = create(apiBase);

/** @typedef {
* Mocha.Suite & {accounts: Object.<string, number>, wxAssetId: string, usdnAssetId: string}
* } MochaSuiteModified
* */

describe('Factory V2 - force stop contract', /** @this {MochaSuiteModified} */() => {
it('Force stop contract by Admin', async function () {
const poolAddress = address(this.accounts.lp, chainId);

const forceStopInvokeTx = invokeScript({
dApp: address(this.accounts.factory, chainId),
call: {
function: 'forceStopContract',
args: [
{ type: 'string', value: poolAddress },
{ type: 'boolean', value: true },
],
},
fee: 1e8 + 9e5,
chainId,
}, this.accounts.forceStopAdmin);

await api.transactions.broadcast(forceStopInvokeTx, {});
const { stateChanges } = await waitForTx(forceStopInvokeTx.id, { apiBase });

expect(stateChanges.data).to.deep.eql([
{
key: `%s%s__stop__${poolAddress}`,
type: 'boolean',
value: true,
},
]);
});
});
54 changes: 54 additions & 0 deletions test/components/factory_v2/forceStopByManager.spec.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import chai, { expect } from 'chai';
import chaiAsPromised from 'chai-as-promised';
import chaiSubset from 'chai-subset';
import { address } from '@waves/ts-lib-crypto';
import {
invokeScript, nodeInteraction,
} from '@waves/waves-transactions';
import { create } from '@waves/node-api-js';

chai.use(chaiAsPromised);
chai.use(chaiSubset);
// const { expect } = chai;

const { waitForTx } = nodeInteraction;

const apiBase = process.env.API_NODE_URL;
const chainId = 'R';

const api = create(apiBase);

/** @typedef {
* Mocha.Suite & {accounts: Object.<string, number>, wxAssetId: string, usdnAssetId: string}
* } MochaSuiteModified
* */

describe('Factory V2 - force stop contract', /** @this {MochaSuiteModified} */() => {
it('Force stop contract bu Manager', async function () {
const poolAddress = address(this.accounts.lp, chainId);

const forceStopInvokeTx = invokeScript({
dApp: address(this.accounts.factory, chainId),
call: {
function: 'forceStopContract',
args: [
{ type: 'string', value: poolAddress },
{ type: 'boolean', value: true },
],
},
fee: 1e8 + 9e5,
chainId,
}, this.accounts.factory);

await api.transactions.broadcast(forceStopInvokeTx, {});
const { stateChanges } = await waitForTx(forceStopInvokeTx.id, { apiBase });

expect(stateChanges.data).to.deep.eql([
{
key: `%s%s__stop__${poolAddress}`,
type: 'boolean',
value: true,
},
]);
});
});