|
| 1 | +import BigNumber from 'bignumber.js'; |
| 2 | +import { BitGoBase, CoinConstructor, NamedCoinConstructor, VerifyTransactionOptions } from '@bitgo/sdk-core'; |
| 3 | +import { coins, JettonTokenConfig, NetworkType, tokens } from '@bitgo/statics'; |
| 4 | + |
| 5 | +import { Transaction } from './lib'; |
| 6 | +import { Ton } from './ton'; |
| 7 | + |
| 8 | +export class JettonToken extends Ton { |
| 9 | + public readonly tokenConfig: JettonTokenConfig; |
| 10 | + |
| 11 | + constructor(bitgo: BitGoBase, tokenConfig: JettonTokenConfig) { |
| 12 | + const staticsCoin = tokenConfig.network === NetworkType.MAINNET ? coins.get('ton') : coins.get('tton'); |
| 13 | + super(bitgo, staticsCoin); |
| 14 | + this.tokenConfig = tokenConfig; |
| 15 | + } |
| 16 | + |
| 17 | + static createTokenConstructor(config: JettonTokenConfig): CoinConstructor { |
| 18 | + return (bitgo: BitGoBase) => new JettonToken(bitgo, config); |
| 19 | + } |
| 20 | + |
| 21 | + static createTokenConstructors( |
| 22 | + tokenConfig: JettonTokenConfig[] = [...tokens.bitcoin.ton.tokens, ...tokens.testnet.ton.tokens] |
| 23 | + ): NamedCoinConstructor[] { |
| 24 | + const tokensCtors: NamedCoinConstructor[] = []; |
| 25 | + for (const token of tokenConfig) { |
| 26 | + const tokenConstructor = JettonToken.createTokenConstructor(token); |
| 27 | + tokensCtors.push({ name: token.type, coinConstructor: tokenConstructor }); |
| 28 | + } |
| 29 | + return tokensCtors; |
| 30 | + } |
| 31 | + |
| 32 | + get name(): string { |
| 33 | + return this.tokenConfig.name; |
| 34 | + } |
| 35 | + |
| 36 | + get coin(): string { |
| 37 | + return this.tokenConfig.coin; |
| 38 | + } |
| 39 | + |
| 40 | + get contractAddress(): string { |
| 41 | + return this.tokenConfig.contractAddress; |
| 42 | + } |
| 43 | + |
| 44 | + get decimalPlaces(): number { |
| 45 | + return this.tokenConfig.decimalPlaces; |
| 46 | + } |
| 47 | + |
| 48 | + getChain(): string { |
| 49 | + return this.tokenConfig.type; |
| 50 | + } |
| 51 | + |
| 52 | + getBaseChain(): string { |
| 53 | + return this.coin; |
| 54 | + } |
| 55 | + |
| 56 | + getFullName(): string { |
| 57 | + return 'Jetton Token'; |
| 58 | + } |
| 59 | + |
| 60 | + getBaseFactor(): number { |
| 61 | + return Math.pow(10, this.tokenConfig.decimalPlaces); |
| 62 | + } |
| 63 | + |
| 64 | + async verifyTransaction(params: VerifyTransactionOptions): Promise<boolean> { |
| 65 | + const { txPrebuild: txPrebuild, txParams: txParams } = params; |
| 66 | + const rawTx = txPrebuild.txHex; |
| 67 | + let totalAmount = new BigNumber(0); |
| 68 | + if (!rawTx) { |
| 69 | + throw new Error('missing required tx prebuild property txHex'); |
| 70 | + } |
| 71 | + const coinConfig = coins.get(this.getChain()); |
| 72 | + const transaction = new Transaction(coinConfig); |
| 73 | + transaction.fromRawTransaction(Buffer.from(rawTx, 'hex').toString('base64')); |
| 74 | + const explainedTx = transaction.explainTransaction(); |
| 75 | + if (txParams.recipients !== undefined) { |
| 76 | + txParams.recipients.forEach((recipient) => { |
| 77 | + if (recipient.tokenName && recipient.tokenName !== coinConfig.name) { |
| 78 | + throw new Error('incorrect token name specified in recipients'); |
| 79 | + } |
| 80 | + recipient.tokenName = coinConfig.name; |
| 81 | + }); |
| 82 | + const filteredRecipients = txParams.recipients?.map((recipient) => ({ |
| 83 | + address: recipient.address, |
| 84 | + amount: recipient.amount, |
| 85 | + tokenName: recipient.tokenName, |
| 86 | + })); |
| 87 | + const filteredOutputs = explainedTx.outputs.map((output) => ({ |
| 88 | + address: output.address, |
| 89 | + amount: output.amount, |
| 90 | + tokenName: output.tokenName, |
| 91 | + })); |
| 92 | + const outputsMatch = JSON.stringify(filteredRecipients) === JSON.stringify(filteredOutputs); |
| 93 | + if (!outputsMatch) { |
| 94 | + throw new Error('Tx outputs does not match with expected txParams recipients'); |
| 95 | + } |
| 96 | + for (const recipient of txParams.recipients) { |
| 97 | + totalAmount = totalAmount.plus(recipient.amount); |
| 98 | + } |
| 99 | + if (!totalAmount.isEqualTo(explainedTx.outputAmount)) { |
| 100 | + throw new Error('Tx total amount does not match with expected total amount field'); |
| 101 | + } |
| 102 | + } |
| 103 | + return true; |
| 104 | + } |
| 105 | +} |
0 commit comments