Skip to content

Commit

Permalink
depricate currency public methods (#17)
Browse files Browse the repository at this point in the history
* depricate currency public methods

* fix lint
  • Loading branch information
0xAleksaOpacic authored Sep 24, 2024
1 parent 64dd6a1 commit b27b51d
Show file tree
Hide file tree
Showing 12 changed files with 232 additions and 153 deletions.
6 changes: 5 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ PRIVATE_KEY= ""
# Specify the wallet address associated with your private key
# Wallets can be created using the =nil; CLI
# This address will be used for transactions on the =nil; network
WALLET_ADDR= "0x"
WALLET_ADDR= "0x"

# Specify the faucet address
# Default value: 0x000100000000000000000000000000000FA00CE7
FAUCET_ADDR=0x000100000000000000000000000000000FA00CE7
23 changes: 5 additions & 18 deletions contracts/Currency.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,17 @@ pragma solidity ^0.8.0;
import "@nilfoundation/smart-contracts/contracts/NilCurrencyBase.sol";

contract Currency is NilCurrencyBase {
bytes pubkey;

constructor(string memory _currencyName) payable {
constructor(string memory _currencyName, bytes memory _pubkey) payable {
// Revert if the currency name is an empty string
require(bytes(_currencyName).length > 0, "Currency name must not be empty");

pubkey = _pubkey;
tokenName = _currencyName;
}
receive() external payable {}

/**
* @dev Sends currency to a specified address
* This is a workaround until we are able to send external messages to smart contracts
* For production, consider implementing access control, such as Ownable from OpenZeppelin
*/
function sendCurrencyPublic(address to, uint256 currencyId, uint256 amount) public {
sendCurrencyInternal(to, currencyId, amount);
}

/**
* @dev Mints new currency
* This is a workaround until we are able to send external messages to smart contracts
* For production, consider implementing access control, such as Ownable from OpenZeppelin
*/
function mintCurrencyPublic(uint256 amount) public {
mintCurrencyInternal(amount);
function verifyExternal(uint256 hash, bytes calldata signature) external view returns (bool) {
return Nil.validateSignature(pubkey, hash, signature);
}
}
4 changes: 4 additions & 0 deletions contracts/Faucet.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;

import "@nilfoundation/smart-contracts/contracts/Faucet.sol";
2 changes: 0 additions & 2 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import * as dotenv from "dotenv";

// Currency Tasks
import "./tasks/currency/info";
import "./tasks/currency/send";
import "./tasks/currency/mint";
import "./tasks/currency/mint-wallet";

// Core Tasks
Expand Down
5 changes: 4 additions & 1 deletion ignition/modules/Currency.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { buildModule } from "@nomicfoundation/hardhat-ignition/modules";

module.exports = buildModule("DeployCurrency", (m) => {
const token = m.contract("Currency", [m.getParameter("currencyName", "")]);
const token = m.contract("Currency", [
m.getParameter("currencyName", ""),
m.getParameter("pubkey", ""),
]);

return { token };
});
3 changes: 2 additions & 1 deletion ignition/parameters.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"DeployCurrency": {
"currencyName": "MyCurrency"
"currencyName": "MyCurrency",
"pubkey": "0x0000000000000000000000000000000000000000"
},
"DeployFactory": {
"feeToSetter": "0x0000000000000000000000000000000000000000"
Expand Down
23 changes: 1 addition & 22 deletions tasks/currency/README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ To deploy your own currency contract, use the following command:
npx hardhat ignition deploy ./ignition/modules/Currency.ts --network nil --parameters ./ignition/parameters.json
```

Make sure to set the `currencyName` in your `./ignition/parameters.json` file before deploying. Once the deployment is complete, you will receive the contract address in the response.
Make sure to set the `currencyName` and `public key` in your `./ignition/parameters.json` file before deploying. Once the deployment is complete, you will receive the contract address in the response.

### 2. Fetch Currency Data

Expand All @@ -47,27 +47,6 @@ npx hardhat currency_info --address <Currency Address> --network nil

Replace `<Currency Address>` with the actual deployed contract address

### 3. Send Native Currency

You can send native currency from one contract to another using the `send_currency` task. The task checks if the source contract has enough native currency, mints additional currency if necessary, and transfers it to the destination contract.

The destination contract must be payable to accept the currency

Run the following command to send currency:

```bash
npx hardhat send_currency --network nil --to <Recipient Address> --address <Sender Contract Address> --amount <Amount>
```
### 4. Mint Currency

To mint additional currency, use the `mint_currency` task. You can mint any specified amount of currency to the contract:

```bash
npx hardhat mint_currency --network nil --address <Currency Contract Address> --amount <Amount>
```

This will mint the specified amount of native currency

### Mint and Send Currency to a Wallet

To mint currency from two contracts and send it to a specified wallet, use the mint-wallet task:
Expand Down
94 changes: 74 additions & 20 deletions tasks/currency/mint-wallet.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,103 @@
import { task } from "hardhat/config";
import type { Currency } from "../../typechain-types";
import { createClient } from "../util/client";
import {
faucetWithdrawal,
mintAndSendCurrency,
sleep,
} from "../util/currencyUtils";

task(
"mint-wallet",
"Mint currency from two contracts and send it to a specified wallet",
)
.addParam("currency0", "The contract address of the first currency")
.addParam("currency1", "The contract address of the second currency")
.addParam("wallet", "The address of the wallet to receive the currency")
.addParam("amount", "The amount of currency to mint and send")
.setAction(async (taskArgs, hre) => {
const walletAddress = process.env.WALLET_ADDR;

if (!walletAddress) {
throw new Error("WALLET_ADDR is not set in environment variables");
}

const faucetAddress = process.env.FAUCET_ADDR;

if (!faucetAddress) {
throw new Error("FAUCET_ADDR is not set in environment variables");
}

const { wallet, publicClient, signer } = await createClient();

// Destructure parameters for clarity
const mintAmount = BigInt(taskArgs.amount);
const currency0Address = taskArgs.currency0;
const currency1Address = taskArgs.currency1;
const walletAddress = taskArgs.wallet;

// Attach to the Currency contracts
const CurrencyFactory = await hre.ethers.getContractFactory("Currency");
const currency0 = CurrencyFactory.attach(currency0Address) as Currency;
const currency1 = CurrencyFactory.attach(currency1Address) as Currency;
console.log(
`Starting mint and transfer process for currencies ${currency0Address} and ${currency1Address}`,
);

// Mint and send Currency0
// Withdraw from faucet for both currencies
console.log(
`Minting ${mintAmount} Currency0 to wallet ${walletAddress}...`,
`Withdrawing from faucet for currency 0 (${currency0Address})...`,
);
await currency0.mintCurrencyPublic(mintAmount);
await currency0.sendCurrencyPublic(
walletAddress,
await currency0.getCurrencyId(),
mintAmount,
await faucetWithdrawal(
currency0Address,
100000000000n,
faucetAddress,
hre,
publicClient,
);

// Mint and send Currency1
// Sleep for 2 second
console.log("Waiting 2 second to prevent sequence issues..");
await sleep(2000);

console.log(
`Minting ${mintAmount} Currency1 to wallet ${walletAddress}...`,
`Withdrawing from faucet for currency 1 (${currency1Address})...`,
);
await faucetWithdrawal(
currency1Address,
100000000000n,
faucetAddress,
hre,
publicClient,
);
await currency1.mintCurrencyPublic(mintAmount);
await currency1.sendCurrencyPublic(

// Attach to Currency contracts
const CurrencyFactory = await hre.ethers.getContractFactory("Currency");
const currency0 = CurrencyFactory.attach(currency0Address) as Currency;
const currency1 = CurrencyFactory.attach(currency1Address) as Currency;

// Mint and send currency for both contracts using the refactored utility function
console.log(`Minting and sending currency 0 (${currency0Address})...`);
await mintAndSendCurrency({
publicClient,
signer,
currencyContract: currency0,
contractAddress: currency0Address,
walletAddress,
await currency1.getCurrencyId(),
mintAmount,
);
hre,
});

// Sleep for 2 second
console.log("Waiting 2 second to prevent sequence issues...");
await sleep(2000);

console.log(`Minting and sending currency 1 (${currency1Address})...`);
await mintAndSendCurrency({
publicClient,
signer,
currencyContract: currency1,
contractAddress: currency1Address,
walletAddress,
mintAmount,
hre,
});

// Verify the balance of the recipient wallet for both currencies
// Verify recipient balances
const recipientBalanceCurrency0 =
await currency0.getCurrencyBalanceOf(walletAddress);
const recipientBalanceCurrency1 =
Expand Down
26 changes: 0 additions & 26 deletions tasks/currency/mint.ts

This file was deleted.

60 changes: 0 additions & 60 deletions tasks/currency/send.ts

This file was deleted.

11 changes: 9 additions & 2 deletions tasks/util/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,23 @@ import {
export async function createClient(): Promise<{
wallet: WalletV1;
publicClient: PublicClient;
signer: LocalECDSAKeySigner;
}> {
const walletAddress = process.env.WALLET_ADDR;

if (!walletAddress) {
throw new Error("WALLET_ADDR is not set in environment variables");
}

const endpoint = process.env.NIL_RPC_ENDPOINT;

if (!endpoint) {
throw new Error("NIL_RPC_ENDPOINT is not set in environment variables");
}

const publicClient = new PublicClient({
transport: new HttpTransport({
endpoint: walletAddress,
endpoint: endpoint,
}),
shardId: 1,
});
Expand All @@ -34,5 +41,5 @@ export async function createClient(): Promise<{
signer,
});

return { wallet, publicClient };
return { wallet, publicClient, signer };
}
Loading

0 comments on commit b27b51d

Please sign in to comment.