Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transfer FT: user pay for storage cost #937

Merged
merged 3 commits into from
May 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 61 additions & 25 deletions web-frontend/src/custom/smart-contract/useFTTransfer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from 'react';
import { useCallback, useEffect, useState } from 'react';

import {
Client,
Expand All @@ -8,6 +8,8 @@ import {
Args,
ClientFactory,
MAINNET_CHAIN_ID,
strToBytes,
STORAGE_BYTE_COST,
} from '@massalabs/massa-web3';
import { ToastContent, parseAmount, toast } from '@massalabs/react-ui-kit';
import { providers } from '@massalabs/wallet-provider';
Expand Down Expand Up @@ -57,31 +59,36 @@ export function useFTTransfer(nickname: string) {
callSmartContract,
} = useWriteSmartContract(client, isMainnet);

const transfer = (
recipient: string,
tokenAddress: string,
amount: string,
decimals: number,
) => {
if (!client) {
throw new Error('Massa client not found');
}
const transfer = useCallback(
(
recipient: string,
tokenAddress: string,
amount: string,
decimals: number,
) => {
if (!client) {
throw new Error('Massa client not found');
}

const rawAmount = parseAmount(amount, decimals);
const args = new Args().addString(recipient).addU256(rawAmount);

callSmartContract(
'transfer',
tokenAddress,
args.serialize(),
{
pending: Intl.t('send-coins.steps.ft-transfer-pending'),
success: Intl.t('send-coins.steps.ft-transfer-success'),
error: Intl.t('send-coins.steps.ft-transfer-failed'),
},
BigInt(0),
);
};
const rawAmount = parseAmount(amount, decimals);
const args = new Args().addString(recipient).addU256(rawAmount);

estimateCoinsCost(client, tokenAddress, recipient).then((coins) => {
callSmartContract(
'transfer',
tokenAddress,
args.serialize(),
{
pending: Intl.t('send-coins.steps.ft-transfer-pending'),
success: Intl.t('send-coins.steps.ft-transfer-success'),
error: Intl.t('send-coins.steps.ft-transfer-failed'),
},
coins,
);
});
},
[client, callSmartContract],
);

return {
opId,
Expand Down Expand Up @@ -249,3 +256,32 @@ function useWriteSmartContract(client?: Client, isMainnet?: boolean) {
callSmartContract,
};
}
async function estimateCoinsCost(
client: Client,
tokenAddress: string,
recipient: string,
): Promise<bigint> {
const addrInfo = await client.publicApi().getAddresses([tokenAddress]);
const allKeys = addrInfo[0].candidate_datastore_keys;
const key = balanceKey(recipient);
const foundKey = allKeys.find((k) => {
return JSON.stringify(k) === JSON.stringify(key);
});

if (foundKey) {
return 0n;
}

const storage =
4n + // space of a key/value in the datastore
BigInt(key.length) + // key length
32n; // length of the value of the balance

return STORAGE_BYTE_COST * storage;
}

function balanceKey(address: string): number[] {
const BALANCE_KEY_PREFIX = 'BALANCE';

return Array.from(strToBytes(BALANCE_KEY_PREFIX + address));
}
Loading