Say I have a Solidity smart contract MultiToken.sol
which I am developing and testing using Hardhat and deploying to the RSK network.
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
contract MultiToken is ERC1155 {
constructor(string memory uri) ERC1155(uri) {}
}
I am deploying the smart contract in the tests before
section:
const { expect } = require('chai');
const { ethers } = require('hardhat');
describe('MultiToken', () => {
let multiToken;
const uri = 'https://token-cdn-domain/{id}.json';
before(async () => {
const factory = await ethers.getContractFactory('MultiToken');
multiToken = await factory.deploy(uri);
await multiToken.deployed();
});
it('MultiToken URI must be correct', async () => {
const multiTokenUri = await multiToken.uri(0);
expect(multiTokenUri).to.equal(uri);
});
});
I would like to be able to transfer some RBTC to my smart contract's address during the deployment transaction. Is it possible to top up smart contract's balance at the time of its deployment using Hardhat and Ethers?
Specifically, can I do this with a single transaction?
For reference, this is my hardhat.config.js
:
require('@nomiclabs/hardhat-waffle');
const { mnemonic } = require('./.secret.json');
module.exports = {
solidity: '0.8.4',
defaultNetwork: 'rskregtest',
networks: {
rskregtest: {
url: 'http://localhost:4444',
chainId: 33,
},
rsktestnet: {
chainId: 31,
url: 'https://public-node.testnet.rsk.co/',
accounts: {
mnemonic,
path: "m/44'/60'/0'/0",
},
},
},
};
You can declare the constructor
as payable
, and then override the value
param (default value 0) of the deploying transaction.
contract MultiToken is ERC1155 {
constructor(string memory uri) ERC1155(uri) payable {}
}
multiToken = await factory.deploy(uri, {
value: ethers.utils.parseUnits("1"), // 1 RBTC to wei
});
Docs: