Skip to content

Commit

Permalink
feat(address): add 2 api for address. isAcp & minPaymentAmount
Browse files Browse the repository at this point in the history
  • Loading branch information
johnz1019 committed Oct 20, 2020
1 parent 55863d9 commit f7ae547
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/builders/simple-sudt-builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const test = anyTest as TestInterface<{ builder: SimpleSUDTBuilder }>;

test.before(async (t) => {
const address = new Address(
'ckt1qyqv4yga3pgw2h92hcnur7lepdfzmvg8wj7qn44vz8',
'ckt1qjr2r35c0f9vhcdgslx2fjwa9tylevr5qka7mfgmscd33wlhfykykn2zrv2y5rex4nnyfs2tqkde8zmayrls6d3kwa5',
AddressType.ckb
);
const amount = new Amount('100');
Expand Down
31 changes: 31 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ export const CHAIN_SPECS = {
HashType.type
),
},
acpLockList: [
new Script(
'0xbf43c3602455798c1a61a596e0d95278864c552fafe231c063b3fabf97a8febc',
'0x',
HashType.type
),
new Script(
'0x0fb343953ee78c9986b091defb6252154e0bb51044fd2879fde5b27314506111',
'0x',
HashType.data
),
],
},

Aggron: {
Expand Down Expand Up @@ -155,6 +167,18 @@ export const CHAIN_SPECS = {
HashType.type
),
},
acpLockList: [
new Script(
'0x58c5f491aba6d61678b7cf7edf4910b1f5e00ec0cde2f42e0abb4fd9aff25a63',
'0x',
HashType.type
),
new Script(
'0x86a1c6987a4acbe1a887cca4c9dd2ac9fcb07405bbeda51b861b18bbf7492c4b',
'0x',
HashType.type
),
],
},
// dev - lay2.ckb.dev
Lay2: {
Expand Down Expand Up @@ -228,5 +252,12 @@ export const CHAIN_SPECS = {
HashType.type
),
},
acpLockList: [
new Script(
'0xc9eb3097397836e4d5b8fabed3c0cddd14fefe483caf238ca2e3095a111add0b',
'0x',
HashType.type
),
],
},
};
1 change: 1 addition & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ export interface Config {
multiSigLock: ConfigItem;
pwLock: ConfigItem;
sudtType: ConfigItem;
acpLockList: Script[];
}
36 changes: 36 additions & 0 deletions src/models/address.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,39 @@ test('to lock script', (t) => {
hash_type: PWCore.config.pwLock.script.hashType,
});
});

test('is acp address', (t) => {
t.true(
new Address(
'ckt1qjr2r35c0f9vhcdgslx2fjwa9tylevr5qka7mfgmscd33wlhfykykn2zrv2y5rex4nnyfs2tqkde8zmayrls6d3kwa5',
AddressType.ckb
).isAcp()
);
t.false(
new Address(
'ckt1q35y83078t9h7nwzyvpe9qfuh8qjm08d2ktlegc22tcn4fgem6xnxwlwq4vae7nqgpkl6s59znsqmh9jkrtjhwct56efh7uep9y2xr04d4augtnmauya4s2cdvn0s6nxw9m6k7ndhf2l0un2g0tr7f88fegqns00nq',
AddressType.ckb
).isAcp()
);
});

test('minimal pay amount', (t) => {
t.is(
new Address(
'ckt1q35y83078t9h7nwzyvpe9qfuh8qjm08d2ktlegc22tcn4fgem6xnxwlwq4vae7nqgpkl6s59znsqmh9jkrtjhwct56efh7uep9y2xr04d4augtnmauya4s2cdvn0s6nxw9m6k7ndhf2l0un2g0tr7f88fegqns00nq',
AddressType.ckb
)
.minPaymentAmount()
.toString(),
'105'
);
t.is(
new Address(
'ckt1qyqxpayn272n8km2k08hzldynj992egs0waqnr8zjs',
AddressType.ckb
)
.minPaymentAmount()
.toString(),
'61'
);
});
24 changes: 21 additions & 3 deletions src/models/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import {
LumosConfigs,
verifyCkbAddress,
verifyEthAddress,
cellOccupiedBytes,
} from '../utils';
import {
fullPayloadToAddress,
AddressType as AType,
AddressPrefix as APrefix,
} from '@nervosnetwork/ckb-sdk-utils';
import { Amount, AmountUnit } from './amount';

export enum AddressPrefix {
ckb,
Expand Down Expand Up @@ -69,9 +71,25 @@ export class Address {
}
}

// TODO need check code_hash is in the acplock list
isAcp() {
return true;
minPaymentAmount(): Amount {
if (this.isAcp()) {
return new Amount('1', AmountUnit.shannon);
}
const bytes = cellOccupiedBytes({
lock: this.toLockScript(),
type: null,
data: '0x',
});
return new Amount(bytes.toString());
}

isAcp(): boolean {
const script = this.toLockScript();
const { codeHash, hashType } = script;
const acpLock = PWCore.config.acpLockList.filter(
(x) => x.codeHash === codeHash && x.hashType === hashType
);
return acpLock && acpLock.length > 0;
}

toCKBAddress(): string {
Expand Down

0 comments on commit f7ae547

Please sign in to comment.