Skip to content

Commit 410ba03

Browse files
authored
chore: Migrate transfer endpoints to v5 SDK (#732)
* chore: Migrate transfer endpoints to v5 SDK * update erc20 and erc721 * e2e tests * undo generate sdk change
1 parent 29fa697 commit 410ba03

File tree

20 files changed

+768
-175
lines changed

20 files changed

+768
-175
lines changed

sdk/src/services/BackendWalletService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ export class BackendWalletService {
421421
*/
422422
transactionHash: string;
423423
/**
424-
* An amount in native token (decimals allowed). Example: "1.5"
424+
* An amount in native token (decimals allowed). Example: "0.1"
425425
*/
426426
amount: string;
427427
};

sdk/src/services/Erc1155Service.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,17 +1743,21 @@ export class Erc1155Service {
17431743
xBackendWalletAddress: string,
17441744
requestBody: {
17451745
/**
1746-
* Address of the wallet to transfer to
1746+
* The recipient address.
17471747
*/
17481748
to: string;
17491749
/**
1750-
* the tokenId to transfer
1750+
* The token ID to transfer.
17511751
*/
17521752
tokenId: string;
17531753
/**
1754-
* the amount of tokens to transfer
1754+
* The amount of tokens to transfer.
17551755
*/
17561756
amount: string;
1757+
/**
1758+
* A valid hex string
1759+
*/
1760+
data?: string;
17571761
txOverrides?: {
17581762
/**
17591763
* Gas limit for the transaction
@@ -1838,21 +1842,25 @@ export class Erc1155Service {
18381842
xBackendWalletAddress: string,
18391843
requestBody: {
18401844
/**
1841-
* Address of the token owner
1845+
* The sender address.
18421846
*/
18431847
from: string;
18441848
/**
1845-
* Address of the wallet to transferFrom to
1849+
* The recipient address.
18461850
*/
18471851
to: string;
18481852
/**
1849-
* the tokenId to transferFrom
1853+
* The token ID to transfer.
18501854
*/
18511855
tokenId: string;
18521856
/**
1853-
* the amount of tokens to transfer
1857+
* The amount of tokens to transfer.
18541858
*/
18551859
amount: string;
1860+
/**
1861+
* A valid hex string
1862+
*/
1863+
data?: string;
18561864
txOverrides?: {
18571865
/**
18581866
* Gas limit for the transaction

sdk/src/services/Erc20Service.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -673,17 +673,17 @@ export class Erc20Service {
673673
* @returns any Default Response
674674
* @throws ApiError
675675
*/
676-
public erc20Transfer(
676+
public transfer(
677677
chain: string,
678678
contractAddress: string,
679679
xBackendWalletAddress: string,
680680
requestBody: {
681681
/**
682-
* Address of the wallet you want to send the tokens to
682+
* The recipient address.
683683
*/
684684
toAddress: string;
685685
/**
686-
* The amount of tokens you want to send
686+
* The amount of tokens to transfer.
687687
*/
688688
amount: string;
689689
txOverrides?: {
@@ -770,15 +770,15 @@ export class Erc20Service {
770770
xBackendWalletAddress: string,
771771
requestBody: {
772772
/**
773-
* Address of the wallet sending the tokens
773+
* The sender address.
774774
*/
775775
fromAddress: string;
776776
/**
777-
* Address of the wallet you want to send the tokens to
777+
* The recipient address.
778778
*/
779779
toAddress: string;
780780
/**
781-
* The amount of tokens you want to send
781+
* The amount of tokens to transfer.
782782
*/
783783
amount: string;
784784
txOverrides?: {

sdk/src/services/Erc721Service.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -735,11 +735,11 @@ export class Erc721Service {
735735
xBackendWalletAddress: string,
736736
requestBody: {
737737
/**
738-
* Address of the wallet to transfer to
738+
* The recipient address.
739739
*/
740740
to: string;
741741
/**
742-
* The tokenId to transfer
742+
* The token ID to transfer.
743743
*/
744744
tokenId: string;
745745
txOverrides?: {
@@ -826,15 +826,15 @@ export class Erc721Service {
826826
xBackendWalletAddress: string,
827827
requestBody: {
828828
/**
829-
* Address of the token owner
829+
* The sender address.
830830
*/
831831
from: string;
832832
/**
833-
* Address of the wallet to transferFrom to
833+
* The recipient address.
834834
*/
835835
to: string;
836836
/**
837-
* the tokenId to transferFrom
837+
* The token ID to transfer.
838838
*/
839839
tokenId: string;
840840
txOverrides?: {

src/server/routes/contract/extensions/erc1155/write/transfer.ts

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import { Type, type Static } from "@sinclair/typebox";
22
import type { FastifyInstance } from "fastify";
33
import { StatusCodes } from "http-status-codes";
4-
import { queueTx } from "../../../../../../db/transactions/queueTx";
5-
import { getContract } from "../../../../../../utils/cache/getContract";
4+
import { getContract, type Hex } from "thirdweb";
5+
import { safeTransferFrom } from "thirdweb/extensions/erc1155";
6+
import { getChain } from "../../../../../../utils/chain";
7+
import { getChecksumAddress } from "../../../../../../utils/primitiveTypes";
8+
import { thirdwebClient } from "../../../../../../utils/sdk";
9+
import { queueTransaction } from "../../../../../../utils/transaction/queueTransation";
10+
import { AddressSchema, HexSchema } from "../../../../../schemas/address";
11+
import { NumberStringSchema } from "../../../../../schemas/number";
612
import {
713
erc1155ContractParamSchema,
814
requestQuerystringSchema,
@@ -16,15 +22,19 @@ import { getChainIdFromChain } from "../../../../../utils/chain";
1622
// INPUTS
1723
const requestSchema = erc1155ContractParamSchema;
1824
const requestBodySchema = Type.Object({
19-
to: Type.String({
20-
description: "Address of the wallet to transfer to",
21-
}),
25+
to: {
26+
...AddressSchema,
27+
description: "The recipient address.",
28+
},
2229
tokenId: Type.String({
23-
description: "the tokenId to transfer",
30+
...NumberStringSchema,
31+
description: "The token ID to transfer.",
2432
}),
2533
amount: Type.String({
26-
description: "the amount of tokens to transfer",
34+
...NumberStringSchema,
35+
description: "The amount of tokens to transfer.",
2736
}),
37+
data: Type.Optional(HexSchema),
2838
...txOverridesWithValueSchema.properties,
2939
});
3040

@@ -62,29 +72,43 @@ export async function erc1155transfer(fastify: FastifyInstance) {
6272
handler: async (request, reply) => {
6373
const { chain, contractAddress } = request.params;
6474
const { simulateTx } = request.query;
65-
const { to, tokenId, amount, txOverrides } = request.body;
75+
const { to, tokenId, amount, data, txOverrides } = request.body;
6676
const {
6777
"x-backend-wallet-address": walletAddress,
6878
"x-account-address": accountAddress,
6979
"x-idempotency-key": idempotencyKey,
80+
"x-account-factory-address": accountFactoryAddress,
81+
"x-account-salt": accountSalt,
7082
} = request.headers as Static<typeof walletWithAAHeaderSchema>;
7183

7284
const chainId = await getChainIdFromChain(chain);
7385
const contract = await getContract({
74-
chainId,
75-
contractAddress,
76-
walletAddress,
77-
accountAddress,
86+
client: thirdwebClient,
87+
chain: await getChain(chainId),
88+
address: contractAddress,
7889
});
79-
const tx = await contract.erc1155.transfer.prepare(to, tokenId, amount);
8090

81-
const queueId = await queueTx({
82-
tx,
83-
chainId,
84-
simulateTx,
85-
extension: "erc1155",
86-
idempotencyKey,
91+
const transaction = safeTransferFrom({
92+
contract,
93+
from: getChecksumAddress(walletAddress),
94+
to: getChecksumAddress(to),
95+
tokenId: BigInt(tokenId),
96+
value: BigInt(amount),
97+
data: (data as Hex | undefined) ?? "0x",
98+
});
99+
100+
const queueId = await queueTransaction({
101+
transaction,
102+
fromAddress: getChecksumAddress(walletAddress),
103+
toAddress: getChecksumAddress(contractAddress),
104+
accountAddress: getChecksumAddress(accountAddress),
105+
accountFactoryAddress: getChecksumAddress(accountFactoryAddress),
106+
accountSalt,
87107
txOverrides,
108+
idempotencyKey,
109+
shouldSimulate: simulateTx,
110+
functionName: "safeTransferFrom",
111+
extension: "erc1155",
88112
});
89113

90114
reply.status(StatusCodes.OK).send({

src/server/routes/contract/extensions/erc1155/write/transferFrom.ts

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import { Type, type Static } from "@sinclair/typebox";
22
import type { FastifyInstance } from "fastify";
33
import { StatusCodes } from "http-status-codes";
4-
import { queueTx } from "../../../../../../db/transactions/queueTx";
5-
import { getContract } from "../../../../../../utils/cache/getContract";
4+
import { getContract, type Hex } from "thirdweb";
5+
import { safeTransferFrom } from "thirdweb/extensions/erc1155";
6+
import { getChain } from "../../../../../../utils/chain";
7+
import { getChecksumAddress } from "../../../../../../utils/primitiveTypes";
8+
import { thirdwebClient } from "../../../../../../utils/sdk";
9+
import { queueTransaction } from "../../../../../../utils/transaction/queueTransation";
10+
import { AddressSchema, HexSchema } from "../../../../../schemas/address";
11+
import { NumberStringSchema } from "../../../../../schemas/number";
612
import {
713
erc1155ContractParamSchema,
814
requestQuerystringSchema,
@@ -16,18 +22,23 @@ import { getChainIdFromChain } from "../../../../../utils/chain";
1622
// INPUTS
1723
const requestSchema = erc1155ContractParamSchema;
1824
const requestBodySchema = Type.Object({
19-
from: Type.String({
20-
description: "Address of the token owner",
21-
}),
22-
to: Type.String({
23-
description: "Address of the wallet to transferFrom to",
24-
}),
25-
tokenId: Type.String({
26-
description: "the tokenId to transferFrom",
27-
}),
28-
amount: Type.String({
29-
description: "the amount of tokens to transfer",
30-
}),
25+
from: {
26+
...AddressSchema,
27+
description: "The sender address.",
28+
},
29+
to: {
30+
...AddressSchema,
31+
description: "The recipient address.",
32+
},
33+
tokenId: {
34+
...NumberStringSchema,
35+
description: "The token ID to transfer.",
36+
},
37+
amount: {
38+
...NumberStringSchema,
39+
description: "The amount of tokens to transfer.",
40+
},
41+
data: Type.Optional(HexSchema),
3142
...txOverridesWithValueSchema.properties,
3243
});
3344

@@ -69,34 +80,43 @@ export async function erc1155transferFrom(fastify: FastifyInstance) {
6980
handler: async (request, reply) => {
7081
const { chain, contractAddress } = request.params;
7182
const { simulateTx } = request.query;
72-
const { from, to, tokenId, amount, txOverrides } = request.body;
83+
const { from, to, tokenId, amount, data, txOverrides } = request.body;
7384
const {
7485
"x-backend-wallet-address": walletAddress,
7586
"x-account-address": accountAddress,
7687
"x-idempotency-key": idempotencyKey,
88+
"x-account-factory-address": accountFactoryAddress,
89+
"x-account-salt": accountSalt,
7790
} = request.headers as Static<typeof walletWithAAHeaderSchema>;
7891

7992
const chainId = await getChainIdFromChain(chain);
8093
const contract = await getContract({
81-
chainId,
82-
contractAddress,
83-
walletAddress,
84-
accountAddress,
94+
client: thirdwebClient,
95+
chain: await getChain(chainId),
96+
address: contractAddress,
8597
});
86-
const tx = await contract.erc1155.transferFrom.prepare(
87-
from,
88-
to,
89-
tokenId,
90-
amount,
91-
);
9298

93-
const queueId = await queueTx({
94-
tx,
95-
chainId,
96-
simulateTx,
97-
extension: "erc1155",
98-
idempotencyKey,
99+
const transaction = safeTransferFrom({
100+
contract,
101+
from: getChecksumAddress(from),
102+
to: getChecksumAddress(to),
103+
tokenId: BigInt(tokenId),
104+
value: BigInt(amount),
105+
data: (data as Hex | undefined) ?? "0x",
106+
});
107+
108+
const queueId = await queueTransaction({
109+
transaction,
110+
fromAddress: getChecksumAddress(walletAddress),
111+
toAddress: getChecksumAddress(contractAddress),
112+
accountAddress: getChecksumAddress(accountAddress),
113+
accountFactoryAddress: getChecksumAddress(accountFactoryAddress),
114+
accountSalt,
99115
txOverrides,
116+
idempotencyKey,
117+
shouldSimulate: simulateTx,
118+
functionName: "safeTransferFrom",
119+
extension: "erc1155",
100120
});
101121

102122
reply.status(StatusCodes.OK).send({

0 commit comments

Comments
 (0)