Skip to content

Commit

Permalink
feat: snip20 token info query
Browse files Browse the repository at this point in the history
  • Loading branch information
AustinWoetzel committed Oct 31, 2023
1 parent 1708c34 commit 4a07b90
Show file tree
Hide file tree
Showing 9 changed files with 459 additions and 0 deletions.
148 changes: 148 additions & 0 deletions src/contracts/definitions/snip20.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import { test, expect } from 'vitest';
import { snip20 } from '~/contracts/definitions/snip20';

test('it checks the shape of the snip20 send', () => {
const params = {
recipient: 'MOCK_RECIPIENT',
amount: 'MOCK_AMOUNT',
handleMsg: { msg: 'MOCK_MESSAGE' },
padding: 'MOCK_PADDING',
};

const output = {
msg: {
send: {
recipient: params.recipient,
recipient_code_hash: undefined,
amount: params.amount,
msg: 'eyJtc2ciOiJNT0NLX01FU1NBR0UifQ==',
padding: params.padding,
},
},
};
expect(snip20.messages.send(params)).toStrictEqual(output);

const params2 = {
recipient: 'MOCK_RECIPIENT',
recipientCodeHash: 'MOCK_RECIPIENT_CODE_HASH',
amount: 'MOCK_AMOUNT',
handleMsg: { msg: 'MOCK_MESSAGE' },
padding: 'MOCK_PADDING',
};

const output2 = {
msg: {
send: {
recipient: params2.recipient,
recipient_code_hash: params2.recipientCodeHash,
amount: params2.amount,
msg: 'eyJtc2ciOiJNT0NLX01FU1NBR0UifQ==',
padding: params2.padding,
},
},
};

expect(snip20.messages.send(params2)).toStrictEqual(output2);
});

test('it checks the shape of the snip20 transfer', () => {
const params = {
recipient: 'MOCK_RECIPIENT',
amount: 'MOCK_AMOUNT',
padding: 'MOCK_PADDING',
};

const output = {
msg: {
transfer: {
recipient: params.recipient,
amount: params.amount,
padding: params.padding,
},
},
};

expect(snip20.messages.transfer(params)).toStrictEqual(output);
});

test('it checks the shape of the snip20 deposit', () => {
const inputAmount = 'MOCK_AMOUNT';
const inputDenom = 'MOCK_DENOM';

const msg = { deposit: { } };
const transferAmount = { amount: inputAmount, denom: inputDenom };

const output = {
msg,
transferAmount,
};
expect(snip20.messages.deposit(
inputAmount,
inputDenom,
)).toStrictEqual(output);
});

test('it checks the shape of the snip20 redeem', () => {
const inputAmount = 'MOCK_AMOUNT';
const inputDenom = 'MOCK_DENOM';
const mockPadding = 'MOCK_PADDING';

const output = {
msg: {
redeem: {
amount: inputAmount,
denom: inputDenom,
padding: mockPadding,
},
},
};
expect(snip20.messages.redeem({
amount: inputAmount,
denom: inputDenom,
padding: mockPadding,
})).toStrictEqual(output);
});

test('it checks the shape of the snip20 token info query', () => {
const output = {
token_info: {},
};
expect(snip20.queries.tokenInfo()).toStrictEqual(output);
});

test('it checks the shape of the snip20 increase allowance', () => {
const inputSpender = 'MOCK_SPENDER';
const inputAmount = 'MOCK_AMOUNT';
const inputExpiration = 10;
const mockPadding = 'MOCK_PADDING';

const output = {
msg: {
increase_allowance: {
spender: inputSpender,
amount: inputAmount,
expiration: inputExpiration,
padding: mockPadding,
},
},
};
expect(snip20.messages.increaseAllowance({
spender: inputSpender,
amount: inputAmount,
expiration: inputExpiration,
padding: mockPadding,
})).toStrictEqual(output);
});

test('it checks the shape of the snip20 viewing', () => {
const viewingKey = 'MOCK_VIEWING_KEY';

const output = {
msg: {
set_viewing_key: {
key: viewingKey,
},
},
};
expect(snip20.messages.createViewingKey(viewingKey)).toStrictEqual(output);
});
141 changes: 141 additions & 0 deletions src/contracts/definitions/snip20.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import { encodeJsonToB64 } from '~/lib/utils';
import { Snip20MessageRequest } from '~/types/contracts/snip20/model';

const snip20 = {
queries: {
getBalance(address: string, key:string) {
return {
balance: {
address,
key,
},
};
},
tokenInfo() {
return {
token_info: {},
};
},
},
messages: {
send({
recipient,
recipientCodeHash,
amount,
handleMsg,
padding,
}: {
recipient: string,
recipientCodeHash?: string,
amount: string,
handleMsg: any,
padding: string,
}): Snip20MessageRequest {
const msg = {
send: {
recipient,
recipient_code_hash: recipientCodeHash,
amount,
msg: handleMsg !== null ? encodeJsonToB64(handleMsg) : null,
padding,
},
};
return {
msg,
};
},

transfer({
recipient,
amount,
padding,
}: {
recipient: string,
amount: string,
padding: string,
}): Snip20MessageRequest {
const msg = {
transfer: {
recipient,
amount,
padding,
},
};
return {
msg,
};
},

deposit(
amount: string,
denom: string,
): Snip20MessageRequest {
const msg = { deposit: { } };
const transferAmount = { amount, denom };
return {
msg,
transferAmount,
};
},

redeem({
amount,
denom,
padding,
}:{
amount: string,
denom: string,
padding: string,
}): Snip20MessageRequest {
const msg = {
redeem: {
amount,
denom,
padding,
},
};
return {
msg,
};
},

increaseAllowance({
spender,
amount,
expiration,
padding,
}:{
spender: string,
amount: string,
expiration?: number,
padding: string,
}): Snip20MessageRequest {
const msg = {
increase_allowance: {
spender,
amount,
expiration,
padding,
},
};
return {
msg,
};
},

createViewingKey(viewingKey: string): Snip20MessageRequest {
const msg = {
set_viewing_key: {
key: viewingKey,
},
};
return {
msg,
};
},
},
};

export {
snip20,
};
66 changes: 66 additions & 0 deletions src/contracts/services/snip20.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import {
parseTokenInfo,
querySnip20TokenInfo$,
} from '~/contracts/services/snip20';
import {
test,
expect,
vi,
beforeAll,
} from 'vitest';
import tokenInfoResponse from '~/test/mocks/snip20/tokenInfoResponse.json';
import { tokenInfoParsed } from '~/test/mocks/snip20/tokenInfoParsed';
import { of } from 'rxjs';

const sendSecretClientContractQuery$ = vi.hoisted(() => vi.fn());

beforeAll(() => {
vi.mock('~/contracts/definitions/snip20', () => ({
snip20: {
queries: {
tokenInfo: vi.fn(() => 'TOKEN_INFO_MSG'),
},
},
}));

vi.mock('~/client/index', () => ({
getActiveQueryClient$: vi.fn(() => of({ client: 'CLIENT' })),
}));

vi.mock('~/client/services/clientServices', () => ({
sendSecretClientContractQuery$,
}));
});

test('it can parse the response snip20 token info query', () => {
expect(parseTokenInfo(
tokenInfoResponse,
)).toStrictEqual(tokenInfoParsed);
});

test('it can call the snip20 token info query', () => {
sendSecretClientContractQuery$.mockReturnValue(of(tokenInfoResponse));

const input = {
snip20ContractAddress: 'CONTRACT_ADDRESS',
snip20CodeHash: 'CODE_HASH',
lcdEndpoint: 'LCD_ENDPOINT',
chainId: 'CHAIN_ID',
};

let output;
querySnip20TokenInfo$(input).subscribe({
next: (response) => {
output = response;
},
});

expect(sendSecretClientContractQuery$).toHaveBeenCalledWith({
queryMsg: 'TOKEN_INFO_MSG',
client: 'CLIENT',
contractAddress: input.snip20ContractAddress,
codeHash: input.snip20CodeHash,
});

expect(output).toStrictEqual(tokenInfoParsed);
});
Loading

0 comments on commit 4a07b90

Please sign in to comment.