@leofcoin/contracts 0.1.14 → 0.1.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,96 +1,86 @@
1
- import { expect, use } from 'chai'
2
- import chaiAsPromised from 'chai-as-promised'
3
- import Leofcoin from './../exports/native-token.js'
4
- import Token from '@leofcoin/standards/token.js'
5
-
6
- use(chaiAsPromised)
7
-
8
- describe('Leofcoin', () => {
9
- let state
10
- let token
11
- const receiverAddress = '0xReceiverAddress'
12
- const otherReceiverAddress = '0xOtherReceiverAddress'
13
- const ownerAddress = '0xOwnerAddress'
14
-
15
- beforeEach(() => {
16
- global.msg = {
17
- sender: ownerAddress,
18
- contract: '0xContractAddress',
19
- staticCall: async (currency, method, args) => {
20
- // Mock implementation of msg.staticCall
21
- },
22
- call: async (currency, method, args) => {
23
- // Mock implementation of msg.call
24
- }
25
- }
26
-
27
- token = new Leofcoin()
28
- })
29
-
30
- it('should create an instance of Leofcoin', () => {
31
- expect(token).to.be.instanceOf(Leofcoin)
32
- })
33
-
34
- it('should have correct properties', () => {
35
- expect(token.name).to.equal('Leofcoin')
36
- expect(token.symbol).to.equal('LFC')
37
- expect(token.decimals).to.equal(18)
38
- })
39
-
40
- it('should grant MINT role to owner', async () => {
41
- await token.grantRole(ownerAddress, 'MINT')
42
- expect(await token.hasRole(ownerAddress, 'MINT')).to.equal(true)
43
- })
44
-
45
- it('should be able to mint', async () => {
46
- await token.grantRole(ownerAddress, 'MINT')
47
- await token.mint(ownerAddress, 100000n)
48
- expect(token.balanceOf(ownerAddress)).to.equal(100000n)
49
- })
50
-
51
- it('should be able to transfer', async () => {
52
- await token.grantRole(ownerAddress, 'MINT')
53
- await token.mint(receiverAddress, 100000n)
54
- msg.sender = receiverAddress
55
- await token.transfer(receiverAddress, otherReceiverAddress, 10000n)
56
- expect(token.balanceOf(receiverAddress)).to.equal(90000n)
57
- expect(token.balanceOf(otherReceiverAddress)).to.equal(10000n)
58
- })
59
-
60
- it('should be able to burn', async () => {
61
- await token.grantRole(ownerAddress, 'BURN')
62
- await token.grantRole(ownerAddress, 'MINT')
63
- await token.mint(ownerAddress, 100000n)
64
- await token.burn(ownerAddress, 10000n)
65
- expect(token.balanceOf(ownerAddress)).to.equal(90000n)
66
- })
67
-
68
- it('should not be able to burn more than balance', async () => {
69
- await token.grantRole(ownerAddress, 'MINT')
70
- await token.mint(ownerAddress, 100000n)
71
-
72
- try {
73
- await token.burn(ownerAddress, 100001n)
74
- } catch (error) {
75
- expect(error.message).to.equal('amount exceeds balance')
76
- }
77
- })
78
-
79
- it('should not be able to mint if not MINT role', async () => {
80
- try {
81
- await token.mint(ownerAddress, 100000n)
82
- } catch (error) {
83
- expect(error.message).to.equal('mint role required')
84
- }
85
- })
86
-
87
- it('should not be able to transfer more than balance', async () => {
88
- await token.grantRole(ownerAddress, 'MINT')
89
- await token.mint(receiverAddress, 100000n)
90
- try {
91
- await token.transfer(receiverAddress, otherReceiverAddress, 100001n)
92
- } catch (error) {
93
- expect(error.message).to.equal('amount exceeds balance')
94
- }
95
- })
96
- })
1
+ import { describe, it, beforeEach } from 'node:test'
2
+ import assert from 'node:assert/strict'
3
+ import Leofcoin from './../exports/native-token.js'
4
+
5
+ describe('Leofcoin', () => {
6
+ let token
7
+ const receiverAddress = '0xReceiverAddress'
8
+ const otherReceiverAddress = '0xOtherReceiverAddress'
9
+ const ownerAddress = '0xOwnerAddress'
10
+
11
+ beforeEach(() => {
12
+ global.msg = {
13
+ sender: ownerAddress,
14
+ contract: '0xContractAddress',
15
+ staticCall: async (currency, method, args) => {
16
+ // Mock implementation of msg.staticCall
17
+ },
18
+ call: async (currency, method, args) => {
19
+ // Mock implementation of msg.call
20
+ }
21
+ }
22
+
23
+ token = new Leofcoin()
24
+ })
25
+
26
+ it('should create an instance of Leofcoin', () => {
27
+ assert.ok(token instanceof Leofcoin)
28
+ })
29
+
30
+ it('should have correct properties', () => {
31
+ assert.equal(token.name, 'Leofcoin')
32
+ assert.equal(token.symbol, 'LFC')
33
+ assert.equal(token.decimals, 18)
34
+ })
35
+
36
+ it('should grant MINT role to owner', async () => {
37
+ await token.grantRole(ownerAddress, 'MINT')
38
+ assert.equal(await token.hasRole(ownerAddress, 'MINT'), true)
39
+ })
40
+
41
+ it('should be able to mint', async () => {
42
+ await token.grantRole(ownerAddress, 'MINT')
43
+ await token.mint(ownerAddress, 100000n)
44
+ assert.equal(token.balanceOf(ownerAddress), 100000n)
45
+ })
46
+
47
+ it('should be able to transfer', async () => {
48
+ await token.grantRole(ownerAddress, 'MINT')
49
+ await token.mint(receiverAddress, 100000n)
50
+ msg.sender = receiverAddress
51
+ await token.transfer(receiverAddress, otherReceiverAddress, 10000n)
52
+ assert.equal(token.balanceOf(receiverAddress), 90000n)
53
+ assert.equal(token.balanceOf(otherReceiverAddress), 10000n)
54
+ })
55
+
56
+ it('should be able to burn', async () => {
57
+ await token.grantRole(ownerAddress, 'BURN')
58
+ await token.grantRole(ownerAddress, 'MINT')
59
+ await token.mint(ownerAddress, 100000n)
60
+ await token.burn(ownerAddress, 10000n)
61
+ assert.equal(token.balanceOf(ownerAddress), 90000n)
62
+ })
63
+
64
+ it('should not be able to burn more than balance', async () => {
65
+ await token.grantRole(ownerAddress, 'MINT')
66
+ await token.grantRole(ownerAddress, 'BURN')
67
+ await token.mint(ownerAddress, 100000n)
68
+
69
+ await assert.rejects(async () => await token.burn(ownerAddress, 100001n), {
70
+ message: 'amount exceeds balance'
71
+ })
72
+ })
73
+
74
+ it('should not be able to mint if not MINT role', async () => {
75
+ assert.throws(() => token.mint(ownerAddress, 100000n), { message: 'not allowed' })
76
+ })
77
+
78
+ it('should not be able to transfer more than balance', async () => {
79
+ await token.grantRole(ownerAddress, 'MINT')
80
+ await token.mint(receiverAddress, 100000n)
81
+ msg.sender = receiverAddress
82
+ assert.throws(() => token.transfer(receiverAddress, otherReceiverAddress, 100001n), {
83
+ message: 'amount exceeds balance'
84
+ })
85
+ })
86
+ })
@@ -1,120 +1,119 @@
1
- import { expect, use } from 'chai'
2
- import chaiAsPromised from 'chai-as-promised'
3
- import { describe, it, beforeEach } from 'mocha'
4
- import Validators from '../exports/validators.js'
5
-
6
- use(chaiAsPromised)
7
-
8
- describe('Validators', () => {
9
- let validators
10
- let validatorState
11
- const tokenAddress = '0xTokenAddress'
12
- const ownerAddress = '0xOwnerAddress'
13
- const validatorAddress = '0xValidatorAddress'
14
- const anotherValidatorAddress = '0xAnotherValidatorAddress'
15
-
16
- beforeEach(() => {
17
- global.state = {
18
- peers: [
19
- [ownerAddress, { bw: { up: 100, down: 100 } }],
20
- [validatorAddress, { bw: { up: 100, down: 100 } }],
21
- [anotherValidatorAddress, { bw: { up: 200, down: 200 } }]
22
- ]
23
- }
24
- global.msg = {
25
- sender: ownerAddress,
26
- contract: '0xContractAddress',
27
- staticCall: async (currency, method, args) => {
28
- if (method === 'balanceOf') {
29
- if (args[0] === validatorAddress) return BigInt(100000)
30
- if (args[0] === anotherValidatorAddress) return BigInt(100000)
31
- }
32
- return BigInt(0)
33
- },
34
- call: async (currency, method, args) => {
35
- // Mock implementation of msg.call
36
- }
37
- }
38
- validators = new Validators(tokenAddress, validatorState)
39
- })
40
-
41
- it('should initialize with default values', () => {
42
- expect(validators.name).to.equal('LeofcoinValidators')
43
- expect(validators.currency).to.equal(tokenAddress)
44
- expect(validators.validators).to.deep.equal([ownerAddress])
45
- expect(validators.totalValidators).to.equal(1)
46
- expect(validators.minimumBalance.toString()).to.equal(BigInt(50000).toString())
47
- })
48
-
49
- it('should change currency if sender is owner', () => {
50
- validators.changeCurrency('0xNewCurrencyAddress')
51
- expect(validators.currency).to.equal('0xNewCurrencyAddress')
52
- })
53
-
54
- it('should throw error if non-owner tries to change currency', () => {
55
- global.msg.sender = validatorAddress
56
- expect(() => validators.changeCurrency('0xNewCurrencyAddress')).to.throw('not an owner')
57
- })
58
-
59
- it('should add a validator if conditions are met', async () => {
60
- await validators.addValidator(validatorAddress)
61
- expect(validators.validators).to.include(validatorAddress)
62
- expect(validators.totalValidators).to.equal(2)
63
- })
64
-
65
- it('should throw error if adding an existing validator', async () => {
66
- await validators.addValidator(validatorAddress)
67
- await expect(validators.addValidator(validatorAddress)).to.be.rejectedWith(
68
- 'validator already exists'
69
- )
70
- })
71
-
72
- it('should throw error if validator balance is too low', async () => {
73
- global.msg.staticCall = async (currency, method, args) => BigInt(1000)
74
- await expect(validators.addValidator(anotherValidatorAddress)).to.be.rejectedWith(
75
- 'balance to low! got: 1000 need: 50000'
76
- )
77
- })
78
-
79
- it('should remove a validator if conditions are met', async () => {
80
- await validators.addValidator(validatorAddress)
81
- await validators.removeValidator(validatorAddress)
82
- expect(validators.validators).to.not.include(validatorAddress)
83
- expect(validators.totalValidators).to.equal(1)
84
- })
85
-
86
- it('should throw error if removing a non-existing validator', async () => {
87
- await expect(validators.removeValidator(validatorAddress)).to.be.rejectedWith(
88
- 'validator not found'
89
- )
90
- })
91
-
92
- it('should shuffle validator correctly', async () => {
93
- validators.shuffleValidator()
94
- expect(validators.currentValidator).to.include(ownerAddress)
95
- await validators.removeValidator(ownerAddress)
96
-
97
- global.msg.sender = validatorAddress
98
- await validators.addValidator(validatorAddress)
99
-
100
- validators.shuffleValidator()
101
- expect(validators.currentValidator).to.include(validatorAddress)
102
- })
103
-
104
- it('should return the current validator', () => {
105
- expect(validators.currentValidator).to.equal(ownerAddress)
106
- })
107
-
108
- it('should return state correctly', async () => {
109
- msg.sender = validatorAddress
110
- await validators.addValidator(validatorAddress)
111
- validatorState = validators.state
112
- expect(validatorState.validators).to.deep.equal([ownerAddress, validatorAddress])
113
- })
114
-
115
- it('should restore from state correctly', () => {
116
- const newValidators = new Validators(tokenAddress, validatorState)
117
- expect(newValidators.validators).to.deep.equal([ownerAddress, validatorAddress])
118
- expect(newValidators.totalValidators).to.equal(2)
119
- })
120
- })
1
+ import { describe, it, beforeEach } from 'node:test'
2
+ import assert from 'node:assert/strict'
3
+ import Validators from '../exports/validators.js'
4
+
5
+ describe('Validators', () => {
6
+ let validators
7
+ let validatorState
8
+ const tokenAddress = '0xTokenAddress'
9
+ const ownerAddress = '0xOwnerAddress'
10
+ const validatorAddress = '0xValidatorAddress'
11
+ const anotherValidatorAddress = '0xAnotherValidatorAddress'
12
+
13
+ beforeEach(() => {
14
+ global.state = {
15
+ peers: [
16
+ [ownerAddress, { bw: { up: 100, down: 100 } }],
17
+ [validatorAddress, { bw: { up: 100, down: 100 } }],
18
+ [anotherValidatorAddress, { bw: { up: 200, down: 200 } }]
19
+ ]
20
+ }
21
+ global.msg = {
22
+ sender: ownerAddress,
23
+ contract: '0xContractAddress',
24
+ staticCall: async (currency, method, args) => {
25
+ if (method === 'balanceOf') {
26
+ if (args[0] === validatorAddress) return BigInt(100000)
27
+ if (args[0] === anotherValidatorAddress) return BigInt(100000)
28
+ }
29
+ return BigInt(0)
30
+ },
31
+ call: async (currency, method, args) => {
32
+ // Mock implementation of msg.call
33
+ }
34
+ }
35
+ validators = new Validators(tokenAddress, validatorState)
36
+ })
37
+
38
+ it('should initialize with default values', () => {
39
+ assert.equal(validators.name, 'LeofcoinValidators')
40
+ assert.equal(validators.currency, tokenAddress)
41
+ assert.deepEqual(validators.validators, [ownerAddress])
42
+ assert.equal(validators.totalValidators, 1)
43
+ assert.equal(validators.minimumBalance.toString(), BigInt(50000).toString())
44
+ })
45
+
46
+ it('should change currency if sender is owner', () => {
47
+ validators.changeCurrency('0xNewCurrencyAddress')
48
+ assert.equal(validators.currency, '0xNewCurrencyAddress')
49
+ })
50
+
51
+ it('should throw error if non-owner tries to change currency', () => {
52
+ global.msg.sender = validatorAddress
53
+ assert.throws(() => validators.changeCurrency('0xNewCurrencyAddress'), {
54
+ message: 'not an owner'
55
+ })
56
+ })
57
+
58
+ it('should add a validator if conditions are met', async () => {
59
+ await validators.addValidator(validatorAddress)
60
+ assert.ok(validators.validators.includes(validatorAddress))
61
+ assert.equal(validators.totalValidators, 2)
62
+ })
63
+
64
+ it('should throw error if adding an existing validator', async () => {
65
+ await validators.addValidator(validatorAddress)
66
+ await assert.rejects(async () => await validators.addValidator(validatorAddress), {
67
+ message: 'validator already exists'
68
+ })
69
+ })
70
+
71
+ it('should throw error if validator balance is too low', async () => {
72
+ global.msg.staticCall = async (currency, method, args) => BigInt(1000)
73
+ await assert.rejects(async () => await validators.addValidator(anotherValidatorAddress), {
74
+ message: 'balance to low! got: 1000 need: 50000'
75
+ })
76
+ })
77
+
78
+ it('should remove a validator if conditions are met', async () => {
79
+ await validators.addValidator(validatorAddress)
80
+ await validators.removeValidator(validatorAddress)
81
+ assert.ok(!validators.validators.includes(validatorAddress))
82
+ assert.equal(validators.totalValidators, 1)
83
+ })
84
+
85
+ it('should throw error if removing a non-existing validator', async () => {
86
+ await assert.rejects(async () => await validators.removeValidator(validatorAddress), {
87
+ message: 'validator not found'
88
+ })
89
+ })
90
+
91
+ it('should shuffle validator correctly', async () => {
92
+ validators.shuffleValidator()
93
+ assert.ok(validators.currentValidator.includes(ownerAddress))
94
+ await validators.removeValidator(ownerAddress)
95
+
96
+ global.msg.sender = validatorAddress
97
+ await validators.addValidator(validatorAddress)
98
+
99
+ validators.shuffleValidator()
100
+ assert.ok(validators.currentValidator.includes(validatorAddress))
101
+ })
102
+
103
+ it('should return the current validator', () => {
104
+ assert.equal(validators.currentValidator, ownerAddress)
105
+ })
106
+
107
+ it('should return state correctly', async () => {
108
+ msg.sender = validatorAddress
109
+ await validators.addValidator(validatorAddress)
110
+ validatorState = validators.state
111
+ assert.deepEqual(validatorState.validators, [ownerAddress, validatorAddress])
112
+ })
113
+
114
+ it('should restore from state correctly', () => {
115
+ const newValidators = new Validators(tokenAddress, validatorState)
116
+ assert.deepEqual(newValidators.validators, [ownerAddress, validatorAddress])
117
+ assert.equal(newValidators.totalValidators, 2)
118
+ })
119
+ })
package/tsconfig.json CHANGED
@@ -1,13 +1,14 @@
1
- {
2
- "compilerOptions": {
3
- "module": "NodeNext",
4
- "declarationDir": "./exports",
5
- "moduleResolution": "NodeNext",
6
- "allowJs": true,
7
- "allowSyntheticDefaultImports": true,
8
- "resolveJsonModule": true,
9
- "declaration": true,
10
- "types": ["@leofcoin/types", "@leofcoin/types/global"]
11
- },
12
- "include": ["./src/**/*"]
13
- }
1
+ {
2
+ "compilerOptions": {
3
+ "module": "NodeNext",
4
+ "declarationDir": "./exports",
5
+ "moduleResolution": "NodeNext",
6
+ "allowJs": true,
7
+ "allowSyntheticDefaultImports": true,
8
+ "resolveJsonModule": true,
9
+ "declaration": true,
10
+ "outDir": "exports",
11
+ "types": ["@leofcoin/types", "@leofcoin/types/global"]
12
+ },
13
+ "include": ["./src/**/*"]
14
+ }