-
Notifications
You must be signed in to change notification settings - Fork 54
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
5 changed files
with
152 additions
and
0 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,70 @@ | ||
package customtokens | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
type USDCService struct { | ||
attestationApi string | ||
} | ||
|
||
type USDCAttestationResponse struct { | ||
Status USDCAttestationStatus `json:"status"` | ||
Attestation string `json:"attestation"` | ||
} | ||
|
||
const ( | ||
version = "v1" | ||
attestationPath = "attestations" | ||
) | ||
|
||
type USDCAttestationStatus string | ||
|
||
const ( | ||
USDCAttestationStatusSuccess USDCAttestationStatus = "complete" | ||
USDCAttestationStatusPending USDCAttestationStatus = "pending_confirmations" | ||
) | ||
|
||
func NewUSDCService(usdcAttestationApi string) *USDCService { | ||
return &USDCService{attestationApi: usdcAttestationApi} | ||
} | ||
|
||
func (usdc *USDCService) TryGetAttestation(messageHash string) (USDCAttestationResponse, error) { | ||
fullAttestationUrl := fmt.Sprintf("%s/%s/%s/%s", usdc.attestationApi, version, attestationPath, messageHash) | ||
req, err := http.NewRequest("GET", fullAttestationUrl, nil) | ||
if err != nil { | ||
return USDCAttestationResponse{}, err | ||
} | ||
req.Header.Add("accept", "application/json") | ||
res, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return USDCAttestationResponse{}, err | ||
} | ||
defer res.Body.Close() | ||
body, err := io.ReadAll(res.Body) | ||
if err != nil { | ||
return USDCAttestationResponse{}, err | ||
} | ||
|
||
var response USDCAttestationResponse | ||
err = json.Unmarshal(body, &response) | ||
if err != nil { | ||
return USDCAttestationResponse{}, err | ||
} | ||
|
||
return response, nil | ||
} | ||
|
||
func (usdc *USDCService) IsAttestationComplete(messageHash string) (bool, string, error) { | ||
response, err := usdc.TryGetAttestation(messageHash) | ||
if err != nil { | ||
return false, "", err | ||
} | ||
if response.Status == USDCAttestationStatusSuccess { | ||
return true, response.Attestation, nil | ||
} | ||
return false, "", nil | ||
} |
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,77 @@ | ||
package customtokens | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestUSDCService_TryGetAttestation(t *testing.T) { | ||
t.Skipf("Skipping test because it uses the real USDC attestation API") | ||
usdcMessageHash := "0x912f22a13e9ccb979b621500f6952b2afd6e75be7eadaed93fc2625fe11c52a2" | ||
usdcService := NewUSDCService("https://iris-api-sandbox.circle.com") | ||
|
||
attestation, err := usdcService.TryGetAttestation(usdcMessageHash) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, USDCAttestationStatusPending, attestation.Status) | ||
require.Equal(t, "PENDING", attestation.Attestation) | ||
} | ||
|
||
func TestUSDCService_TryGetAttestationMock(t *testing.T) { | ||
response := USDCAttestationResponse{ | ||
Status: USDCAttestationStatusSuccess, | ||
Attestation: "720502893578a89a8a87982982ef781c18b193", | ||
} | ||
|
||
ts := getMockUSDCEndpoint(t, response) | ||
defer ts.Close() | ||
|
||
usdcService := NewUSDCService(ts.URL) | ||
attestation, err := usdcService.TryGetAttestation("0x912f22a13e9ccb979b621500f6952b2afd6e75be7eadaed93fc2625fe11c52a2") | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, response.Status, attestation.Status) | ||
require.Equal(t, response.Attestation, attestation.Attestation) | ||
} | ||
|
||
func TestUSDCService_TryGetAttestationMockError(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
})) | ||
defer ts.Close() | ||
|
||
usdcService := NewUSDCService(ts.URL) | ||
_, err := usdcService.TryGetAttestation("0x912f22a13e9ccb979b621500f6952b2afd6e75be7eadaed93fc2625fe11c52a2") | ||
require.Error(t, err) | ||
} | ||
|
||
func TestUSDCService_IsAttestationComplete(t *testing.T) { | ||
response := USDCAttestationResponse{ | ||
Status: USDCAttestationStatusSuccess, | ||
Attestation: "720502893578a89a8a87982982ef781c18b193", | ||
} | ||
|
||
ts := getMockUSDCEndpoint(t, response) | ||
defer ts.Close() | ||
|
||
usdcService := NewUSDCService(ts.URL) | ||
isReady, attestation, err := usdcService.IsAttestationComplete("0x912f22a13e9ccb979b621500f6952b2afd6e75be7eadaed93fc2625fe11c52a2") | ||
require.NoError(t, err) | ||
|
||
require.True(t, isReady) | ||
require.Equal(t, response.Attestation, attestation) | ||
} | ||
|
||
func getMockUSDCEndpoint(t *testing.T, response USDCAttestationResponse) *httptest.Server { | ||
responseBytes, err := json.Marshal(response) | ||
require.NoError(t, err) | ||
|
||
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
_, err := w.Write(responseBytes) | ||
require.NoError(t, err) | ||
})) | ||
} |
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