Skip to content

Commit

Permalink
Implement all FA12 Tezos transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
maxima-net committed Sep 5, 2022
1 parent 81ecba8 commit 61e3c44
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import type { Wallet } from '@taquito/taquito';
import BigNumber from 'bignumber.js';

import { atomexProtocolMultiChainHelper } from '../../blockchain/atomexProtocolMultiChain';
import type {
AtomexBlockchainProvider,
Expand All @@ -9,7 +12,9 @@ import type {
import type { AtomexNetwork } from '../../common/index';
import type { DeepReadonly } from '../../core/index';
import type { PriceManager } from '../../exchange';
import type { FA12TezosMultiChainSmartContract } from '../models/contracts';
import type { FA12TezosTaquitoAtomexProtocolMultiChainOptions } from '../models/index';
import { isFA12TezosCurrency } from '../utils';
import { TaquitoAtomexProtocolMultiChain } from './taquitoAtomexProtocolMultiChain';

export class FA12TezosTaquitoAtomexProtocolMultiChain extends TaquitoAtomexProtocolMultiChain {
Expand All @@ -27,16 +32,40 @@ export class FA12TezosTaquitoAtomexProtocolMultiChain extends TaquitoAtomexProto
return this.atomexProtocolOptions.currencyId;
}

initiate(_params: AtomexProtocolMultiChainInitiateParameters): Promise<Transaction> {
throw new Error('Method not implemented.');
async initiate(params: AtomexProtocolMultiChainInitiateParameters): Promise<Transaction> {
const currency = this.atomexBlockchainProvider.getCurrency(this.currencyId);
if (!currency)
throw new Error(`Currency not found for id: ${this.currencyId}`);

if (!isFA12TezosCurrency(currency))
throw new Error(`Currency is not fa1.2; id: ${this.currencyId}`);


const contract = await this.getSwapContract(params.senderAddress);
const multiplier = new BigNumber(10).pow(currency.decimals);
const operation = await contract.methodsObject
.initiate({
totalAmount: params.amount.multipliedBy(multiplier).toNumber(),
tokenAddress: currency.contractAddress,
refundTime: this.formatDate(params.refundTimestamp),
payoffAmount: params.rewardForRedeem.multipliedBy(multiplier).toNumber(),
hashedSecret: params.secretHash,
participant: params.receivingAddress,
})
.send();

return this.getTransaction(operation);
}

getInitiateFees(params: Partial<AtomexProtocolMultiChainInitiateParameters>): Promise<FeesInfo> {
return super.getInitiateFees(params);
}

redeem(_params: AtomexProtocolMultiChainRedeemParameters): Promise<Transaction> {
throw new Error('Method not implemented.');
async redeem(params: AtomexProtocolMultiChainRedeemParameters): Promise<Transaction> {
const contract = await this.getSwapContract(params.senderAddress);
const operation = await contract.methodsObject.redeem(params.secret).send();

return this.getTransaction(operation);
}

getRedeemReward(redeemFee: FeesInfo): Promise<FeesInfo> {
Expand All @@ -47,11 +76,18 @@ export class FA12TezosTaquitoAtomexProtocolMultiChain extends TaquitoAtomexProto
return super.getRedeemFees(params);
}

refund(_params: AtomexProtocolMultiChainRefundParameters): Promise<Transaction> {
throw new Error('Method not implemented.');
async refund(params: AtomexProtocolMultiChainRefundParameters): Promise<Transaction> {
const contract = await this.getSwapContract(params.senderAddress);
const operation = await contract.methodsObject.refund(params.secret).send();

return this.getTransaction(operation);
}

getRefundFees(params: Partial<AtomexProtocolMultiChainInitiateParameters>): Promise<FeesInfo> {
return super.getRefundFees(params);
}

protected async getSwapContract(address: string): Promise<FA12TezosMultiChainSmartContract<Wallet>> {
return this.getSwapContractCore<FA12TezosMultiChainSmartContract<Wallet>>(address);
}
}
16 changes: 16 additions & 0 deletions src/tezos/models/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,19 @@ export type TezosMultiChainSmartContract<T extends ContractProvider | Wallet = C
refund(secret: string): ContractMethod<T>;
}
};

export type FA12TezosMultiChainSmartContract<T extends ContractProvider | Wallet = ContractProvider> = ContractAbstraction<T> & {
methodsObject: {
initiate(args: {
tokenAddress: string,
totalAmount: number,
hashedSecret: string,
participant: string,
payoffAmount: number,
refundTime: string,
}): ContractMethod<T>;

redeem(secret: string): ContractMethod<T>;
refund(secret: string): ContractMethod<T>;
}
};
6 changes: 5 additions & 1 deletion src/tezos/utils/guards.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import type { Currency } from '../../common';
import type { TezosCurrency } from '../models';
import type { FA12TezosCurrency, TezosCurrency } from '../models';

export const isTezosCurrency = (currency: Currency): currency is TezosCurrency => {
return currency.blockchain === 'tezos';
};

export const isFA12TezosCurrency = (currency: Currency): currency is FA12TezosCurrency => {
return currency.blockchain === 'tezos' && currency.type === 'fa1.2';
};
2 changes: 1 addition & 1 deletion src/tezos/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { b58cdecode, prefix, validatePkAndExtractPrefix } from '@taquito/utils';
import BigNumber from 'bignumber.js';

import { Buffer } from '../../native';
export { isTezosCurrency } from './guards';
export { isTezosCurrency, isFA12TezosCurrency } from './guards';

export const mutezInTez = new BigNumber(1_000_000);

Expand Down

0 comments on commit 61e3c44

Please sign in to comment.