Skip to content

Commit

Permalink
Handlers: Implement Mappers to Transform Generic TNftTransferClaimObj…
Browse files Browse the repository at this point in the history
…ect into Chain Specific ClaimData
  • Loading branch information
imsk17 committed Feb 12, 2024
1 parent 847aa7f commit 8cbea81
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/handlers/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ export type TGetProvider<T> = {
getProvider: () => T;
};

export type TMapTransferDetailsToChainClaimData<To> = {
transform: (input: TNftTransferDetailsObject) => To;
};

/**
* Represents a type that has all the methods required to implement on a chain that can be used in the bridge to transfer Non Fungible Tokens. It is a combination of some of the types defined above.
* @template Signer The type of the signer. ie {Signer} on EVM from ethers
Expand All @@ -236,7 +240,8 @@ export type TSingularNftChain<Signer, ClaimData, ExtraArgs, RetTx, Provider> =
TClaimNFT<Signer, ClaimData, ExtraArgs, RetTx> &
TGetBalance<Signer, ExtraArgs> &
TGetClaimData &
TGetProvider<Provider>;
TGetProvider<Provider> &
TMapTransferDetailsToChainClaimData<ClaimData>;

/**
* Represents a type that has all the methods required to implement on a chain that can be used in the bridge to transfer Semi Fungible Tokens. It is a combination of some of the types defined above.
Expand Down
3 changes: 3 additions & 0 deletions src/handlers/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ export function evmHandler({
getBalance(signer) {
return provider.getBalance(signer);
},
transform(input) {
return { ...input };
},
async nftData(tokenId, contract, overrides) {
const nft = ERC721Royalty__factory.connect(contract, provider);
return {
Expand Down
19 changes: 19 additions & 0 deletions src/handlers/multiversx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,25 @@ export function multiversxHandler({
getProvider() {
return provider;
},
transform(input) {
return {
attrs: input.metadata,
destinationChain: input.destinationChain,
destinationUserAddress: input.destinationUserAddress,
fee: input.fee,
metadata: input.metadata,
name: input.name,
nftType: input.nftType,
royalty: input.royalty,
royaltyReceiver: input.royaltyReceiver,
sourceChain: input.sourceChain,
sourceNftContractAddress: input.sourceNftContractAddress,
symbol: input.symbol,
tokenAmount: input.tokenAmount,
tokenId: input.tokenId,
transactionHash: input.transactionHash,
};
},
async approveNft(_signer, _tokenId, _contract) {
return Promise.resolve("Not Required for MultiversX");
},
Expand Down
19 changes: 18 additions & 1 deletion src/handlers/secret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,24 @@ export function secretHandler({
);
return tx;
},

transform(input) {
return {
destination_chain: input.destinationChain,
destination_user_address: input.destinationUserAddress,
fee: input.fee,
name: input.name,
symbol: input.symbol,
metadata: input.metadata,
royalty: parseInt(input.royalty),
nft_type: input.nftType,
royalty_receiver: input.royaltyReceiver,
source_chain: input.sourceChain,
source_nft_contract_address: input.sourceNftContractAddress,
token_amount: input.tokenAmount,
token_id: input.tokenId,
transaction_hash: input.transactionHash,
};
},
async getClaimData(txHash) {
const eventId = "LockedEventInfo";
const tx = await provider.query.getTx(txHash);
Expand Down
18 changes: 18 additions & 0 deletions src/handlers/tezos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,24 @@ export function tezosHandler({
getProvider() {
return Tezos;
},
transform(input) {
return {
dest_address: tas.address(input.destinationUserAddress),
dest_chain: input.destinationChain,
fee: tas.mutez(input.fee),
metadata: input.metadata,
name: input.name,
nft_type: input.nftType,
royalty: tas.nat(input.royalty),
royalty_receiver: tas.address(input.royaltyReceiver),
source_chain: input.sourceChain,
source_nft_contract_address: input.sourceNftContractAddress,
symbol: input.symbol,
token_amount: tas.nat(input.tokenAmount),
token_id: tas.nat(input.tokenId),
transaction_hash: input.transactionHash,
};
},
async getBalance(signer, _) {
return BigInt(
(await Tezos.tz.getBalance(await signer.publicKeyHash())).toString(),
Expand Down
72 changes: 72 additions & 0 deletions src/handlers/ton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,78 @@ export function tonHandler({
}
throw new Error("No locked event found");
},
transform(input) {
let destinationAddress: Address;
try {
destinationAddress = Address.parseFriendly(
input.destinationUserAddress,
).address;
} catch (e) {
destinationAddress = Address.parseFriendly(
input.royaltyReceiver,
).address;
}
let sourceNftContractAddress_ = beginCell()
.storeSlice(
beginCell()
.storeStringTail(input.sourceNftContractAddress)
.endCell()
.asSlice(),
)
.endCell();
try {
sourceNftContractAddress_ = beginCell()
.storeSlice(
beginCell()
.storeAddress(
Address.parseFriendly(input.sourceNftContractAddress).address,
)
.endCell()
.asSlice(),
)
.endCell();
} catch (e) {
console.log("Not Native TON Address");
}
return {
$$type: "ClaimData",
data1: {
$$type: "ClaimData1",
destinationChain: input.destinationChain,
destinationUserAddress: destinationAddress,
sourceChain: input.sourceChain,
tokenAmount: BigInt(input.tokenAmount),
tokenId: BigInt(input.tokenId),
},
data2: {
$$type: "ClaimData2",
name: input.name,
nftType: input.nftType,
symbol: input.symbol,
},
data3: {
$$type: "ClaimData3",
fee: BigInt(input.fee),
metadata: input.metadata,
royaltyReceiver: Address.parseFriendly(input.royaltyReceiver).address,
sourceNftContractAddress: sourceNftContractAddress_,
},
data4: {
$$type: "ClaimData4",
newContent: beginCell()
.storeInt(0x01, 8)
.storeStringRefTail(input.metadata)
.endCell(),
royalty: {
$$type: "RoyaltyParams",
denominator: 10000n,
destination: Address.parseFriendly(input.royaltyReceiver).address,
numerator: BigInt(input.royalty),
},
transactionHash: input.transactionHash,
},
};
},
async lockNft(signer, sourceNft, destinationChain, to, tokenId, _) {
if (!signer.address) {
throw new Error("No Address present in signer");
Expand Down

0 comments on commit 8cbea81

Please sign in to comment.