Skip to content

Commit

Permalink
Upgrade to an extended version of the Agent feature
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKim20 committed Oct 30, 2024
1 parent 6caa28e commit 87d9dc9
Show file tree
Hide file tree
Showing 26 changed files with 535 additions and 101 deletions.
File renamed without changes.
15 changes: 10 additions & 5 deletions packages/client-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,16 @@
"12-to_be_receive:user_total": "npx ts-node scripts/12-to_be_receve/user_total.ts",
"12-to_be_receive:shop": "npx ts-node scripts/12-to_be_receve/shop.ts",
"12-to_be_receive:shop_total": "npx ts-node scripts/12-to_be_receve/shop_total.ts",
"13-provider:is_provider": "npx ts-node scripts/13-provider/is_provider.ts",
"13-provider:register_provider": "npx ts-node scripts/13-provider/register_provider.ts",
"13-provider:register_assistant": "npx ts-node scripts/13-provider/register_assistant.ts",
"13-provider:unregister_assistant": "npx ts-node scripts/13-provider/unregister_assistant.ts",
"13-provider:register_provider_forced": "npx ts-node scripts/13-provider/register_provider_forced.ts"
"13-provision:is_provider": "npx ts-node scripts/13-provision/is_provider.ts",
"13-provision:register_provider": "npx ts-node scripts/13-provision/register_provider.ts",
"13-provision:register_provider_forced": "npx ts-node scripts/13-provision/register_provider_forced.ts",
"13-provision:unregister_provider_forced": "npx ts-node scripts/13-provision/unregister_provider_forced.ts",
"14-agent:register_agent_provision": "npx ts-node scripts/14-agent/register_agent_provision.ts",
"14-agent:unregister_agent_provision": "npx ts-node scripts/14-agent/unregister_agent_provision.ts",
"14-agent:register_agent_refund": "npx ts-node scripts/14-agent/register_agent_refund.ts",
"14-agent:unregister_agent_refund": "npx ts-node scripts/14-agent/unregister_agent_refund.ts",
"14-agent:register_agent_withdrawal": "npx ts-node scripts/14-agent/register_agent_withdrawal.ts",
"14-agent:unregister_agent_withdrawal": "npx ts-node scripts/14-agent/unregister_agent_withdrawal.ts"
},
"directories": {
"test": "test"
Expand Down
8 changes: 8 additions & 0 deletions packages/client-test/scripts/02-summary/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ async function main() {
console.log(` - symbol: ${summary.exchangeRate.currency.symbol}`);
console.log(` - value: ${new BOACoin(summary.exchangeRate.currency.value).toDisplayString(true, 4)}`);

console.log(`- provision`);
console.log(` - enable: ${summary.provision.enable}`);

console.log(`- agent`);
console.log(` - provision: ${summary.agent.provision}`);
console.log(` - refund: ${summary.agent.refund}`);
console.log(` - withdrawal: ${summary.agent.withdrawal}`);

console.log(`- ledger`);
console.log(` - point.balance: ${new BOACoin(summary.ledger.point.balance).toDisplayString(true, 4)}`);
console.log(` - point.value: ${new BOACoin(summary.ledger.point.value).toDisplayString(true, 4)}`);
Expand Down
9 changes: 9 additions & 0 deletions packages/client-test/scripts/02-summary/shop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ async function main() {
console.log(` - delegator: ${summary.shopInfo.delegator}`);
console.log(` - providedAmount: ${new BOACoin(summary.shopInfo.providedAmount).toDisplayString(true, 4)}`);
console.log(` - usedAmount: ${new BOACoin(summary.shopInfo.usedAmount).toDisplayString(true, 4)}`);
console.log(` - collectedAmount: ${new BOACoin(summary.shopInfo.collectedAmount).toDisplayString(true, 4)}`);
console.log(` - refundedAmount: ${new BOACoin(summary.shopInfo.refundedAmount).toDisplayString(true, 4)}`);
console.log(` - refundableAmount: ${new BOACoin(summary.shopInfo.refundableAmount).toDisplayString(true, 4)}`);
console.log(` - refundableToken: ${new BOACoin(summary.shopInfo.refundableToken).toDisplayString(true, 4)}`);
Expand All @@ -36,6 +37,14 @@ async function main() {
console.log(` - symbol: ${summary.exchangeRate.currency.symbol}`);
console.log(` - value: ${new BOACoin(summary.exchangeRate.currency.value).toDisplayString(true, 4)}`);

console.log(`- settlement`);
console.log(` - manager: ${summary.settlement.manager}`);

console.log(`- agent`);
console.log(` - provision: ${summary.agent.provision}`);
console.log(` - refund: ${summary.agent.refund}`);
console.log(` - withdrawal: ${summary.agent.withdrawal}`);

console.log(`- ledger`);
console.log(` - point.balance: ${new BOACoin(summary.ledger.point.balance).toDisplayString(true, 4)}`);
console.log(` - point.value: ${new BOACoin(summary.ledger.point.value).toDisplayString(true, 4)}`);
Expand Down
4 changes: 4 additions & 0 deletions packages/client-test/scripts/03-shop/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ async function main() {
console.log(`delegator: ${info.delegator}`);
console.log(`providedAmount: ${new BOACoin(info.providedAmount).toDisplayString(true, 2)}`);
console.log(`usedAmount: ${new BOACoin(info.usedAmount).toDisplayString(true, 2)}`);
console.log(`collectedAmount: ${new BOACoin(info.collectedAmount).toDisplayString(true, 2)}`);
console.log(`refundedAmount: ${new BOACoin(info.refundedAmount).toDisplayString(true, 2)}`);

const refundable = await client.shop.getRefundableAmount(shopInfo.shopId);
console.log(`refundableAmount: ${new BOACoin(refundable.refundableAmount).toDisplayString(true, 2)}`);
}

main().catch((error) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { Helper } from "../utils";
import { Client, Context, ContextBuilder } from "acc-sdk-client-v2";
import { BOACoin } from "../../src/Amount";
import { Ledger, Ledger__factory } from "acc-contracts-lib-v2";
import { Wallet } from "ethers";

async function main() {
const userInfo = Helper.loadUserInfo();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Helper } from "../utils";
import { Client, Context, ContextBuilder } from "acc-sdk-client-v2";
import { BOACoin } from "../../src/Amount";
import { Ledger, Ledger__factory } from "acc-contracts-lib-v2";
import { Wallet } from "ethers";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Helper } from "../utils";
import { Client, Context, ContextBuilder } from "acc-sdk-client-v2";
import { Ledger, Ledger__factory } from "acc-contracts-lib-v2";
import { Wallet } from "ethers";

async function main() {
const userInfo = Helper.loadUserInfo();
const contextParams = ContextBuilder.buildContextParams(Helper.NETWORK, userInfo.wallet.privateKey);
if (Helper.RELAY_ENDPOINT !== "") contextParams.relayEndpoint = Helper.RELAY_ENDPOINT;
if (Helper.WEB3_ENDPOINT !== "") contextParams.web3Provider = Helper.WEB3_ENDPOINT;
const context: Context = new Context(contextParams);
const client = new Client(context);

const contractOwner = new Wallet(process.env.CONTRACT_OWNER || "", client.web3.getProvider());
const ledgerContract: Ledger = Ledger__factory.connect(client.web3.getLedgerAddress(), contractOwner);
await ledgerContract.unregisterProvider(userInfo.wallet.address);
}

// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Helper } from "../utils";
import { Client, Context, ContextBuilder, NormalSteps } from "acc-sdk-client-v2";
import { BOACoin } from "../../src/Amount";

async function main() {
const userInfo = Helper.loadUserInfo();
Expand All @@ -9,10 +8,10 @@ async function main() {
if (Helper.WEB3_ENDPOINT !== "") contextParams.web3Provider = Helper.WEB3_ENDPOINT;
const context: Context = new Context(contextParams);
const client = new Client(context);
console.log(`client.ledger.getAssistant() : ${await client.ledger.getAssistant()}`);
const assistantInfo = Helper.loadAssistantInfo();
console.log(`assistantInfo.wallet.address : ${assistantInfo.wallet.address}`);
for await (const step of client.ledger.registerAssistant(assistantInfo.wallet.address)) {
console.log(`client.ledger.getAgentOfProvision() : ${await client.ledger.getAgentOfProvision()}`);
const agentInfo = Helper.loadAssistantInfo();
console.log(`agentInfo.wallet.address : ${agentInfo.wallet.address}`);
for await (const step of client.ledger.registerAgentOfProvision(agentInfo.wallet.address)) {
switch (step.key) {
case NormalSteps.PREPARED:
console.log("NormalSteps.PREPARED");
Expand All @@ -27,7 +26,7 @@ async function main() {
throw new Error("Unexpected change payable point step: " + JSON.stringify(step, null, 2));
}
}
console.log(`client.ledger.getAssistant() : ${await client.ledger.getAssistant()}`);
console.log(`client.ledger.getAgentOfProvision() : ${await client.ledger.getAgentOfProvision()}`);
}

// We recommend this pattern to be able to use async/await everywhere
Expand Down
37 changes: 37 additions & 0 deletions packages/client-test/scripts/14-agent/register_agent_refund.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Helper } from "../utils";
import { Client, Context, ContextBuilder, NormalSteps } from "acc-sdk-client-v2";

async function main() {
const userInfo = Helper.loadUserInfo();
const contextParams = ContextBuilder.buildContextParams(Helper.NETWORK, userInfo.wallet.privateKey);
if (Helper.RELAY_ENDPOINT !== "") contextParams.relayEndpoint = Helper.RELAY_ENDPOINT;
if (Helper.WEB3_ENDPOINT !== "") contextParams.web3Provider = Helper.WEB3_ENDPOINT;
const context: Context = new Context(contextParams);
const client = new Client(context);
console.log(`client.ledger.getAgentOfRefund() : ${await client.ledger.getAgentOfRefund()}`);
const agentInfo = Helper.loadAssistantInfo();
console.log(`agentInfo.wallet.address : ${agentInfo.wallet.address}`);
for await (const step of client.ledger.registerAgentOfRefund(agentInfo.wallet.address)) {
switch (step.key) {
case NormalSteps.PREPARED:
console.log("NormalSteps.PREPARED");
break;
case NormalSteps.SENT:
console.log("NormalSteps.SENT");
break;
case NormalSteps.DONE:
console.log("NormalSteps.DONE");
break;
default:
throw new Error("Unexpected change payable point step: " + JSON.stringify(step, null, 2));
}
}
console.log(`client.ledger.getAgentOfRefund() : ${await client.ledger.getAgentOfRefund()}`);
}

// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
37 changes: 37 additions & 0 deletions packages/client-test/scripts/14-agent/register_agent_withdrawal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Helper } from "../utils";
import { Client, Context, ContextBuilder, NormalSteps } from "acc-sdk-client-v2";

async function main() {
const userInfo = Helper.loadUserInfo();
const contextParams = ContextBuilder.buildContextParams(Helper.NETWORK, userInfo.wallet.privateKey);
if (Helper.RELAY_ENDPOINT !== "") contextParams.relayEndpoint = Helper.RELAY_ENDPOINT;
if (Helper.WEB3_ENDPOINT !== "") contextParams.web3Provider = Helper.WEB3_ENDPOINT;
const context: Context = new Context(contextParams);
const client = new Client(context);
console.log(`client.ledger.getAgentOfWithdrawal() : ${await client.ledger.getAgentOfWithdrawal()}`);
const agentInfo = Helper.loadAssistantInfo();
console.log(`agentInfo.wallet.address : ${agentInfo.wallet.address}`);
for await (const step of client.ledger.registerAgentOfWithdrawal(agentInfo.wallet.address)) {
switch (step.key) {
case NormalSteps.PREPARED:
console.log("NormalSteps.PREPARED");
break;
case NormalSteps.SENT:
console.log("NormalSteps.SENT");
break;
case NormalSteps.DONE:
console.log("NormalSteps.DONE");
break;
default:
throw new Error("Unexpected change payable point step: " + JSON.stringify(step, null, 2));
}
}
console.log(`client.ledger.getAgentOfWithdrawal() : ${await client.ledger.getAgentOfWithdrawal()}`);
}

// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Helper } from "../utils";
import { Client, Context, ContextBuilder, NormalSteps } from "acc-sdk-client-v2";
import { BOACoin } from "../../src/Amount";

async function main() {
const userInfo = Helper.loadUserInfo();
Expand All @@ -9,8 +8,8 @@ async function main() {
if (Helper.WEB3_ENDPOINT !== "") contextParams.web3Provider = Helper.WEB3_ENDPOINT;
const context: Context = new Context(contextParams);
const client = new Client(context);
console.log(`client.ledger.getAssistant() : ${await client.ledger.getAssistant()}`);
for await (const step of client.ledger.unregisterAssistant()) {
console.log(`client.ledger.getAgentOfProvision() : ${await client.ledger.getAgentOfProvision()}`);
for await (const step of client.ledger.unregisterAgentOfProvision()) {
switch (step.key) {
case NormalSteps.PREPARED:
console.log("NormalSteps.PREPARED");
Expand All @@ -25,7 +24,7 @@ async function main() {
throw new Error("Unexpected change payable point step: " + JSON.stringify(step, null, 2));
}
}
console.log(`client.ledger.getAssistant() : ${await client.ledger.getAssistant()}`);
console.log(`client.ledger.getAgentOfProvision() : ${await client.ledger.getAgentOfProvision()}`);
}

// We recommend this pattern to be able to use async/await everywhere
Expand Down
35 changes: 35 additions & 0 deletions packages/client-test/scripts/14-agent/unregister_agent_refund.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Helper } from "../utils";
import { Client, Context, ContextBuilder, NormalSteps } from "acc-sdk-client-v2";

async function main() {
const userInfo = Helper.loadUserInfo();
const contextParams = ContextBuilder.buildContextParams(Helper.NETWORK, userInfo.wallet.privateKey);
if (Helper.RELAY_ENDPOINT !== "") contextParams.relayEndpoint = Helper.RELAY_ENDPOINT;
if (Helper.WEB3_ENDPOINT !== "") contextParams.web3Provider = Helper.WEB3_ENDPOINT;
const context: Context = new Context(contextParams);
const client = new Client(context);
console.log(`client.ledger.getAgentOfRefund() : ${await client.ledger.getAgentOfRefund()}`);
for await (const step of client.ledger.unregisterAgentOfRefund()) {
switch (step.key) {
case NormalSteps.PREPARED:
console.log("NormalSteps.PREPARED");
break;
case NormalSteps.SENT:
console.log("NormalSteps.SENT");
break;
case NormalSteps.DONE:
console.log("NormalSteps.DONE");
break;
default:
throw new Error("Unexpected change payable point step: " + JSON.stringify(step, null, 2));
}
}
console.log(`client.ledger.getAgentOfRefund() : ${await client.ledger.getAgentOfRefund()}`);
}

// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Helper } from "../utils";
import { Client, Context, ContextBuilder, NormalSteps } from "acc-sdk-client-v2";

async function main() {
const userInfo = Helper.loadUserInfo();
const contextParams = ContextBuilder.buildContextParams(Helper.NETWORK, userInfo.wallet.privateKey);
if (Helper.RELAY_ENDPOINT !== "") contextParams.relayEndpoint = Helper.RELAY_ENDPOINT;
if (Helper.WEB3_ENDPOINT !== "") contextParams.web3Provider = Helper.WEB3_ENDPOINT;
const context: Context = new Context(contextParams);
const client = new Client(context);
console.log(`client.ledger.getAgentOfWithdrawal() : ${await client.ledger.getAgentOfWithdrawal()}`);
for await (const step of client.ledger.unregisterAgentOfWithdrawal()) {
switch (step.key) {
case NormalSteps.PREPARED:
console.log("NormalSteps.PREPARED");
break;
case NormalSteps.SENT:
console.log("NormalSteps.SENT");
break;
case NormalSteps.DONE:
console.log("NormalSteps.DONE");
break;
default:
throw new Error("Unexpected change payable point step: " + JSON.stringify(step, null, 2));
}
}
console.log(`client.ledger.getAgentOfWithdrawal() : ${await client.ledger.getAgentOfWithdrawal()}`);
}

// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
2 changes: 1 addition & 1 deletion packages/client-test/scripts/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class Helper {
const data: {
phone: string;
privateKey: string;
} = JSON.parse(fs.readFileSync("./data/assistant_info.json", "utf8"));
} = JSON.parse(fs.readFileSync("./data/agent_info.json", "utf8"));
return {
phone: data.phone,
wallet: new Wallet(data.privateKey),
Expand Down
4 changes: 2 additions & 2 deletions packages/client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "acc-sdk-client-v2",
"version": "2.11.0",
"version": "2.12.0",
"author": "BOSagora Foundation",
"license": "AGPL-3.0-or-later",
"main": "dist/index.js",
Expand Down Expand Up @@ -81,7 +81,7 @@
"@ethersproject/providers": "^5.7.0",
"@ethersproject/random": "^5.7.0",
"@ethersproject/wallet": "^5.7.0",
"acc-contracts-lib-v2": "~2.7.0",
"acc-contracts-lib-v2": "~2.10.0",
"acc-sdk-common-v2": "~2.0.0",
"google-libphonenumber": "^3.2.35",
"unfetch": "~4.2.0"
Expand Down
25 changes: 19 additions & 6 deletions packages/client/src/interface/ILedger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import {
WithdrawViaBridgeStepValue,
LedgerAction,
IAccountSummary,
RegisterAssistantStepValue, ISystemInfo
RegisterAgentStepValue,
ISystemInfo
} from "../interfaces";
import { BigNumber } from "@ethersproject/bignumber";
import { BytesLike } from "@ethersproject/bytes";
Expand Down Expand Up @@ -114,13 +115,25 @@ export interface ILedgerMethods extends IClientCore {
// History of Side Chain
getTransferHistoryInSideChain: (account: string, pageNumber?: number, pageSize?: number) => Promise<any>;

// Provider
// Provision
isProvider: () => Promise<boolean>;
registerProvider: () => Promise<void>;
registerAssistant: (assistant: string) => AsyncGenerator<RegisterAssistantStepValue>;
unregisterAssistant: () => AsyncGenerator<RegisterAssistantStepValue>;
getAssistant: () => Promise<string>;

// System
getSystemInfo:() => Promise<ISystemInfo>;
getSystemInfo: () => Promise<ISystemInfo>;

// Agent - Provision
getAgentOfProvision: () => Promise<string>;
registerAgentOfProvision: (agent: string) => AsyncGenerator<RegisterAgentStepValue>;
unregisterAgentOfProvision: () => AsyncGenerator<RegisterAgentStepValue>;

// Agent - Refund
getAgentOfRefund: () => Promise<string>;
registerAgentOfRefund: (agent: string) => AsyncGenerator<RegisterAgentStepValue>;
unregisterAgentOfRefund: () => AsyncGenerator<RegisterAgentStepValue>;

// Agent - Withdrawal
getAgentOfWithdrawal: () => Promise<string>;
registerAgentOfWithdrawal: (agent: string) => AsyncGenerator<RegisterAgentStepValue>;
unregisterAgentOfWithdrawal: () => AsyncGenerator<RegisterAgentStepValue>;
}
Loading

0 comments on commit 87d9dc9

Please sign in to comment.