|
| 1 | +import { beginCell, Cell, toNano } from '@ton/core' |
| 2 | +import { Blockchain, SandboxContract, TreasuryContract } from '@ton/sandbox' |
| 3 | +import { crc32 } from 'zlib' |
| 4 | + |
| 5 | +import * as coverage from '../../coverage/coverage' |
| 6 | +import { generateRandomContractId } from '../../../src/utils' |
| 7 | + |
| 8 | +import * as counter from '../../../wrappers/examples/Counter' |
| 9 | +import * as dep from '../../../wrappers/libraries/Deployable' |
| 10 | + |
| 11 | +describe('Deployable - Opcodes', () => { |
| 12 | + it('should match opcodes', () => { |
| 13 | + expect(dep.Opcodes.initialize).toBe(crc32('Deployable_Initialize')) |
| 14 | + expect(dep.Opcodes.initializeAndSend).toBe(crc32('Deployable_InitializeAndSend')) |
| 15 | + }) |
| 16 | +}) |
| 17 | + |
| 18 | +describe('Deployable - Unit Tests', () => { |
| 19 | + let blockchain: Blockchain |
| 20 | + let deployer: SandboxContract<TreasuryContract> |
| 21 | + let deployableCode: Cell |
| 22 | + let counterCode: Cell |
| 23 | + let deployable: SandboxContract<dep.ContractClient> |
| 24 | + |
| 25 | + beforeAll(async () => { |
| 26 | + deployableCode = await dep.ContractClient.code() |
| 27 | + counterCode = await counter.ContractClient.code() |
| 28 | + |
| 29 | + blockchain = await Blockchain.create() |
| 30 | + blockchain.verbosity.debugLogs = true |
| 31 | + |
| 32 | + if (process.env['COVERAGE'] === 'true') { |
| 33 | + blockchain.enableCoverage() |
| 34 | + blockchain.verbosity.print = false |
| 35 | + blockchain.verbosity.vmLogs = 'vm_logs_verbose' |
| 36 | + } |
| 37 | + }) |
| 38 | + |
| 39 | + beforeEach(async () => { |
| 40 | + deployer = await blockchain.treasury('deployer') |
| 41 | + |
| 42 | + const data: dep.DeployableStorage = { |
| 43 | + owner: deployer.address, |
| 44 | + id: beginCell().storeStringTail('DeployableTests').storeUint(generateRandomContractId(), 32), |
| 45 | + } |
| 46 | + |
| 47 | + deployable = blockchain.openContract(dep.ContractClient.createFromConfig(data, deployableCode)) |
| 48 | + }) |
| 49 | + |
| 50 | + it('should initialize and replace code and data', async () => { |
| 51 | + const data = counter.builder.data.contractData |
| 52 | + .encode({ |
| 53 | + id: Number(generateRandomContractId()), |
| 54 | + value: 0, |
| 55 | + ownable: { |
| 56 | + owner: deployer.address, |
| 57 | + pendingOwner: undefined, |
| 58 | + }, |
| 59 | + }) |
| 60 | + .asCell() |
| 61 | + const result = await deployable.sendInitialize(deployer.getSender(), toNano('0.05'), { |
| 62 | + stateInit: { |
| 63 | + code: counterCode, |
| 64 | + data, |
| 65 | + }, |
| 66 | + }) |
| 67 | + expect(result.transactions).toHaveTransaction({ |
| 68 | + to: deployable.address, |
| 69 | + deploy: true, |
| 70 | + success: true, |
| 71 | + }) |
| 72 | + |
| 73 | + const counterContract = blockchain.openContract( |
| 74 | + counter.ContractClient.newAt(deployable.address), |
| 75 | + ) |
| 76 | + expect(await counterContract.getValue()).toBe(0) |
| 77 | + |
| 78 | + const resultIncrease = await counterContract.sendSetCount( |
| 79 | + deployer.getSender(), |
| 80 | + toNano('0.01'), |
| 81 | + { |
| 82 | + queryId: 1n, |
| 83 | + newCount: 42, |
| 84 | + }, |
| 85 | + ) |
| 86 | + expect(resultIncrease.transactions).toHaveTransaction({ |
| 87 | + to: counterContract.address, |
| 88 | + success: true, |
| 89 | + }) |
| 90 | + expect(await counterContract.getValue()).toBe(42) |
| 91 | + }) |
| 92 | + |
| 93 | + it('should initialize and send a message to self', async () => { |
| 94 | + const data = counter.builder.data.contractData |
| 95 | + .encode({ |
| 96 | + id: Number(generateRandomContractId()), |
| 97 | + value: 0, |
| 98 | + ownable: { |
| 99 | + owner: deployable.address, |
| 100 | + pendingOwner: undefined, |
| 101 | + }, |
| 102 | + }) |
| 103 | + .asCell() |
| 104 | + const result = await deployable.sendInitializeAndSend(deployer.getSender(), toNano('0.05'), { |
| 105 | + stateInit: { |
| 106 | + code: await counter.ContractClient.code(), |
| 107 | + data, |
| 108 | + }, |
| 109 | + selfMessage: { |
| 110 | + value: toNano('0.02'), |
| 111 | + body: counter.builder.message.in.setCount |
| 112 | + .encode({ |
| 113 | + queryId: 1n, |
| 114 | + newCount: 42, |
| 115 | + }) |
| 116 | + .asCell(), |
| 117 | + }, |
| 118 | + }) |
| 119 | + expect(result.transactions).toHaveTransaction({ |
| 120 | + to: deployable.address, |
| 121 | + deploy: true, |
| 122 | + success: true, |
| 123 | + }) |
| 124 | + expect(result.transactions).toHaveTransaction({ |
| 125 | + from: deployable.address, |
| 126 | + to: deployable.address, |
| 127 | + success: true, |
| 128 | + op: counter.opcodes.in.SetCount, |
| 129 | + }) |
| 130 | + |
| 131 | + const counterContract = blockchain.openContract( |
| 132 | + counter.ContractClient.newAt(deployable.address), |
| 133 | + ) |
| 134 | + expect(await counterContract.getValue()).toBe(42) |
| 135 | + }) |
| 136 | + |
| 137 | + it('should not allow non-owner to initialize', async () => { |
| 138 | + const other = await blockchain.treasury('other') |
| 139 | + |
| 140 | + const result = await deployable.sendInitialize(other.getSender(), toNano('0.05'), { |
| 141 | + stateInit: { |
| 142 | + code: Cell.EMPTY, |
| 143 | + data: Cell.EMPTY, |
| 144 | + }, |
| 145 | + }) |
| 146 | + expect(result.transactions).toHaveTransaction({ |
| 147 | + to: deployable.address, |
| 148 | + success: false, |
| 149 | + exitCode: dep.Errors.ErrorNotOwner, |
| 150 | + }) |
| 151 | + }) |
| 152 | + |
| 153 | + it('should not allow non-owner to initialize and send', async () => { |
| 154 | + const other = await blockchain.treasury('other') |
| 155 | + |
| 156 | + const result = await deployable.sendInitializeAndSend(other.getSender(), toNano('0.05'), { |
| 157 | + stateInit: { |
| 158 | + code: Cell.EMPTY, |
| 159 | + data: Cell.EMPTY, |
| 160 | + }, |
| 161 | + selfMessage: { |
| 162 | + value: 0n, |
| 163 | + body: Cell.EMPTY, |
| 164 | + }, |
| 165 | + }) |
| 166 | + expect(result.transactions).toHaveTransaction({ |
| 167 | + to: deployable.address, |
| 168 | + success: false, |
| 169 | + exitCode: dep.Errors.ErrorNotOwner, |
| 170 | + }) |
| 171 | + }) |
| 172 | + |
| 173 | + afterAll(async () => { |
| 174 | + if (process.env['COVERAGE'] === 'true') { |
| 175 | + await coverage.generateCoverageArtifacts(blockchain, 'deployable_unit_tests', [ |
| 176 | + { |
| 177 | + code: deployableCode, |
| 178 | + name: 'deployable', |
| 179 | + }, |
| 180 | + ]) |
| 181 | + } |
| 182 | + }) |
| 183 | +}) |
0 commit comments