Skip to content

Commit

Permalink
Merge pull request #261 from chain4travel/aeddaqqa/fixes
Browse files Browse the repository at this point in the history
fix(MyMessenger): fixed configure accepted currencies and withdrawing
  • Loading branch information
aeddaqqa authored Oct 21, 2024
2 parents 3350abb + e8e3240 commit 40ed4e2
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 85 deletions.
17 changes: 17 additions & 0 deletions src/constants/apps-consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,23 @@ export const BUSINESS_FIELDS = [
'Climate Technology',
]

export const ERC20_ABI = [
'function name() view returns (string)',
'function symbol() view returns (string)',
'function decimals() view returns (uint8)',
'function totalSupply() view returns (uint256)',
'function balanceOf(address) view returns (uint256)',
]
export const ERC20_BALANCE_ABI = [
{
constant: true,
inputs: [{ name: '_owner', type: 'address' }],
name: 'balanceOf',
outputs: [{ name: 'balance', type: 'uint256' }],
type: 'function',
},
]

export const CONTRACTCMACCOUNTMANAGERADDRESSCOLUMBUS = '0xE5B2f76C778D082b07BDd7D51FFe83E3E055B47F'

export const CONTRACTCMACCOUNTMANAGERADDRESSCAMINO = '0xf9FE1eaAB73a2902136FE7A83E0703338D3b9F1e'
16 changes: 6 additions & 10 deletions src/helpers/usePartnerConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ export const usePartnerConfig = () => {
)

const transferERC20 = useCallback(
async (address, value) => {
async (tokenAddress, to, value) => {
if (!account) {
console.error('Account is not initialized')
return
Expand All @@ -387,16 +387,12 @@ export const usePartnerConfig = () => {
},
]

// Create a contract instance
const contract = new ethers.Contract(address, abi, provider)

// Call the balanceOf function
const balance = await contract.balanceOf(contractCMAccountAddress)
const tx = await accountWriteContract.transferERC20(address, wallet.address, value)
const tx = await accountWriteContract.transferERC20(
tokenAddress,
ethers.getAddress(to),
value,
)
await tx.wait()
const afterBalance = await contract.balanceOf(contractCMAccountAddress)
const afterFormattedBalance = ethers.formatUnits(afterBalance, 18)
return afterFormattedBalance
} catch (error) {
const decodedError = accountWriteContract.interface.parseError(error.data)
console.error('Message:', error.message)
Expand Down
46 changes: 32 additions & 14 deletions src/redux/services/partners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { PartnerDataType, PartnersResponseType } from '../../@types/partners'
import {
CONTRACTCMACCOUNTMANAGERADDRESSCAMINO,
CONTRACTCMACCOUNTMANAGERADDRESSCOLUMBUS,
ERC20_ABI,
} from '../../constants/apps-consts'
import CMAccount from '../../helpers/CMAccountManagerModule#CMAccount.json'
import CMAccountManager from '../../helpers/ManagerProxyModule#CMAccountManager.json'
Expand All @@ -16,7 +17,12 @@ const BASE_URLS = {
}

function createPartnerContract(address: string, provider: ethers.Provider) {
return new ethers.Contract(address, CMAccount, provider)
try {
const contract = new ethers.Contract(address, CMAccount, provider)
return contract
} catch (e) {
return null
}
}

const getListOfBots = async contract => {
Expand All @@ -36,13 +42,22 @@ const getListOfBots = async contract => {
}
}

const getSupportedCurrencies = async contract => {
const getSupportedCurrencies = async (contract, provider) => {
try {
const offChainPaymentSupported = await contract.offChainPaymentSupported()
const isCam = (await contract.getSupportedTokens()).find(
elem => elem === ethers.ZeroAddress,
)
return { offChainPaymentSupported, isCam: !!isCam }
const supportedTokens = await contract.getSupportedTokens()
let tokens = []
for (const token of supportedTokens) {
if (token !== ethers.ZeroAddress) {
const tokenContract = new ethers.Contract(token, ERC20_ABI, provider)
const name = await tokenContract.name()
const symbol = await tokenContract.symbol()
const decimals = await tokenContract.decimals()
tokens.push({ name, symbol, decimals })
}
}
const isCam = supportedTokens.find(elem => elem === ethers.ZeroAddress)
return { offChainPaymentSupported, isCam: !!isCam, tokens }
} catch (error) {
throw error
}
Expand All @@ -52,15 +67,18 @@ async function fetchContractServices(contractAddress: string, provider: ethers.P
const contract = createPartnerContract(contractAddress, provider)

try {
const [supportedServices, wantedServices, bots, supportedCurrencies] = await Promise.all([
contract.getSupportedServices(),
contract.getWantedServices(),
getListOfBots(contract),
getSupportedCurrencies(contract),
])
return { supportedServices, wantedServices, bots, supportedCurrencies }
if (contract) {
const [supportedServices, wantedServices, bots, supportedCurrencies] =
await Promise.all([
contract.getSupportedServices(),
contract.getWantedServices(),
getListOfBots(contract),
getSupportedCurrencies(contract, provider),
])
return { supportedServices, wantedServices, bots, supportedCurrencies }
}
return { supportedServices: [], wantedServices: [] }
} catch (error) {
console.error(`Error fetching services for ${contractAddress}:`, error)
return { supportedServices: [], wantedServices: [] }
}
}
Expand Down
Loading

0 comments on commit 40ed4e2

Please sign in to comment.