github.com/bang9ming9/go-hardhat


License
MIT
Install
go get github.com/bang9ming9/go-hardhat

Documentation

go-hardhat

golang ์œผ๋กœ ๊ฐœ๋ฐœ๋œ ์ปจํŠธ๋ž™ํŠธ ๊ฐœ๋ฐœ ๋„๊ตฌ(tool) ์ž…๋‹ˆ๋‹ค.
์ปจํŠธ๋ž™ํŠธ ๊ฐœ๋ฐœ์„ ์œ„ํ•œ ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ์„ ๋„์™€์ฃผ๋ฉฐ, ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•œ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ๊ณผ ์œ ํ‹ธ ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

Build

# go 1.22
git clone https://github.com/bang9ming9/go-hardhat && cd go-hardhat

### bms: bang9ming9 solidity ###

# case1: /usr/local/bin ์— ๋นŒ๋“œํ•˜๊ธฐ
sudo go build -o /usr/local/bin/bms .

# case2: ์›ํ•˜๋Š” ๊ฒฝ๋กœ ๋นŒ๋“œ ํ›„ PATH ๋“ฑ๋กํ•˜๊ธฐ
go build -o <YOUEPATH>/bms .
echo 'export PATH=$PATH:<YOUEPATH>/bms' >> ~/.zshrc # or ~/.bashrc

Basic Useage

ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

mkdir my-contract && cd my-contract
bms init my-contract

go.mod ํŒŒ์ผ ์ƒ์„ฑ๊ณผ ๋™์‹œ์— contracts, test ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

contracts ๋””๋ ‰ํ† ๋ฆฌ์—๋Š” remapping.txt, .prettierrc ํŒŒ์ผ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
ํ•ด๋‹น ํŒŒ์ผ์€ VSCode ์˜ Solidity Extention ์—์„œ ์œ ์šฉํ•˜๊ฒŒ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.


VSCode์—์„œ editor.formatOnSave๋ฅผ true๋กœ ์„ค์ •ํ•˜๋ฉด ํŒŒ์ผ์„ ์ €์žฅํ•  ๋•Œ ์ž๋™์œผ๋กœ ์ฝ”๋“œ๊ฐ€ ํฌ๋งทํŒ…๋ฉ๋‹ˆ๋‹ค.

setting.json

{
    ...
    "editor.formatOnSave": true,
    ...
}

test ๋””๋ ‰ํ† ๋ฆฌ์—๋Š” bms ๋ช…๋ น์–ด์™€ go-hardhat ์ฝ”๋“œ ๋ฒ„์ „์„ ์ผ์น˜์‹œํ‚ค๊ธฐ ์œ„ํ•ด base.go ํŒŒ์ผ์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.

์ปจํŠธ๋ž™ํŠธ ์ปดํŒŒ์ผ

ํ”„๋กœ์ ํŠธ์˜ contracts ๋””๋ ‰ํ† ๋ฆฌ์— ์žˆ๋Š” ๋ชจ๋“  .sol ํŒŒ์ผ์„ ์ฐพ์•„ ์ปดํŒŒ์ผํ•œ ํ›„, golang ์œผ๋กœ ๋ฐ”์ธ๋”ฉ ํ•ฉ๋‹ˆ๋‹ค.

bms compile <solc-version>

compile ๋ช…๋ น์–ด์—๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์˜ต์…˜์ด ์žˆ์œผ๋ฉฐ, bms compile -h๋ฅผ ํ†ตํ•ด ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, OpenZeppelin ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๊ณ  ํ•ด๋‹น ๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ contracts ํด๋”์— ํฌํ•จ๋˜์–ด ์žˆ๋‹ค๋ฉด,
--exclude ./contracts/openzeppelin-contracts ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ์ปดํŒŒ์ผ ๋Œ€์ƒ์—์„œ ์ œ์™ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ…Œ์ŠคํŠธ ์ฝ”๋“œ

import (
    ...
	"testing"
	"github.com/bang9ming9/go-hardhat/bms"
	utils "github.com/bang9ming9/go-hardhat/bms/utils"
    ...
)

// ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ๋ฐฑ์•ค๋“œ ์ƒ์„ฑ
func ... (t *testing.T){
    ...
    backend := bms.NewBacked(t)
    ...
}

// ํ…Œ์ŠคํŠธ ์ง€๊ฐ‘ ๊ฐ€์ ธ์˜ค๊ธฐ
func ... (t *testing.T){
    ...
    eoas := bms.GetEOAs(t, 10)
    ...
}

// ํŠธ๋žœ์žญ์…˜ ์‹คํ–‰ํ•˜๊ธฐ
func ...(t *testing.T) {
    ...
    backend := bms.NewBacked(t)
    eoas := bms.GetEOAs(t, 10)
    txs := utils.NewTxPool(backend)

    // case: ๋‹จ์ผ ํŠธ๋žœ์žญ์…˜ ์„ฑ๊ณต
    require.NoError(t, txs.Exec(utils.SendDynamicTx(backend, owner, &eoa.From, []byte{})))

    // case: ํŠธ๋žœ์žญ์…˜ ์„ฑ๊ณต 
    for _, eoa := range eoas {
        // TxPool.Exec ์„ ํ†ตํ•ด ํŠธ๋žœ์žญ์…˜ ๋ชฉ๋ก ์บ์‹ฑ
        require.NoError(t, txs.Exec(utils.SendDynamicTx(backend, owner, &eoa.From, []byte{})))
    }
    // TxPool.AllReceiptStatusSuccessful ์—์„œ ์—๋Ÿฌ๊ฐ€ ๋ฆฌํ„ด๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด ๋ชจ๋‘ ์„ฑ๊ณตํ•œ ํŠธ๋žœ์žญ์…˜
    require.NoError(t, txs.AllReceiptStatusSuccessful(ctx))

    // case: ์˜ˆ์ƒ๋˜๋Š” ํŠธ๋žœ์žญ์…˜ ์‹คํŒจ
    require.Error(t, txs.Exec(utils.SendDynamicTx(backend, owner, &eoa.From, []byte{})))
    _, err := txs.Exec(utils.SendDynamicTx(backend, owner, &eoa.From, []byte{}))
    t.Log(err) // err ์„ ํ†ตํ•ด์„œ revert message ํ™•์ธ ๊ฐ€๋Šฅ
}

๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์€ bm-governance/test/b9m9_test.go ์„ ์ฐธ๊ณ ํ•ด ์ฃผ์„ธ์š”.