@arcana/ca-sdk
Version:
Arcana Network's chain abstraction SDK for unified balance in Web3 apps
114 lines (113 loc) • 3.37 kB
JavaScript
export const INTENT_ACCEPTED = {
type: "INTENT_ACCEPTED",
typeID: "IA",
};
export const INTENT_HASH_SIGNED = {
type: "INTENT_HASH_SIGNED",
typeID: "IHS",
};
export const INTENT_SUBMITTED = {
type: "INTENT_SUBMITTED",
typeID: "IS",
};
export const INTENT_INIT_STEPS = [
INTENT_ACCEPTED,
INTENT_HASH_SIGNED,
{
...INTENT_SUBMITTED,
data: {
explorerURL: "",
intentID: 0,
},
},
];
export const INTENT_FULFILLED = {
type: "INTENT_FULFILLED",
typeID: "IF",
};
export const ALLOWANCE_APPROVAL_REQ = (chainID) => ({
type: "ALLOWANCE_USER_APPROVAL",
typeID: `AUA_${chainID}`,
});
export const ALLOWANCE_APPROVAL_MINED = (chainID) => ({
type: "ALLOWANCE_APPROVAL_MINED",
typeID: `AAM_${chainID}`,
});
export const ALLOWANCE_COMPLETE = {
type: "ALLOWANCE_ALL_DONE",
typeID: "AAD",
};
export const INTENT_DEPOSIT_REQ = (id) => ({
type: "INTENT_DEPOSIT",
typeID: `ID_${id}`,
});
export const INTENT_DEPOSITS_CONFIRMED = {
type: "INTENT_DEPOSITS_CONFIRMED",
typeID: "UIDC",
};
export const INTENT_COLLECTION_COMPLETE = {
type: "INTENT_COLLECTION_COMPLETE",
typeID: "ICC",
};
export const INTENT_COLLECTION = (id) => ({
type: "INTENT_COLLECTION",
typeID: `IC_${id}`,
});
const INTENT_FINISH_STEPS = [INTENT_FULFILLED];
export const createSteps = (intent, kind, chainList, unallowedSources) => {
const steps = [];
if (kind === "erc20") {
if (unallowedSources && unallowedSources?.length > 0) {
for (const source of unallowedSources) {
steps.push({
...ALLOWANCE_APPROVAL_REQ(source.chain.id),
data: {
chainID: source.chain.id,
chainName: source.chain.name,
},
}, {
...ALLOWANCE_APPROVAL_MINED(source.chain.id),
data: {
chainID: source.chain.id,
chainName: source.chain.name,
},
});
}
steps.push(ALLOWANCE_COMPLETE);
}
}
steps.push(...INTENT_INIT_STEPS);
const sources = intent.sources.filter((s) => s.chainID !== intent.destination.chainID);
if (kind === "native") {
for (const [i, s] of sources.entries()) {
const chain = chainList.getChainByID(s.chainID);
if (!chain) {
throw new Error(`Unknown chain ID ${s.chainID} while building steps`);
}
steps.push({
...INTENT_DEPOSIT_REQ(i + 1),
data: {
amount: s.amount.toString(),
chainID: chain.id,
chainName: chain.name,
symbol: chain.nativeCurrency.symbol,
},
});
}
steps.push(INTENT_DEPOSITS_CONFIRMED);
}
if (kind === "erc20") {
for (const [i] of sources.entries()) {
steps.push({
...INTENT_COLLECTION(i + 1),
data: {
confirmed: i + 1,
total: sources.length,
},
});
}
steps.push(INTENT_COLLECTION_COMPLETE);
}
steps.push(...INTENT_FINISH_STEPS);
return steps;
};