-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
0b698b8
commit 6000257
Showing
3 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |