Skip to content

Commit 4570d1c

Browse files
committed
adding erc20.go/erc721/etc to ethcontract
1 parent 8295203 commit 4570d1c

File tree

4 files changed

+333
-7
lines changed

4 files changed

+333
-7
lines changed

ethartifact/registry.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ package ethartifact
33
import (
44
"fmt"
55
"sort"
6+
"strings"
67

7-
"github.com/0xsequence/ethkit/ethcontract"
8+
"github.com/0xsequence/ethkit/ethcoder"
89
"github.com/0xsequence/ethkit/go-ethereum/accounts/abi"
910
)
1011

@@ -27,9 +28,9 @@ func (c *ContractRegistry) Add(artifact Artifact) error {
2728
if artifact.ContractName == "" {
2829
return fmt.Errorf("unable to register contract with empty name")
2930
}
30-
c.contracts[artifact.ContractName] = artifact
31+
c.contracts[strings.ToLower(artifact.ContractName)] = artifact
3132
c.names = append(c.names, artifact.ContractName)
32-
sort.Sort(sort.StringSlice(c.names))
33+
sort.Strings(c.names)
3334
return nil
3435
}
3536

@@ -43,7 +44,7 @@ func (c *ContractRegistry) Register(contractName string, contractABI abi.ABI, co
4344
}
4445

4546
func (s *ContractRegistry) RegisterJSON(contractName string, contractABIJSON string, contractBin []byte) (Artifact, error) {
46-
parsedABI, err := ethcontract.ParseABI(contractABIJSON)
47+
parsedABI, err := ethcoder.ParseABI(contractABIJSON)
4748
if err != nil {
4849
return Artifact{}, err
4950
}
@@ -74,7 +75,7 @@ func (c *ContractRegistry) MustRegisterJSON(contractName string, contractABIJSON
7475
}
7576

7677
func (c *ContractRegistry) MustGet(name string) Artifact {
77-
artifact, ok := c.Get(name)
78+
artifact, ok := c.Get(strings.ToLower(name))
7879
if !ok {
7980
panic(fmt.Sprintf("ethartifact: ContractRegistry#MustGet failed to get '%s'", name))
8081
}
@@ -86,15 +87,15 @@ func (c *ContractRegistry) ContractNames() []string {
8687
}
8788

8889
func (c *ContractRegistry) Get(name string) (Artifact, bool) {
89-
artifact, ok := c.contracts[name]
90+
artifact, ok := c.contracts[strings.ToLower(name)]
9091
return artifact, ok
9192
}
9293

9394
func (c *ContractRegistry) Encode(contractName, method string, args ...interface{}) ([]byte, error) {
9495
if c.contracts == nil {
9596
return nil, fmt.Errorf("contract registry cannot find contract %s", contractName)
9697
}
97-
artifact, ok := c.contracts[contractName]
98+
artifact, ok := c.contracts[strings.ToLower(contractName)]
9899
if !ok {
99100
return nil, fmt.Errorf("contract registry cannot find contract %s", contractName)
100101
}

ethcoder/abi_helpers.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,22 @@ import (
1313
"github.com/0xsequence/ethkit/go-ethereum/common/hexutil"
1414
)
1515

16+
func ParseABI(abiJSON string) (abi.ABI, error) {
17+
parsed, err := abi.JSON(strings.NewReader(abiJSON))
18+
if err != nil {
19+
return abi.ABI{}, fmt.Errorf("unable to parse abi json: %w", err)
20+
}
21+
return parsed, nil
22+
}
23+
24+
func MustParseABI(abiJSON string) abi.ABI {
25+
parsed, err := ParseABI(abiJSON)
26+
if err != nil {
27+
panic(err)
28+
}
29+
return parsed
30+
}
31+
1632
func ABIPackArguments(argTypes []string, argValues []interface{}) ([]byte, error) {
1733
if len(argTypes) != len(argValues) {
1834
return nil, errors.New("invalid arguments - types and values do not match")

ethcontract/contracts.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package ethcontract
2+
3+
import "github.com/0xsequence/ethkit/ethartifact"
4+
5+
var (
6+
7+
//go:embed contracts/ERC20.json
8+
artifact_erc20 string
9+
10+
// Contracts registry to have some contracts on hand during testing
11+
contractRegistry = ethartifact.NewContractRegistry()
12+
)
13+
14+
func init() {
15+
contractRegistry.MustAdd(ethartifact.MustParseArtifactJSON(artifact_erc20))
16+
// TODO: erc721, erc1155
17+
}
18+
19+
func GetContractArtifact(name string) (ethartifact.Artifact, bool) {
20+
return contractRegistry.Get(name)
21+
}

ethcontract/contracts/ERC20.json

Lines changed: 288 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,288 @@
1+
{
2+
"_format": "hh-sol-artifact-1",
3+
"contractName": "ERC20Mock",
4+
"sourceName": "contracts/mocks/ERC20Mock.sol",
5+
"abi": [
6+
{
7+
"inputs": [],
8+
"stateMutability": "nonpayable",
9+
"type": "constructor"
10+
},
11+
{
12+
"anonymous": false,
13+
"inputs": [
14+
{
15+
"indexed": true,
16+
"internalType": "address",
17+
"name": "owner",
18+
"type": "address"
19+
},
20+
{
21+
"indexed": true,
22+
"internalType": "address",
23+
"name": "spender",
24+
"type": "address"
25+
},
26+
{
27+
"indexed": false,
28+
"internalType": "uint256",
29+
"name": "value",
30+
"type": "uint256"
31+
}
32+
],
33+
"name": "Approval",
34+
"type": "event"
35+
},
36+
{
37+
"anonymous": false,
38+
"inputs": [
39+
{
40+
"indexed": true,
41+
"internalType": "address",
42+
"name": "from",
43+
"type": "address"
44+
},
45+
{
46+
"indexed": true,
47+
"internalType": "address",
48+
"name": "to",
49+
"type": "address"
50+
},
51+
{
52+
"indexed": false,
53+
"internalType": "uint256",
54+
"name": "value",
55+
"type": "uint256"
56+
}
57+
],
58+
"name": "Transfer",
59+
"type": "event"
60+
},
61+
{
62+
"inputs": [
63+
{
64+
"internalType": "address",
65+
"name": "owner",
66+
"type": "address"
67+
},
68+
{
69+
"internalType": "address",
70+
"name": "spender",
71+
"type": "address"
72+
}
73+
],
74+
"name": "allowance",
75+
"outputs": [
76+
{
77+
"internalType": "uint256",
78+
"name": "",
79+
"type": "uint256"
80+
}
81+
],
82+
"stateMutability": "view",
83+
"type": "function"
84+
},
85+
{
86+
"inputs": [
87+
{
88+
"internalType": "address",
89+
"name": "spender",
90+
"type": "address"
91+
},
92+
{
93+
"internalType": "uint256",
94+
"name": "value",
95+
"type": "uint256"
96+
}
97+
],
98+
"name": "approve",
99+
"outputs": [
100+
{
101+
"internalType": "bool",
102+
"name": "",
103+
"type": "bool"
104+
}
105+
],
106+
"stateMutability": "nonpayable",
107+
"type": "function"
108+
},
109+
{
110+
"inputs": [
111+
{
112+
"internalType": "address",
113+
"name": "owner",
114+
"type": "address"
115+
}
116+
],
117+
"name": "balanceOf",
118+
"outputs": [
119+
{
120+
"internalType": "uint256",
121+
"name": "",
122+
"type": "uint256"
123+
}
124+
],
125+
"stateMutability": "view",
126+
"type": "function"
127+
},
128+
{
129+
"inputs": [
130+
{
131+
"internalType": "address[]",
132+
"name": "_tokens",
133+
"type": "address[]"
134+
},
135+
{
136+
"internalType": "address",
137+
"name": "_to",
138+
"type": "address"
139+
},
140+
{
141+
"internalType": "uint256[]",
142+
"name": "_amounts",
143+
"type": "uint256[]"
144+
}
145+
],
146+
"name": "batchTransfer",
147+
"outputs": [],
148+
"stateMutability": "nonpayable",
149+
"type": "function"
150+
},
151+
{
152+
"inputs": [
153+
{
154+
"internalType": "address",
155+
"name": "spender",
156+
"type": "address"
157+
},
158+
{
159+
"internalType": "uint256",
160+
"name": "subtractedValue",
161+
"type": "uint256"
162+
}
163+
],
164+
"name": "decreaseAllowance",
165+
"outputs": [
166+
{
167+
"internalType": "bool",
168+
"name": "",
169+
"type": "bool"
170+
}
171+
],
172+
"stateMutability": "nonpayable",
173+
"type": "function"
174+
},
175+
{
176+
"inputs": [
177+
{
178+
"internalType": "address",
179+
"name": "spender",
180+
"type": "address"
181+
},
182+
{
183+
"internalType": "uint256",
184+
"name": "addedValue",
185+
"type": "uint256"
186+
}
187+
],
188+
"name": "increaseAllowance",
189+
"outputs": [
190+
{
191+
"internalType": "bool",
192+
"name": "",
193+
"type": "bool"
194+
}
195+
],
196+
"stateMutability": "nonpayable",
197+
"type": "function"
198+
},
199+
{
200+
"inputs": [
201+
{
202+
"internalType": "address",
203+
"name": "_address",
204+
"type": "address"
205+
},
206+
{
207+
"internalType": "uint256",
208+
"name": "_amount",
209+
"type": "uint256"
210+
}
211+
],
212+
"name": "mockMint",
213+
"outputs": [],
214+
"stateMutability": "nonpayable",
215+
"type": "function"
216+
},
217+
{
218+
"inputs": [],
219+
"name": "totalSupply",
220+
"outputs": [
221+
{
222+
"internalType": "uint256",
223+
"name": "",
224+
"type": "uint256"
225+
}
226+
],
227+
"stateMutability": "view",
228+
"type": "function"
229+
},
230+
{
231+
"inputs": [
232+
{
233+
"internalType": "address",
234+
"name": "to",
235+
"type": "address"
236+
},
237+
{
238+
"internalType": "uint256",
239+
"name": "value",
240+
"type": "uint256"
241+
}
242+
],
243+
"name": "transfer",
244+
"outputs": [
245+
{
246+
"internalType": "bool",
247+
"name": "",
248+
"type": "bool"
249+
}
250+
],
251+
"stateMutability": "nonpayable",
252+
"type": "function"
253+
},
254+
{
255+
"inputs": [
256+
{
257+
"internalType": "address",
258+
"name": "from",
259+
"type": "address"
260+
},
261+
{
262+
"internalType": "address",
263+
"name": "to",
264+
"type": "address"
265+
},
266+
{
267+
"internalType": "uint256",
268+
"name": "value",
269+
"type": "uint256"
270+
}
271+
],
272+
"name": "transferFrom",
273+
"outputs": [
274+
{
275+
"internalType": "bool",
276+
"name": "",
277+
"type": "bool"
278+
}
279+
],
280+
"stateMutability": "nonpayable",
281+
"type": "function"
282+
}
283+
],
284+
"bytecode": "0x608060405234801561001057600080fd5b50610997806100206000396000f3fe608060405234801561001057600080fd5b50600436106100be5760003560e01c80633950935111610076578063a457c2d71161005b578063a457c2d7146102f0578063a9059cbb14610329578063dd62ed3e14610362576100be565b8063395093511461028457806370a08231146102bd576100be565b806323b872dd116100a757806323b872dd1461012a5780632e72102f1461016d578063378934b41461024b576100be565b8063095ea7b3146100c357806318160ddd14610110575b600080fd5b6100fc600480360360408110156100d957600080fd5b5073ffffffffffffffffffffffffffffffffffffffff813516906020013561039d565b604080519115158252519081900360200190f35b6101186103b3565b60408051918252519081900360200190f35b6100fc6004803603606081101561014057600080fd5b5073ffffffffffffffffffffffffffffffffffffffff8135811691602081013590911690604001356103b9565b6102496004803603606081101561018357600080fd5b81019060208101813564010000000081111561019e57600080fd5b8201836020820111156101b057600080fd5b803590602001918460208302840111640100000000831117156101d257600080fd5b9193909273ffffffffffffffffffffffffffffffffffffffff8335169260408101906020013564010000000081111561020a57600080fd5b82018360208201111561021c57600080fd5b8035906020019184602083028401116401000000008311171561023e57600080fd5b509092509050610417565b005b6102496004803603604081101561026157600080fd5b5073ffffffffffffffffffffffffffffffffffffffff8135169060200135610509565b6100fc6004803603604081101561029a57600080fd5b5073ffffffffffffffffffffffffffffffffffffffff8135169060200135610517565b610118600480360360208110156102d357600080fd5b503573ffffffffffffffffffffffffffffffffffffffff1661055a565b6100fc6004803603604081101561030657600080fd5b5073ffffffffffffffffffffffffffffffffffffffff8135169060200135610582565b6100fc6004803603604081101561033f57600080fd5b5073ffffffffffffffffffffffffffffffffffffffff81351690602001356105c5565b6101186004803603604081101561037857600080fd5b5073ffffffffffffffffffffffffffffffffffffffff813581169160200135166105d2565b60006103aa33848461060a565b50600192915050565b60025490565b60006103c68484846106b9565b73ffffffffffffffffffffffffffffffffffffffff841660009081526001602090815260408083203380855292529091205461040d91869161040890866107ac565b61060a565b5060019392505050565b60005b818110156105015785858281811061042e57fe5b9050602002013573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1663a9059cbb8585858581811061047357fe5b905060200201356040518363ffffffff1660e01b8152600401808373ffffffffffffffffffffffffffffffffffffffff16815260200182815260200192505050602060405180830381600087803b1580156104cd57600080fd5b505af11580156104e1573d6000803e3d6000fd5b505050506040513d60208110156104f757600080fd5b505060010161041a565b505050505050565b6105138282610823565b5050565b33600081815260016020908152604080832073ffffffffffffffffffffffffffffffffffffffff8716845290915281205490916103aa91859061040890866108e6565b73ffffffffffffffffffffffffffffffffffffffff1660009081526020819052604090205490565b33600081815260016020908152604080832073ffffffffffffffffffffffffffffffffffffffff8716845290915281205490916103aa91859061040890866107ac565b60006103aa3384846106b9565b73ffffffffffffffffffffffffffffffffffffffff918216600090815260016020908152604080832093909416825291909152205490565b73ffffffffffffffffffffffffffffffffffffffff821661062a57600080fd5b73ffffffffffffffffffffffffffffffffffffffff831661064a57600080fd5b73ffffffffffffffffffffffffffffffffffffffff808416600081815260016020908152604080832094871680845294825291829020859055815185815291517f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b9259281900390910190a3505050565b73ffffffffffffffffffffffffffffffffffffffff82166106d957600080fd5b73ffffffffffffffffffffffffffffffffffffffff831660009081526020819052604090205461070990826107ac565b73ffffffffffffffffffffffffffffffffffffffff808516600090815260208190526040808220939093559084168152205461074590826108e6565b73ffffffffffffffffffffffffffffffffffffffff8084166000818152602081815260409182902094909455805185815290519193928716927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef92918290030190a3505050565b60008282111561081d57604080517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601760248201527f536166654d617468237375623a20554e444552464c4f57000000000000000000604482015290519081900360640190fd5b50900390565b73ffffffffffffffffffffffffffffffffffffffff821661084357600080fd5b60025461085090826108e6565b60025573ffffffffffffffffffffffffffffffffffffffff821660009081526020819052604090205461088390826108e6565b73ffffffffffffffffffffffffffffffffffffffff83166000818152602081815260408083209490945583518581529351929391927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9281900390910190a35050565b60008282018381101561095a57604080517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601660248201527f536166654d617468236164643a204f564552464c4f5700000000000000000000604482015290519081900360640190fd5b939250505056fea26469706673582212201e8282683b3b9580e5722aa29d3e976acdd4cd35c3e88cdd1abb688867d0547a64736f6c63430007040033",
285+
"deployedBytecode": "0x608060405234801561001057600080fd5b50600436106100be5760003560e01c80633950935111610076578063a457c2d71161005b578063a457c2d7146102f0578063a9059cbb14610329578063dd62ed3e14610362576100be565b8063395093511461028457806370a08231146102bd576100be565b806323b872dd116100a757806323b872dd1461012a5780632e72102f1461016d578063378934b41461024b576100be565b8063095ea7b3146100c357806318160ddd14610110575b600080fd5b6100fc600480360360408110156100d957600080fd5b5073ffffffffffffffffffffffffffffffffffffffff813516906020013561039d565b604080519115158252519081900360200190f35b6101186103b3565b60408051918252519081900360200190f35b6100fc6004803603606081101561014057600080fd5b5073ffffffffffffffffffffffffffffffffffffffff8135811691602081013590911690604001356103b9565b6102496004803603606081101561018357600080fd5b81019060208101813564010000000081111561019e57600080fd5b8201836020820111156101b057600080fd5b803590602001918460208302840111640100000000831117156101d257600080fd5b9193909273ffffffffffffffffffffffffffffffffffffffff8335169260408101906020013564010000000081111561020a57600080fd5b82018360208201111561021c57600080fd5b8035906020019184602083028401116401000000008311171561023e57600080fd5b509092509050610417565b005b6102496004803603604081101561026157600080fd5b5073ffffffffffffffffffffffffffffffffffffffff8135169060200135610509565b6100fc6004803603604081101561029a57600080fd5b5073ffffffffffffffffffffffffffffffffffffffff8135169060200135610517565b610118600480360360208110156102d357600080fd5b503573ffffffffffffffffffffffffffffffffffffffff1661055a565b6100fc6004803603604081101561030657600080fd5b5073ffffffffffffffffffffffffffffffffffffffff8135169060200135610582565b6100fc6004803603604081101561033f57600080fd5b5073ffffffffffffffffffffffffffffffffffffffff81351690602001356105c5565b6101186004803603604081101561037857600080fd5b5073ffffffffffffffffffffffffffffffffffffffff813581169160200135166105d2565b60006103aa33848461060a565b50600192915050565b60025490565b60006103c68484846106b9565b73ffffffffffffffffffffffffffffffffffffffff841660009081526001602090815260408083203380855292529091205461040d91869161040890866107ac565b61060a565b5060019392505050565b60005b818110156105015785858281811061042e57fe5b9050602002013573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1663a9059cbb8585858581811061047357fe5b905060200201356040518363ffffffff1660e01b8152600401808373ffffffffffffffffffffffffffffffffffffffff16815260200182815260200192505050602060405180830381600087803b1580156104cd57600080fd5b505af11580156104e1573d6000803e3d6000fd5b505050506040513d60208110156104f757600080fd5b505060010161041a565b505050505050565b6105138282610823565b5050565b33600081815260016020908152604080832073ffffffffffffffffffffffffffffffffffffffff8716845290915281205490916103aa91859061040890866108e6565b73ffffffffffffffffffffffffffffffffffffffff1660009081526020819052604090205490565b33600081815260016020908152604080832073ffffffffffffffffffffffffffffffffffffffff8716845290915281205490916103aa91859061040890866107ac565b60006103aa3384846106b9565b73ffffffffffffffffffffffffffffffffffffffff918216600090815260016020908152604080832093909416825291909152205490565b73ffffffffffffffffffffffffffffffffffffffff821661062a57600080fd5b73ffffffffffffffffffffffffffffffffffffffff831661064a57600080fd5b73ffffffffffffffffffffffffffffffffffffffff808416600081815260016020908152604080832094871680845294825291829020859055815185815291517f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b9259281900390910190a3505050565b73ffffffffffffffffffffffffffffffffffffffff82166106d957600080fd5b73ffffffffffffffffffffffffffffffffffffffff831660009081526020819052604090205461070990826107ac565b73ffffffffffffffffffffffffffffffffffffffff808516600090815260208190526040808220939093559084168152205461074590826108e6565b73ffffffffffffffffffffffffffffffffffffffff8084166000818152602081815260409182902094909455805185815290519193928716927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef92918290030190a3505050565b60008282111561081d57604080517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601760248201527f536166654d617468237375623a20554e444552464c4f57000000000000000000604482015290519081900360640190fd5b50900390565b73ffffffffffffffffffffffffffffffffffffffff821661084357600080fd5b60025461085090826108e6565b60025573ffffffffffffffffffffffffffffffffffffffff821660009081526020819052604090205461088390826108e6565b73ffffffffffffffffffffffffffffffffffffffff83166000818152602081815260408083209490945583518581529351929391927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9281900390910190a35050565b60008282018381101561095a57604080517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601660248201527f536166654d617468236164643a204f564552464c4f5700000000000000000000604482015290519081900360640190fd5b939250505056fea26469706673582212201e8282683b3b9580e5722aa29d3e976acdd4cd35c3e88cdd1abb688867d0547a64736f6c63430007040033",
286+
"linkReferences": {},
287+
"deployedLinkReferences": {}
288+
}

0 commit comments

Comments
 (0)