-
Notifications
You must be signed in to change notification settings - Fork 105
chore: Migrate transfer endpoints to v5 SDK #732
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,14 @@ | ||
| import { Type, type Static } from "@sinclair/typebox"; | ||
| import type { FastifyInstance } from "fastify"; | ||
| import { StatusCodes } from "http-status-codes"; | ||
| import { queueTx } from "../../../../../../db/transactions/queueTx"; | ||
| import { getContract } from "../../../../../../utils/cache/getContract"; | ||
| import { getContract } from "thirdweb"; | ||
| import { safeTransferFrom } from "thirdweb/extensions/erc1155"; | ||
| import { getChain } from "../../../../../../utils/chain"; | ||
| import { getChecksumAddress } from "../../../../../../utils/primitiveTypes"; | ||
| import { thirdwebClient } from "../../../../../../utils/sdk"; | ||
| import { queueTransaction } from "../../../../../../utils/transaction/queueTransation"; | ||
| import { AddressSchema } from "../../../../../schemas/address"; | ||
| import { NumberStringSchema } from "../../../../../schemas/number"; | ||
| import { | ||
| erc1155ContractParamSchema, | ||
| requestQuerystringSchema, | ||
|
|
@@ -16,14 +22,17 @@ import { getChainIdFromChain } from "../../../../../utils/chain"; | |
| // INPUTS | ||
| const requestSchema = erc1155ContractParamSchema; | ||
| const requestBodySchema = Type.Object({ | ||
| to: Type.String({ | ||
| description: "Address of the wallet to transfer to", | ||
| }), | ||
| to: { | ||
| ...AddressSchema, | ||
| description: "The recipient address.", | ||
| }, | ||
| tokenId: Type.String({ | ||
| description: "the tokenId to transfer", | ||
| ...NumberStringSchema, | ||
| description: "The token ID to transfer.", | ||
| }), | ||
| amount: Type.String({ | ||
| description: "the amount of tokens to transfer", | ||
| ...NumberStringSchema, | ||
| description: "The amount of tokens to transfer.", | ||
| }), | ||
| ...txOverridesWithValueSchema.properties, | ||
| }); | ||
|
|
@@ -67,24 +76,38 @@ export async function erc1155transfer(fastify: FastifyInstance) { | |
| "x-backend-wallet-address": walletAddress, | ||
| "x-account-address": accountAddress, | ||
| "x-idempotency-key": idempotencyKey, | ||
| "x-account-factory-address": accountFactoryAddress, | ||
| "x-account-salt": accountSalt, | ||
| } = request.headers as Static<typeof walletWithAAHeaderSchema>; | ||
|
|
||
| const chainId = await getChainIdFromChain(chain); | ||
| const contract = await getContract({ | ||
| chainId, | ||
| contractAddress, | ||
| walletAddress, | ||
| accountAddress, | ||
| client: thirdwebClient, | ||
| chain: await getChain(chainId), | ||
| address: contractAddress, | ||
| }); | ||
| const tx = await contract.erc1155.transfer.prepare(to, tokenId, amount); | ||
|
|
||
| const queueId = await queueTx({ | ||
| tx, | ||
| chainId, | ||
| simulateTx, | ||
| extension: "erc1155", | ||
| idempotencyKey, | ||
| const transaction = safeTransferFrom({ | ||
| contract, | ||
| from: getChecksumAddress(walletAddress), | ||
| to: getChecksumAddress(to), | ||
| tokenId: BigInt(tokenId), | ||
| value: BigInt(amount), | ||
| data: "0x", | ||
|
||
| }); | ||
|
|
||
| const queueId = await queueTransaction({ | ||
| transaction, | ||
| fromAddress: getChecksumAddress(walletAddress), | ||
| toAddress: getChecksumAddress(contractAddress), | ||
|
Comment on lines
+102
to
+103
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Making sure the from/to here is always backend wallet -> contract (as opposed to above when building the transaction where from/to are the sender/recipient addresses). |
||
| accountAddress: getChecksumAddress(accountAddress), | ||
| accountFactoryAddress: getChecksumAddress(accountFactoryAddress), | ||
| accountSalt, | ||
| txOverrides, | ||
| idempotencyKey, | ||
| shouldSimulate: simulateTx, | ||
| functionName: "safeTransferFrom", | ||
| extension: "erc1155", | ||
| }); | ||
|
|
||
| reply.status(StatusCodes.OK).send({ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,14 @@ | ||
| import { Type, type Static } from "@sinclair/typebox"; | ||
| import type { FastifyInstance } from "fastify"; | ||
| import { StatusCodes } from "http-status-codes"; | ||
| import { queueTx } from "../../../../../../db/transactions/queueTx"; | ||
| import { getContract } from "../../../../../../utils/cache/getContract"; | ||
| import { getContract } from "thirdweb"; | ||
| import { safeTransferFrom } from "thirdweb/extensions/erc1155"; | ||
| import { getChain } from "../../../../../../utils/chain"; | ||
| import { getChecksumAddress } from "../../../../../../utils/primitiveTypes"; | ||
| import { thirdwebClient } from "../../../../../../utils/sdk"; | ||
| import { queueTransaction } from "../../../../../../utils/transaction/queueTransation"; | ||
| import { AddressSchema } from "../../../../../schemas/address"; | ||
| import { NumberStringSchema } from "../../../../../schemas/number"; | ||
| import { | ||
| erc1155ContractParamSchema, | ||
| requestQuerystringSchema, | ||
|
|
@@ -16,18 +22,22 @@ import { getChainIdFromChain } from "../../../../../utils/chain"; | |
| // INPUTS | ||
| const requestSchema = erc1155ContractParamSchema; | ||
| const requestBodySchema = Type.Object({ | ||
| from: Type.String({ | ||
| description: "Address of the token owner", | ||
| }), | ||
| to: Type.String({ | ||
| description: "Address of the wallet to transferFrom to", | ||
| }), | ||
| tokenId: Type.String({ | ||
| description: "the tokenId to transferFrom", | ||
| }), | ||
| amount: Type.String({ | ||
| description: "the amount of tokens to transfer", | ||
| }), | ||
| from: { | ||
| ...AddressSchema, | ||
| description: "The sender address.", | ||
| }, | ||
| to: { | ||
| ...AddressSchema, | ||
| description: "The recipient address.", | ||
| }, | ||
| tokenId: { | ||
| ...NumberStringSchema, | ||
| description: "The token ID to transfer.", | ||
| }, | ||
| amount: { | ||
| ...NumberStringSchema, | ||
| description: "The amount of tokens to transfer.", | ||
| }, | ||
| ...txOverridesWithValueSchema.properties, | ||
| }); | ||
|
|
||
|
|
@@ -74,29 +84,38 @@ export async function erc1155transferFrom(fastify: FastifyInstance) { | |
| "x-backend-wallet-address": walletAddress, | ||
| "x-account-address": accountAddress, | ||
| "x-idempotency-key": idempotencyKey, | ||
| "x-account-factory-address": accountFactoryAddress, | ||
| "x-account-salt": accountSalt, | ||
| } = request.headers as Static<typeof walletWithAAHeaderSchema>; | ||
|
|
||
| const chainId = await getChainIdFromChain(chain); | ||
| const contract = await getContract({ | ||
| chainId, | ||
| contractAddress, | ||
| walletAddress, | ||
| accountAddress, | ||
| client: thirdwebClient, | ||
| chain: await getChain(chainId), | ||
| address: contractAddress, | ||
| }); | ||
| const tx = await contract.erc1155.transferFrom.prepare( | ||
| from, | ||
| to, | ||
| tokenId, | ||
| amount, | ||
| ); | ||
|
|
||
| const queueId = await queueTx({ | ||
| tx, | ||
| chainId, | ||
| simulateTx, | ||
| extension: "erc1155", | ||
| idempotencyKey, | ||
| const transaction = safeTransferFrom({ | ||
| contract, | ||
| from: getChecksumAddress(from), | ||
| to: getChecksumAddress(to), | ||
| tokenId: BigInt(tokenId), | ||
| value: BigInt(amount), | ||
| data: "0x", | ||
|
||
| }); | ||
|
|
||
| const queueId = await queueTransaction({ | ||
| transaction, | ||
| fromAddress: getChecksumAddress(walletAddress), | ||
| toAddress: getChecksumAddress(contractAddress), | ||
| accountAddress: getChecksumAddress(accountAddress), | ||
| accountFactoryAddress: getChecksumAddress(accountFactoryAddress), | ||
| accountSalt, | ||
| txOverrides, | ||
| idempotencyKey, | ||
| shouldSimulate: simulateTx, | ||
| functionName: "safeTransferFrom", | ||
| extension: "erc1155", | ||
| }); | ||
|
|
||
| reply.status(StatusCodes.OK).send({ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
safeTransferFrom can fail if the recipient is a contract that does not implement the onReceived hook. This is generally desirable behaviour, but could fail transfers to smart accounts that do not implement this method. Most smart accounts do implement this though, including ours I think, but would be best to confirm once.