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

feat: added new contract to demonstrate new keyword usage (#450) #467

Merged
merged 3 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
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)
})
})