From e5803a21a078ed59fe730839dd167e4247ac0421 Mon Sep 17 00:00:00 2001 From: dtfiedler Date: Thu, 11 Apr 2024 08:24:38 -0600 Subject: [PATCH] fix(transfer): set denomination in transfer, by default to mIO and allow IO --- schemas/transfer.js | 5 ++++ src/actions/write/transferToken.test.ts | 32 +++++++++++++++++++++++-- src/actions/write/transferTokens.ts | 5 ++-- tests/transfer.test.ts | 3 +++ tools/transfer-tokens.ts | 1 + 5 files changed, 42 insertions(+), 4 deletions(-) diff --git a/schemas/transfer.js b/schemas/transfer.js index 4c9b5eaf..24be84e7 100644 --- a/schemas/transfer.js +++ b/schemas/transfer.js @@ -14,6 +14,11 @@ const transferTokensSchema = { type: 'number', minimum: 1, }, + denomination: { + type: 'string', + enum: ['IO', 'mIO'], + default: 'mIO', + }, }, required: ['target', 'qty'], additionalProperties: false, diff --git a/src/actions/write/transferToken.test.ts b/src/actions/write/transferToken.test.ts index b907f956..35e71028 100644 --- a/src/actions/write/transferToken.test.ts +++ b/src/actions/write/transferToken.test.ts @@ -3,7 +3,7 @@ import { INVALID_INPUT_MESSAGE, } from '../../constants'; import { getBaselineState, stubbedArweaveTxId } from '../../tests/stubs'; -import { IOToken } from '../../types'; +import { IOToken, mIOToken } from '../../types'; import { transferTokens } from './transferTokens'; describe('transferTokens', () => { @@ -64,7 +64,7 @@ describe('transferTokens', () => { ); }); - it('should transfer balances if the user has sufficient balance', async () => { + it('should transfer balances if the user has sufficient balance using IO denomination', async () => { const initialState = { ...getBaselineState(), balances: { @@ -76,6 +76,7 @@ describe('transferTokens', () => { input: { qty: new IOToken(100).valueOf(), target: stubbedArweaveTxId, + denomination: 'IO', }, }); expect(state).toEqual({ @@ -86,5 +87,32 @@ describe('transferTokens', () => { }, }); }); + + it.each([undefined, { denomination: 'mIO' }])( + 'should transfer balances if the user has sufficient balance using mIO denomination', + async (denomination: { denomination: string } | undefined) => { + const initialState = { + ...getBaselineState(), + balances: { + test: new mIOToken(10_000).valueOf(), + }, + }; + const { state } = await transferTokens(initialState, { + caller: 'test', + input: { + qty: new mIOToken(100).valueOf(), + target: stubbedArweaveTxId, + ...denomination, + }, + }); + expect(state).toEqual({ + ...initialState, + balances: { + test: new mIOToken(9900).valueOf(), + [stubbedArweaveTxId]: new mIOToken(100).valueOf(), + }, + }); + }, + ); }); }); diff --git a/src/actions/write/transferTokens.ts b/src/actions/write/transferTokens.ts index 45366122..1f8e0a1e 100644 --- a/src/actions/write/transferTokens.ts +++ b/src/actions/write/transferTokens.ts @@ -20,9 +20,10 @@ export class TransferToken { getInvalidAjvMessage(validateTransferToken, input, 'transferToken'), ); } - const { target, qty } = input; + const { target, qty, denomination = 'mIO' } = input; this.target = target; - this.qty = new IOToken(qty).toMIO(); + this.qty = + denomination === 'mIO' ? new mIOToken(qty) : new IOToken(qty).toMIO(); } } diff --git a/tests/transfer.test.ts b/tests/transfer.test.ts index cda7d690..2410a501 100644 --- a/tests/transfer.test.ts +++ b/tests/transfer.test.ts @@ -43,6 +43,7 @@ describe('Transfers', () => { function: 'transfer', target: targetAddress, qty: TRANSFER_QTY.valueOf(), + denomination: 'IO', }); expect(writeInteraction?.originalTxId).not.toBe(undefined); @@ -69,6 +70,7 @@ describe('Transfers', () => { function: 'transfer', target: srcContractId, // The smartweave contract id acts as the protocol balance qty: TRANSFER_QTY.valueOf(), + denomination: 'IO', }); expect(writeInteraction?.originalTxId).not.toBe(undefined); @@ -171,6 +173,7 @@ describe('Transfers', () => { function: 'transfer', target: targetAddress, qty: TRANSFER_QTY.valueOf(), + denomination: 'IO', }); expect(writeInteraction?.originalTxId).not.toBe(undefined); diff --git a/tools/transfer-tokens.ts b/tools/transfer-tokens.ts index c55bbc7f..a97afa60 100644 --- a/tools/transfer-tokens.ts +++ b/tools/transfer-tokens.ts @@ -47,6 +47,7 @@ import { function: 'transfer', target, qty, + denomination: 'IO', // by default transfer in IO }, { disableBundling: true,