|
| 1 | +import * as nock from 'nock'; |
| 2 | +import * as should from 'should'; |
| 3 | +import fixtures from '../../fixtures/staking/goStakingWallet'; |
| 4 | + |
| 5 | +import { Enterprise, Environments, GoStakingWallet, Wallet } from '@bitgo/sdk-core'; |
| 6 | +import { TestBitGo } from '@bitgo/sdk-test'; |
| 7 | +import { BitGo } from '../../../../src'; |
| 8 | +import * as sinon from 'sinon'; |
| 9 | +import { OfcToken } from '../../../../src/v2/coins'; |
| 10 | +import { tokens } from '@bitgo/statics'; |
| 11 | + |
| 12 | +describe('Go Staking Wallet Common', function () { |
| 13 | + const microservicesUri = Environments['mock'].uri; |
| 14 | + let bitgo; |
| 15 | + let baseCoin; |
| 16 | + let enterprise; |
| 17 | + let stakingWallet: GoStakingWallet; |
| 18 | + const coin = 'tsol'; |
| 19 | + const ofcCoin = `ofc${coin}`; |
| 20 | + |
| 21 | + before(async function () { |
| 22 | + bitgo = TestBitGo.decorate(BitGo, { env: 'mock', microservicesUri } as any); |
| 23 | + bitgo.initializeTestVars(); |
| 24 | + baseCoin = bitgo.coin(ofcCoin); |
| 25 | + baseCoin.keychains(); |
| 26 | + const ofcToken = tokens.testnet.ofc.tokens.filter((token) => token.type === `ofc${coin}`)[0]; |
| 27 | + const tokenConstructor = OfcToken.createTokenConstructor(ofcToken); |
| 28 | + bitgo.register(ofcToken.type, tokenConstructor); |
| 29 | + |
| 30 | + enterprise = new Enterprise(bitgo, baseCoin, { id: '5cf940949449412d00f53b3d92dbcaa3', name: 'Test Enterprise' }); |
| 31 | + const walletData = { |
| 32 | + id: 'walletId', |
| 33 | + coin: ofcCoin, |
| 34 | + enterprise: enterprise.id, |
| 35 | + keys: ['5b3424f91bf349930e340175', '5b3424f91bf349930e340174', '5b3424f91bf349930e340173'], |
| 36 | + }; |
| 37 | + const wallet = new Wallet(bitgo, baseCoin, walletData); |
| 38 | + stakingWallet = wallet.toGoStakingWallet(); |
| 39 | + nock(microservicesUri) |
| 40 | + .get(`/api/v2/${ofcCoin}/key/${stakingWallet.wallet.keyIds()[0]}`) |
| 41 | + .reply(200, { |
| 42 | + id: stakingWallet.wallet.keyIds()[0], |
| 43 | + pub: 'xpub661MyMwAqRbcFq65dvGMeEVb81KKDRRkWkawSVesWcyevGc5gr8V27LjNfkktaMuKtM362jhgKy2eu35RdArcmmEAoULzAvgKkJpWQPvLXM', |
| 44 | + source: 'user', |
| 45 | + encryptedPrv: bitgo.encrypt({ |
| 46 | + input: |
| 47 | + 'xprvA41z7zogVVwxVSgdKUHDy1SKmdb533PjDz7J6N6mV6uS3ze1ai8FHa8kmHScGpWmj4WggLyQjgPie1rFSruoUihUZREPSL39UNdE3BBDu76', |
| 48 | + password: 'passphrase', |
| 49 | + }), |
| 50 | + coinSpecific: {}, |
| 51 | + }); |
| 52 | + |
| 53 | + nock(microservicesUri) |
| 54 | + .get(`/api/v2/${ofcCoin}/key/${stakingWallet.wallet.keyIds()[1]}`) |
| 55 | + .reply(200, { |
| 56 | + id: stakingWallet.wallet.keyIds()[1], |
| 57 | + pub: 'xpub661MyMwAqRbcFq65dvGMeEVb81KKDRRkWkawSVesWcyevGc5gr8V27LjNfkktaMuKtM362jhgKy2eu35RdArcmmEAoULzAvgKkJpWQPvLXM', |
| 58 | + source: 'user', |
| 59 | + encryptedPrv: bitgo.encrypt({ |
| 60 | + input: |
| 61 | + 'xprvA41z7zogVVwxVSgdKUHDy1SKmdb533PjDz7J6N6mV6uS3ze1ai8FHa8kmHScGpWmj4WggLyQjgPie1rFSruoUihUZREPSL39UNdE3BBDu76', |
| 62 | + password: 'passphrase', |
| 63 | + }), |
| 64 | + coinSpecific: {}, |
| 65 | + }); |
| 66 | + |
| 67 | + nock(microservicesUri) |
| 68 | + .get(`/api/v2/${ofcCoin}/key/${stakingWallet.wallet.keyIds()[2]}`) |
| 69 | + .reply(200, { |
| 70 | + id: stakingWallet.wallet.keyIds()[2], |
| 71 | + pub: 'xpub661MyMwAqRbcFq65dvGMeEVb81KKDRRkWkawSVesWcyevGc5gr8V27LjNfkktaMuKtM362jhgKy2eu35RdArcmmEAoULzAvgKkJpWQPvLXM', |
| 72 | + source: 'user', |
| 73 | + encryptedPrv: bitgo.encrypt({ |
| 74 | + input: |
| 75 | + 'xprvA41z7zogVVwxVSgdKUHDy1SKmdb533PjDz7J6N6mV6uS3ze1ai8FHa8kmHScGpWmj4WggLyQjgPie1rFSruoUihUZREPSL39UNdE3BBDu76', |
| 76 | + password: 'passphrase', |
| 77 | + }), |
| 78 | + coinSpecific: {}, |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + const sandbox = sinon.createSandbox(); |
| 83 | + |
| 84 | + afterEach(function () { |
| 85 | + sandbox.verifyAndRestore(); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('stake', function () { |
| 89 | + it('should call go-staking-service to stake', async function () { |
| 90 | + const preview = fixtures.previewGoStakingRequest(coin); |
| 91 | + const msScope1 = nock(microservicesUri) |
| 92 | + .post(`/api/go-staking/v1/${ofcCoin}/accounts/${stakingWallet.accountId}/requests/preview`, { |
| 93 | + amount: '1', |
| 94 | + clientId: 'clientId', |
| 95 | + type: 'STAKE', |
| 96 | + }) |
| 97 | + .reply(201, preview); |
| 98 | + |
| 99 | + const expected = fixtures.finalizeGoStakingRequest(coin, 'STAKE'); |
| 100 | + const msScope2 = nock(microservicesUri) |
| 101 | + .post(`/api/go-staking/v1/${ofcCoin}/accounts/${stakingWallet.accountId}/requests/finalize`, { |
| 102 | + amount: '1', |
| 103 | + clientId: 'clientId', |
| 104 | + frontTransferSendRequest: { |
| 105 | + halfSigned: { |
| 106 | + payload: preview.payload, |
| 107 | + }, |
| 108 | + }, |
| 109 | + type: 'STAKE', |
| 110 | + }) |
| 111 | + .reply(201, expected); |
| 112 | + |
| 113 | + const stakingRequest = await stakingWallet.stake({ |
| 114 | + amount: '1', |
| 115 | + clientId: 'clientId', |
| 116 | + walletPassphrase: 'passphrase', |
| 117 | + }); |
| 118 | + |
| 119 | + should.exist(stakingRequest); |
| 120 | + |
| 121 | + stakingRequest.should.deepEqual(expected); |
| 122 | + msScope1.isDone().should.be.True(); |
| 123 | + msScope2.isDone().should.be.True(); |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + describe('unstake', function () { |
| 128 | + it('should call go-staking-service to unstake', async function () { |
| 129 | + const expected = fixtures.finalizeGoStakingRequest(coin, 'UNSTAKE'); |
| 130 | + const msScope = nock(microservicesUri) |
| 131 | + .post(`/api/go-staking/v1/${ofcCoin}/accounts/${stakingWallet.accountId}/requests/finalize`, { |
| 132 | + amount: '1', |
| 133 | + clientId: 'clientId', |
| 134 | + type: 'UNSTAKE', |
| 135 | + }) |
| 136 | + .reply(201, expected); |
| 137 | + |
| 138 | + const stakingRequest = await stakingWallet.unstake({ |
| 139 | + amount: '1', |
| 140 | + clientId: 'clientId', |
| 141 | + }); |
| 142 | + |
| 143 | + should.exist(stakingRequest); |
| 144 | + |
| 145 | + stakingRequest.should.deepEqual(expected); |
| 146 | + msScope.isDone().should.be.True(); |
| 147 | + }); |
| 148 | + }); |
| 149 | + |
| 150 | + describe('getGoStakingRequest', function () { |
| 151 | + it('should call gostaking-service to get go staking request', async function () { |
| 152 | + const stakingRequestId = '8638284a-dab2-46b9-b07f-21109a6e7220'; |
| 153 | + const expected = fixtures.finalizeGoStakingRequest(coin, 'STAKE'); |
| 154 | + const msScope = nock(microservicesUri) |
| 155 | + .get(`/api/go-staking/v1/${ofcCoin}/accounts/${stakingWallet.accountId}/requests/${stakingRequestId}`) |
| 156 | + .reply(200, expected); |
| 157 | + |
| 158 | + const stakingRequest = await stakingWallet.getGoStakingRequest(stakingRequestId); |
| 159 | + |
| 160 | + should.exist(stakingRequest); |
| 161 | + |
| 162 | + stakingRequest.should.deepEqual(expected); |
| 163 | + msScope.isDone().should.be.True(); |
| 164 | + }); |
| 165 | + }); |
| 166 | +}); |
0 commit comments