Skip to content

Commit

Permalink
setup structure
Browse files Browse the repository at this point in the history
  • Loading branch information
dlpigpen committed Jun 14, 2024
1 parent 7906129 commit b2c233f
Show file tree
Hide file tree
Showing 33 changed files with 55,692 additions and 1 deletion.
4 changes: 4 additions & 0 deletions examples/swaps-api-nextjs/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
PORT=3004
GENERATE_SOURCEMAP=false
DATAHUB_API_KEY=
RPC_URL=https://cosmos-mainnet-rpc.allthatnode.com:26657
4 changes: 4 additions & 0 deletions examples/swaps-api-nextjs/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
PORT=3004
GENERATE_SOURCEMAP=false
DATAHUB_API_KEY=
RPC_URL=https://cosmos-mainnet-rpc.allthatnode.com:26657
Binary file added examples/swaps-api-nextjs/.yarn/install-state.gz
Binary file not shown.
36 changes: 35 additions & 1 deletion examples/swaps-api-nextjs/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,35 @@
# Cross-chain Swaps using the Swing API
# Cross-chain Swaps using the Swing API in Next.js

The Cross-chain Swap feature is used for sending and signing an API transaction with Metamask, Keplr, Ton, and MultiversX wallets.


## Demo


## Getting started

To get started with this template, first install the npm dependencies:

```bash
yarn install
```

Next, run the development server:

```bash
Copy .env.example variables to your new .env at the same directoty
```

```bash
yarn start
```

Finally, open [http://localhost:3004](http://localhost:3004) in your browser to view the website.

## Swing integration

Visit [@swing.xyz/api](https://developers.swing.xyz/reference/api) find helpful API guides and references to integrate Swing API directly into your dApp, web or mobile application.

## Customizing

You can start editing this template by modifying the files in the `/src` folder. The site will auto-update as you edit these files.
29 changes: 29 additions & 0 deletions examples/swaps-api-nextjs/config-overrides.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require("dotenv").config()
const webpack = require("webpack")

module.exports = function override(config, env) {
config.plugins.push(
new webpack.ProvidePlugin({
Buffer: ["buffer", "Buffer"],
}),
new webpack.EnvironmentPlugin(["RPC_URL"]),
)
config.module.rules.push({
test: /\.m?js/,
resolve: {
fullySpecified: false,
},
})
config.resolve.fallback = {
buffer: false,
crypto: false,
events: false,
path: false,
stream: false,
fs: false,
string_decoder: false,
os: false,
url: false,
}
return config
}
62 changes: 62 additions & 0 deletions examples/swaps-api-nextjs/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package main

import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
)

var hostname string
var port int

func main() {
// flags declaration using flag package
// flag.StringVar(&hostname, "H", "https://rpc-cosmoshub.keplr.app", "Specify hostname")
flag.StringVar(&hostname, "H", "https://rpc.cosmos.network", "Specify hostname")
flag.IntVar(&port, "p", 8081, "Specify port")

flag.Parse() // after declaring flags we
http.HandleFunc("/", serveCorsProxy)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
}

// Serve a reverse proxy for a given url
func serveCorsProxy(res http.ResponseWriter, req *http.Request) {
proxyRequest, err := http.NewRequest(req.Method, hostname, req.Body)
proxyRequest.URL.Path = req.URL.Path
proxyRequest.URL.RawQuery = req.URL.RawQuery
if err != nil {
fmt.Printf("create request error: %v", err)
return
}
response, err := http.DefaultClient.Do(proxyRequest)
if err != nil {
fmt.Printf("proxy request error: %v", err)
return
}
setHeaders(response, &res)
body, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Printf("response read error: %v", err)
return
}
fmt.Printf("Running...")
res.WriteHeader(response.StatusCode)
_, _ = res.Write(body)
}

func setHeaders(src *http.Response, dest *http.ResponseWriter) {
header := (*dest).Header()
for name, values := range (*src).Header {
for _, value := range values {
header.Set(name, value)
}
}
header.Set("access-control-allow-headers", "Accept,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With")
header.Set("access-control-allow-methods", "GET, POST, OPTIONS")
header.Set("access-control-allow-origin", "http://localhost:3001")
header.Set("access-control-expose-headers", "Content-Length,Content-Range")
header.Set("access-control-max-age", "1728000")
}
Loading

0 comments on commit b2c233f

Please sign in to comment.