Skip to content

Commit

Permalink
fix token type (helium#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
allenan authored Jul 25, 2022
1 parent 6800dfa commit 6b8c626
Show file tree
Hide file tree
Showing 14 changed files with 142 additions and 73 deletions.
6 changes: 3 additions & 3 deletions integration_tests/tests/create_and_submit_payment.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ test('create and submit a PaymentV2 txn', async () => {
const signedPaymentTxn = await paymentTxn.sign({ payer: bob })
const serializedTxn = signedPaymentTxn.toString()
expect(serializedTxn).toBe(
'wgGOAQohATUaccIv7+wiMZNq0oJrIX7OOdn3f8bEljmSYpnDhpKVEiUKIQGcZZ1yPMHoEKcuePfer0c2qH8Q74/PyAEAtTMn5+5JpBAKIAEqQK88GjmG9CrESHVdcL//ZfWD+KsBnbKmZqKlx8oD89FUms7OjZNcL5NiQ4o0jREg+ahkjc2jX4SgKBBniM+QoAA=',
'wgGQAQohATUaccIv7+wiMZNq0oJrIX7OOdn3f8bEljmSYpnDhpKVEicKIQGcZZ1yPMHoEKcuePfer0c2qH8Q74/PyAEAtTMn5+5JpBAKKAAgASpAJCR6qHf+krZ4WU5bTyOgbh+tgc7bJZwVjNQigHNj47736g5jgxbIxCjpwwlvQCH5TrqELYJ75tFrOkU/h4qHCw==',
)

nock('https://api.helium.io')
Expand Down Expand Up @@ -94,7 +94,7 @@ test('using the bip39 checksum word should match serialization', async () => {
const serializedTxn = signedPaymentTxn.toString()

expect(serializedTxn).toBe(
'wgGOAQohATUaccIv7+wiMZNq0oJrIX7OOdn3f8bEljmSYpnDhpKVEiUKIQGcZZ1yPMHoEKcuePfer0c2qH8Q74/PyAEAtTMn5+5JpBAKIAEqQK88GjmG9CrESHVdcL//ZfWD+KsBnbKmZqKlx8oD89FUms7OjZNcL5NiQ4o0jREg+ahkjc2jX4SgKBBniM+QoAA=',
'wgGQAQohATUaccIv7+wiMZNq0oJrIX7OOdn3f8bEljmSYpnDhpKVEicKIQGcZZ1yPMHoEKcuePfer0c2qH8Q74/PyAEAtTMn5+5JpBAKKAAgASpAJCR6qHf+krZ4WU5bTyOgbh+tgc7bJZwVjNQigHNj47736g5jgxbIxCjpwwlvQCH5TrqELYJ75tFrOkU/h4qHCw==',
)
})

Expand Down Expand Up @@ -131,6 +131,6 @@ test('create and sign multisig payment', async () => {

const serializedTxn = paymentTxn.toString()
expect(serializedTxn).toBe(
'wgHXAQolAgECEiBqqzKbCO7og1KrG7VnpqrgT+wIowchqqdNAdWDQAa5HRIlCiEBnGWdcjzB6BCnLnj33q9HNqh/EO+Pz8gBALUzJ+fuSaQQCiABKoQBATUaccIv7+wiMZNq0oJrIX7OOdn3f8bEljmSYpnDhpKVAZxlnXI8wegQpy54996vRzaofxDvj8/IAQC1Myfn7kmkAEDfDD6a0GpxsreMPBmr+VACsNHtdEpBnCL1RUzTvqS6N7x9dmSEt8SZeqlTFTmzaLoC8zi4OCNf6zcf+Z347fsH',
'wgHZAQolAgECEiBqqzKbCO7og1KrG7VnpqrgT+wIowchqqdNAdWDQAa5HRInCiEBnGWdcjzB6BCnLnj33q9HNqh/EO+Pz8gBALUzJ+fuSaQQCigAIAEqhAEBNRpxwi/v7CIxk2rSgmshfs452fd/xsSWOZJimcOGkpUBnGWdcjzB6BCnLnj33q9HNqh/EO+Pz8gBALUzJ+fuSaQAQDZ3LrtMNKjBCAs1ti0LlRRWlmpMrILOa2Uc9DAmfVKHwqePE8ta6ca+dI3W3lUF3xUJagXNsWJAR4AwmtOERQw=',
)
})
1 change: 0 additions & 1 deletion packages/currency/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"build": "yarn run clean && tsc"
},
"dependencies": {
"@helium/transactions": "^4.6.2",
"bignumber.js": "^9.0.0"
},
"gitHead": "16442bef09f90dd9a83d4c9e1a347de3e80575e4"
Expand Down
13 changes: 6 additions & 7 deletions packages/currency/src/CurrencyType.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { TokenType } from '@helium/transactions'
import {
BaseCurrencyType,
DataCredits,
Expand All @@ -20,16 +19,16 @@ export type AnyCurrencyType =
| USDollars

export default class CurrencyType {
static fromTokenType(type: TokenType): BaseCurrencyType {
switch (type) {
static fromTicker(ticker?: string): BaseCurrencyType {
switch (ticker?.toUpperCase()) {
default:
case TokenType.hnt:
case this.default.ticker:
return this.default
case TokenType.hst:
case this.security.ticker:
return this.security
case TokenType.mobile:
case this.mobile.ticker:
return this.mobile
case TokenType.iot:
case this.iot.ticker:
return this.iot
}
}
Expand Down
16 changes: 10 additions & 6 deletions packages/currency/src/__tests__/Balance.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { TokenType } from '@helium/transactions'
import { Balance, CurrencyType } from '..'
import { UnsupportedCurrencyConversionError } from '../Errors'

Expand Down Expand Up @@ -256,10 +255,15 @@ describe('trying to convert a security token balance', () => {
})

describe('CurrencyType', () => {
it('fromTokenType', () => {
expect(CurrencyType.fromTokenType(TokenType.hnt).ticker).toBe('HNT')
expect(CurrencyType.fromTokenType(TokenType.hst).ticker).toBe('HST')
expect(CurrencyType.fromTokenType(TokenType.mobile).ticker).toBe('MOBILE')
expect(CurrencyType.fromTokenType(TokenType.iot).ticker).toBe('IOT')
it('fromTicker', () => {
expect(CurrencyType.fromTicker(undefined).ticker).toBe('HNT')
expect(CurrencyType.fromTicker('hnt').ticker).toBe('HNT')
expect(CurrencyType.fromTicker('HNT').ticker).toBe('HNT')
expect(CurrencyType.fromTicker('hst').ticker).toBe('HST')
expect(CurrencyType.fromTicker('HST').ticker).toBe('HST')
expect(CurrencyType.fromTicker('mobile').ticker).toBe('MOBILE')
expect(CurrencyType.fromTicker('MOBILE').ticker).toBe('MOBILE')
expect(CurrencyType.fromTicker('iot').ticker).toBe('IOT')
expect(CurrencyType.fromTicker('IOT').ticker).toBe('IOT')
})
})
1 change: 0 additions & 1 deletion packages/http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"dependencies": {
"@helium/address": "^4.6.2",
"@helium/currency": "^4.6.2",
"@helium/transactions": "^4.6.2",
"axios": "^0.21.1",
"camelcase-keys": "^6.2.2",
"qs": "^6.9.3",
Expand Down
17 changes: 10 additions & 7 deletions packages/http/src/models/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
IotTokens,
MobileTokens,
} from '@helium/currency'
import { TokenType } from '@helium/transactions'
import Challenge, { HTTPChallengeObject } from './Challenge'
import DataModel from './DataModel'

Expand All @@ -22,7 +21,7 @@ export interface TxnJsonObject {
rewards?: any[]
fee?: number
amount_to_seller?: number
token_type?: number
token_type?: string
}

export class AddGatewayV1 extends DataModel {
Expand Down Expand Up @@ -495,7 +494,11 @@ function prepareTxn(txn: any, { deep } = { deep: false }) {

balanceConversions.forEach(({ key, currencyType }) => {
if (txn[key] && typeof txn[key] === 'number') {
txn[key] = new Balance(txn[key], currencyType)
if (txn.token_type !== undefined) {
txn[key] = new Balance(txn[key], CurrencyType.fromTicker(txn.token_type))
} else {
txn[key] = new Balance(txn[key], currencyType)
}
}
})

Expand Down Expand Up @@ -574,7 +577,7 @@ export class SubnetworkRewardsV1 extends DataModel {

time!: number

tokenType!: TokenType
tokenType!: string

startEpoch!: number

Expand All @@ -601,13 +604,13 @@ export class TokenRedeemV1 extends DataModel {

account!: string

amount!: Balance<NetworkTokens>
amount!: Balance<MobileTokens | IotTokens>

fee!: number

nonce!: number

tokenType!: TokenType
tokenType!: string

signature!: string

Expand Down Expand Up @@ -764,7 +767,7 @@ export default class Transaction {
}

static toSubnetworkRewardsV1(json: TxnJsonObject): SubnetworkRewardsV1 {
const currencyType = CurrencyType.fromTokenType(json.token_type as TokenType)
const currencyType = CurrencyType.fromTicker(json.token_type)
const rewards = (json.rewards || []).map((r) => ({
...r,
amount: new Balance(r.amount, currencyType),
Expand Down
22 changes: 11 additions & 11 deletions packages/http/src/models/__tests__/Transaction.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { TokenType } from '@helium/transactions'
import Transaction, {
PaymentV2,
PocReceiptsV1,
Expand Down Expand Up @@ -118,6 +117,7 @@ describe('PaymentV2', () => {
expect(txn.data.hash).toBe(txn.hash)
expect(txn.payments[0].payee).toBe('13DKymsEaCSpNTithKUbyn7zDEYV3xfoAsA2iFM6bsw8YtPaoCZ')
expect(txn.payments[0].amount.integerBalance).toBe(75)
expect(txn.payments[0].amount.type.ticker).toBe('HNT')
expect(txn.payments[0].memo).toBe('memo')
})
})
Expand Down Expand Up @@ -291,7 +291,7 @@ describe('SubnetworkRewardsV1', () => {
it('correctly converts mobile token json', () => {
const json = {
type: 'subnetwork_rewards_v1',
token_type: TokenType.mobile,
token_type: 'mobile',
time: 1587424041,
start_epoch: 300165,
rewards: [
Expand All @@ -309,7 +309,7 @@ describe('SubnetworkRewardsV1', () => {
end_epoch: 300195,
}
const txn = Transaction.fromJsonObject(json) as SubnetworkRewardsV1
expect(txn.tokenType).toBe(TokenType.mobile)
expect(txn.tokenType).toBe('mobile')
expect(txn.rewards.length).toBe(2)
expect(txn.rewards[0].amount.floatBalance).toBe(0.00002)
expect(txn.rewards[0].amount.type.ticker).toBe('MOBILE')
Expand All @@ -318,7 +318,7 @@ describe('SubnetworkRewardsV1', () => {
it('correctly converts iot token json', () => {
const json = {
type: 'subnetwork_rewards_v1',
token_type: TokenType.iot,
token_type: 'iot',
time: 1587424041,
start_epoch: 300165,
rewards: [
Expand All @@ -336,7 +336,7 @@ describe('SubnetworkRewardsV1', () => {
end_epoch: 300195,
}
const txn = Transaction.fromJsonObject(json) as SubnetworkRewardsV1
expect(txn.tokenType).toBe(TokenType.iot)
expect(txn.tokenType).toBe('iot')
expect(txn.rewards.length).toBe(2)
expect(txn.rewards[0].amount.floatBalance).toBe(0.00002)
expect(txn.rewards[0].amount.type.ticker).toBe('IOT')
Expand All @@ -349,30 +349,30 @@ describe('TokenRedeemV1', () => {
type: 'token_redeem_v1',
account: 'fake-owner-address',
amount: 2000,
token_type: TokenType.mobile,
token_type: 'mobile',
nonce: 1,
time: 1587424041,
height: 123456,
hash: 'fake-txn-hash',
}
const txn = Transaction.fromJsonObject(json) as TokenRedeemV1
expect(txn.amount.type.ticker).toBe('HNT')
expect(txn.tokenType).toBe(TokenType.mobile)
expect(txn.amount.type.ticker).toBe('MOBILE')
expect(txn.tokenType).toBe('mobile')
})

it('correctly converts hnt to iot txn json', () => {
const json = {
type: 'token_redeem_v1',
account: 'fake-owner-address',
amount: 2000,
token_type: TokenType.iot,
token_type: 'iot',
nonce: 1,
time: 1587424041,
height: 123456,
hash: 'fake-txn-hash',
}
const txn = Transaction.fromJsonObject(json) as TokenRedeemV1
expect(txn.amount.type.ticker).toBe('HNT')
expect(txn.tokenType).toBe(TokenType.iot)
expect(txn.amount.type.ticker).toBe('IOT')
expect(txn.tokenType).toBe('iot')
})
})
29 changes: 14 additions & 15 deletions packages/transactions/src/PaymentV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ import proto from '@helium/proto'
import * as JSLong from 'long'
import Transaction from './Transaction'
import {
EMPTY_SIGNATURE, toAddressable, toNumber, toString, toUint8Array,
EMPTY_SIGNATURE,
toAddressable,
toNumber,
toString,
toTicker,
toTokenType,
toUint8Array,
} from './utils'
import {
Addressable, Base64Memo, SignableKeypair, TokenType,
} from './types'
import { Addressable, Base64Memo, SignableKeypair } from './types'

interface PaymentOptions {
payer?: Addressable
Expand All @@ -20,7 +24,7 @@ interface Payment {
payee: Addressable
amount: number
memo?: Base64Memo
tokenType?: TokenType
tokenType?: string
}

interface SignOptions {
Expand Down Expand Up @@ -69,7 +73,7 @@ export default class PaymentV2 extends Transaction {
payee: toAddressable(p!.payee) as Addressable,
amount: toNumber(p!.amount) as number,
memo: toString(p!.memo),
tokenType: p!.tokenType === undefined ? TokenType.hnt : toNumber(p!.tokenType) as number,
tokenType: toTicker(toNumber(p!.tokenType)),
}))
const fee = toNumber(decoded.paymentV2?.fee)
const nonce = toNumber(decoded.paymentV2?.nonce)
Expand All @@ -92,20 +96,16 @@ export default class PaymentV2 extends Transaction {
return this
}

private toProto(
forSigning: boolean = false,
): proto.helium.blockchain_txn_payment_v2 {
private toProto(forSigning: boolean = false): proto.helium.blockchain_txn_payment_v2 {
const PaymentTxn = proto.helium.blockchain_txn_payment_v2
const Payment = proto.helium.payment
const payments = this.payments.map(({
payee, amount, memo, tokenType,
}) => {
const payments = this.payments.map(({ payee, amount, memo, tokenType }) => {
const memoBuffer = memo ? Buffer.from(memo, 'base64') : undefined
return Payment.create({
payee: toUint8Array(payee.bin),
amount,
memo: memoBuffer ? JSLong.fromBytes(Array.from(memoBuffer), true, true) : undefined,
tokenType: tokenType as number,
tokenType: toTokenType(tokenType),
})
})

Expand All @@ -114,8 +114,7 @@ export default class PaymentV2 extends Transaction {
payments,
fee: this.fee && this.fee > 0 ? this.fee : null,
nonce: this.nonce,
signature:
this.signature && !forSigning ? toUint8Array(this.signature) : null,
signature: this.signature && !forSigning ? toUint8Array(this.signature) : null,
})
}

Expand Down
17 changes: 11 additions & 6 deletions packages/transactions/src/SubnetworkRewardsV1.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import proto from '@helium/proto'
import Transaction from './Transaction'
import {
EMPTY_SIGNATURE, toAddressable, toNumber, toUint8Array,
EMPTY_SIGNATURE,
toAddressable,
toNumber,
toTicker,
toTokenType,
toUint8Array,
} from './utils'
import { Addressable, SignableKeypair, TokenType } from './types'
import { Addressable, SignableKeypair } from './types'

interface Options {
tokenType: number
tokenType: string
startEpoch: number
endEpoch: number
rewards: Array<SubnetworkReward>
Expand All @@ -23,7 +28,7 @@ export interface SubnetworkReward {
}

export default class SubnetworkRewardsV1 extends Transaction {
public tokenType?: number
public tokenType?: string

public startEpoch?: number

Expand Down Expand Up @@ -66,7 +71,7 @@ export default class SubnetworkRewardsV1 extends Transaction {
static fromString(serializedTxnString: string) {
const buf = Buffer.from(serializedTxnString, 'base64')
const decoded = proto.helium.blockchain_txn.decode(buf)
const tokenType = toNumber(decoded.subnetworkRewards?.tokenType) || TokenType.hnt
const tokenType = toTicker(toNumber(decoded.subnetworkRewards?.tokenType))
const startEpoch = toNumber(decoded.subnetworkRewards?.startEpoch) || 0
const endEpoch = toNumber(decoded.subnetworkRewards?.endEpoch) || 0
const rewards = (decoded.subnetworkRewards?.rewards || []).map((p) => ({
Expand Down Expand Up @@ -94,7 +99,7 @@ export default class SubnetworkRewardsV1 extends Transaction {
}))

return SubnetworkRewards.create({
tokenType: this.tokenType,
tokenType: toTokenType(this.tokenType),
startEpoch: this.startEpoch,
endEpoch: this.endEpoch,
rewards,
Expand Down
Loading

0 comments on commit 6b8c626

Please sign in to comment.