Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions modules/sdk-coin-ton/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export { TransactionBuilder } from './transactionBuilder';
export { TransferBuilder } from './transferBuilder';
export { TransactionBuilderFactory } from './transactionBuilderFactory';
export { TonWhalesVestingDepositBuilder } from './tonWhalesVestingDepositBuilder';
export { TonWhalesVestingWithdrawBuilder } from './tonWhalesVestingWithdrawBuilder';
export { Interface, Utils };
35 changes: 35 additions & 0 deletions modules/sdk-coin-ton/src/lib/tonWhalesVestingWithdrawBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { BaseCoin as CoinConfig } from '@bitgo/statics';
import { Recipient, TransactionType } from '@bitgo/sdk-core';
import { TransactionBuilder } from './transactionBuilder';
import { Transaction } from './transaction';

export class TonWhalesVestingWithdrawBuilder extends TransactionBuilder {
constructor(_coinConfig: Readonly<CoinConfig>) {
super(_coinConfig);
this._transaction = new Transaction(_coinConfig);
}
protected get transactionType(): TransactionType {
return TransactionType.TonWhalesVestingWithdrawal;
}
setWithdrawMessage(): TonWhalesVestingWithdrawBuilder {
this.transaction.message = 'Withdraw';
return this;
}

setForwardAmount(amount: string): TonWhalesVestingWithdrawBuilder {
if (!this.transaction.recipient) {
this.transaction.recipient = { address: '', amount: amount };
} else {
this.transaction.recipient.amount = amount;
}
return this;
}

send(recipient: Recipient): TonWhalesVestingWithdrawBuilder {
this.transaction.recipient = recipient;
return this;
}
setMessage(msg: string): TonWhalesVestingWithdrawBuilder {
throw new Error('Method not implemented. Use setWithdrawMessage() instead.');
}
}
8 changes: 7 additions & 1 deletion modules/sdk-coin-ton/src/lib/transactionBuilderFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { TokenTransaction } from './tokenTransaction';
import { TonWhalesDepositBuilder } from './tonWhalesDepositBuilder';
import { TonWhalesWithdrawalBuilder } from './tonWhalesWithdrawalBuilder';
import { TonWhalesVestingDepositBuilder } from './tonWhalesVestingDepositBuilder';
import { TonWhalesVestingWithdrawBuilder } from './tonWhalesVestingWithdrawBuilder';

export class TransactionBuilderFactory extends BaseTransactionBuilderFactory {
constructor(_coinConfig: Readonly<CoinConfig>) {
Expand Down Expand Up @@ -50,7 +51,8 @@ export class TransactionBuilderFactory extends BaseTransactionBuilderFactory {
builder = this.getTonWhalesVestingDepositBuilder();
break;
case TransactionType.TonWhalesVestingWithdrawal:
throw new InvalidTransactionError('TonWhalesVestingWithdrawal builder not implemented yet');
builder = this.getTonWhalesVestingWithdrawBuilder();
break;
default:
throw new InvalidTransactionError('unsupported transaction');
}
Expand Down Expand Up @@ -96,4 +98,8 @@ export class TransactionBuilderFactory extends BaseTransactionBuilderFactory {
getTonWhalesVestingDepositBuilder(): TonWhalesVestingDepositBuilder {
return new TonWhalesVestingDepositBuilder(this._coinConfig);
}

getTonWhalesVestingWithdrawBuilder(): TonWhalesVestingWithdrawBuilder {
return new TonWhalesVestingWithdrawBuilder(this._coinConfig);
}
}
12 changes: 12 additions & 0 deletions modules/sdk-coin-ton/test/resources/ton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,15 @@ export const tonWhalesVestingDepositFixture = {
expireTime: 1234567890,
bounceable: true,
};

export const tonWhalesVestingWithdrawFixture = {
recipient: {
address: 'EQDr9Sq482A6ikIUh5mUUjJaBUUJBrye13CJiDB-R31_lwIq',
amount: '200000000',
},
sender: 'EQBkD52LACNxGgaoAxm5Nhs0SN6gg8hNaceNYifev88Y7qoZ',
publicKey: '9d6d3714aeb1f007f6e6aa728f79fdd005ea2c7ad459b2f54d73f9e672426230',
seqno: 0,
expireTime: 1234567890,
bounceable: true,
};
120 changes: 120 additions & 0 deletions modules/sdk-coin-ton/test/unit/tonWhalesVestingWithdrawBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import should from 'should';
import { TransactionType } from '@bitgo/sdk-core';
import { TransactionBuilderFactory } from '../../src';
import { coins } from '@bitgo/statics';
import * as testData from '../resources/ton';

describe('Ton Whales Vesting Withdraw Builder', () => {
const factory = new TransactionBuilderFactory(coins.get('tton'));
const fixture = testData.tonWhalesVestingWithdrawFixture;

it('should build an unsigned vesting withdraw transaction', async function () {
const txBuilder = factory.getTonWhalesVestingWithdrawBuilder();

txBuilder.sender(fixture.sender);
txBuilder.publicKey(fixture.publicKey);
txBuilder.sequenceNumber(fixture.seqno);
txBuilder.expireTime(fixture.expireTime);
txBuilder.bounceable(fixture.bounceable);

txBuilder.send({
address: fixture.recipient.address,
amount: fixture.recipient.amount,
});
txBuilder.setForwardAmount(fixture.recipient.amount);
txBuilder.setWithdrawMessage();

const tx = await txBuilder.build();

should.equal(tx.type, TransactionType.TonWhalesVestingWithdrawal);
should.equal(tx.toJson().bounceable, fixture.bounceable);
should.equal(tx.toJson().destination, fixture.recipient.address);
should.equal(tx.toJson().amount, fixture.recipient.amount);

tx.inputs.length.should.equal(1);
tx.inputs[0].should.deepEqual({
address: fixture.sender,
value: fixture.recipient.amount,
coin: 'tton',
});

tx.outputs.length.should.equal(1);
tx.outputs[0].should.deepEqual({
address: fixture.recipient.address,
value: fixture.recipient.amount,
coin: 'tton',
});
});

it('should build and parse a vesting withdraw transaction', async function () {
const txBuilder = factory.getTonWhalesVestingWithdrawBuilder();

txBuilder.sender(fixture.sender);
txBuilder.publicKey(fixture.publicKey);
txBuilder.sequenceNumber(fixture.seqno);
txBuilder.expireTime(fixture.expireTime);
txBuilder.bounceable(fixture.bounceable);

txBuilder.send({
address: fixture.recipient.address,
amount: fixture.recipient.amount,
});
txBuilder.setForwardAmount(fixture.recipient.amount);
txBuilder.setWithdrawMessage();

const tx = await txBuilder.build();
const rawTx = tx.toBroadcastFormat();

const txBuilder2 = factory.from(rawTx);
const tx2 = await txBuilder2.build();

should.equal(tx2.type, TransactionType.TonWhalesVestingWithdrawal);
should.equal(tx2.toBroadcastFormat(), rawTx);
});

it('should set the correct message for vesting withdraw', async function () {
const txBuilder = factory.getTonWhalesVestingWithdrawBuilder();

txBuilder.sender(fixture.sender);
txBuilder.publicKey(fixture.publicKey);
txBuilder.sequenceNumber(fixture.seqno);
txBuilder.expireTime(fixture.expireTime);
txBuilder.bounceable(fixture.bounceable);

txBuilder.send({
address: fixture.recipient.address,
amount: fixture.recipient.amount,
});
txBuilder.setForwardAmount(fixture.recipient.amount);
txBuilder.setWithdrawMessage();

const tx = await txBuilder.build();
const message = tx['message'];
should.equal(message, 'Withdraw');
});

it('should support vesting contract specific flags', async function () {
const txBuilder = factory.getTonWhalesVestingWithdrawBuilder();

txBuilder.sender(fixture.sender);
txBuilder.publicKey(fixture.publicKey);
txBuilder.sequenceNumber(fixture.seqno);
txBuilder.expireTime(fixture.expireTime);
txBuilder.bounceable(true);
txBuilder.isV3ContractMessage(true);
txBuilder.subWalletId(268);

txBuilder.send({
address: fixture.recipient.address,
amount: fixture.recipient.amount,
});
txBuilder.setForwardAmount(fixture.recipient.amount);
txBuilder.setWithdrawMessage();

const tx = await txBuilder.build();

should.equal(tx.type, TransactionType.TonWhalesVestingWithdrawal);
should.equal(tx.toJson().bounceable, true);
should.equal(tx.toJson().sub_wallet_id, 268);
});
});
Loading