Skip to content

Commit adaac43

Browse files
committed
fix units
1 parent cab1f44 commit adaac43

File tree

5 files changed

+111
-95
lines changed

5 files changed

+111
-95
lines changed

packages/thirdweb/src/assets/create-token-by-impl-config.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,17 @@ export async function createTokenByImplConfig(options: CreateTokenOptions) {
5252
const tokenImpl = await getOrDeployERC20AssetImpl(options);
5353

5454
let hookData: Hex = "0x";
55-
55+
let amount = toUnits(
56+
params.maxSupply.toString() || DEFAULT_MAX_SUPPLY_ERC20.toString(),
57+
18,
58+
);
5659
if (launchConfig?.kind === "pool") {
5760
hookData = encodePoolConfig(launchConfig.config);
61+
amount = toUnits(
62+
launchConfig.config.amount.toString() ||
63+
DEFAULT_MAX_SUPPLY_ERC20.toString(),
64+
18,
65+
);
5866
} else if (launchConfig?.kind === "market") {
5967
const currencyContract =
6068
launchConfig.config.tokenOut &&
@@ -97,10 +105,7 @@ export async function createTokenByImplConfig(options: CreateTokenOptions) {
97105
createHookData: hookData,
98106
},
99107
params: {
100-
amount: toUnits(
101-
params.maxSupply.toString() || DEFAULT_MAX_SUPPLY_ERC20.toString(),
102-
18,
103-
),
108+
amount,
104109
referrer: ZERO_ADDRESS,
105110
salt,
106111
data: encodedInitData,

packages/thirdweb/src/assets/create-token.test.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { totalSupply } from "src/extensions/erc20/__generated__/IERC20/read/totalSupply.js";
12
import { describe, expect, it } from "vitest";
23
import { ANVIL_CHAIN } from "../../test/src/chains.js";
34
import { TEST_CLIENT } from "../../test/src/test-clients.js";
@@ -15,7 +16,7 @@ describe.runIf(process.env.TW_SECRET_KEY)("create token by impl config", () => {
1516
account: TEST_ACCOUNT_A,
1617
params: {
1718
name: "Test",
18-
maxSupply: 10_000_000_000n,
19+
maxSupply: 10_00n,
1920
},
2021
salt: "salt123",
2122
});
@@ -31,14 +32,14 @@ describe.runIf(process.env.TW_SECRET_KEY)("create token by impl config", () => {
3132
});
3233
expect(tokenName).to.eq("Test");
3334

34-
// const supply = await totalSupply({
35-
// contract: getContract({
36-
// client: TEST_CLIENT,
37-
// chain: ANVIL_CHAIN,
38-
// address: token,
39-
// }),
40-
// });
35+
const supply = await totalSupply({
36+
contract: getContract({
37+
client: TEST_CLIENT,
38+
chain: ANVIL_CHAIN,
39+
address: token,
40+
}),
41+
});
4142

42-
// console.log("supply: ", supply);
43+
console.log("supply: ", supply);
4344
});
4445
});

packages/thirdweb/src/assets/distribute-token.test.ts

Lines changed: 88 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { type ThirdwebContract, getContract } from "src/contract/contract.js";
22
import { getBalance } from "src/extensions/erc20/read/getBalance.js";
33
import { approve } from "src/extensions/erc20/write/approve.js";
44
import { sendAndConfirmTransaction } from "src/transaction/actions/send-and-confirm-transaction.js";
5+
import { toUnits } from "src/utils/units.js";
56
import { beforeAll, describe, expect, it } from "vitest";
67
import { ANVIL_CHAIN } from "../../test/src/chains.js";
78
import { TEST_CLIENT } from "../../test/src/test-clients.js";
@@ -15,92 +16,101 @@ import { createTokenByImplConfig } from "./create-token-by-impl-config.js";
1516
import { distributeToken } from "./distribute-token.js";
1617
import { getDeployedEntrypointERC20 } from "./get-entrypoint-erc20.js";
1718

18-
describe.runIf(process.env.TW_SECRET_KEY)("create token by impl config", () => {
19-
let token: ThirdwebContract;
20-
beforeAll(async () => {
21-
// create token
22-
const tokenAddress = await createTokenByImplConfig({
23-
chain: ANVIL_CHAIN,
24-
client: TEST_CLIENT,
25-
account: TEST_ACCOUNT_A,
26-
params: {
27-
name: "Test",
28-
maxSupply: 10_000_000_000n,
29-
},
30-
salt: "salt123",
31-
});
19+
describe.runIf(process.env.TW_SECRET_KEY)(
20+
"create token by impl config",
21+
{
22+
timeout: 20000,
23+
},
24+
() => {
25+
let token: ThirdwebContract;
26+
beforeAll(async () => {
27+
// create token
28+
const tokenAddress = await createTokenByImplConfig({
29+
chain: ANVIL_CHAIN,
30+
client: TEST_CLIENT,
31+
account: TEST_ACCOUNT_A,
32+
params: {
33+
name: "Test",
34+
maxSupply: 10_000_000_000n,
35+
},
36+
salt: "salt123",
37+
});
3238

33-
token = getContract({
34-
address: tokenAddress,
35-
client: TEST_CLIENT,
36-
chain: ANVIL_CHAIN,
37-
});
39+
token = getContract({
40+
address: tokenAddress,
41+
client: TEST_CLIENT,
42+
chain: ANVIL_CHAIN,
43+
});
3844

39-
// approve tokens to entrypoint for distribution
40-
const entrypoint = await getDeployedEntrypointERC20({
41-
chain: ANVIL_CHAIN,
42-
client: TEST_CLIENT,
43-
});
45+
// approve tokens to entrypoint for distribution
46+
const entrypoint = await getDeployedEntrypointERC20({
47+
chain: ANVIL_CHAIN,
48+
client: TEST_CLIENT,
49+
});
4450

45-
const approvalTx = approve({
46-
contract: token,
47-
spender: entrypoint?.address as string,
48-
amountWei: 1000n,
49-
});
50-
await sendAndConfirmTransaction({
51-
transaction: approvalTx,
52-
account: TEST_ACCOUNT_A,
53-
});
54-
});
51+
const approvalTx = approve({
52+
contract: token,
53+
spender: entrypoint?.address as string,
54+
amountWei: toUnits("1000", 18),
55+
});
56+
await sendAndConfirmTransaction({
57+
transaction: approvalTx,
58+
account: TEST_ACCOUNT_A,
59+
});
60+
}, 20000);
5561

56-
it("should distrbute tokens to specified recipients", async () => {
57-
const contents = [
58-
{ recipient: TEST_ACCOUNT_B.address, amount: 10n },
59-
{ recipient: TEST_ACCOUNT_C.address, amount: 15n },
60-
{ recipient: TEST_ACCOUNT_D.address, amount: 20n },
61-
];
62+
it("should distrbute tokens to specified recipients", async () => {
63+
const contents = [
64+
{ recipient: TEST_ACCOUNT_B.address, amount: 10n },
65+
{ recipient: TEST_ACCOUNT_C.address, amount: 15n },
66+
{ recipient: TEST_ACCOUNT_D.address, amount: 20n },
67+
];
6268

63-
await distributeToken({
64-
account: TEST_ACCOUNT_A,
65-
client: TEST_CLIENT,
66-
chain: ANVIL_CHAIN,
67-
tokenAddress: token.address,
68-
contents,
69-
});
69+
const transaction = await distributeToken({
70+
client: TEST_CLIENT,
71+
chain: ANVIL_CHAIN,
72+
tokenAddress: token.address,
73+
contents,
74+
});
7075

71-
const balanceB = (
72-
await getBalance({
73-
contract: token,
74-
address: TEST_ACCOUNT_B.address,
75-
})
76-
).value;
76+
await sendAndConfirmTransaction({ transaction, account: TEST_ACCOUNT_A });
7777

78-
const balanceC = (
79-
await getBalance({
80-
contract: token,
81-
address: TEST_ACCOUNT_C.address,
82-
})
83-
).value;
78+
const balanceB = (
79+
await getBalance({
80+
contract: token,
81+
address: TEST_ACCOUNT_B.address,
82+
})
83+
).value;
8484

85-
const balanceD = (
86-
await getBalance({
87-
contract: token,
88-
address: TEST_ACCOUNT_D.address,
89-
})
90-
).value;
85+
const balanceC = (
86+
await getBalance({
87+
contract: token,
88+
address: TEST_ACCOUNT_C.address,
89+
})
90+
).value;
9191

92-
// admin balance
93-
const balanceA = (
94-
await getBalance({
95-
contract: token,
96-
address: TEST_ACCOUNT_A.address,
97-
})
98-
).value;
92+
const balanceD = (
93+
await getBalance({
94+
contract: token,
95+
address: TEST_ACCOUNT_D.address,
96+
})
97+
).value;
98+
99+
// admin balance
100+
const balanceA = (
101+
await getBalance({
102+
contract: token,
103+
address: TEST_ACCOUNT_A.address,
104+
})
105+
).value;
99106

100-
expect(balanceB).to.equal(10n);
101-
expect(balanceC).to.equal(15n);
102-
expect(balanceD).to.equal(20n);
107+
expect(balanceB).to.equal(toUnits("10", 18));
108+
expect(balanceC).to.equal(toUnits("15", 18));
109+
expect(balanceD).to.equal(toUnits("20", 18));
103110

104-
expect(balanceA).to.equal(10_000_000_000n - balanceB - balanceC - balanceD);
105-
});
106-
});
111+
expect(balanceA).to.equal(
112+
toUnits("10000000000", 18) - balanceB - balanceC - balanceD,
113+
);
114+
});
115+
},
116+
);

packages/thirdweb/src/assets/distribute-token.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { distributeAsset } from "../extensions/assets/__generated__/AssetEntrypointERC20/write/distributeAsset.js";
2-
import type { ClientAndChainAndAccount } from "../utils/types.js";
2+
import type { ClientAndChain } from "../utils/types.js";
33
import { toUnits } from "../utils/units.js";
44
import { getDeployedEntrypointERC20 } from "./get-entrypoint-erc20.js";
55
import type { DistributeContent } from "./types.js";
66

7-
type DistrbuteTokenParams = ClientAndChainAndAccount & {
7+
type DistrbuteTokenParams = ClientAndChain & {
88
tokenAddress: string;
99
contents: DistributeContent[];
1010
};

packages/thirdweb/src/assets/token-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export function encodePoolConfig(poolConfig: PoolConfig): Hex {
7070

7171
return encodeAbiParameters(POOL_PARAMS, [
7272
poolConfig.currency || NATIVE_TOKEN_ADDRESS,
73-
poolConfig.amount,
73+
toUnits(poolConfig.amount.toString(), 18),
7474
poolConfig.fee || DEFAULT_POOL_FEE,
7575
poolConfig.initialTick || DEFAULT_POOL_INITIAL_TICK,
7676
]);

0 commit comments

Comments
 (0)