Skip to content

Commit

Permalink
fix: update address refund view
Browse files Browse the repository at this point in the history
  • Loading branch information
bucurdavid committed May 3, 2024
1 parent 1ada886 commit b75dc3b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 8 deletions.
22 changes: 21 additions & 1 deletion src/abis/core-mx-life-bonding-sc.abi.json
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,27 @@
],
"outputs": [
{
"type": "Option<tuple<Compensation,Option<Refund>>>"
"type": "Option<Refund>"
}
]
},
{
"name": "getAddressRefundForCompensations",
"mutability": "readonly",
"inputs": [
{
"name": "address",
"type": "Address"
},
{
"name": "compensation_ids",
"type": "variadic<u64>",
"multi_arg": true
}
],
"outputs": [
{
"type": "List<Refund>"
}
]
},
Expand Down
51 changes: 44 additions & 7 deletions src/bond.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
BondConfiguration,
Compensation,
PenaltyType,
Refund,
State
} from './interfaces';

Expand Down Expand Up @@ -285,16 +286,16 @@ export class BondContract extends Contract {
}

/**
* Returns an Optional `Compensation` and `Refund` object for the given address and compensation id
* Returns an `Refund` object for the given address
* @param address address to query
* @param tokenIdentifier token identifier to query
* @param nonce nonce to query
*/
async viewAddressRefundForCompensation(
async viewAddressRefund(
address: IAddress,
tokenIdentifier: string,
nonce: number
) {
): Promise<Refund> {
const interaction =
this.contract.methodsExplicit.getAddressRefundForCompensation([
new AddressValue(address),
Expand All @@ -308,17 +309,53 @@ export class BondContract extends Contract {
queryResponse,
endpointDefinition
);

if (returnCode.isSuccess()) {
const returnValue = firstValue?.valueOf();
const { field0: compensation, field1: refund } = returnValue;
const parsedCompensation = parseCompensation(compensation);
const parsedRefund = refund ? parseRefund(refund) : null;
return { compensation: parsedCompensation, refund: parsedRefund };
const parsedRefund = parseRefund(returnValue);
return parsedRefund;
} else {
throw new ErrContractQuery('viewAddressRefund', returnCode.toString());
}
}

/**
* Returns an `Refund` object array for the given address
* @param address address to query
* @param compensation_ids compensation ids to query
*
*/
viewAddressRefunds(
address: IAddress,
compensation_ids: number[]
): Promise<Refund[]> {
const compensation_ids_as_u64 = compensation_ids.map(
(id) => new U64Value(id)
);
const interaction =
this.contract.methodsExplicit.getAddressRefundForCompensations([
new AddressValue(address),
...compensation_ids_as_u64
]);
const query = interaction.buildQuery();
return this.networkProvider.queryContract(query).then((queryResponse) => {
const endpointDefinition = interaction.getEndpoint();
const { firstValue, returnCode } = new ResultsParser().parseQueryResponse(
queryResponse,
endpointDefinition
);
if (returnCode.isSuccess()) {
const returnValue = firstValue?.valueOf();
const refunds: Refund[] = returnValue.map((refund: any) =>
parseRefund(refund)
);
return refunds;
} else {
throw new ErrContractQuery('viewAddressRefunds', returnCode.toString());
}
});
}

/**
* Returns a `Compensation` object array for the given indexes
* @param start_index index to start
Expand Down

0 comments on commit b75dc3b

Please sign in to comment.