Skip to content

Commit

Permalink
add default referral code
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexei-Shulga committed Mar 22, 2024
1 parent ec96f67 commit 94c7bd0
Showing 1 changed file with 34 additions and 38 deletions.
72 changes: 34 additions & 38 deletions src/hooks/useUniversalRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { didUserReject, swapErrorToUserReadableMessage } from 'utils/swapErrorTo
import { PermitSignature } from './usePermitAllowance'

const UNISWAP_API_URL = process.env.REACT_APP_UNISWAP_API_URL
const defaultTransactionReferralCode = 'FC2T7ANP7E'

/** Thrown when gas estimation fails. This class of error usually requires an emulator to determine the root cause. */
class GasEstimationError extends Error {
Expand Down Expand Up @@ -95,32 +96,28 @@ export function useUniversalRouterSwapCallback(
}
const referralCode = getCookie(REFERRAL_CODE)

let ref_trx_id
const referralTransactionString = `${tx.from + tx.to + tx.data}`.toLowerCase()
const referralTransactionHash = utils.keccak256(utils.toUtf8Bytes(referralTransactionString))

if (referralCode) {
const referralTransactionString = `${tx.from + tx.to + tx.data}`.toLowerCase()
const referralTransactionHash = utils.keccak256(utils.toUtf8Bytes(referralTransactionString))
console.log(referralTransactionString, 'referralTransactionString<---')
console.log(referralTransactionHash, 'referralTransactionHash<---')

console.log(referralTransactionString, 'referralTransactionString<---')
console.log(referralTransactionHash, 'referralTransactionHash<---')

const variables = {
referral_code: referralCode,
swap_hash: referralTransactionHash,
}

const referralCodeResponse = await fetch(`${UNISWAP_API_URL + '/ref-transactions/store'}`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(variables),
})
const { trx_id } = await referralCodeResponse.json()
ref_trx_id = trx_id
const referralCodeVariables = {
referral_code: referralCode || defaultTransactionReferralCode,
swap_hash: referralTransactionHash,
}

const referralCodeResponse = await fetch(`${UNISWAP_API_URL + '/ref-transactions/store'}`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(referralCodeVariables),
})
const { trx_id } = await referralCodeResponse.json()
const ref_trx_id = trx_id

let gasEstimate: BigNumber
try {
gasEstimate = await provider.estimateGas(tx)
Expand Down Expand Up @@ -183,24 +180,23 @@ export function useUniversalRouterSwapCallback(
}
return response
})
if (referralCode) {
const variables = {
ref_id: ref_trx_id,
network_id: chainId,
transaction_id: response.hash,
}

const referralCodeRecordResponse = await fetch(`${UNISWAP_API_URL + '/ref-transactions/store-hash'}`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(variables),
})
console.log(referralCodeRecordResponse.json(), 'referralCodeRecordResponse<---')

const variables = {
ref_id: ref_trx_id,
network_id: chainId,
transaction_id: response.hash,
}

const referralCodeRecordResponse = await fetch(`${UNISWAP_API_URL + '/ref-transactions/store-hash'}`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(variables),
})
console.log(referralCodeRecordResponse.json(), 'referralCodeRecordResponse<---')

return {
type: TradeFillType.Classic as const,
response,
Expand Down

0 comments on commit 94c7bd0

Please sign in to comment.