Skip to content

Commit

Permalink
Merge pull request #47 from tonlabs/SDK-4254-accept-token-units-inste…
Browse files Browse the repository at this point in the history
…ad-of-tokens-in-trader-deploy-ever-wallet

Sdk 4254 accept token units instead of tokens in trader deploy ever wallet
  • Loading branch information
melsomino authored Feb 10, 2023
2 parents 92ce881 + 4c609d1 commit f3350c7
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 84 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file.
1) hex string with `0x` prefix;
2) hex string of 64 char length without `0x` prefix;
3) decimal string.
- `Trader.deployEverWallet` accepts token parameters as `TokenValue` instead of `number`'s.

## [0.10.2] – 2022-12-14

Expand Down
6 changes: 3 additions & 3 deletions flex/client/deploy-client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Flex } from "../flex";
import { AccountOptionsEx, UserDataConfigAccount } from "../../contracts";
import { EverWallet, SignerOption, toUnits } from "../web3";
import { EverWallet, SignerOption, toUnitsBigIntString } from "../web3";

export type DeployClientOptions = {
/**
Expand Down Expand Up @@ -48,13 +48,13 @@ export async function deployClient(flex: Flex, options: DeployClientOptions): Pr
const everWallet = new EverWallet(flex.evr, options.everWallet);
await everWallet.transfer({
dest: await userConfig.getAddress(),
value: toUnits(transferEvers),
value: toUnitsBigIntString(transferEvers),
payload: {
abi: UserDataConfigAccount.package.abi,
fn: "deployFlexClient",
params: {
pubkey,
deploy_evers: toUnits(deployEvers),
deploy_evers: toUnitsBigIntString(deployEvers),
signature,
},
},
Expand Down
34 changes: 17 additions & 17 deletions flex/exchange/deploy-exchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
} from "../../contracts/generated/SuperRootOwnerAccount";
import { addEverTokenType, EverTokenTypeOptions, TokenTypeInfo } from "./ever-token-type";
import { addTip3TokenType, Tip3TokenTypeOptions } from "./tip3-token-type";
import { EverWallet, SignerOption, toUnits, Evr } from "../web3";
import { EverWallet, SignerOption, toUnitsBigIntString, Evr } from "../web3";

export type DeployExchangeOptions = {
everWallet: AccountOptionsEx;
Expand Down Expand Up @@ -138,7 +138,7 @@ export async function deployExchange(
if (!superRootAddress || !(await web3.accounts.isActive(superRootAddress))) {
superRootAddress = (
await superRootOwner.runDeploySuperRoot({
evers: toUnits(options.superRootEvers ?? DEFAULTS.superRootEvers),
evers: toUnitsBigIntString(options.superRootEvers ?? DEFAULTS.superRootEvers),
prev_super_root: options.prevSuperRoot,
})
).output.value0;
Expand Down Expand Up @@ -210,9 +210,9 @@ function wrappersConfig(
const options = exchangeOptions.wrappers;
const defaults = DEFAULTS.wrappers;
return {
main_evers: toUnits(options?.mainEvers ?? defaults.mainEvers),
deploy_evers: toUnits(options?.deployEvers ?? defaults.deployEvers),
wrappers_cfg_keep_evers: toUnits(options?.keepEvers ?? defaults.keepEvers),
main_evers: toUnitsBigIntString(options?.mainEvers ?? defaults.mainEvers),
deploy_evers: toUnitsBigIntString(options?.deployEvers ?? defaults.deployEvers),
wrappers_cfg_keep_evers: toUnitsBigIntString(options?.keepEvers ?? defaults.keepEvers),
token_version: options?.version ?? exchangeOptions.version?.wallet ?? 1,
};
}
Expand All @@ -221,25 +221,25 @@ function flexConfig(exchangeOptions: DeployExchangeOptions): SuperRootOwnerDeplo
const options = exchangeOptions.flex;
const defaults = DEFAULTS.flex;
return {
main_evers: toUnits(options?.mainEvers ?? defaults.mainEvers),
deploy_evers: toUnits(options?.deployEvers ?? defaults.deployEvers),
keep_evers: toUnits(options?.keepEvers ?? defaults.keepEvers),
main_evers: toUnitsBigIntString(options?.mainEvers ?? defaults.mainEvers),
deploy_evers: toUnitsBigIntString(options?.deployEvers ?? defaults.deployEvers),
keep_evers: toUnitsBigIntString(options?.keepEvers ?? defaults.keepEvers),
evers: {
deploy: toUnits(options?.evers?.deploy ?? defaults.evers.deploy),
setnext: toUnits(options?.evers?.setNext ?? defaults.evers.setNext),
pair_keep: toUnits(options?.evers?.pairKeep ?? defaults.evers.pairKeep),
deploy: toUnitsBigIntString(options?.evers?.deploy ?? defaults.evers.deploy),
setnext: toUnitsBigIntString(options?.evers?.setNext ?? defaults.evers.setNext),
pair_keep: toUnitsBigIntString(options?.evers?.pairKeep ?? defaults.evers.pairKeep),
},
old_flex: options?.oldFlex,
exchange_version: options?.version ?? exchangeOptions.version?.exchange ?? 1,
ev_cfg: {
transfer_tip3: toUnits(options?.fees?.transferTip3 ?? defaults.fees.transferTip3),
return_ownership: toUnits(
transfer_tip3: toUnitsBigIntString(options?.fees?.transferTip3 ?? defaults.fees.transferTip3),
return_ownership: toUnitsBigIntString(
options?.fees?.returnOwnership ?? defaults.fees.returnOwnership,
),
order_answer: toUnits(options?.fees?.orderAnswer ?? defaults.fees.orderAnswer),
process_queue: toUnits(options?.fees?.processQueue ?? defaults.fees.processQueue),
send_notify: toUnits(options?.fees?.sendNotify ?? defaults.fees.sendNotify),
dest_wallet_keep_evers: toUnits(
order_answer: toUnitsBigIntString(options?.fees?.orderAnswer ?? defaults.fees.orderAnswer),
process_queue: toUnitsBigIntString(options?.fees?.processQueue ?? defaults.fees.processQueue),
send_notify: toUnitsBigIntString(options?.fees?.sendNotify ?? defaults.fees.sendNotify),
dest_wallet_keep_evers: toUnitsBigIntString(
options?.fees?.destWalletKeepEvers ?? defaults.fees.destWalletKeepEvers,
),
},
Expand Down
12 changes: 6 additions & 6 deletions flex/exchange/ever-token-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
WrapperEverAccount,
AccountOptionsEx,
} from "../../contracts";
import { EverWallet, toUnits, Evr, SignerOption } from "../web3";
import { EverWallet, toUnitsBigIntString, Evr, SignerOption } from "../web3";

export const EVER_TOKEN_TYPE = 1;

Expand Down Expand Up @@ -64,9 +64,9 @@ export async function addEverTokenType(
pubkey: `0x${deployerPublicKey}`,
wrapper_pubkey: `0x${wrapperPublicKey}`,
super_root: options.superRoot,
wrapper_deploy_value: toUnits(options.wrapperDeployEvers ?? DEFAULTS.wrapperDeployEvers),
wrapper_keep_balance: toUnits(options.wrapperKeepEvers ?? DEFAULTS.wrapperKeepEvers),
reserve_wallet_value: toUnits(options.reserveWalletEvers ?? DEFAULTS.reserveWalletEvers),
wrapper_deploy_value: toUnitsBigIntString(options.wrapperDeployEvers ?? DEFAULTS.wrapperDeployEvers),
wrapper_keep_balance: toUnitsBigIntString(options.wrapperKeepEvers ?? DEFAULTS.wrapperKeepEvers),
reserve_wallet_value: toUnitsBigIntString(options.reserveWalletEvers ?? DEFAULTS.reserveWalletEvers),
});
await deployer.runSetWrapperEverCode({ code: WrapperEverAccount.package.code });
// await deployer.runSetExtWalletCode({ code: TONTokenWalletAccount.package.code });
Expand All @@ -75,8 +75,8 @@ export async function addEverTokenType(
const superRootOwner = await web3.accounts.get(SuperRootOwnerAccount, options.superRootOwner);
await superRootOwner.runAddWrapperType({
type: EVER_TOKEN_TYPE,
main_evers: toUnits(options.mainEvers ?? DEFAULTS.mainEvers),
wrappers_cfg_keep_evers: toUnits(options.keepEvers ?? DEFAULTS.keepEvers),
main_evers: toUnitsBigIntString(options.mainEvers ?? DEFAULTS.mainEvers),
wrappers_cfg_keep_evers: toUnitsBigIntString(options.keepEvers ?? DEFAULTS.keepEvers),
wrappers_cfg: options.wrappersConfigAddress,
wrapper_deployer: await deployer.getAddress(),
});
Expand Down
14 changes: 7 additions & 7 deletions flex/exchange/tip3-token-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
AccountOptionsEx,
} from "../../contracts";
import { TokenTypeInfo } from "./ever-token-type";
import { EverWallet, SignerOption, toUnits, Evr } from "../web3";
import { EverWallet, SignerOption, toUnitsBigIntString, Evr } from "../web3";

export const TIP3_TOKEN_TYPE = 0;
export type Tip3TokenTypeOptions = {
Expand Down Expand Up @@ -60,10 +60,10 @@ export async function addTip3TokenType(
pubkey: `0x${deployerPublicKey}`,
wrapper_pubkey: `0x${wrapperPublicKey}`,
super_root: options.superRoot,
wrapper_deploy_value: toUnits(options.wrapperDeployEvers ?? DEFAULTS.wrapperDeployEvers),
wrapper_keep_balance: toUnits(options.wrapperKeepEvers ?? DEFAULTS.wrapperKeepEvers),
reserve_wallet_value: toUnits(options.reserveWalletEvers ?? DEFAULTS.reserveWalletEvers),
ext_wallet_value: toUnits(options.extWalletEvers ?? DEFAULTS.extWalletEvers),
wrapper_deploy_value: toUnitsBigIntString(options.wrapperDeployEvers ?? DEFAULTS.wrapperDeployEvers),
wrapper_keep_balance: toUnitsBigIntString(options.wrapperKeepEvers ?? DEFAULTS.wrapperKeepEvers),
reserve_wallet_value: toUnitsBigIntString(options.reserveWalletEvers ?? DEFAULTS.reserveWalletEvers),
ext_wallet_value: toUnitsBigIntString(options.extWalletEvers ?? DEFAULTS.extWalletEvers),
});
await deployer.runSetWrapperCode({ code: WrapperAccount.package.code });
await deployer.runSetExtWalletCode({ code: TONTokenWalletAccount.package.code });
Expand All @@ -72,8 +72,8 @@ export async function addTip3TokenType(
const superRootOwner = await web3.accounts.get(SuperRootOwnerAccount, options.superRootOwner);
await superRootOwner.runAddWrapperType({
type: TIP3_TOKEN_TYPE,
main_evers: toUnits(options.mainEvers ?? DEFAULTS.mainEvers),
wrappers_cfg_keep_evers: toUnits(options.keepEvers ?? DEFAULTS.keepEvers),
main_evers: toUnitsBigIntString(options.mainEvers ?? DEFAULTS.mainEvers),
wrappers_cfg_keep_evers: toUnitsBigIntString(options.keepEvers ?? DEFAULTS.keepEvers),
wrappers_cfg: options.wrappersConfigAddress,
wrapper_deployer: await deployer.getAddress(),
});
Expand Down
17 changes: 9 additions & 8 deletions flex/trader/deploy-ever-wallet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Flex } from "../flex";
import { WrapperEverAccount, AccountOptionsEx } from "../../contracts";
import { EverWallet, toUnits, uint256 } from "../web3";
import { EverWallet, TokenValue, toUnitsBigIntString, uint256 } from "../web3";
import { toUnitsBigInt } from "../web3/utils";

export type DeployTraderEverWalletOptions = {
/**
Expand All @@ -22,17 +23,17 @@ export type DeployTraderEverWalletOptions = {
/**
* Amount of EVERs to deposit
*/
tokens: number;
tokens: TokenValue;

/**
* Amount of native EVERs that the deposit message carries. Later on, DEX wallet will spend them to pay for gas.
*/
evers?: number;
evers?: TokenValue;
/**
* Minimum amount of EVERs on DEX wallet. If balance drops below this amount,
* wallet is automatically topped-up from the Trader's Index wallet.
*/
keepEvers?: number;
keepEvers?: TokenValue;
};

export type EverWalletInfo = {
Expand Down Expand Up @@ -62,17 +63,17 @@ export async function deployTraderEverWallet(
const keepEvers = options.keepEvers ?? DEFAULTS.keepEvers;
await everWallet.transfer({
dest: options.wrapperAddress,
value: toUnits(options.tokens + evers),
value: (toUnitsBigInt(options.tokens) + toUnitsBigInt(evers)).toString(),
payload: {
abi: WrapperEverAccount.package.abi,
fn: "onEverTransfer",
params: {
tokens: toUnits(options.tokens),
tokens: toUnitsBigIntString(options.tokens),
args: {
pubkey,
owner: options.clientAddress,
evers: toUnits(evers),
keep_evers: toUnits(keepEvers),
evers: toUnitsBigIntString(evers),
keep_evers: toUnitsBigIntString(keepEvers),
},
answer_addr: await everWallet.getAddress(),
},
Expand Down
8 changes: 4 additions & 4 deletions flex/trader/deploy-tip31-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
WrapperAccount,
AccountOptionsEx,
} from "../../contracts";
import { EverWallet, toUnits, uint256 } from "../web3";
import { EverWallet, toUnitsBigIntString, uint256 } from "../web3";

export type DeployTraderTip31WalletOptions = {
/**
Expand Down Expand Up @@ -85,15 +85,15 @@ export async function deployTraderTip31Wallet(
await client.getPayloadForDeployInternalWallet({
owner_addr: options.clientAddress,
owner_pubkey: pubkey,
evers: toUnits(evers),
keep_evers: toUnits(keepEvers),
evers: toUnitsBigIntString(evers),
keep_evers: toUnitsBigIntString(keepEvers),
})
).output.value0;

const everWallet = new EverWallet(flex.evr, options.everWallet);
await everWallet.transfer({
dest: options.tokenWalletAddress,
value: toUnits(options.transferEvers ?? DEFAULTS.transferEvers),
value: toUnitsBigIntString(options.transferEvers ?? DEFAULTS.transferEvers),
payload: {
abi: Tip31WalletAccount.package.abi,
fn: "transferToWallet",
Expand Down
4 changes: 2 additions & 2 deletions flex/trader/make-order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from "../../contracts";
import { getWallet } from "./internals";
import { TraderOptions } from "./types";
import { toUnits, Evr, TokenValue } from "../web3";
import { toUnitsBigIntString, Evr, TokenValue } from "../web3";
import { AccountClass } from "../../contracts";
import { resolveDerivativeTransaction, SdkError } from "./processing";
import { DerivativeTransactionMessage } from "../web3/accounts";
Expand Down Expand Up @@ -138,7 +138,7 @@ export async function makeOrder(flex: Flex, options: MakeOrderOptions): Promise<
const priceCode = (await pair.getPriceXchgCode({ salted: false })).output.value0;
const saltedPriceCode = (await pair.getPriceXchgCode({ salted: true })).output.value0;
const priceSalt = (await pair.getPriceXchgSalt()).output.value0;
const amount = toUnits(options.amount, pairDetails.major_tip3cfg.decimals);
const amount = toUnitsBigIntString(options.amount, pairDetails.major_tip3cfg.decimals);
const resolvedOrderId = options.orderId ?? (await generateRandomOrderId(flex.evr));
const orderId = `0x${BigInt(resolvedOrderId).toString(16)}`;
const price = priceToUnits(
Expand Down
4 changes: 2 additions & 2 deletions flex/trader/top-up.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Flex } from "../flex";
import { toUnits, TokenValue, AccountOptionsEx, uint256, Evr, EverWallet } from "../web3";
import { toUnitsBigIntString, TokenValue, AccountOptionsEx, uint256, Evr, EverWallet } from "../web3";
import { TvmErrorCode } from "@eversdk/core";
import { decimalFromNumAndDenomAsPowerOf10 } from "../web3/utils";
import { FlexError } from "../error";
import { STD_ERROR } from "../../contracts";

function toNativeUnits(value: TokenValue): bigint {
return BigInt(toUnits(value, Evr.NATIVE_DECIMALS));
return BigInt(toUnitsBigIntString(value, Evr.NATIVE_DECIMALS));
}

export type TopUpOptions = {
Expand Down
4 changes: 2 additions & 2 deletions flex/web3/ever-wallet.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AccountOptionsEx, MultisigWalletAccount } from "../../contracts";
import { abiContract, AbiContract, signerNone } from "@eversdk/core";
import { Evr } from "./evr";
import { toUnits } from "./utils";
import { toUnitsBigIntString } from "./utils";

export type TransferOptions = {
dest: string;
Expand Down Expand Up @@ -52,7 +52,7 @@ export class EverWallet {
}

async topUp(address: string, value: number) {
await this.topUpUnits(address, BigInt(toUnits(value, Evr.NATIVE_DECIMALS)));
await this.topUpUnits(address, BigInt(toUnitsBigIntString(value, Evr.NATIVE_DECIMALS)));
}

async topUpUnits(address: string, value: bigint) {
Expand Down
4 changes: 2 additions & 2 deletions flex/web3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { EvrAccounts, AccountOptionsEx } from "./accounts";
import { EvrSigners, SignerOption } from "./signers";
import { EverWallet, TransferOptions } from "./ever-wallet";
import { EvrConfig, Evr } from "./evr";
import { toUnits, uint256, TokenValue, ExplicitTokens, ExplicitUnits } from "./utils";
import { toUnitsBigIntString, uint256, TokenValue, ExplicitTokens, ExplicitUnits } from "./utils";

export {
EvrSigners,
Expand All @@ -18,7 +18,7 @@ export {
TransferOptions,
Evr,
EvrConfig,
toUnits,
toUnitsBigIntString,
uint256,
TokenValue,
ExplicitUnits,
Expand Down
17 changes: 14 additions & 3 deletions flex/web3/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ function toUnitsMulDiv(value: TokenValue, decimals: DecimalNumber): MulDiv {
return result;
}

export function toUnitsString(value: TokenValue, decimals: DecimalNumber): string {
export function toUnitsDecimalString(value: TokenValue, decimals: DecimalNumber): string {
return mulDivToString(toUnitsMulDiv(value, decimals));
}

Expand All @@ -140,9 +140,20 @@ function tokensToUnitsMulDiv(value: DecimalNumber, decimals: DecimalNumber): Mul
return f;
}

export function toUnits(value: TokenValue, decimals: DecimalNumber = 9): string {
export function toUnitsBigInt(value: TokenValue, decimals: DecimalNumber = 9): bigint {
const f = toUnitsMulDiv(value, decimals);
return (f.mul / f.div).toString();
return f.mul / f.div;
}

export function toUnitsBigIntString(value: TokenValue, decimals: DecimalNumber = 9): string {
return toUnitsBigInt(value, decimals).toString();
}

/**
* @deprecated use toUnitsBigInt, toUnitsBigIntString, toUnitsDecimalString
*/
export function toUnits(value: TokenValue, decimals: DecimalNumber = 9): string {
return toUnitsBigIntString(value, decimals);
}

export function priceToUnits(
Expand Down
16 changes: 0 additions & 16 deletions integration-test/07-conversions.test.ts

This file was deleted.

Loading

0 comments on commit f3350c7

Please sign in to comment.