Skip to content

Commit

Permalink
Merge pull request #158 from peanutprotocol/develop
Browse files Browse the repository at this point in the history
token release
  • Loading branch information
Hugo0 authored Oct 17, 2024
2 parents 47bbb79 + c34005b commit f6e5dea
Show file tree
Hide file tree
Showing 13 changed files with 382 additions and 193 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export ALCHEMY_API_KEY=""
export MORALIS_API_KEY=""

# Peanut API key
export PEANUT_API_URL="https://api.staging.peanut.to"
export PEANUT_DEV_API_KEY=""
export ETHERSCAN_API_KEY=""

3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ src/data/package.json
test/basic/playground.test.ts
**.env

**/venv/


# Logs
logs
*.log
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"type": "module",
"scripts": {
"test": "npm run pkg-move && jest test/basic --silent --coverage",
"test:basic": "npm run pkg-move && jest test/basic --silent --coverage",
"test:unit": "jest test/unit --silent --coverage",
"test:basic": "npm run pkg-move && jest 'test/(unit|basic)' --silent --coverage",
"test:live": "npm run pkg-move && jest test/live --silent --runInBand",
"test:full": "npm run pkg-move && jest --coverage --silent --runInBand",
"prettier": "prettier . --write",
Expand Down
2 changes: 1 addition & 1 deletion src/consts/interfaces.consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export interface IPrepareDepositTxsResponse {
unsignedTxs: IPeanutUnsignedTransaction[]
}

export interface IPrepareXchainRequestFulfillmentTransactionProps {
export interface IPrepareXchainRequestFulfillmentTransactionResponse {
unsignedTxs: IPeanutUnsignedTransaction[]
feeEstimation: string
estimatedFromAmount: string
Expand Down
2 changes: 2 additions & 0 deletions src/data/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
requests==2.31.0
python-dotenv==1.0.0
14 changes: 14 additions & 0 deletions src/data/tokenDetails.json
Original file line number Diff line number Diff line change
Expand Up @@ -1708,6 +1708,20 @@
"name": "Coinbase Wrapped Staked ETH",
"symbol": "cbETH",
"logoURI": "https://market-data-images.s3.us-east-1.amazonaws.com/tokenImages/0xa6b0437c3347c5218ac238a541ae6a9c47e40725495c4c99053d5f8f1735f673.png"
},
{
"address": "0xA12cd3110a2496d3F87F9D9830FDdFC408f5b2E4",
"name": "Ed Nah-cat",
"symbol": "Ed",
"decimals": 18,
"logoURI": "https://dd.dexscreener.com/ds-data/tokens/base/0xa12cd3110a2496d3f87f9d9830fddfc408f5b2e4.png?size=xl&key=387099"
},
{
"address": "0x0c03ce270b4826ec62e7dd007f0b716068639f7b",
"name": "The Innovation Game",
"symbol": "TIG",
"decimals": 18,
"logoURI": "https://assets.coingecko.com/coins/images/50669/standard/TIG_Logo_-_200x200.png?1728670027"
}
]
},
Expand Down
7 changes: 7 additions & 0 deletions src/data/tokenDetailsManual.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@
"symbol": "Ed",
"decimals": 18,
"logoURI": "https://dd.dexscreener.com/ds-data/tokens/base/0xa12cd3110a2496d3f87f9d9830fddfc408f5b2e4.png?size=xl&key=387099"
},
{
"address": "0x0c03ce270b4826ec62e7dd007f0b716068639f7b",
"name": "The Innovation Game",
"symbol": "TIG",
"decimals": 18,
"logoURI": "https://assets.coingecko.com/coins/images/50669/standard/TIG_Logo_-_200x200.png?1728670027"
}
]
}
Expand Down
54 changes: 33 additions & 21 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ethers, getDefaultProvider, utils } from 'ethersv5'
import { EPeanutLinkType, IPeanutUnsignedTransaction } from './consts/interfaces.consts'
import { ERC20_ABI, LATEST_STABLE_BATCHER_VERSION } from './data'
import { config, getSquidRoute, interfaces, prepareApproveERC20Tx, resolveFromEnsName } from '.'
import { prepareXchainFromAmountCalculation } from './util'
import { prepareXchainFromAmountCalculation, normalizePath } from './util'

// INTERFACES
export interface ICreateRequestLinkProps {
Expand Down Expand Up @@ -36,17 +36,27 @@ export interface IPrepareRequestLinkFulfillmentTransactionProps {
tokenDecimals: number
}

export interface IPrepareXchainRequestFulfillmentTransactionProps {
export type IPrepareXchainRequestFulfillmentTransactionProps = {
senderAddress: string
fromToken: string
fromTokenDecimals: number
fromChainId: string
link: string
squidRouterUrl: string
provider: ethers.providers.Provider
apiUrl?: string
tokenType: EPeanutLinkType
}
} & (
| {
link: string
apiUrl?: string
APIKey?: string
}
| {
linkDetails: Pick<
IGetRequestLinkDetailsResponse,
'chainId' | 'recipientAddress' | 'tokenAmount' | 'tokenDecimals' | 'tokenAddress'
>
}
)

export interface ISubmitRequestLinkFulfillmentProps {
hash: string
Expand Down Expand Up @@ -124,7 +134,7 @@ export async function createRequestLink({
if (tokenSymbol) formData.append('tokenSymbol', tokenSymbol)
if (attachment) formData.append('attachment', attachment)

const apiResponse = await fetch(`${apiUrl}/request-links`, {
const apiResponse = await fetch(normalizePath(`${apiUrl}/request-links`), {
method: 'POST',
body: formData,
headers: {
Expand All @@ -151,7 +161,7 @@ export async function getRequestLinkDetails({
}: IGetRequestLinkDetailsProps): Promise<IGetRequestLinkDetailsResponse> {
const uuid = getUuidFromLink(link)

const apiResponse = await fetch(`${apiUrl}/request-links/${uuid}`, {
const apiResponse = await fetch(normalizePath(`${apiUrl}/request-links/${uuid}`), {
method: 'GET',
headers: {
'api-key': APIKey!,
Expand All @@ -167,24 +177,26 @@ export async function getRequestLinkDetails({
return responseData
}

export async function prepareXchainRequestFulfillmentTransaction({
senderAddress,
fromToken,
fromTokenDecimals,
fromChainId,
link,
squidRouterUrl,
provider,
apiUrl = 'https://api.peanut.to/',
tokenType,
}: IPrepareXchainRequestFulfillmentTransactionProps): Promise<interfaces.IPrepareXchainRequestFulfillmentTransactionProps> {
const linkDetails = await getRequestLinkDetails({ link: link, apiUrl: apiUrl })
let { tokenAddress: destinationToken } = linkDetails
export async function prepareXchainRequestFulfillmentTransaction(
props: IPrepareXchainRequestFulfillmentTransactionProps
): Promise<interfaces.IPrepareXchainRequestFulfillmentTransactionResponse> {
let { senderAddress, fromToken, fromTokenDecimals, fromChainId, squidRouterUrl, provider, tokenType } = props
let linkDetails: Pick<
IGetRequestLinkDetailsResponse,
'chainId' | 'recipientAddress' | 'tokenAmount' | 'tokenDecimals' | 'tokenAddress'
>
if ('linkDetails' in props) {
linkDetails = props.linkDetails
} else {
const { link, apiUrl = 'https://api.peanut.to/', APIKey } = props
linkDetails = await getRequestLinkDetails({ link, apiUrl, APIKey })
}
let {
chainId: destinationChainId,
recipientAddress,
tokenAmount: destinationTokenAmount,
tokenDecimals: destinationTokenDecimals,
tokenAddress: destinationToken,
} = linkDetails
if (recipientAddress.endsWith('.eth')) {
recipientAddress = await resolveFromEnsName({ ensName: recipientAddress })
Expand Down Expand Up @@ -379,7 +391,7 @@ export async function submitRequestLinkFulfillment({
}: ISubmitRequestLinkFulfillmentProps): Promise<ISubmitRequestLinkFulfillmentResponse> {
try {
const uuid = getUuidFromLink(link)
const apiResponse = await fetch(`${apiUrl}request-links/${uuid}`, {
const apiResponse = await fetch(normalizePath(`${apiUrl}/request-links/${uuid}`), {
method: 'PATCH',
body: JSON.stringify({
destinationChainFulfillmentHash: hash,
Expand Down
11 changes: 11 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -753,3 +753,14 @@ export async function prepareXchainFromAmountCalculation({
return null
}
}

export function normalizePath(url: string): string {
try {
const urlObject = new URL(url)
urlObject.pathname = urlObject.pathname.replace(/\/+/g, '/')
return urlObject.toString()
} catch (error: unknown) {
// Assume we are getting only a path
return url.replace(/\/+/g, '/')
}
}
Loading

0 comments on commit f6e5dea

Please sign in to comment.