-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
806 additions
and
469 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import { translate } from "@translations"; | ||
import BigNumber from "bignumber.js"; | ||
import { DfTxSigner } from "@waveshq/walletkit-ui/dist/store"; | ||
import { WhaleWalletAccount } from "@defichain/whale-api-wallet"; | ||
import { | ||
CTransactionSegWit, | ||
TransactionSegWit, | ||
} from "@defichain/jellyfish-transaction"; | ||
import { ConvertDirection } from "@screens/enum"; | ||
|
||
const TRANSFER_DOMAIN_TYPE = { | ||
DVM: 2, | ||
EVM: 3, | ||
}; | ||
|
||
export interface TransferDomainToken { | ||
tokenId: string; | ||
displaySymbol: string; | ||
displayTextSymbol: string; | ||
balance: BigNumber; | ||
} | ||
|
||
export async function transferDomainSigner( | ||
account: WhaleWalletAccount, | ||
sourceTokenId: string, | ||
targetTokenId: string, | ||
amount: BigNumber, | ||
convertDirection: ConvertDirection | ||
): Promise<CTransactionSegWit> { | ||
const dvmScript = await account.getScript(); | ||
const evmScript = await account.getEvmScript(); | ||
const builder = account.withTransactionBuilder(); | ||
|
||
const [sourceScript, dstScript] = | ||
convertDirection === ConvertDirection.evmToDvm | ||
? [evmScript, dvmScript] | ||
: [dvmScript, evmScript]; | ||
|
||
const [srcDomain, dstDomain] = | ||
convertDirection === ConvertDirection.evmToDvm | ||
? [TRANSFER_DOMAIN_TYPE.EVM, TRANSFER_DOMAIN_TYPE.DVM] | ||
: [TRANSFER_DOMAIN_TYPE.DVM, TRANSFER_DOMAIN_TYPE.EVM]; | ||
|
||
const signed: TransactionSegWit = await builder.account.transferDomain( | ||
{ | ||
items: [ | ||
{ | ||
src: { | ||
address: sourceScript, | ||
amount: { | ||
token: Number(sourceTokenId), | ||
amount, | ||
}, | ||
domain: srcDomain, | ||
}, | ||
dst: { | ||
address: dstScript, | ||
amount: { | ||
token: Number(targetTokenId), | ||
amount, | ||
}, | ||
domain: dstDomain, | ||
}, | ||
}, | ||
], | ||
}, | ||
dvmScript | ||
); | ||
|
||
return new CTransactionSegWit(signed); | ||
} | ||
|
||
export function transferDomainCrafter( | ||
amount: BigNumber, | ||
convertDirection: ConvertDirection, | ||
sourceToken: TransferDomainToken, | ||
targetToken: TransferDomainToken, | ||
onBroadcast: () => any, | ||
onConfirmation: () => void, | ||
submitButtonLabel?: string | ||
): DfTxSigner { | ||
if ( | ||
![ConvertDirection.evmToDvm, ConvertDirection.dvmToEvm].includes( | ||
convertDirection | ||
) | ||
) { | ||
throw new Error("Unexpected transfer domain"); | ||
} | ||
|
||
const [symbolA, symbolB] = | ||
convertDirection === ConvertDirection.dvmToEvm | ||
? [sourceToken.displayTextSymbol, `${targetToken.displayTextSymbol}-EVM`] | ||
: [`${targetToken.displayTextSymbol}-EVM`, sourceToken.displayTextSymbol]; | ||
|
||
return { | ||
sign: async (account: WhaleWalletAccount) => | ||
await transferDomainSigner( | ||
account, | ||
sourceToken.tokenId, | ||
targetToken.tokenId, | ||
amount, | ||
convertDirection | ||
), | ||
title: translate( | ||
"screens/ConvertConfirmScreen", | ||
"Convert {{amount}} {{symbolA}} to {{symbolB}} tokens", | ||
{ | ||
amount: amount.toFixed(8), | ||
symbolA, | ||
symbolB, | ||
} | ||
), | ||
drawerMessages: { | ||
preparing: translate("screens/OceanInterface", "Preparing to convert…"), | ||
waiting: translate( | ||
"screens/OceanInterface", | ||
"Converting {{amount}} {{symbolA}} to {{symbolB}} tokens", | ||
{ | ||
symbolA: symbolA, | ||
symbolB: symbolB, | ||
amount: amount.toFixed(8), | ||
} | ||
), | ||
complete: translate( | ||
"screens/OceanInterface", | ||
"{{amount}} {{symbolA}} converted to {{symbolB}} tokens", | ||
{ | ||
symbolA: symbolA, | ||
symbolB: symbolB, | ||
amount: amount.toFixed(8), | ||
} | ||
), | ||
}, | ||
onBroadcast, | ||
onConfirmation, | ||
submitButtonLabel: | ||
submitButtonLabel !== undefined | ||
? translate("screens/ConvertConfirmScreen", submitButtonLabel) | ||
: undefined, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.