@xlink-network/xlink-sdk
Version:
137 lines (135 loc) • 4.41 kB
JavaScript
import {
BridgeTimeLockAbi
} from "./chunk-66RPLHDE.mjs";
import {
BridgeEndpointAbi
} from "./chunk-UNVARCOY.mjs";
import {
getEVMContractCallInfo,
getEVMTokenContractInfo,
numberFromSolidityContractNumber
} from "./chunk-UJDCORME.mjs";
import {
toSDKNumberOrUndefined
} from "./chunk-FZQUIYHC.mjs";
import {
nativeCurrencyAddress
} from "./chunk-ZXKZ4Z37.mjs";
import {
decodeHex
} from "./chunk-UZKY7HXV.mjs";
import {
UnsupportedChainError
} from "./chunk-KTUPNUM5.mjs";
import {
_allKnownEVMTokens
} from "./chunk-GZDQGY7B.mjs";
import {
isNotNull
} from "./chunk-E5Y3FGR6.mjs";
import {
BigNumber
} from "./chunk-KHTJNOQE.mjs";
// src/xlinkSdkUtils/timelockFromEVM.ts
import { encodeFunctionData, zeroAddress } from "viem";
import { estimateGas, readContract } from "viem/actions";
var getTimeLockContractCallInfo = async (ctx, chain) => {
const info = await getEVMContractCallInfo(ctx, chain);
if (info == null) return;
const timeLockContractAddress = info.timeLockContractAddress ?? await readContract(info.client, {
abi: BridgeEndpointAbi,
address: info.bridgeEndpointContractAddress,
functionName: "timeLock"
}).catch((err) => {
console.groupCollapsed(
`Failed to read timeLock contract address from ${info.bridgeEndpointContractAddress} (${chain})`
);
console.debug(err);
console.groupEnd();
return zeroAddress;
});
if (timeLockContractAddress === zeroAddress) return;
return { client: info.client, timeLockContractAddress };
};
async function getTimeLockedAssetsFromEVM(ctx, input) {
const promises = input.chains.map(async (chain) => {
const timeLockCallInfo = await getTimeLockContractCallInfo(ctx, chain);
if (timeLockCallInfo == null) return [];
const tokenCallInfos = (await Promise.all(
_allKnownEVMTokens.map(
(token) => getEVMTokenContractInfo(ctx, chain, token).then(
(info) => info == null || info.tokenContractAddress === nativeCurrencyAddress ? null : { ...info, chain, token }
)
)
)).filter(isNotNull);
const agreements = (await Promise.all(
tokenCallInfos.map(
(info) => readContract(timeLockCallInfo.client, {
abi: BridgeTimeLockAbi,
address: timeLockCallInfo.timeLockContractAddress,
functionName: "agreementsByUser",
args: [input.walletAddress, 0, info.tokenContractAddress, "0x"]
}).then(
(agreementId) => agreementId === 0n ? [] : [{ agreementId, info }]
)
)
)).flat();
return Promise.all(
agreements.map(async ({ agreementId, info }) => {
const agreement = await readContract(timeLockCallInfo.client, {
abi: BridgeTimeLockAbi,
address: timeLockCallInfo.timeLockContractAddress,
functionName: "agreements",
args: [agreementId]
});
return {
id: String(agreementId),
chain: info.chain,
token: info.token,
amount: toSDKNumberOrUndefined(
numberFromSolidityContractNumber(agreement[0])
),
releaseTime: new Date(agreement[6] * 1e3)
};
})
);
});
return {
assets: (await Promise.all(promises)).flat()
};
}
var claimTimeLockedAssetsFromEVM = async (ctx, input) => {
const info = await getTimeLockContractCallInfo(ctx, input.chain);
if (info == null) throw new UnsupportedChainError(input.chain);
const functionData = encodeFunctionData({
abi: BridgeTimeLockAbi,
functionName: "claim",
args: [
input.lockedAssetIds.map(
(id) => BigNumber.toBigInt({ roundingMode: BigNumber.roundDown }, id)
)
]
});
const estimated = await estimateGas(info.client, {
account: input.walletAddress,
to: info.timeLockContractAddress,
data: functionData
}).then((n) => BigNumber.round({ precision: 0 }, BigNumber.mul(n, 1.2))).catch(
// add a fallback in case estimate failed
() => (
// https://bscscan.com/tx/0x28a81312ca7bc93e7ef07867c9906a41b251ea3ea630b0a4837bdb3066489b32
1 * 1e6
)
);
return await input.sendTransaction({
from: input.walletAddress,
to: info.timeLockContractAddress,
data: decodeHex(functionData),
recommendedGasLimit: toSDKNumberOrUndefined(estimated)
});
};
export {
getTimeLockedAssetsFromEVM,
claimTimeLockedAssetsFromEVM
};
//# sourceMappingURL=chunk-P5KNCVD4.mjs.map