sdk/aptos: move transfer logic to sdk

This commit is contained in:
aki 2022-10-25 06:13:45 +00:00 committed by Evan Gray
parent 9ae43b3dd9
commit 027354f319
2 changed files with 37 additions and 16 deletions

View File

@ -202,31 +202,39 @@ export const transferTokens = (
recipientChain: ChainId | ChainName, recipientChain: ChainId | ChainName,
recipient: Uint8Array, recipient: Uint8Array,
relayerFee: string, relayerFee: string,
nonce: number, nonce: number
payload: string = "",
): Types.EntryFunctionPayload => { ): Types.EntryFunctionPayload => {
if (!tokenBridgeAddress) throw new Error("Need token bridge address."); if (!tokenBridgeAddress) throw new Error("Need token bridge address.");
if (!isValidAptosType(fullyQualifiedType)) { if (!isValidAptosType(fullyQualifiedType)) {
throw new Error("Need fully qualified address"); throw new Error("Invalid qualified type");
} }
const recipientChainId = coalesceChainId(recipientChain); const recipientChainId = coalesceChainId(recipientChain);
if (payload) { return {
throw new Error("Transfer with payload are not yet supported in the sdk"); function: `${tokenBridgeAddress}::transfer_tokens::transfer_tokens_entry`,
} else { type_arguments: [fullyQualifiedType],
return { arguments: [amount, recipientChainId, recipient, relayerFee, nonce],
function: `${tokenBridgeAddress}::transfer_tokens::transfer_tokens_entry`, };
type_arguments: [fullyQualifiedType], };
arguments: [amount, recipientChainId, recipient, relayerFee, nonce],
}; export const transferTokensWithPayload = (
} tokenBridgeAddress: string,
fullyQualifiedType: string,
amount: string,
recipientChain: ChainId | ChainName,
recipient: Uint8Array,
relayerFee: string,
nonce: number,
payload: string
): Types.EntryFunctionPayload => {
throw new Error("Transfer with payload are not yet supported in the sdk");
}; };
// Created wrapped coin // Created wrapped coin
export const createWrappedCoinType = ( export const createWrappedCoinType = (
tokenBridgeAddress: string, tokenBridgeAddress: string,
vaa: Uint8Array, vaa: Uint8Array
): Types.EntryFunctionPayload => { ): Types.EntryFunctionPayload => {
if (!tokenBridgeAddress) throw new Error("Need token bridge address."); if (!tokenBridgeAddress) throw new Error("Need token bridge address.");
return { return {

View File

@ -53,7 +53,7 @@ const BN = require("bn.js");
import { FunctionCallOptions } from "near-api-js/lib/account"; import { FunctionCallOptions } from "near-api-js/lib/account";
import { Provider } from "near-api-js/lib/providers"; import { Provider } from "near-api-js/lib/providers";
import { MsgExecuteContract as XplaMsgExecuteContract } from "@xpla/xpla.js"; import { MsgExecuteContract as XplaMsgExecuteContract } from "@xpla/xpla.js";
import { transferTokens as transferTokensAptos } from "../aptos"; import { transferTokens as transferTokensAptos, transferTokensWithPayload } from "../aptos";
export async function getAllowanceEth( export async function getAllowanceEth(
tokenBridgeAddress: string, tokenBridgeAddress: string,
@ -981,6 +981,20 @@ export function transferFromAptos(
relayerFee: string = "0", relayerFee: string = "0",
payload: string = "" payload: string = ""
): Types.EntryFunctionPayload { ): Types.EntryFunctionPayload {
if (payload) {
// Currently unsupported
return transferTokensWithPayload(
tokenBridgeAddress,
fullyQualifiedType,
amount,
recipientChain,
recipient,
relayerFee,
createNonce().readUInt32LE(0),
payload
);
}
return transferTokensAptos( return transferTokensAptos(
tokenBridgeAddress, tokenBridgeAddress,
fullyQualifiedType, fullyQualifiedType,
@ -988,7 +1002,6 @@ export function transferFromAptos(
recipientChain, recipientChain,
recipient, recipient,
relayerFee, relayerFee,
createNonce().readUInt32LE(0), createNonce().readUInt32LE(0)
payload
); );
} }