-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
46 changed files
with
1,335 additions
and
7 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
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,11 @@ | ||
# Contract | ||
|
||
The contract struct is used internally to associate an address and an ABI, so we can easily interact with the deployed contract. | ||
|
||
## New Contract | ||
|
||
This function is used mostly to instantiate the TxBuilder with the list of contracts that can be interacted with. | ||
|
||
```go | ||
contract := NewContract(address, abi) | ||
``` |
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,29 @@ | ||
# Transaction | ||
|
||
To create and send transactions, we need some on-chain info, encode the data correctly and broadcast the transaction to the network. | ||
|
||
## Send Transaction | ||
|
||
The `SendTransaction` function takes care of building the correct transaction and broadcasting it. | ||
|
||
```go | ||
txHash, err := t.SendTransaction(contractName, address, privateKey, value, message, args) | ||
``` | ||
|
||
:::info | ||
This function is meant to be used just internally by the `InteractWithContract` function. | ||
::: | ||
|
||
## Send Transaction to Smart Contracts | ||
|
||
This helper function makes it easy to send a transaction to a smart contract, it automatically instantie your wallet based on the `TxBuilder` mnemonic and the account ID. | ||
|
||
```go | ||
txHash, err := t.InteractWithContract(contractName, accountID, value, message, args) | ||
``` | ||
|
||
:::info | ||
The `contractName` variable is the string set in the TxBuilder constructor for the contract. | ||
|
||
The `accountID` is the id related to the path of the `mnemonic` usually you just need to use the id `0` | ||
::: |
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,23 @@ | ||
# Transaction Builder | ||
|
||
To create a transaction first you need to create a TxBuilder object. | ||
|
||
## Constructor | ||
|
||
Using the `NewTxBuilder` you can instantiate the TxBuilder. | ||
|
||
```go | ||
builder := NexTxBuilder( | ||
contracts: map[string]Contract{"erc20": NewContract{address, abi}}, | ||
mnemonic: mnemonic, | ||
customGasLimit: map[string]uint64{}, | ||
defaultGasLimit: uint64(200_000), | ||
requester: requester.NewClient().WithUnsecureWeb3Endpoint("https://proxy.evmos.org/web3"), | ||
) | ||
``` | ||
|
||
:::info | ||
`customGasLimit` allows you to set a custom gas limit for different `methods`, i.e. `map[string]uint64{"transfer":uint64(20_000)}` | ||
::: | ||
|
||
After having the builder instantiated, you can send [transactions](/lib/txbuilder/transaction) |
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,35 @@ | ||
package txbuilder | ||
|
||
import ( | ||
"github.com/hanchon/hanchond/lib/requester" | ||
) | ||
|
||
type TxBuilder struct { | ||
contracts map[string]Contract | ||
mnemonic string | ||
|
||
customGasLimit map[string]uint64 | ||
defaultGasLimit uint64 | ||
|
||
currentNonce map[string]uint64 | ||
|
||
requester requester.Client | ||
} | ||
|
||
func NexTxBuilder( | ||
contracts map[string]Contract, | ||
mnemonic string, | ||
customGasLimit map[string]uint64, | ||
defaultGasLimit uint64, | ||
requester requester.Client, | ||
) *TxBuilder { | ||
return &TxBuilder{ | ||
contracts: contracts, | ||
mnemonic: mnemonic, | ||
customGasLimit: customGasLimit, | ||
defaultGasLimit: defaultGasLimit, | ||
currentNonce: map[string]uint64{}, | ||
|
||
requester: requester, | ||
} | ||
} |
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,20 @@ | ||
package txbuilder | ||
|
||
import ( | ||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/accounts/abi" | ||
"github.com/ethereum/go-ethereum/common" | ||
) | ||
|
||
type Contract struct { | ||
address common.Address | ||
ABI abi.ABI | ||
} | ||
|
||
func NewContract(address string, abi abi.ABI) Contract { | ||
return Contract{ | ||
address: common.HexToAddress(address), | ||
ABI: abi, | ||
} | ||
} |
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,23 @@ | ||
package txbuilder | ||
|
||
import "math/big" | ||
|
||
func (t *TxBuilder) InteractWithContract( | ||
contractName string, | ||
accountID int, | ||
value *big.Int, | ||
message string, | ||
args ...interface{}, | ||
) (string, error) { | ||
wallet, account, err := WalletFromMnemonicWithAccountID(t.mnemonic, accountID) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
privateKey, err := wallet.PrivateKey(account) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return t.SendTransaction(contractName, account.Address, privateKey, value, message, args...) | ||
} |
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,9 @@ | ||
package txbuilder | ||
|
||
func (t *TxBuilder) GetGasLimit(method string) uint64 { | ||
v, ok := t.customGasLimit[method] | ||
if ok { | ||
return v | ||
} | ||
return t.defaultGasLimit | ||
} |
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,68 @@ | ||
package txbuilder | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
"fmt" | ||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/accounts/abi" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
) | ||
|
||
func (t *TxBuilder) SendTransaction(contractName string, address common.Address, privateKey *ecdsa.PrivateKey, value *big.Int, message string, args ...interface{}) (string, error) { | ||
var contractABI abi.ABI | ||
var contractAddress common.Address | ||
var err error | ||
|
||
if v, ok := t.contracts[contractName]; ok { | ||
contractABI = v.ABI | ||
contractAddress = v.address | ||
} else { | ||
return "", fmt.Errorf("invalid contract name") | ||
} | ||
|
||
v, ok := t.currentNonce[address.Hex()] | ||
nonce := uint64(0) | ||
if ok { | ||
nonce = v | ||
} else { | ||
nonce, err = t.requester.GetNonce(address.Hex()) | ||
if err != nil { | ||
return "", err | ||
} | ||
} | ||
|
||
gasLimit := t.GetGasLimit(message) | ||
gasPrice, err := t.requester.GasPrice() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
var data []byte | ||
data, err = contractABI.Pack(message, args...) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
tx := types.NewTransaction(nonce, contractAddress, value, gasLimit, gasPrice, data) | ||
|
||
chainID, err := t.requester.ChanID() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), privateKey) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
txhash, err := t.requester.BroadcastTx(signedTx) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
t.currentNonce[address.Hex()] = nonce + 1 | ||
|
||
return txhash, nil | ||
} |
Oops, something went wrong.