This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Factor out get or create address into one sql string * Factor out getChecksumAddress method in address repo * Update address repo methods to not need a receiver * Move address repository to libraries/shared
- Loading branch information
1 parent
0310431
commit f6ab938
Showing
8 changed files
with
115 additions
and
110 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// VulcanizeDB | ||
// Copyright © 2019 Vulcanize | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
|
||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package repository | ||
|
||
import ( | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/jmoiron/sqlx" | ||
|
||
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" | ||
) | ||
|
||
const getOrCreateAddressQuery = `WITH addressId AS ( | ||
INSERT INTO addresses (address) VALUES ($1) ON CONFLICT DO NOTHING RETURNING id | ||
) | ||
SELECT id FROM addresses WHERE address = $1 | ||
UNION | ||
SELECT id FROM addressId` | ||
|
||
func GetOrCreateAddress(db *postgres.DB, address string) (int64, error) { | ||
checksumAddress := getChecksumAddress(address) | ||
|
||
var addressId int64 | ||
getOrCreateErr := db.Get(&addressId, getOrCreateAddressQuery, checksumAddress) | ||
|
||
return addressId, getOrCreateErr | ||
} | ||
|
||
func GetOrCreateAddressInTransaction(tx *sqlx.Tx, address string) (int64, error) { | ||
checksumAddress := getChecksumAddress(address) | ||
|
||
var addressId int64 | ||
getOrCreateErr := tx.Get(&addressId, getOrCreateAddressQuery, checksumAddress) | ||
|
||
return addressId, getOrCreateErr | ||
} | ||
|
||
func GetAddressById(db *postgres.DB, id int64) (string, error) { | ||
var address string | ||
getErr := db.Get(&address, `SELECT address FROM public.addresses WHERE id = $1`, id) | ||
return address, getErr | ||
} | ||
|
||
func getChecksumAddress(address string) string { | ||
stringAddressToCommonAddress := common.HexToAddress(address) | ||
return stringAddressToCommonAddress.Hex() | ||
} |
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
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.