Skip to content

Commit

Permalink
chore: pay-request v2 migration (#1464)
Browse files Browse the repository at this point in the history
Co-authored-by: Anh Nguyen <anhnh@dwarvesv.com>
  • Loading branch information
anhnh12 and Anh Nguyen authored Aug 26, 2023
1 parent 42592f8 commit ad9b763
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 8 deletions.
5 changes: 4 additions & 1 deletion src/adapters/mochi-pay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ class MochiPay extends Fetcher {
}) {
return await this.jsonFetch(`${MOCHI_PAY_API_BASE_URL}/pay-requests`, {
method: "POST",
body,
body: {
...body,
from_platform: "discord",
},
})
}

Expand Down
6 changes: 5 additions & 1 deletion src/commands/pay/link/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import mochiPay from "adapters/mochi-pay"
import profile from "adapters/profile"
import { CommandInteraction, Message, MessageOptions } from "discord.js"
import { APIError } from "errors"
import { parseUnits } from "ethers/lib/utils"
import fs from "fs"
import * as qrcode from "qrcode"
import { RunResult } from "types/common"
Expand Down Expand Up @@ -135,7 +136,10 @@ export async function run({

const res: any = await mochiPay.generatePaymentCode({
profileId,
amount: amount.toString(),
amount: parseUnits(
amount.toLocaleString().replaceAll(",", ""),
decimal,
).toString(),
token,
note,
type: "paylink",
Expand Down
11 changes: 9 additions & 2 deletions src/commands/pay/me/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { MOCHI_ACTION_PAY_ME, MOCHI_PLATFORM_DISCORD } from "utils/constants"
import { reply } from "utils/discord"
import { sendNotificationMsg } from "utils/kafka"
import { dmUser } from "../../../utils/dm"
import { parseUnits } from "ethers/lib/utils"
import { getToken } from "../../../utils/tip-bot"

const typePayRequest = 16
export async function run({
Expand Down Expand Up @@ -53,9 +55,14 @@ export async function run({
}
const { id: profileId, profile_name, associated_accounts: accounts } = pfRes

const t = await getToken(token)

const res: any = await mochiPay.generatePaymentCode({
profileId,
amount: amount.toString(),
amount: parseUnits(
amount.toLocaleString().replaceAll(",", ""),
t?.decimal ?? 0,
).toString(),
token,
type: "payme",
note,
Expand Down Expand Up @@ -270,7 +277,7 @@ export function preprocessTarget(

function isValidEmail(target?: string): boolean {
if (!target) return false
const expression: RegExp = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i
const expression = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i
return expression.test(target)
}

Expand Down
7 changes: 6 additions & 1 deletion src/commands/tip/mail/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { UnsupportedTokenError } from "../../../errors/unsupported-token"
import { composeDiscordSelectionRow } from "../../../ui/discord/select-menu"
import { formatDigit, isValidTipAmount } from "../../../utils/defi"
import { getProfileIdByDiscord } from "../../../utils/profile"
import { parseUnits } from "ethers/lib/utils"

type MailUser = {
email: string
Expand Down Expand Up @@ -81,7 +82,10 @@ export async function execute(
// create pay link
const res: any = await mochiPay.generatePaymentCode({
profileId: payload.from.profile_global_id,
amount: payload.originalAmount.toString(),
amount: parseUnits(
payload.originalAmount.toLocaleString().replaceAll(",", ""),
payload.decimal,
).toString(),
token: payload.token,
type: "paylink",
note: payload.note,
Expand Down Expand Up @@ -298,6 +302,7 @@ async function validateAndTransfer(
fractionDigits: decimal,
})
payload.token_price = balance.token?.price
payload.decimal = decimal
return execute(msgOrInteraction, payload)
}

Expand Down
7 changes: 6 additions & 1 deletion src/commands/tip/telegram/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { InsufficientBalanceError } from "../../../errors/insufficient-balance"
import { UnsupportedTokenError } from "../../../errors/unsupported-token"
import { formatDigit, isValidTipAmount } from "../../../utils/defi"
import { getProfileIdByDiscord } from "../../../utils/profile"
import { parseUnits } from "ethers/lib/utils"

type TelegramUser = {
id: number
Expand Down Expand Up @@ -91,7 +92,10 @@ export async function execute(
// create pay link
const res: any = await mochiPay.generatePaymentCode({
profileId: payload.from.profile_global_id,
amount: payload.originalAmount.toString(),
amount: parseUnits(
payload.originalAmount.toLocaleString().replaceAll(",", ""),
payload.decimal,
).toString(),
token: payload.token,
note: payload.note,
type: "paylink",
Expand Down Expand Up @@ -303,6 +307,7 @@ async function validateAndTransfer(
fractionDigits: decimal,
})
payload.token_price = balance.token?.price
payload.deciaml = decimal
return execute(msgOrInteraction, payload)
}

Expand Down
7 changes: 6 additions & 1 deletion src/commands/tip/twitter/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import profile from "../../../adapters/profile"
import { formatDigit, isValidTipAmount } from "../../../utils/defi"
import { DiscordWalletTransferError } from "../../../errors/discord-wallet-transfer"
import { composeDiscordSelectionRow } from "../../../ui/discord/select-menu"
import { parseUnits } from "ethers/lib/utils"

type TwitterUser = {
username: string
Expand Down Expand Up @@ -79,7 +80,10 @@ export async function execute(
// create pay link
const res: any = await mochiPay.generatePaymentCode({
profileId: payload.from.profile_global_id,
amount: payload.originalAmount.toString(),
amount: parseUnits(
payload.originalAmount.toLocaleString().replaceAll(",", ""),
payload.decimal,
).toString(),
token: payload.token,
note: payload.note,
type: "paylink",
Expand Down Expand Up @@ -291,6 +295,7 @@ async function validateAndTransfer(
fractionDigits: decimal,
})
payload.token_price = balance.token?.price
payload.decimal = decimal
return execute(msgOrInteraction, payload)
}

Expand Down
10 changes: 9 additions & 1 deletion src/commands/wallet/new/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import mochiPay from "adapters/mochi-pay"
import profile from "adapters/profile"
import { CommandInteraction, Message, MessageOptions } from "discord.js"
import { APIError } from "errors"
import { parseUnits } from "ethers/lib/utils"
import fs from "fs"
import * as qrcode from "qrcode"
import { RunResult } from "types/common"
Expand All @@ -21,6 +22,7 @@ import {
TokenEmojiKey,
} from "utils/common"
import { reply } from "utils/discord"
import { getToken } from "../../../utils/tip-bot"

export async function run({
msgOrInteraction,
Expand Down Expand Up @@ -87,10 +89,16 @@ export async function run({
await i.deferUpdate()
return
}

const t = await getToken(token)

await i.deferReply({ ephemeral: true })
const res: any = await mochiPay.generatePaymentCode({
profileId,
amount: amount.toString(),
amount: parseUnits(
amount.toLocaleString().replaceAll(",", ""),
t?.decimal ?? 0,
).toString(),
token,
note,
type: "paylink",
Expand Down
80 changes: 80 additions & 0 deletions src/types/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,13 +332,26 @@ export interface ModelProductBotCommand {
code?: string
created_at?: string
description?: string
discord_alias?: string
discord_command?: string
id?: number
scope?: number
telegram_alias?: string
telegram_command?: string
updated_at?: string
}

export interface ModelProductChangelogs {
content?: string
created_at?: string
github_url?: string
id?: number
product?: number
thumbnail_url?: string
title?: string
updated_at?: string
}

export interface ModelProductMetadataEmojis {
code?: string
discord_id?: string
Expand Down Expand Up @@ -743,6 +756,11 @@ export interface RequestOffchainTransferRequest {
transfer_type?: string
}

export interface RequestOnboardingStartRequest {
platform: "discord" | "telegram"
profile_id: string
}

export interface RequestRoleReactionRequest {
guild_id?: string
message_id?: string
Expand Down Expand Up @@ -1120,6 +1138,42 @@ export interface ResponseDefaultRoleResponse {
data?: ResponseDefaultRole
}

export interface ResponseDexPair {
address?: string
base_token?: ResponseDexToken
chain_id?: string
created_at?: number
dex_id?: string
fdv?: number
holders?: ResponseDexTokenHolder[]
id?: string
liquidity_usd?: number
market_cap_usd?: number
name?: string
owner?: string
price?: number
price_percent_change_24h?: number
price_usd?: number
quote_token?: ResponseDexToken
txn_24h_buy?: number
txn_24h_sell?: number
url?: Record<string, string>
volume_usd_24h?: number
}

export interface ResponseDexToken {
address?: string
name?: string
symbol?: string
}

export interface ResponseDexTokenHolder {
address?: string
alias?: string
balance?: number
percent?: number
}

export interface ResponseDiscordGuildResponse {
bot_addable?: boolean
bot_arrived?: boolean
Expand Down Expand Up @@ -1464,6 +1518,10 @@ export interface ResponseGetUserResponse {
data?: ResponseUser
}

export interface ResponseGetVaultResponse {
data?: ModelVault
}

export interface ResponseGetVaultsResponse {
data?: ModelVault[]
}
Expand Down Expand Up @@ -1923,6 +1981,20 @@ export interface ResponseOffchainTipBotTransferTokenResponse {
data?: ResponseOffchainTipBotTransferToken[]
}

export interface ResponseOnboardingStartData {
reward?: ResponseOnboardingStartReward
user_already_started?: boolean
}

export interface ResponseOnboardingStartResponse {
data?: ResponseOnboardingStartData
}

export interface ResponseOnboardingStartReward {
amount?: string
token?: string
}

export interface ResponseOnchainInvestData {
tx_object?: ResponseTxObject
}
Expand All @@ -1943,6 +2015,10 @@ export interface ResponseProductBotCommand {
data?: ModelProductBotCommand[]
}

export interface ResponseProductChangelogs {
data?: ModelProductChangelogs[]
}

export interface ResponseProfileAirdropCampaignResponse {
data?: ModelProfileAirdropCampaign
}
Expand Down Expand Up @@ -2018,6 +2094,10 @@ export interface ResponseSearchCoinResponse {
data?: ModelCoingeckoSupportedTokens[]
}

export interface ResponseSearchDexPairResponse {
pairs?: ResponseDexPair[]
}

export interface ResponseSparkLineIn7D {
price?: number[]
}
Expand Down

0 comments on commit ad9b763

Please sign in to comment.