Skip to content

Commit

Permalink
feat: added new contract to demonstrate new keyword usage (#450) (#467
Browse files Browse the repository at this point in the history
)

* feat: added new contract to demonstrate `new` keyword usage

Signed-off-by: Logan Nguyen <[email protected]>

* test: added unit tests for New contract

Signed-off-by: Logan Nguyen <[email protected]>

* feat: added createContractWithSalt() for New contract

Signed-off-by: Logan Nguyen <[email protected]>

---------

Signed-off-by: Logan Nguyen <[email protected]>
  • Loading branch information
quiet-node authored Oct 17, 2023
1 parent 0b698b8 commit 6000257
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
56 changes: 56 additions & 0 deletions contracts/solidity/new/New.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.20;

contract Target {
string public message;

function setMessage(string calldata _message) external {
message = _message;
}
}

contract TargetWithConstructor {
string public message;

constructor(string memory _message) {
message = _message;
}
}

contract New {
struct ContractInformation {
address contractAddr;
string message;
}

mapping(string => ContractInformation) public newContractsInfo;

function createContract(string calldata contractName, string calldata message) external {
Target newTarget = new Target();

newTarget.setMessage(message);

newContractsInfo[contractName] = ContractInformation({
contractAddr: address(newTarget),
message: newTarget.message()
});
}

function createContractWithData(string calldata contractName, string calldata message) external {
TargetWithConstructor newTargetWithConstructor = new TargetWithConstructor(message);

newContractsInfo[contractName] = ContractInformation({
contractAddr: address(newTargetWithConstructor),
message: newTargetWithConstructor.message()
});
}

function createContractWithSalt(bytes32 salt, string calldata contractName, string calldata message) external {
TargetWithConstructor newContractsWithSalt = new TargetWithConstructor{salt: salt}(message);

newContractsInfo[contractName] = ContractInformation({
contractAddr: address(newContractsWithSalt),
message: newContractsWithSalt.message()
});
}
}
1 change: 1 addition & 0 deletions test/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ const Contract = {
Errors: 'Errors',
Transaction: 'Transaction',
MessageFrameAddresses: 'MessageFrameAddresses',
New: 'New',
}

const CALL_EXCEPTION = 'CALL_EXCEPTION'
Expand Down
67 changes: 67 additions & 0 deletions test/solidity/new/New.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*-
*
* Hedera JSON RPC Relay - Hardhat Example
*
* Copyright (C) 2023 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

const { expect } = require('chai')
const { ethers } = require('hardhat')
const Constants = require('../../constants')

describe('New tests', () => {
let newContract
const CONTRACT_ALPHA = 'Alpha'
const MESSAGE_ALPHA = 'Message from Alpha contract'

before(async () => {
const newContractFactory = await ethers.getContractFactory(
Constants.Contract.New
)

newContract = await newContractFactory.deploy()
})

it('Create new contract using `new` keyword', async () => {
await newContract.createContract(CONTRACT_ALPHA, MESSAGE_ALPHA)
const newContractsInfo = await newContract.newContractsInfo(CONTRACT_ALPHA)

expect(ethers.utils.isAddress(newContractsInfo.contractAddr)).to.be.true
expect(newContractsInfo.message).to.eq(MESSAGE_ALPHA)
})

it('Create new contract using `new` keyword with data', async () => {
await newContract.createContractWithData(CONTRACT_ALPHA, MESSAGE_ALPHA)
const newContractsInfo = await newContract.newContractsInfo(CONTRACT_ALPHA)

expect(ethers.utils.isAddress(newContractsInfo.contractAddr)).to.be.true
expect(newContractsInfo.message).to.eq(MESSAGE_ALPHA)
})

it('Create new contract using `new` keyword with salt', async () => {
const SALT = ethers.utils.formatBytes32String('salt')

await newContract.createContractWithSalt(
SALT,
CONTRACT_ALPHA,
MESSAGE_ALPHA
)
const newContractsInfo = await newContract.newContractsInfo(CONTRACT_ALPHA)

expect(ethers.utils.isAddress(newContractsInfo.contractAddr)).to.be.true
expect(newContractsInfo.message).to.eq(MESSAGE_ALPHA)
})
})

0 comments on commit 6000257

Please sign in to comment.