package main
import (
"context"
"encoding/json"
"fmt"
"io"
"math/big"
"net/http"
"net/url"
"os"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
)
const (
baseURL = "https://api.paxoslabs.com"
vaultAddress = "0xbbbb000000000000000000000000000000000001"
depositAsset = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
chainID = 1
amount = "1000000"
)
type Transaction struct {
To string `json:"to"`
Data string `json:"data"`
Value string `json:"value"`
}
func apiGet(path string, params url.Values) (map[string]interface{}, error) {
apiKey := os.Getenv("AMPLIFY_API_KEY")
u := fmt.Sprintf("%s%s?%s", baseURL, path, params.Encode())
req, _ := http.NewRequest("GET", u, nil)
req.Header.Set("x-api-key", apiKey)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
var result map[string]interface{}
json.Unmarshal(body, &result)
return result, nil
}
func main() {
pk, _ := crypto.HexToECDSA(os.Getenv("PRIVATE_KEY"))
fromAddr := crypto.PubkeyToAddress(pk.PublicKey)
client, _ := ethclient.Dial("https://eth.llamarpc.com")
ctx := context.Background()
// Step 1: Check authorization
fmt.Println("Checking authorization method...")
permitResp, _ := apiGet("/v2/core/permit", url.Values{
"vaultAddress": {vaultAddress},
"tokenAddress": {depositAsset},
"amount": {amount},
"userAddress": {fromAddr.Hex()},
"chainId": {fmt.Sprint(chainID)},
})
method := permitResp["method"].(string)
fmt.Printf("Authorization method: %s\n", method)
// Step 2: Handle authorization
depositParams := url.Values{
"vaultAddress": {vaultAddress},
"depositAsset": {depositAsset},
"depositAmount": {amount},
"userAddress": {fromAddr.Hex()},
"chainId": {fmt.Sprint(chainID)},
}
if method == "approval" {
fmt.Println("Sending approval transaction...")
approvalTx := permitResp["approvalTransaction"].(map[string]interface{})
encoded := approvalTx["encoded"].(string)
nonce, _ := client.PendingNonceAt(ctx, fromAddr)
gasPrice, _ := client.SuggestGasPrice(ctx)
tx := types.NewTransaction(
nonce,
common.HexToAddress(depositAsset),
big.NewInt(0),
60000,
gasPrice,
common.FromHex(encoded),
)
signer := types.NewEIP155Signer(big.NewInt(chainID))
signedTx, _ := types.SignTx(tx, signer, pk)
client.SendTransaction(ctx, signedTx)
fmt.Printf("Approval submitted: %s\n", signedTx.Hash().Hex())
}
// For permit: sign EIP-712 typed data and add to depositParams
// Step 3: Get deposit calldata
fmt.Println("Fetching deposit calldata...")
depositResp, _ := apiGet("/v2/amplify/deposit", depositParams)
txData := depositResp["transaction"].(map[string]interface{})
// Step 4: Sign and submit
fmt.Println("Submitting deposit transaction...")
nonce, _ := client.PendingNonceAt(ctx, fromAddr)
gasPrice, _ := client.SuggestGasPrice(ctx)
depositTx := types.NewTransaction(
nonce,
common.HexToAddress(txData["to"].(string)),
big.NewInt(0),
300000,
gasPrice,
common.FromHex(txData["data"].(string)),
)
signedDepositTx, _ := types.SignTx(
depositTx, types.NewEIP155Signer(big.NewInt(chainID)), pk,
)
client.SendTransaction(ctx, signedDepositTx)
fmt.Printf("Deposit submitted: %s\n", signedDepositTx.Hash().Hex())
}